Chromium Code Reviews| 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 "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "components/filesystem/file_impl.h" | 15 #include "components/filesystem/file_impl.h" |
| 16 #include "components/filesystem/lock_table.h" | 16 #include "components/filesystem/lock_table.h" |
| 17 #include "components/filesystem/util.h" | 17 #include "components/filesystem/util.h" |
| 18 #include "mojo/common/common_type_converters.h" | 18 #include "mojo/common/common_type_converters.h" |
| 19 #include "mojo/public/cpp/bindings/strong_binding.h" | 19 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 20 #include "mojo/public/cpp/system/platform_handle.h" | |
| 21 | 20 |
| 22 namespace filesystem { | 21 namespace filesystem { |
| 23 | 22 |
| 24 DirectoryImpl::DirectoryImpl(base::FilePath directory_path, | 23 DirectoryImpl::DirectoryImpl(base::FilePath directory_path, |
| 25 scoped_refptr<SharedTempDir> temp_dir, | 24 scoped_refptr<SharedTempDir> temp_dir, |
| 26 scoped_refptr<LockTable> lock_table) | 25 scoped_refptr<LockTable> lock_table) |
| 27 : directory_path_(directory_path), | 26 : directory_path_(directory_path), |
| 28 temp_dir_(std::move(temp_dir)), | 27 temp_dir_(std::move(temp_dir)), |
| 29 lock_table_(std::move(lock_table)) {} | 28 lock_table_(std::move(lock_table)) {} |
| 30 | 29 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 base::MakeUnique<FileImpl>(path, std::move(base_file), temp_dir_, | 84 base::MakeUnique<FileImpl>(path, std::move(base_file), temp_dir_, |
| 86 lock_table_), | 85 lock_table_), |
| 87 std::move(file)); | 86 std::move(file)); |
| 88 } | 87 } |
| 89 callback.Run(mojom::FileError::OK); | 88 callback.Run(mojom::FileError::OK); |
| 90 } | 89 } |
| 91 | 90 |
| 92 void DirectoryImpl::OpenFileHandle(const std::string& raw_path, | 91 void DirectoryImpl::OpenFileHandle(const std::string& raw_path, |
| 93 uint32_t open_flags, | 92 uint32_t open_flags, |
| 94 const OpenFileHandleCallback& callback) { | 93 const OpenFileHandleCallback& callback) { |
| 95 mojom::FileError error = mojom::FileError::OK; | 94 base::File file = OpenFileHandleImpl(raw_path, open_flags); |
| 96 mojo::ScopedHandle handle = OpenFileHandleImpl(raw_path, open_flags, &error); | 95 mojom::FileError error = GetError(file); |
| 97 callback.Run(error, std::move(handle)); | 96 callback.Run(error, std::move(file)); |
| 98 } | 97 } |
| 99 | 98 |
| 100 void DirectoryImpl::OpenFileHandles( | 99 void DirectoryImpl::OpenFileHandles( |
| 101 std::vector<mojom::FileOpenDetailsPtr> details, | 100 std::vector<mojom::FileOpenDetailsPtr> details, |
| 102 const OpenFileHandlesCallback& callback) { | 101 const OpenFileHandlesCallback& callback) { |
| 103 std::vector<mojom::FileOpenResultPtr> results(details.size()); | 102 std::vector<mojom::FileOpenResultPtr> results(details.size()); |
| 104 size_t i = 0; | 103 size_t i = 0; |
| 105 for (const auto& detail : details) { | 104 for (const auto& detail : details) { |
| 106 mojom::FileOpenResultPtr result(mojom::FileOpenResult::New()); | 105 mojom::FileOpenResultPtr result(mojom::FileOpenResult::New()); |
| 107 result->path = detail->path; | 106 result->path = detail->path; |
| 108 result->file_handle = | 107 result->file_handle = OpenFileHandleImpl(detail->path, detail->open_flags); |
| 109 OpenFileHandleImpl(detail->path, detail->open_flags, &result->error); | 108 result->error = GetError(result->file_handle); |
| 110 results[i++] = std::move(result); | 109 results[i++] = std::move(result); |
| 111 } | 110 } |
| 112 callback.Run(std::move(results)); | 111 callback.Run(std::move(results)); |
| 113 } | 112 } |
| 114 | 113 |
| 115 void DirectoryImpl::OpenDirectory(const std::string& raw_path, | 114 void DirectoryImpl::OpenDirectory(const std::string& raw_path, |
| 116 mojom::DirectoryRequest directory, | 115 mojom::DirectoryRequest directory, |
| 117 uint32_t open_flags, | 116 uint32_t open_flags, |
| 118 const OpenDirectoryCallback& callback) { | 117 const OpenDirectoryCallback& callback) { |
| 119 base::FilePath path; | 118 base::FilePath path; |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 328 if (base_file.Write(0, reinterpret_cast<const char*>(&data.front()), | 327 if (base_file.Write(0, reinterpret_cast<const char*>(&data.front()), |
| 329 data_size) == -1) { | 328 data_size) == -1) { |
| 330 callback.Run(GetError(base_file)); | 329 callback.Run(GetError(base_file)); |
| 331 return; | 330 return; |
| 332 } | 331 } |
| 333 } | 332 } |
| 334 | 333 |
| 335 callback.Run(mojom::FileError::OK); | 334 callback.Run(mojom::FileError::OK); |
| 336 } | 335 } |
| 337 | 336 |
| 338 mojo::ScopedHandle DirectoryImpl::OpenFileHandleImpl( | 337 base::File DirectoryImpl::OpenFileHandleImpl(const std::string& raw_path, |
| 339 const std::string& raw_path, | 338 uint32_t open_flags) { |
| 340 uint32_t open_flags, | |
| 341 mojom::FileError* error) { | |
| 342 base::FilePath path; | 339 base::FilePath path; |
| 343 *error = ValidatePath(raw_path, directory_path_, &path); | 340 mojom::FileError error = ValidatePath(raw_path, directory_path_, &path); |
| 344 if (*error != mojom::FileError::OK) | 341 if (error != mojom::FileError::OK) |
| 345 return mojo::ScopedHandle(); | 342 return base::File(static_cast<base::File::Error>(error)); |
|
dcheng
2016/12/05 19:33:04
This is a little ugly. Maybe we can/should just ty
Sam McNally
2016/12/06 23:15:18
Acknowledged.
| |
| 346 | 343 |
| 347 if (base::DirectoryExists(path)) { | 344 if (base::DirectoryExists(path)) { |
| 348 // We must not return directories as files. In the file abstraction, we | 345 // We must not return directories as files. In the file abstraction, we |
| 349 // can fetch raw file descriptors over mojo pipes, and passing a file | 346 // can fetch raw file descriptors over mojo pipes, and passing a file |
| 350 // descriptor to a directory is a sandbox escape on Windows. | 347 // descriptor to a directory is a sandbox escape on Windows. |
| 351 *error = mojom::FileError::NOT_A_FILE; | 348 return base::File(base::File::FILE_ERROR_NOT_A_FILE); |
| 352 return mojo::ScopedHandle(); | |
| 353 } | 349 } |
| 354 | 350 |
| 355 base::File base_file(path, open_flags); | 351 return base::File(path, open_flags); |
| 356 if (!base_file.IsValid()) { | |
| 357 *error = GetError(base_file); | |
| 358 return mojo::ScopedHandle(); | |
| 359 } | |
| 360 | |
| 361 *error = mojom::FileError::OK; | |
| 362 return mojo::WrapPlatformFile(base_file.TakePlatformFile()); | |
| 363 } | 352 } |
| 364 | 353 |
| 365 } // namespace filesystem | 354 } // namespace filesystem |
| OLD | NEW |