Viet-Trung Luu | 2a39449 | 2015-03-04 08:03:20 -0800 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "services/files/util.h" |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <time.h> |
| 11 | |
| 12 | #include <limits> |
| 13 | |
| 14 | #include "base/logging.h" |
| 15 | #include "base/strings/string_util.h" |
| 16 | #include "mojo/public/cpp/bindings/string.h" |
| 17 | |
| 18 | namespace mojo { |
| 19 | namespace files { |
| 20 | |
| 21 | Error IsPathValid(const String& path) { |
| 22 | DCHECK(!path.is_null()); |
| 23 | if (!base::IsStringUTF8(path.get())) |
John Grossman | 87fe514 | 2015-10-02 10:11:43 -0700 | [diff] [blame] | 24 | return Error::INVALID_ARGUMENT; |
Viet-Trung Luu | 2a39449 | 2015-03-04 08:03:20 -0800 | [diff] [blame] | 25 | if (path.size() > 0 && path[0] == '/') |
John Grossman | 87fe514 | 2015-10-02 10:11:43 -0700 | [diff] [blame] | 26 | return Error::PERMISSION_DENIED; |
| 27 | return Error::OK; |
Viet-Trung Luu | 2a39449 | 2015-03-04 08:03:20 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | Error IsWhenceValid(Whence whence) { |
John Grossman | 87fe514 | 2015-10-02 10:11:43 -0700 | [diff] [blame] | 31 | return (whence == Whence::FROM_CURRENT || whence == Whence::FROM_START || |
| 32 | whence == Whence::FROM_END) |
| 33 | ? Error::OK |
| 34 | : Error::UNIMPLEMENTED; |
Viet-Trung Luu | 2a39449 | 2015-03-04 08:03:20 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | Error IsOffsetValid(int64_t offset) { |
| 38 | return (offset >= std::numeric_limits<off_t>::min() && |
| 39 | offset <= std::numeric_limits<off_t>::max()) |
John Grossman | 87fe514 | 2015-10-02 10:11:43 -0700 | [diff] [blame] | 40 | ? Error::OK |
| 41 | : Error::OUT_OF_RANGE; |
Viet-Trung Luu | 2a39449 | 2015-03-04 08:03:20 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | Error ErrnoToError(int errno_value) { |
| 45 | // TODO(vtl) |
John Grossman | 87fe514 | 2015-10-02 10:11:43 -0700 | [diff] [blame] | 46 | return Error::UNKNOWN; |
Viet-Trung Luu | 2a39449 | 2015-03-04 08:03:20 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | int WhenceToStandardWhence(Whence whence) { |
John Grossman | 87fe514 | 2015-10-02 10:11:43 -0700 | [diff] [blame] | 50 | DCHECK_EQ(IsWhenceValid(whence), Error::OK); |
Viet-Trung Luu | 2a39449 | 2015-03-04 08:03:20 -0800 | [diff] [blame] | 51 | switch (whence) { |
John Grossman | 87fe514 | 2015-10-02 10:11:43 -0700 | [diff] [blame] | 52 | case Whence::FROM_CURRENT: |
Viet-Trung Luu | 2a39449 | 2015-03-04 08:03:20 -0800 | [diff] [blame] | 53 | return SEEK_CUR; |
John Grossman | 87fe514 | 2015-10-02 10:11:43 -0700 | [diff] [blame] | 54 | case Whence::FROM_START: |
Viet-Trung Luu | 2a39449 | 2015-03-04 08:03:20 -0800 | [diff] [blame] | 55 | return SEEK_SET; |
John Grossman | 87fe514 | 2015-10-02 10:11:43 -0700 | [diff] [blame] | 56 | case Whence::FROM_END: |
Viet-Trung Luu | 2a39449 | 2015-03-04 08:03:20 -0800 | [diff] [blame] | 57 | return SEEK_END; |
| 58 | } |
| 59 | NOTREACHED(); |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | Error TimespecToStandardTimespec(const Timespec* in, struct timespec* out) { |
| 64 | if (!in) { |
| 65 | out->tv_sec = 0; |
| 66 | out->tv_nsec = UTIME_OMIT; |
John Grossman | 87fe514 | 2015-10-02 10:11:43 -0700 | [diff] [blame] | 67 | return Error::OK; |
Viet-Trung Luu | 2a39449 | 2015-03-04 08:03:20 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static_assert(sizeof(int64_t) >= sizeof(time_t), "whoa, time_t is huge"); |
| 71 | if (in->seconds < std::numeric_limits<time_t>::min() || |
| 72 | in->seconds > std::numeric_limits<time_t>::max()) |
John Grossman | 87fe514 | 2015-10-02 10:11:43 -0700 | [diff] [blame] | 73 | return Error::OUT_OF_RANGE; |
Viet-Trung Luu | 2a39449 | 2015-03-04 08:03:20 -0800 | [diff] [blame] | 74 | |
| 75 | if (in->nanoseconds < 0 || in->nanoseconds >= 1000000000) |
John Grossman | 87fe514 | 2015-10-02 10:11:43 -0700 | [diff] [blame] | 76 | return Error::INVALID_ARGUMENT; |
Viet-Trung Luu | 2a39449 | 2015-03-04 08:03:20 -0800 | [diff] [blame] | 77 | |
| 78 | out->tv_sec = static_cast<time_t>(in->seconds); |
| 79 | out->tv_nsec = static_cast<long>(in->nanoseconds); |
John Grossman | 87fe514 | 2015-10-02 10:11:43 -0700 | [diff] [blame] | 80 | return Error::OK; |
Viet-Trung Luu | 2a39449 | 2015-03-04 08:03:20 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | Error TimespecOrNowToStandardTimespec(const TimespecOrNow* in, |
| 84 | struct timespec* out) { |
| 85 | if (!in) { |
| 86 | out->tv_sec = 0; |
| 87 | out->tv_nsec = UTIME_OMIT; |
John Grossman | 87fe514 | 2015-10-02 10:11:43 -0700 | [diff] [blame] | 88 | return Error::OK; |
Viet-Trung Luu | 2a39449 | 2015-03-04 08:03:20 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if (in->now) { |
| 92 | if (!in->timespec.is_null()) |
John Grossman | 87fe514 | 2015-10-02 10:11:43 -0700 | [diff] [blame] | 93 | return Error::INVALID_ARGUMENT; |
Viet-Trung Luu | 2a39449 | 2015-03-04 08:03:20 -0800 | [diff] [blame] | 94 | out->tv_sec = 0; |
| 95 | out->tv_nsec = UTIME_NOW; |
John Grossman | 87fe514 | 2015-10-02 10:11:43 -0700 | [diff] [blame] | 96 | return Error::OK; |
Viet-Trung Luu | 2a39449 | 2015-03-04 08:03:20 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | return TimespecToStandardTimespec(in->timespec.get(), out); |
| 100 | } |
| 101 | |
| 102 | } // namespace files |
| 103 | } // namespace mojo |