Modify isolate_driver.py to always generate a wrapping .isolate.
[chromium-blink-merge.git] / components / filesystem / util.cc
blob257d0083dd8502b287d2982b381e7aa19b5dc371
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.
5 #include "components/filesystem/util.h"
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <sys/stat.h>
10 #include <time.h>
12 #include <limits>
14 #include "base/logging.h"
15 #include "base/strings/string_util.h"
16 #include "mojo/public/cpp/bindings/string.h"
18 #if defined(OS_WIN)
19 #include "base/strings/utf_string_conversions.h"
20 #endif
22 // module filesystem has various constants which must line up with enum values
23 // in base::File::Flags.
24 static_assert(filesystem::kFlagOpen ==
25 static_cast<uint32>(base::File::FLAG_OPEN),
26 "");
27 static_assert(filesystem::kFlagCreate ==
28 static_cast<uint32>(base::File::FLAG_CREATE),
29 "");
30 static_assert(filesystem::kFlagOpenAlways ==
31 static_cast<uint32>(base::File::FLAG_OPEN_ALWAYS),
32 "");
33 static_assert(filesystem::kCreateAlways ==
34 static_cast<uint32>(base::File::FLAG_CREATE_ALWAYS),
35 "");
36 static_assert(filesystem::kFlagOpenTruncated ==
37 static_cast<uint32>(base::File::FLAG_OPEN_TRUNCATED),
38 "");
39 static_assert(filesystem::kFlagRead ==
40 static_cast<uint32>(base::File::FLAG_READ),
41 "");
42 static_assert(filesystem::kFlagWrite ==
43 static_cast<uint32>(base::File::FLAG_WRITE),
44 "");
45 static_assert(filesystem::kFlagAppend ==
46 static_cast<uint32>(base::File::FLAG_APPEND),
47 "");
49 // filesystem.Error in types.mojom must be the same as base::File::Error.
50 static_assert(static_cast<int>(filesystem::FILE_ERROR_OK) ==
51 static_cast<int>(base::File::FILE_OK),
52 "");
53 static_assert(static_cast<int>(filesystem::FILE_ERROR_FAILED) ==
54 static_cast<int>(base::File::FILE_ERROR_FAILED),
55 "");
56 static_assert(static_cast<int>(filesystem::FILE_ERROR_IN_USE) ==
57 static_cast<int>(base::File::FILE_ERROR_IN_USE),
58 "");
59 static_assert(static_cast<int>(filesystem::FILE_ERROR_EXISTS) ==
60 static_cast<int>(base::File::FILE_ERROR_EXISTS),
61 "");
62 static_assert(static_cast<int>(filesystem::FILE_ERROR_NOT_FOUND) ==
63 static_cast<int>(base::File::FILE_ERROR_NOT_FOUND),
64 "");
65 static_assert(static_cast<int>(filesystem::FILE_ERROR_ACCESS_DENIED) ==
66 static_cast<int>(base::File::FILE_ERROR_ACCESS_DENIED),
67 "");
68 static_assert(static_cast<int>(filesystem::FILE_ERROR_TOO_MANY_OPENED) ==
69 static_cast<int>(base::File::FILE_ERROR_TOO_MANY_OPENED),
70 "");
71 static_assert(static_cast<int>(filesystem::FILE_ERROR_NO_MEMORY) ==
72 static_cast<int>(base::File::FILE_ERROR_NO_MEMORY),
73 "");
74 static_assert(static_cast<int>(filesystem::FILE_ERROR_NO_SPACE) ==
75 static_cast<int>(base::File::FILE_ERROR_NO_SPACE),
76 "");
77 static_assert(static_cast<int>(filesystem::FILE_ERROR_NOT_A_DIRECTORY) ==
78 static_cast<int>(base::File::FILE_ERROR_NOT_A_DIRECTORY),
79 "");
80 static_assert(static_cast<int>(filesystem::FILE_ERROR_INVALID_OPERATION) ==
81 static_cast<int>(base::File::FILE_ERROR_INVALID_OPERATION),
82 "");
83 static_assert(static_cast<int>(filesystem::FILE_ERROR_SECURITY) ==
84 static_cast<int>(base::File::FILE_ERROR_SECURITY),
85 "");
86 static_assert(static_cast<int>(filesystem::FILE_ERROR_ABORT) ==
87 static_cast<int>(base::File::FILE_ERROR_ABORT),
88 "");
89 static_assert(static_cast<int>(filesystem::FILE_ERROR_NOT_A_FILE) ==
90 static_cast<int>(base::File::FILE_ERROR_NOT_A_FILE),
91 "");
92 static_assert(static_cast<int>(filesystem::FILE_ERROR_NOT_EMPTY) ==
93 static_cast<int>(base::File::FILE_ERROR_NOT_EMPTY),
94 "");
95 static_assert(static_cast<int>(filesystem::FILE_ERROR_INVALID_URL) ==
96 static_cast<int>(base::File::FILE_ERROR_INVALID_URL),
97 "");
98 static_assert(static_cast<int>(filesystem::FILE_ERROR_IO) ==
99 static_cast<int>(base::File::FILE_ERROR_IO),
100 "");
102 // filesystem.Whence in types.mojom must be the same as base::File::Whence.
103 static_assert(static_cast<int>(filesystem::WHENCE_FROM_BEGIN) ==
104 static_cast<int>(base::File::FROM_BEGIN),
105 "");
106 static_assert(static_cast<int>(filesystem::WHENCE_FROM_CURRENT) ==
107 static_cast<int>(base::File::FROM_CURRENT),
108 "");
109 static_assert(static_cast<int>(filesystem::WHENCE_FROM_END) ==
110 static_cast<int>(base::File::FROM_END),
111 "");
113 namespace filesystem {
115 FileError IsWhenceValid(Whence whence) {
116 return (whence == WHENCE_FROM_CURRENT || whence == WHENCE_FROM_BEGIN ||
117 whence == WHENCE_FROM_END)
118 ? FILE_ERROR_OK
119 : FILE_ERROR_INVALID_OPERATION;
122 FileError IsOffsetValid(int64_t offset) {
123 return (offset >= std::numeric_limits<off_t>::min() &&
124 offset <= std::numeric_limits<off_t>::max())
125 ? FILE_ERROR_OK
126 : FILE_ERROR_INVALID_OPERATION;
129 FileError GetError(const base::File& file) {
130 return static_cast<filesystem::FileError>(file.error_details());
133 FileInformationPtr MakeFileInformation(const base::File::Info& info) {
134 FileInformationPtr file_info(FileInformation::New());
135 file_info->type =
136 info.is_directory ? FS_FILE_TYPE_DIRECTORY : FS_FILE_TYPE_REGULAR_FILE;
137 file_info->size = info.size;
139 file_info->atime = info.last_accessed.ToDoubleT();
140 file_info->mtime = info.last_modified.ToDoubleT();
141 file_info->ctime = info.creation_time.ToDoubleT();
143 return file_info.Pass();
146 FileError ValidatePath(const mojo::String& raw_path,
147 const base::FilePath& filesystem_base,
148 base::FilePath* out) {
149 DCHECK(!raw_path.is_null());
150 if (!base::IsStringUTF8(raw_path.get()))
151 return FILE_ERROR_INVALID_OPERATION;
153 #if defined(OS_POSIX)
154 base::FilePath::StringType path = raw_path;
155 #elif defined(OS_WIN)
156 base::FilePath::StringType path = base::UTF8ToUTF16(raw_path.get());
157 #endif
159 // TODO(erg): This isn't really what we want. FilePath::AppendRelativePath()
160 // is closer. We need to deal with entirely hostile apps trying to bust this
161 // function to use a possibly maliciously provided |raw_path| to bust out of
162 // |filesystem_base|.
163 base::FilePath full_path = filesystem_base.Append(path);
164 if (full_path.ReferencesParent()) {
165 // TODO(erg): For now, if it references a parent, we'll consider this bad.
166 return FILE_ERROR_ACCESS_DENIED;
169 *out = full_path;
170 return FILE_ERROR_OK;
173 } // namespace filesystem