OLD | NEW |
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" |
18 | 19 |
19 namespace filesystem { | 20 namespace filesystem { |
20 | 21 |
21 DirectoryImpl::DirectoryImpl(mojo::InterfaceRequest<Directory> request, | 22 DirectoryImpl::DirectoryImpl(mojo::InterfaceRequest<Directory> request, |
22 base::FilePath directory_path, | 23 base::FilePath directory_path, |
23 scoped_ptr<base::ScopedTempDir> temp_dir) | 24 scoped_ptr<base::ScopedTempDir> temp_dir) |
24 : binding_(this, std::move(request)), | 25 : binding_(this, std::move(request)), |
25 directory_path_(directory_path), | 26 directory_path_(directory_path), |
26 temp_dir_(std::move(temp_dir)) {} | 27 temp_dir_(std::move(temp_dir)) {} |
27 | 28 |
(...skipping 29 matching lines...) Expand all Loading... |
57 const OpenFileCallback& callback) { | 58 const OpenFileCallback& callback) { |
58 base::FilePath path; | 59 base::FilePath path; |
59 FileError error = ValidatePath(raw_path, directory_path_, &path); | 60 FileError error = ValidatePath(raw_path, directory_path_, &path); |
60 if (error != FileError::OK) { | 61 if (error != FileError::OK) { |
61 callback.Run(error); | 62 callback.Run(error); |
62 return; | 63 return; |
63 } | 64 } |
64 | 65 |
65 #if defined(OS_WIN) | 66 #if defined(OS_WIN) |
66 // On Windows, FILE_FLAG_BACKUP_SEMANTICS is needed to open a directory. | 67 // On Windows, FILE_FLAG_BACKUP_SEMANTICS is needed to open a directory. |
67 if (DirectoryExists(path)) | 68 if (base::DirectoryExists(path)) |
68 open_flags |= base::File::FLAG_BACKUP_SEMANTICS; | 69 open_flags |= base::File::FLAG_BACKUP_SEMANTICS; |
69 #endif // OS_WIN | 70 #endif // OS_WIN |
70 | 71 |
71 base::File base_file(path, open_flags); | 72 base::File base_file(path, open_flags); |
72 if (!base_file.IsValid()) { | 73 if (!base_file.IsValid()) { |
73 callback.Run(GetError(base_file)); | 74 callback.Run(GetError(base_file)); |
74 return; | 75 return; |
75 } | 76 } |
76 | 77 |
77 base::File::Info info; | 78 base::File::Info info; |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 } | 209 } |
209 | 210 |
210 if (!file.Flush()) { | 211 if (!file.Flush()) { |
211 callback.Run(FileError::FAILED); | 212 callback.Run(FileError::FAILED); |
212 return; | 213 return; |
213 } | 214 } |
214 | 215 |
215 callback.Run(FileError::OK); | 216 callback.Run(FileError::OK); |
216 } | 217 } |
217 | 218 |
| 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 |
218 } // namespace filesystem | 283 } // namespace filesystem |
OLD | NEW |