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"
14 #include "base/logging.h"
15 #include "base/strings/string_util.h"
16 #include "mojo/public/cpp/bindings/string.h"
19 #include "base/strings/utf_string_conversions.h"
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
),
27 static_assert(filesystem::kFlagCreate
==
28 static_cast<uint32
>(base::File::FLAG_CREATE
),
30 static_assert(filesystem::kFlagOpenAlways
==
31 static_cast<uint32
>(base::File::FLAG_OPEN_ALWAYS
),
33 static_assert(filesystem::kCreateAlways
==
34 static_cast<uint32
>(base::File::FLAG_CREATE_ALWAYS
),
36 static_assert(filesystem::kFlagOpenTruncated
==
37 static_cast<uint32
>(base::File::FLAG_OPEN_TRUNCATED
),
39 static_assert(filesystem::kFlagRead
==
40 static_cast<uint32
>(base::File::FLAG_READ
),
42 static_assert(filesystem::kFlagWrite
==
43 static_cast<uint32
>(base::File::FLAG_WRITE
),
45 static_assert(filesystem::kFlagAppend
==
46 static_cast<uint32
>(base::File::FLAG_APPEND
),
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
),
53 static_assert(static_cast<int>(filesystem::FILE_ERROR_FAILED
) ==
54 static_cast<int>(base::File::FILE_ERROR_FAILED
),
56 static_assert(static_cast<int>(filesystem::FILE_ERROR_IN_USE
) ==
57 static_cast<int>(base::File::FILE_ERROR_IN_USE
),
59 static_assert(static_cast<int>(filesystem::FILE_ERROR_EXISTS
) ==
60 static_cast<int>(base::File::FILE_ERROR_EXISTS
),
62 static_assert(static_cast<int>(filesystem::FILE_ERROR_NOT_FOUND
) ==
63 static_cast<int>(base::File::FILE_ERROR_NOT_FOUND
),
65 static_assert(static_cast<int>(filesystem::FILE_ERROR_ACCESS_DENIED
) ==
66 static_cast<int>(base::File::FILE_ERROR_ACCESS_DENIED
),
68 static_assert(static_cast<int>(filesystem::FILE_ERROR_TOO_MANY_OPENED
) ==
69 static_cast<int>(base::File::FILE_ERROR_TOO_MANY_OPENED
),
71 static_assert(static_cast<int>(filesystem::FILE_ERROR_NO_MEMORY
) ==
72 static_cast<int>(base::File::FILE_ERROR_NO_MEMORY
),
74 static_assert(static_cast<int>(filesystem::FILE_ERROR_NO_SPACE
) ==
75 static_cast<int>(base::File::FILE_ERROR_NO_SPACE
),
77 static_assert(static_cast<int>(filesystem::FILE_ERROR_NOT_A_DIRECTORY
) ==
78 static_cast<int>(base::File::FILE_ERROR_NOT_A_DIRECTORY
),
80 static_assert(static_cast<int>(filesystem::FILE_ERROR_INVALID_OPERATION
) ==
81 static_cast<int>(base::File::FILE_ERROR_INVALID_OPERATION
),
83 static_assert(static_cast<int>(filesystem::FILE_ERROR_SECURITY
) ==
84 static_cast<int>(base::File::FILE_ERROR_SECURITY
),
86 static_assert(static_cast<int>(filesystem::FILE_ERROR_ABORT
) ==
87 static_cast<int>(base::File::FILE_ERROR_ABORT
),
89 static_assert(static_cast<int>(filesystem::FILE_ERROR_NOT_A_FILE
) ==
90 static_cast<int>(base::File::FILE_ERROR_NOT_A_FILE
),
92 static_assert(static_cast<int>(filesystem::FILE_ERROR_NOT_EMPTY
) ==
93 static_cast<int>(base::File::FILE_ERROR_NOT_EMPTY
),
95 static_assert(static_cast<int>(filesystem::FILE_ERROR_INVALID_URL
) ==
96 static_cast<int>(base::File::FILE_ERROR_INVALID_URL
),
98 static_assert(static_cast<int>(filesystem::FILE_ERROR_IO
) ==
99 static_cast<int>(base::File::FILE_ERROR_IO
),
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
),
106 static_assert(static_cast<int>(filesystem::WHENCE_FROM_CURRENT
) ==
107 static_cast<int>(base::File::FROM_CURRENT
),
109 static_assert(static_cast<int>(filesystem::WHENCE_FROM_END
) ==
110 static_cast<int>(base::File::FROM_END
),
113 namespace filesystem
{
115 FileError
IsWhenceValid(Whence whence
) {
116 return (whence
== WHENCE_FROM_CURRENT
|| whence
== WHENCE_FROM_BEGIN
||
117 whence
== WHENCE_FROM_END
)
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())
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());
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());
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
;
170 return FILE_ERROR_OK
;
173 } // namespace filesystem