1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "webkit/browser/fileapi/native_file_util.h"
#include "base/file_util.h"
#include "base/files/file_enumerator.h"
#include "base/memory/scoped_ptr.h"
#include "webkit/browser/fileapi/file_system_operation_context.h"
namespace fileapi {
namespace {
// Sets permissions on directory at |dir_path| based on the target platform.
// Returns true on success, or false otherwise.
//
// TODO(benchan): Find a better place outside webkit to host this function.
bool SetPlatformSpecificDirectoryPermissions(const base::FilePath& dir_path) {
#if defined(OS_CHROMEOS)
// System daemons on Chrome OS may run as a user different than the Chrome
// process but need to access files under the directories created here.
// Because of that, grant the execute permission on the created directory
// to group and other users.
if (HANDLE_EINTR(chmod(dir_path.value().c_str(),
S_IRWXU | S_IXGRP | S_IXOTH)) != 0) {
return false;
}
#endif
// Keep the directory permissions unchanged on non-Chrome OS platforms.
return true;
}
} // namespace
using base::PlatformFile;
using base::PlatformFileError;
class NativeFileEnumerator : public FileSystemFileUtil::AbstractFileEnumerator {
public:
NativeFileEnumerator(const base::FilePath& root_path,
bool recursive,
int file_type)
: file_enum_(root_path, recursive, file_type) {
}
virtual ~NativeFileEnumerator() {}
virtual base::FilePath Next() OVERRIDE;
virtual int64 Size() OVERRIDE;
virtual base::Time LastModifiedTime() OVERRIDE;
virtual bool IsDirectory() OVERRIDE;
private:
base::FileEnumerator file_enum_;
base::FileEnumerator::FileInfo file_util_info_;
};
base::FilePath NativeFileEnumerator::Next() {
base::FilePath rv = file_enum_.Next();
if (!rv.empty())
file_util_info_ = file_enum_.GetInfo();
return rv;
}
int64 NativeFileEnumerator::Size() {
return file_util_info_.GetSize();
}
base::Time NativeFileEnumerator::LastModifiedTime() {
return file_util_info_.GetLastModifiedTime();
}
bool NativeFileEnumerator::IsDirectory() {
return file_util_info_.IsDirectory();
}
PlatformFileError NativeFileUtil::CreateOrOpen(
const base::FilePath& path, int file_flags,
PlatformFile* file_handle, bool* created) {
if (!base::DirectoryExists(path.DirName())) {
// If its parent does not exist, should return NOT_FOUND error.
return base::PLATFORM_FILE_ERROR_NOT_FOUND;
}
if (base::DirectoryExists(path))
return base::PLATFORM_FILE_ERROR_NOT_A_FILE;
PlatformFileError error_code = base::PLATFORM_FILE_OK;
*file_handle = base::CreatePlatformFile(path, file_flags,
created, &error_code);
return error_code;
}
PlatformFileError NativeFileUtil::Close(PlatformFile file_handle) {
if (!base::ClosePlatformFile(file_handle))
return base::PLATFORM_FILE_ERROR_FAILED;
return base::PLATFORM_FILE_OK;
}
PlatformFileError NativeFileUtil::EnsureFileExists(
const base::FilePath& path,
bool* created) {
if (!base::DirectoryExists(path.DirName()))
// If its parent does not exist, should return NOT_FOUND error.
return base::PLATFORM_FILE_ERROR_NOT_FOUND;
PlatformFileError error_code = base::PLATFORM_FILE_OK;
// Tries to create the |path| exclusively. This should fail
// with base::PLATFORM_FILE_ERROR_EXISTS if the path already exists.
PlatformFile handle = base::CreatePlatformFile(
path,
base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ,
created, &error_code);
if (error_code == base::PLATFORM_FILE_ERROR_EXISTS) {
// Make sure created_ is false.
if (created)
*created = false;
error_code = base::PLATFORM_FILE_OK;
}
if (handle != base::kInvalidPlatformFileValue)
base::ClosePlatformFile(handle);
return error_code;
}
PlatformFileError NativeFileUtil::CreateDirectory(
const base::FilePath& path,
bool exclusive,
bool recursive) {
// If parent dir of file doesn't exist.
if (!recursive && !base::PathExists(path.DirName()))
return base::PLATFORM_FILE_ERROR_NOT_FOUND;
bool path_exists = base::PathExists(path);
if (exclusive && path_exists)
return base::PLATFORM_FILE_ERROR_EXISTS;
// If file exists at the path.
if (path_exists && !base::DirectoryExists(path))
return base::PLATFORM_FILE_ERROR_EXISTS;
if (!file_util::CreateDirectory(path))
return base::PLATFORM_FILE_ERROR_FAILED;
if (!SetPlatformSpecificDirectoryPermissions(path)) {
// Since some file systems don't support permission setting, we do not treat
// an error from the function as the failure of copying. Just log it.
LOG(WARNING) << "Setting directory permission failed: "
<< path.AsUTF8Unsafe();
}
return base::PLATFORM_FILE_OK;
}
PlatformFileError NativeFileUtil::GetFileInfo(
const base::FilePath& path,
base::PlatformFileInfo* file_info) {
if (!base::PathExists(path))
return base::PLATFORM_FILE_ERROR_NOT_FOUND;
if (!file_util::GetFileInfo(path, file_info))
return base::PLATFORM_FILE_ERROR_FAILED;
return base::PLATFORM_FILE_OK;
}
scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator>
NativeFileUtil::CreateFileEnumerator(const base::FilePath& root_path,
bool recursive) {
return make_scoped_ptr(new NativeFileEnumerator(
root_path, recursive,
base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES))
.PassAs<FileSystemFileUtil::AbstractFileEnumerator>();
}
PlatformFileError NativeFileUtil::Touch(
const base::FilePath& path,
const base::Time& last_access_time,
const base::Time& last_modified_time) {
if (!file_util::TouchFile(
path, last_access_time, last_modified_time))
return base::PLATFORM_FILE_ERROR_FAILED;
return base::PLATFORM_FILE_OK;
}
PlatformFileError NativeFileUtil::Truncate(
const base::FilePath& path, int64 length) {
PlatformFileError error_code(base::PLATFORM_FILE_ERROR_FAILED);
PlatformFile file =
base::CreatePlatformFile(
path,
base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
NULL,
&error_code);
if (error_code != base::PLATFORM_FILE_OK) {
return error_code;
}
DCHECK_NE(base::kInvalidPlatformFileValue, file);
if (!base::TruncatePlatformFile(file, length))
error_code = base::PLATFORM_FILE_ERROR_FAILED;
base::ClosePlatformFile(file);
return error_code;
}
bool NativeFileUtil::PathExists(const base::FilePath& path) {
return base::PathExists(path);
}
bool NativeFileUtil::DirectoryExists(const base::FilePath& path) {
return base::DirectoryExists(path);
}
PlatformFileError NativeFileUtil::CopyOrMoveFile(
const base::FilePath& src_path,
const base::FilePath& dest_path,
FileSystemOperation::CopyOrMoveOption option,
bool copy) {
base::PlatformFileInfo info;
base::PlatformFileError error = NativeFileUtil::GetFileInfo(src_path, &info);
if (error != base::PLATFORM_FILE_OK)
return error;
if (info.is_directory)
return base::PLATFORM_FILE_ERROR_NOT_A_FILE;
base::Time last_modified = info.last_modified;
error = NativeFileUtil::GetFileInfo(dest_path, &info);
if (error != base::PLATFORM_FILE_OK &&
error != base::PLATFORM_FILE_ERROR_NOT_FOUND)
return error;
if (info.is_directory)
return base::PLATFORM_FILE_ERROR_INVALID_OPERATION;
if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) {
error = NativeFileUtil::GetFileInfo(dest_path.DirName(), &info);
if (error != base::PLATFORM_FILE_OK)
return error;
if (!info.is_directory)
return base::PLATFORM_FILE_ERROR_NOT_FOUND;
}
if (copy) {
if (!base::CopyFile(src_path, dest_path))
return base::PLATFORM_FILE_ERROR_FAILED;
} else {
if (!base::Move(src_path, dest_path))
return base::PLATFORM_FILE_ERROR_FAILED;
}
// Preserve the last modified time. Do not return error here even if
// the setting is failed, because the copy itself is successfully done.
if (option == FileSystemOperation::OPTION_PRESERVE_LAST_MODIFIED)
file_util::SetLastModifiedTime(dest_path, last_modified);
return base::PLATFORM_FILE_OK;
}
PlatformFileError NativeFileUtil::DeleteFile(const base::FilePath& path) {
if (!base::PathExists(path))
return base::PLATFORM_FILE_ERROR_NOT_FOUND;
if (base::DirectoryExists(path))
return base::PLATFORM_FILE_ERROR_NOT_A_FILE;
if (!base::DeleteFile(path, false))
return base::PLATFORM_FILE_ERROR_FAILED;
return base::PLATFORM_FILE_OK;
}
PlatformFileError NativeFileUtil::DeleteDirectory(const base::FilePath& path) {
if (!base::PathExists(path))
return base::PLATFORM_FILE_ERROR_NOT_FOUND;
if (!base::DirectoryExists(path))
return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY;
if (!base::IsDirectoryEmpty(path))
return base::PLATFORM_FILE_ERROR_NOT_EMPTY;
if (!base::DeleteFile(path, false))
return base::PLATFORM_FILE_ERROR_FAILED;
return base::PLATFORM_FILE_OK;
}
} // namespace fileapi
|