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