blob: ce90c2cf3315cc219e3b6b406fb06c6d7cddb31e [file] [log] [blame]
Viet-Trung Luu2a394492015-03-04 08:03:20 -08001// 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
18namespace mojo {
19namespace files {
20
21Error IsPathValid(const String& path) {
22 DCHECK(!path.is_null());
23 if (!base::IsStringUTF8(path.get()))
John Grossman87fe5142015-10-02 10:11:43 -070024 return Error::INVALID_ARGUMENT;
Viet-Trung Luu2a394492015-03-04 08:03:20 -080025 if (path.size() > 0 && path[0] == '/')
John Grossman87fe5142015-10-02 10:11:43 -070026 return Error::PERMISSION_DENIED;
27 return Error::OK;
Viet-Trung Luu2a394492015-03-04 08:03:20 -080028}
29
30Error IsWhenceValid(Whence whence) {
John Grossman87fe5142015-10-02 10:11:43 -070031 return (whence == Whence::FROM_CURRENT || whence == Whence::FROM_START ||
32 whence == Whence::FROM_END)
33 ? Error::OK
34 : Error::UNIMPLEMENTED;
Viet-Trung Luu2a394492015-03-04 08:03:20 -080035}
36
37Error IsOffsetValid(int64_t offset) {
38 return (offset >= std::numeric_limits<off_t>::min() &&
39 offset <= std::numeric_limits<off_t>::max())
John Grossman87fe5142015-10-02 10:11:43 -070040 ? Error::OK
41 : Error::OUT_OF_RANGE;
Viet-Trung Luu2a394492015-03-04 08:03:20 -080042}
43
44Error ErrnoToError(int errno_value) {
45 // TODO(vtl)
John Grossman87fe5142015-10-02 10:11:43 -070046 return Error::UNKNOWN;
Viet-Trung Luu2a394492015-03-04 08:03:20 -080047}
48
49int WhenceToStandardWhence(Whence whence) {
John Grossman87fe5142015-10-02 10:11:43 -070050 DCHECK_EQ(IsWhenceValid(whence), Error::OK);
Viet-Trung Luu2a394492015-03-04 08:03:20 -080051 switch (whence) {
John Grossman87fe5142015-10-02 10:11:43 -070052 case Whence::FROM_CURRENT:
Viet-Trung Luu2a394492015-03-04 08:03:20 -080053 return SEEK_CUR;
John Grossman87fe5142015-10-02 10:11:43 -070054 case Whence::FROM_START:
Viet-Trung Luu2a394492015-03-04 08:03:20 -080055 return SEEK_SET;
John Grossman87fe5142015-10-02 10:11:43 -070056 case Whence::FROM_END:
Viet-Trung Luu2a394492015-03-04 08:03:20 -080057 return SEEK_END;
58 }
59 NOTREACHED();
60 return 0;
61}
62
63Error TimespecToStandardTimespec(const Timespec* in, struct timespec* out) {
64 if (!in) {
65 out->tv_sec = 0;
66 out->tv_nsec = UTIME_OMIT;
John Grossman87fe5142015-10-02 10:11:43 -070067 return Error::OK;
Viet-Trung Luu2a394492015-03-04 08:03:20 -080068 }
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 Grossman87fe5142015-10-02 10:11:43 -070073 return Error::OUT_OF_RANGE;
Viet-Trung Luu2a394492015-03-04 08:03:20 -080074
75 if (in->nanoseconds < 0 || in->nanoseconds >= 1000000000)
John Grossman87fe5142015-10-02 10:11:43 -070076 return Error::INVALID_ARGUMENT;
Viet-Trung Luu2a394492015-03-04 08:03:20 -080077
78 out->tv_sec = static_cast<time_t>(in->seconds);
79 out->tv_nsec = static_cast<long>(in->nanoseconds);
John Grossman87fe5142015-10-02 10:11:43 -070080 return Error::OK;
Viet-Trung Luu2a394492015-03-04 08:03:20 -080081}
82
83Error TimespecOrNowToStandardTimespec(const TimespecOrNow* in,
84 struct timespec* out) {
85 if (!in) {
86 out->tv_sec = 0;
87 out->tv_nsec = UTIME_OMIT;
John Grossman87fe5142015-10-02 10:11:43 -070088 return Error::OK;
Viet-Trung Luu2a394492015-03-04 08:03:20 -080089 }
90
91 if (in->now) {
92 if (!in->timespec.is_null())
John Grossman87fe5142015-10-02 10:11:43 -070093 return Error::INVALID_ARGUMENT;
Viet-Trung Luu2a394492015-03-04 08:03:20 -080094 out->tv_sec = 0;
95 out->tv_nsec = UTIME_NOW;
John Grossman87fe5142015-10-02 10:11:43 -070096 return Error::OK;
Viet-Trung Luu2a394492015-03-04 08:03:20 -080097 }
98
99 return TimespecToStandardTimespec(in->timespec.get(), out);
100}
101
102} // namespace files
103} // namespace mojo