forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectory_impl.cc
336 lines (283 loc) · 10.1 KB
/
directory_impl.cc
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright 2015 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 "components/filesystem/directory_impl.h"
#include <utility>
#include "base/files/file.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "build/build_config.h"
#include "components/filesystem/file_impl.h"
#include "components/filesystem/util.h"
#include "mojo/common/common_type_converters.h"
#include "mojo/platform_handle/platform_handle_functions.h"
using mojo::ScopedHandle;
namespace filesystem {
DirectoryImpl::DirectoryImpl(mojo::InterfaceRequest<Directory> request,
base::FilePath directory_path,
scoped_ptr<base::ScopedTempDir> temp_dir,
LockTable* lock_table)
: binding_(this, std::move(request)),
directory_path_(directory_path),
temp_dir_(std::move(temp_dir)),
lock_table_(lock_table) {}
DirectoryImpl::~DirectoryImpl() {
}
void DirectoryImpl::Read(const ReadCallback& callback) {
mojo::Array<DirectoryEntryPtr> entries;
base::FileEnumerator directory_enumerator(
directory_path_, false,
base::FileEnumerator::DIRECTORIES | base::FileEnumerator::FILES);
for (base::FilePath name = directory_enumerator.Next(); !name.empty();
name = directory_enumerator.Next()) {
base::FileEnumerator::FileInfo info = directory_enumerator.GetInfo();
DirectoryEntryPtr entry = DirectoryEntry::New();
entry->type =
info.IsDirectory() ? FsFileType::DIRECTORY : FsFileType::REGULAR_FILE;
entry->name = info.GetName().AsUTF8Unsafe();
entries.push_back(std::move(entry));
}
callback.Run(FileError::OK, std::move(entries));
}
// TODO(erg): Consider adding an implementation of Stat()/Touch() to the
// directory, too. Right now, the base::File abstractions do not really deal
// with directories properly, so these are broken for now.
// TODO(vtl): Move the implementation to a thread pool.
void DirectoryImpl::OpenFile(const mojo::String& raw_path,
mojo::InterfaceRequest<File> file,
uint32_t open_flags,
const OpenFileCallback& callback) {
base::FilePath path;
FileError error = ValidatePath(raw_path, directory_path_, &path);
if (error != FileError::OK) {
callback.Run(error);
return;
}
if (base::DirectoryExists(path)) {
// We must not return directories as files. In the file abstraction, we can
// fetch raw file descriptors over mojo pipes, and passing a file
// descriptor to a directory is a sandbox escape on Windows.
callback.Run(FileError::NOT_A_FILE);
return;
}
base::File base_file(path, open_flags);
if (!base_file.IsValid()) {
callback.Run(GetError(base_file));
return;
}
if (file.is_pending()) {
new FileImpl(std::move(file), path, std::move(base_file), lock_table_);
}
callback.Run(FileError::OK);
}
void DirectoryImpl::OpenFileHandle(const mojo::String& raw_path,
uint32_t open_flags,
const OpenFileHandleCallback& callback) {
base::FilePath path;
FileError error = ValidatePath(raw_path, directory_path_, &path);
if (error != FileError::OK) {
callback.Run(error, ScopedHandle());
return;
}
if (base::DirectoryExists(path)) {
// We must not return directories as files. In the file abstraction, we can
// fetch raw file descriptors over mojo pipes, and passing a file
// descriptor to a directory is a sandbox escape on Windows.
callback.Run(FileError::NOT_A_FILE, ScopedHandle());
return;
}
base::File base_file(path, open_flags);
if (!base_file.IsValid()) {
callback.Run(GetError(base_file), ScopedHandle());
return;
}
MojoHandle mojo_handle;
MojoResult create_result = MojoCreatePlatformHandleWrapper(
base_file.TakePlatformFile(), &mojo_handle);
if (create_result != MOJO_RESULT_OK) {
callback.Run(FileError::FAILED, ScopedHandle());
return;
}
callback.Run(FileError::OK, ScopedHandle(mojo::Handle(mojo_handle)));
}
void DirectoryImpl::OpenDirectory(const mojo::String& raw_path,
mojo::InterfaceRequest<Directory> directory,
uint32_t open_flags,
const OpenDirectoryCallback& callback) {
base::FilePath path;
FileError error = ValidatePath(raw_path, directory_path_, &path);
if (error != FileError::OK) {
callback.Run(error);
return;
}
if (!base::DirectoryExists(path)) {
if (base::PathExists(path)) {
callback.Run(FileError::NOT_A_DIRECTORY);
return;
}
if (!(open_flags & kFlagOpenAlways || open_flags & kFlagCreate)) {
// The directory doesn't exist, and we weren't passed parameters to
// create it.
callback.Run(FileError::NOT_FOUND);
return;
}
base::File::Error error;
if (!base::CreateDirectoryAndGetError(path, &error)) {
callback.Run(static_cast<filesystem::FileError>(error));
return;
}
}
if (directory.is_pending())
new DirectoryImpl(std::move(directory), path,
scoped_ptr<base::ScopedTempDir>(), lock_table_);
callback.Run(FileError::OK);
}
void DirectoryImpl::Rename(const mojo::String& raw_old_path,
const mojo::String& raw_new_path,
const RenameCallback& callback) {
base::FilePath old_path;
FileError error = ValidatePath(raw_old_path, directory_path_, &old_path);
if (error != FileError::OK) {
callback.Run(error);
return;
}
base::FilePath new_path;
error = ValidatePath(raw_new_path, directory_path_, &new_path);
if (error != FileError::OK) {
callback.Run(error);
return;
}
if (!base::Move(old_path, new_path)) {
callback.Run(FileError::FAILED);
return;
}
callback.Run(FileError::OK);
}
void DirectoryImpl::Delete(const mojo::String& raw_path,
uint32_t delete_flags,
const DeleteCallback& callback) {
base::FilePath path;
FileError error = ValidatePath(raw_path, directory_path_, &path);
if (error != FileError::OK) {
callback.Run(error);
return;
}
bool recursive = delete_flags & kDeleteFlagRecursive;
if (!base::DeleteFile(path, recursive)) {
callback.Run(FileError::FAILED);
return;
}
callback.Run(FileError::OK);
}
void DirectoryImpl::Exists(const mojo::String& raw_path,
const ExistsCallback& callback) {
base::FilePath path;
FileError error = ValidatePath(raw_path, directory_path_, &path);
if (error != FileError::OK) {
callback.Run(error, false);
return;
}
bool exists = base::PathExists(path);
callback.Run(FileError::OK, exists);
}
void DirectoryImpl::IsWritable(const mojo::String& raw_path,
const IsWritableCallback& callback) {
base::FilePath path;
FileError error = ValidatePath(raw_path, directory_path_, &path);
if (error != FileError::OK) {
callback.Run(error, false);
return;
}
callback.Run(FileError::OK, base::PathIsWritable(path));
}
void DirectoryImpl::Flush(const FlushCallback& callback) {
base::File file(directory_path_, base::File::FLAG_READ);
if (!file.IsValid()) {
callback.Run(GetError(file));
return;
}
if (!file.Flush()) {
callback.Run(FileError::FAILED);
return;
}
callback.Run(FileError::OK);
}
void DirectoryImpl::StatFile(const mojo::String& raw_path,
const StatFileCallback& callback) {
base::FilePath path;
FileError error = ValidatePath(raw_path, directory_path_, &path);
if (error != FileError::OK) {
callback.Run(error, nullptr);
return;
}
base::File base_file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
if (!base_file.IsValid()) {
callback.Run(GetError(base_file), nullptr);
return;
}
base::File::Info info;
if (!base_file.GetInfo(&info)) {
callback.Run(FileError::FAILED, nullptr);
return;
}
callback.Run(FileError::OK, MakeFileInformation(info));
}
void DirectoryImpl::ReadEntireFile(const mojo::String& raw_path,
const ReadEntireFileCallback& callback) {
base::FilePath path;
FileError error = ValidatePath(raw_path, directory_path_, &path);
if (error != FileError::OK) {
callback.Run(error, mojo::Array<uint8_t>());
return;
}
if (base::DirectoryExists(path)) {
callback.Run(FileError::NOT_A_FILE, mojo::Array<uint8_t>());
return;
}
base::File base_file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
if (!base_file.IsValid()) {
callback.Run(GetError(base_file), mojo::Array<uint8_t>());
return;
}
std::string contents;
const int kBufferSize = 1 << 16;
scoped_ptr<char[]> buf(new char[kBufferSize]);
int len;
while ((len = base_file.ReadAtCurrentPos(buf.get(), kBufferSize)) > 0)
contents.append(buf.get(), len);
callback.Run(FileError::OK, mojo::Array<uint8_t>::From(contents));
}
void DirectoryImpl::WriteFile(const mojo::String& raw_path,
mojo::Array<uint8_t> data,
const WriteFileCallback& callback) {
base::FilePath path;
FileError error = ValidatePath(raw_path, directory_path_, &path);
if (error != FileError::OK) {
callback.Run(error);
return;
}
if (base::DirectoryExists(path)) {
callback.Run(FileError::NOT_A_FILE);
return;
}
base::File base_file(path,
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
if (!base_file.IsValid()) {
callback.Run(GetError(base_file));
return;
}
// If we're given empty data, we don't write and just truncate the file.
if (data.size()) {
const int data_size = static_cast<int>(data.size());
if (base_file.Write(0, reinterpret_cast<char*>(&data.front()),
data_size) == -1) {
callback.Run(GetError(base_file));
return;
}
}
callback.Run(FileError::OK);
}
} // namespace filesystem