Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: components/filesystem/directory_impl.cc

Issue 1643733002: Revert of mojo filesystem: Simplify full file reading/writing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/filesystem/directory_impl.h" 5 #include "components/filesystem/directory_impl.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/files/file.h" 9 #include "base/files/file.h"
10 #include "base/files/file_enumerator.h" 10 #include "base/files/file_enumerator.h"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h" 12 #include "base/files/scoped_temp_dir.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "build/build_config.h" 15 #include "build/build_config.h"
16 #include "components/filesystem/file_impl.h" 16 #include "components/filesystem/file_impl.h"
17 #include "components/filesystem/util.h" 17 #include "components/filesystem/util.h"
18 #include "mojo/common/common_type_converters.h"
19 18
20 namespace filesystem { 19 namespace filesystem {
21 20
22 DirectoryImpl::DirectoryImpl(mojo::InterfaceRequest<Directory> request, 21 DirectoryImpl::DirectoryImpl(mojo::InterfaceRequest<Directory> request,
23 base::FilePath directory_path, 22 base::FilePath directory_path,
24 scoped_ptr<base::ScopedTempDir> temp_dir) 23 scoped_ptr<base::ScopedTempDir> temp_dir)
25 : binding_(this, std::move(request)), 24 : binding_(this, std::move(request)),
26 directory_path_(directory_path), 25 directory_path_(directory_path),
27 temp_dir_(std::move(temp_dir)) {} 26 temp_dir_(std::move(temp_dir)) {}
28 27
(...skipping 29 matching lines...) Expand all
58 const OpenFileCallback& callback) { 57 const OpenFileCallback& callback) {
59 base::FilePath path; 58 base::FilePath path;
60 FileError error = ValidatePath(raw_path, directory_path_, &path); 59 FileError error = ValidatePath(raw_path, directory_path_, &path);
61 if (error != FileError::OK) { 60 if (error != FileError::OK) {
62 callback.Run(error); 61 callback.Run(error);
63 return; 62 return;
64 } 63 }
65 64
66 #if defined(OS_WIN) 65 #if defined(OS_WIN)
67 // On Windows, FILE_FLAG_BACKUP_SEMANTICS is needed to open a directory. 66 // On Windows, FILE_FLAG_BACKUP_SEMANTICS is needed to open a directory.
68 if (base::DirectoryExists(path)) 67 if (DirectoryExists(path))
69 open_flags |= base::File::FLAG_BACKUP_SEMANTICS; 68 open_flags |= base::File::FLAG_BACKUP_SEMANTICS;
70 #endif // OS_WIN 69 #endif // OS_WIN
71 70
72 base::File base_file(path, open_flags); 71 base::File base_file(path, open_flags);
73 if (!base_file.IsValid()) { 72 if (!base_file.IsValid()) {
74 callback.Run(GetError(base_file)); 73 callback.Run(GetError(base_file));
75 return; 74 return;
76 } 75 }
77 76
78 base::File::Info info; 77 base::File::Info info;
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 } 208 }
210 209
211 if (!file.Flush()) { 210 if (!file.Flush()) {
212 callback.Run(FileError::FAILED); 211 callback.Run(FileError::FAILED);
213 return; 212 return;
214 } 213 }
215 214
216 callback.Run(FileError::OK); 215 callback.Run(FileError::OK);
217 } 216 }
218 217
219 void DirectoryImpl::ReadEntireFile(const mojo::String& raw_path,
220 const ReadEntireFileCallback& callback) {
221 base::FilePath path;
222 FileError error = ValidatePath(raw_path, directory_path_, &path);
223 if (error != FileError::OK) {
224 callback.Run(error, mojo::Array<uint8_t>(0));
225 return;
226 }
227
228 if (base::DirectoryExists(path)) {
229 callback.Run(FileError::NOT_A_FILE, mojo::Array<uint8_t>(0));
230 return;
231 }
232
233 base::File base_file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
234 if (!base_file.IsValid()) {
235 callback.Run(GetError(base_file), mojo::Array<uint8_t>(0));
236 return;
237 }
238
239 std::string contents;
240 const int kBufferSize = 1 << 16;
241 scoped_ptr<char[]> buf(new char[kBufferSize]);
242 int len;
243 while ((len = base_file.ReadAtCurrentPos(buf.get(), kBufferSize)) > 0)
244 contents.append(buf.get(), len);
245
246 callback.Run(FileError::OK, mojo::Array<uint8_t>::From(contents));
247 }
248
249 void DirectoryImpl::WriteFile(const mojo::String& raw_path,
250 mojo::Array<uint8_t> data,
251 const WriteFileCallback& callback) {
252 base::FilePath path;
253 FileError error = ValidatePath(raw_path, directory_path_, &path);
254 if (error != FileError::OK) {
255 callback.Run(error);
256 return;
257 }
258
259 if (base::DirectoryExists(path)) {
260 callback.Run(FileError::NOT_A_FILE);
261 return;
262 }
263
264 base::File base_file(path,
265 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
266 if (!base_file.IsValid()) {
267 callback.Run(GetError(base_file));
268 return;
269 }
270
271 // If we're given empty data, we don't write and just truncate the file.
272 if (data.size()) {
273 if (base_file.Write(0, reinterpret_cast<char*>(&data.front()),
274 data.size()) == -1) {
275 callback.Run(GetError(base_file));
276 return;
277 }
278 }
279
280 callback.Run(FileError::OK);
281 }
282
283 } // namespace filesystem 218 } // namespace filesystem
OLDNEW
« no previous file with comments | « components/filesystem/directory_impl.h ('k') | components/filesystem/directory_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698