| 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/platform_handle/platform_handle_functions.h" | 19 #include "mojo/platform_handle/platform_handle_functions.h" |
| 20 | 20 |
| 21 using mojo::ScopedHandle; | |
| 22 | |
| 23 namespace filesystem { | 21 namespace filesystem { |
| 24 | 22 |
| 25 DirectoryImpl::DirectoryImpl(mojo::InterfaceRequest<Directory> request, | 23 DirectoryImpl::DirectoryImpl(mojo::InterfaceRequest<Directory> request, |
| 26 base::FilePath directory_path, | 24 base::FilePath directory_path, |
| 27 scoped_refptr<SharedTempDir> temp_dir, | 25 scoped_refptr<SharedTempDir> temp_dir, |
| 28 scoped_refptr<LockTable> lock_table) | 26 scoped_refptr<LockTable> lock_table) |
| 29 : binding_(this, std::move(request)), | 27 : binding_(this, std::move(request)), |
| 30 directory_path_(directory_path), | 28 directory_path_(directory_path), |
| 31 temp_dir_(std::move(temp_dir)), | 29 temp_dir_(std::move(temp_dir)), |
| 32 lock_table_(std::move(lock_table)) {} | 30 lock_table_(std::move(lock_table)) {} |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 if (file.is_pending()) { | 82 if (file.is_pending()) { |
| 85 new FileImpl(std::move(file), path, std::move(base_file), temp_dir_, | 83 new FileImpl(std::move(file), path, std::move(base_file), temp_dir_, |
| 86 lock_table_); | 84 lock_table_); |
| 87 } | 85 } |
| 88 callback.Run(FileError::OK); | 86 callback.Run(FileError::OK); |
| 89 } | 87 } |
| 90 | 88 |
| 91 void DirectoryImpl::OpenFileHandle(const mojo::String& raw_path, | 89 void DirectoryImpl::OpenFileHandle(const mojo::String& raw_path, |
| 92 uint32_t open_flags, | 90 uint32_t open_flags, |
| 93 const OpenFileHandleCallback& callback) { | 91 const OpenFileHandleCallback& callback) { |
| 94 base::FilePath path; | 92 FileError error = FileError::OK; |
| 95 FileError error = ValidatePath(raw_path, directory_path_, &path); | 93 mojo::ScopedHandle handle = OpenFileHandleImpl(raw_path, open_flags, &error); |
| 96 if (error != FileError::OK) { | 94 callback.Run(error, std::move(handle)); |
| 97 callback.Run(error, ScopedHandle()); | 95 } |
| 98 return; | 96 |
| 97 void DirectoryImpl::OpenFileHandles(mojo::Array<FileOpenDetailsPtr> details, |
| 98 const OpenFileHandlesCallback& callback) { |
| 99 mojo::Array<FileOpenResultPtr> results( |
| 100 mojo::Array<FileOpenResultPtr>::New(details.size())); |
| 101 size_t i = 0; |
| 102 for (const auto& detail : details) { |
| 103 FileOpenResultPtr result(FileOpenResult::New()); |
| 104 result->path = detail->path; |
| 105 result->file_handle = |
| 106 OpenFileHandleImpl(detail->path, detail->open_flags, &result->error); |
| 107 results[i++] = std::move(result); |
| 99 } | 108 } |
| 100 | 109 callback.Run(std::move(results)); |
| 101 if (base::DirectoryExists(path)) { | |
| 102 // We must not return directories as files. In the file abstraction, we can | |
| 103 // fetch raw file descriptors over mojo pipes, and passing a file | |
| 104 // descriptor to a directory is a sandbox escape on Windows. | |
| 105 callback.Run(FileError::NOT_A_FILE, ScopedHandle()); | |
| 106 return; | |
| 107 } | |
| 108 | |
| 109 base::File base_file(path, open_flags); | |
| 110 if (!base_file.IsValid()) { | |
| 111 callback.Run(GetError(base_file), ScopedHandle()); | |
| 112 return; | |
| 113 } | |
| 114 | |
| 115 MojoHandle mojo_handle; | |
| 116 MojoResult create_result = MojoCreatePlatformHandleWrapper( | |
| 117 base_file.TakePlatformFile(), &mojo_handle); | |
| 118 if (create_result != MOJO_RESULT_OK) { | |
| 119 callback.Run(FileError::FAILED, ScopedHandle()); | |
| 120 return; | |
| 121 } | |
| 122 | |
| 123 callback.Run(FileError::OK, ScopedHandle(mojo::Handle(mojo_handle))); | |
| 124 } | 110 } |
| 125 | 111 |
| 126 void DirectoryImpl::OpenDirectory(const mojo::String& raw_path, | 112 void DirectoryImpl::OpenDirectory(const mojo::String& raw_path, |
| 127 mojo::InterfaceRequest<Directory> directory, | 113 mojo::InterfaceRequest<Directory> directory, |
| 128 uint32_t open_flags, | 114 uint32_t open_flags, |
| 129 const OpenDirectoryCallback& callback) { | 115 const OpenDirectoryCallback& callback) { |
| 130 base::FilePath path; | 116 base::FilePath path; |
| 131 FileError error = ValidatePath(raw_path, directory_path_, &path); | 117 FileError error = ValidatePath(raw_path, directory_path_, &path); |
| 132 if (error != FileError::OK) { | 118 if (error != FileError::OK) { |
| 133 callback.Run(error); | 119 callback.Run(error); |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 if (base_file.Write(0, reinterpret_cast<char*>(&data.front()), | 318 if (base_file.Write(0, reinterpret_cast<char*>(&data.front()), |
| 333 data_size) == -1) { | 319 data_size) == -1) { |
| 334 callback.Run(GetError(base_file)); | 320 callback.Run(GetError(base_file)); |
| 335 return; | 321 return; |
| 336 } | 322 } |
| 337 } | 323 } |
| 338 | 324 |
| 339 callback.Run(FileError::OK); | 325 callback.Run(FileError::OK); |
| 340 } | 326 } |
| 341 | 327 |
| 328 mojo::ScopedHandle DirectoryImpl::OpenFileHandleImpl( |
| 329 const mojo::String& raw_path, |
| 330 uint32_t open_flags, |
| 331 FileError* error) { |
| 332 base::FilePath path; |
| 333 *error = ValidatePath(raw_path, directory_path_, &path); |
| 334 if (*error != FileError::OK) |
| 335 return mojo::ScopedHandle(); |
| 336 |
| 337 if (base::DirectoryExists(path)) { |
| 338 // We must not return directories as files. In the file abstraction, we |
| 339 // can fetch raw file descriptors over mojo pipes, and passing a file |
| 340 // descriptor to a directory is a sandbox escape on Windows. |
| 341 *error = FileError::NOT_A_FILE; |
| 342 return mojo::ScopedHandle(); |
| 343 } |
| 344 |
| 345 base::File base_file(path, open_flags); |
| 346 if (!base_file.IsValid()) { |
| 347 *error = GetError(base_file); |
| 348 return mojo::ScopedHandle(); |
| 349 } |
| 350 |
| 351 MojoHandle mojo_handle; |
| 352 MojoResult create_result = MojoCreatePlatformHandleWrapper( |
| 353 base_file.TakePlatformFile(), &mojo_handle); |
| 354 if (create_result != MOJO_RESULT_OK) { |
| 355 *error = FileError::FAILED; |
| 356 return mojo::ScopedHandle(); |
| 357 } |
| 358 |
| 359 *error = FileError::OK; |
| 360 return mojo::ScopedHandle(mojo::Handle(mojo_handle)); |
| 361 } |
| 362 |
| 342 } // namespace filesystem | 363 } // namespace filesystem |
| OLD | NEW |