| 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" | |
| 15 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 16 #include "components/filesystem/file_impl.h" | 15 #include "components/filesystem/file_impl.h" |
| 17 #include "components/filesystem/lock_table.h" | 16 #include "components/filesystem/lock_table.h" |
| 18 #include "components/filesystem/util.h" | 17 #include "components/filesystem/util.h" |
| 19 #include "mojo/common/common_type_converters.h" | 18 #include "mojo/common/common_type_converters.h" |
| 20 #include "mojo/platform_handle/platform_handle_functions.h" | 19 #include "mojo/platform_handle/platform_handle_functions.h" |
| 21 | 20 |
| 22 using mojo::ScopedHandle; | 21 using mojo::ScopedHandle; |
| 23 | 22 |
| 24 namespace filesystem { | 23 namespace filesystem { |
| 25 | 24 |
| 26 DirectoryImpl::DirectoryImpl(mojo::InterfaceRequest<Directory> request, | 25 DirectoryImpl::DirectoryImpl(mojo::InterfaceRequest<Directory> request, |
| 27 base::FilePath directory_path, | 26 base::FilePath directory_path, |
| 28 scoped_ptr<base::ScopedTempDir> temp_dir, | 27 std::unique_ptr<base::ScopedTempDir> temp_dir, |
| 29 scoped_refptr<LockTable> lock_table) | 28 scoped_refptr<LockTable> lock_table) |
| 30 : binding_(this, std::move(request)), | 29 : binding_(this, std::move(request)), |
| 31 directory_path_(directory_path), | 30 directory_path_(directory_path), |
| 32 temp_dir_(std::move(temp_dir)), | 31 temp_dir_(std::move(temp_dir)), |
| 33 lock_table_(std::move(lock_table)) {} | 32 lock_table_(std::move(lock_table)) {} |
| 34 | 33 |
| 35 DirectoryImpl::~DirectoryImpl() { | 34 DirectoryImpl::~DirectoryImpl() { |
| 36 } | 35 } |
| 37 | 36 |
| 38 void DirectoryImpl::Read(const ReadCallback& callback) { | 37 void DirectoryImpl::Read(const ReadCallback& callback) { |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 } | 148 } |
| 150 | 149 |
| 151 base::File::Error error; | 150 base::File::Error error; |
| 152 if (!base::CreateDirectoryAndGetError(path, &error)) { | 151 if (!base::CreateDirectoryAndGetError(path, &error)) { |
| 153 callback.Run(static_cast<filesystem::FileError>(error)); | 152 callback.Run(static_cast<filesystem::FileError>(error)); |
| 154 return; | 153 return; |
| 155 } | 154 } |
| 156 } | 155 } |
| 157 | 156 |
| 158 if (directory.is_pending()) | 157 if (directory.is_pending()) |
| 159 new DirectoryImpl(std::move(directory), path, | 158 new DirectoryImpl(std::move(directory), path, nullptr, lock_table_); |
| 160 scoped_ptr<base::ScopedTempDir>(), lock_table_); | |
| 161 callback.Run(FileError::OK); | 159 callback.Run(FileError::OK); |
| 162 } | 160 } |
| 163 | 161 |
| 164 void DirectoryImpl::Rename(const mojo::String& raw_old_path, | 162 void DirectoryImpl::Rename(const mojo::String& raw_old_path, |
| 165 const mojo::String& raw_new_path, | 163 const mojo::String& raw_new_path, |
| 166 const RenameCallback& callback) { | 164 const RenameCallback& callback) { |
| 167 base::FilePath old_path; | 165 base::FilePath old_path; |
| 168 FileError error = ValidatePath(raw_old_path, directory_path_, &old_path); | 166 FileError error = ValidatePath(raw_old_path, directory_path_, &old_path); |
| 169 if (error != FileError::OK) { | 167 if (error != FileError::OK) { |
| 170 callback.Run(error); | 168 callback.Run(error); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 } | 282 } |
| 285 | 283 |
| 286 base::File base_file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 284 base::File base_file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 287 if (!base_file.IsValid()) { | 285 if (!base_file.IsValid()) { |
| 288 callback.Run(GetError(base_file), mojo::Array<uint8_t>()); | 286 callback.Run(GetError(base_file), mojo::Array<uint8_t>()); |
| 289 return; | 287 return; |
| 290 } | 288 } |
| 291 | 289 |
| 292 std::string contents; | 290 std::string contents; |
| 293 const int kBufferSize = 1 << 16; | 291 const int kBufferSize = 1 << 16; |
| 294 scoped_ptr<char[]> buf(new char[kBufferSize]); | 292 std::unique_ptr<char[]> buf(new char[kBufferSize]); |
| 295 int len; | 293 int len; |
| 296 while ((len = base_file.ReadAtCurrentPos(buf.get(), kBufferSize)) > 0) | 294 while ((len = base_file.ReadAtCurrentPos(buf.get(), kBufferSize)) > 0) |
| 297 contents.append(buf.get(), len); | 295 contents.append(buf.get(), len); |
| 298 | 296 |
| 299 callback.Run(FileError::OK, mojo::Array<uint8_t>::From(contents)); | 297 callback.Run(FileError::OK, mojo::Array<uint8_t>::From(contents)); |
| 300 } | 298 } |
| 301 | 299 |
| 302 void DirectoryImpl::WriteFile(const mojo::String& raw_path, | 300 void DirectoryImpl::WriteFile(const mojo::String& raw_path, |
| 303 mojo::Array<uint8_t> data, | 301 mojo::Array<uint8_t> data, |
| 304 const WriteFileCallback& callback) { | 302 const WriteFileCallback& callback) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 328 data_size) == -1) { | 326 data_size) == -1) { |
| 329 callback.Run(GetError(base_file)); | 327 callback.Run(GetError(base_file)); |
| 330 return; | 328 return; |
| 331 } | 329 } |
| 332 } | 330 } |
| 333 | 331 |
| 334 callback.Run(FileError::OK); | 332 callback.Run(FileError::OK); |
| 335 } | 333 } |
| 336 | 334 |
| 337 } // namespace filesystem | 335 } // namespace filesystem |
| OLD | NEW |