| 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 "base/files/file.h" | 7 #include "base/files/file.h" |
| 8 #include "base/files/file_enumerator.h" | 8 #include "base/files/file_enumerator.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 void DirectoryImpl::Read(const ReadCallback& callback) { | 30 void DirectoryImpl::Read(const ReadCallback& callback) { |
| 31 mojo::Array<DirectoryEntryPtr> entries(0); | 31 mojo::Array<DirectoryEntryPtr> entries(0); |
| 32 base::FileEnumerator directory_enumerator( | 32 base::FileEnumerator directory_enumerator( |
| 33 directory_path_, false, | 33 directory_path_, false, |
| 34 base::FileEnumerator::DIRECTORIES | base::FileEnumerator::FILES); | 34 base::FileEnumerator::DIRECTORIES | base::FileEnumerator::FILES); |
| 35 for (base::FilePath name = directory_enumerator.Next(); !name.empty(); | 35 for (base::FilePath name = directory_enumerator.Next(); !name.empty(); |
| 36 name = directory_enumerator.Next()) { | 36 name = directory_enumerator.Next()) { |
| 37 base::FileEnumerator::FileInfo info = directory_enumerator.GetInfo(); | 37 base::FileEnumerator::FileInfo info = directory_enumerator.GetInfo(); |
| 38 DirectoryEntryPtr entry = DirectoryEntry::New(); | 38 DirectoryEntryPtr entry = DirectoryEntry::New(); |
| 39 entry->type = info.IsDirectory() | 39 entry->type = info.IsDirectory() |
| 40 ? FS_FILE_TYPE_DIRECTORY : FS_FILE_TYPE_REGULAR_FILE; | 40 ? FILE_TYPE_DIRECTORY : FILE_TYPE_REGULAR_FILE; |
| 41 entry->name = info.GetName().AsUTF8Unsafe(); | 41 entry->name = info.GetName().AsUTF8Unsafe(); |
| 42 entries.push_back(entry.Pass()); | 42 entries.push_back(entry.Pass()); |
| 43 } | 43 } |
| 44 | 44 |
| 45 callback.Run(FILE_ERROR_OK, entries.Pass()); | 45 callback.Run(ERROR_OK, entries.Pass()); |
| 46 } | 46 } |
| 47 | 47 |
| 48 // TODO(erg): Consider adding an implementation of Stat()/Touch() to the | 48 // TODO(erg): Consider adding an implementation of Stat()/Touch() to the |
| 49 // directory, too. Right now, the base::File abstractions do not really deal | 49 // directory, too. Right now, the base::File abstractions do not really deal |
| 50 // with directories properly, so these are broken for now. | 50 // with directories properly, so these are broken for now. |
| 51 | 51 |
| 52 // TODO(vtl): Move the implementation to a thread pool. | 52 // TODO(vtl): Move the implementation to a thread pool. |
| 53 void DirectoryImpl::OpenFile(const mojo::String& raw_path, | 53 void DirectoryImpl::OpenFile(const mojo::String& raw_path, |
| 54 mojo::InterfaceRequest<File> file, | 54 mojo::InterfaceRequest<File> file, |
| 55 uint32_t open_flags, | 55 uint32_t open_flags, |
| 56 const OpenFileCallback& callback) { | 56 const OpenFileCallback& callback) { |
| 57 base::FilePath path; | 57 base::FilePath path; |
| 58 if (FileError error = ValidatePath(raw_path, directory_path_, &path)) { | 58 if (Error error = ValidatePath(raw_path, directory_path_, &path)) { |
| 59 callback.Run(error); | 59 callback.Run(error); |
| 60 return; | 60 return; |
| 61 } | 61 } |
| 62 | 62 |
| 63 base::File base_file(path, open_flags); | 63 base::File base_file(path, open_flags); |
| 64 if (!base_file.IsValid()) { | 64 if (!base_file.IsValid()) { |
| 65 callback.Run(FILE_ERROR_FAILED); | 65 callback.Run(ERROR_FAILED); |
| 66 return; | 66 return; |
| 67 } | 67 } |
| 68 | 68 |
| 69 base::File::Info info; | 69 base::File::Info info; |
| 70 if (!base_file.GetInfo(&info)) { | 70 if (!base_file.GetInfo(&info)) { |
| 71 callback.Run(FILE_ERROR_FAILED); | 71 callback.Run(ERROR_FAILED); |
| 72 return; | 72 return; |
| 73 } | 73 } |
| 74 | 74 |
| 75 if (info.is_directory) { | 75 if (info.is_directory) { |
| 76 // We must not return directories as files. In the file abstraction, we can | 76 // We must not return directories as files. In the file abstraction, we can |
| 77 // fetch raw file descriptors over mojo pipes, and passing a file | 77 // fetch raw file descriptors over mojo pipes, and passing a file |
| 78 // descriptor to a directory is a sandbox escape on Windows. | 78 // descriptor to a directory is a sandbox escape on Windows. |
| 79 callback.Run(FILE_ERROR_NOT_A_FILE); | 79 callback.Run(ERROR_NOT_A_FILE); |
| 80 return; | 80 return; |
| 81 } | 81 } |
| 82 | 82 |
| 83 if (file.is_pending()) { | 83 if (file.is_pending()) { |
| 84 new FileImpl(file.Pass(), base_file.Pass()); | 84 new FileImpl(file.Pass(), base_file.Pass()); |
| 85 } | 85 } |
| 86 callback.Run(FILE_ERROR_OK); | 86 callback.Run(ERROR_OK); |
| 87 } | 87 } |
| 88 | 88 |
| 89 void DirectoryImpl::OpenDirectory(const mojo::String& raw_path, | 89 void DirectoryImpl::OpenDirectory(const mojo::String& raw_path, |
| 90 mojo::InterfaceRequest<Directory> directory, | 90 mojo::InterfaceRequest<Directory> directory, |
| 91 uint32_t open_flags, | 91 uint32_t open_flags, |
| 92 const OpenDirectoryCallback& callback) { | 92 const OpenDirectoryCallback& callback) { |
| 93 base::FilePath path; | 93 base::FilePath path; |
| 94 if (FileError error = ValidatePath(raw_path, directory_path_, &path)) { | 94 if (Error error = ValidatePath(raw_path, directory_path_, &path)) { |
| 95 callback.Run(error); | 95 callback.Run(error); |
| 96 return; | 96 return; |
| 97 } | 97 } |
| 98 | 98 |
| 99 if (!base::DirectoryExists(path)) { | 99 if (!base::DirectoryExists(path)) { |
| 100 if (base::PathExists(path)) { | 100 if (base::PathExists(path)) { |
| 101 callback.Run(FILE_ERROR_NOT_A_DIRECTORY); | 101 callback.Run(ERROR_NOT_A_DIRECTORY); |
| 102 return; | 102 return; |
| 103 } | 103 } |
| 104 | 104 |
| 105 if (!(open_flags & kFlagOpenAlways || open_flags & kFlagCreate)) { | 105 if (!(open_flags & kFlagOpenAlways || open_flags & kFlagCreate)) { |
| 106 // The directory doesn't exist, and we weren't passed parameters to | 106 // The directory doesn't exist, and we weren't passed parameters to |
| 107 // create it. | 107 // create it. |
| 108 callback.Run(FILE_ERROR_NOT_FOUND); | 108 callback.Run(ERROR_NOT_FOUND); |
| 109 return; | 109 return; |
| 110 } | 110 } |
| 111 | 111 |
| 112 base::File::Error error; | 112 base::File::Error error; |
| 113 if (!base::CreateDirectoryAndGetError(path, &error)) { | 113 if (!base::CreateDirectoryAndGetError(path, &error)) { |
| 114 callback.Run(static_cast<filesystem::FileError>(error)); | 114 callback.Run(static_cast<filesystem::Error>(error)); |
| 115 return; | 115 return; |
| 116 } | 116 } |
| 117 } | 117 } |
| 118 | 118 |
| 119 if (directory.is_pending()) | 119 if (directory.is_pending()) |
| 120 new DirectoryImpl(directory.Pass(), path, | 120 new DirectoryImpl(directory.Pass(), path, |
| 121 scoped_ptr<base::ScopedTempDir>()); | 121 scoped_ptr<base::ScopedTempDir>()); |
| 122 callback.Run(FILE_ERROR_OK); | 122 callback.Run(ERROR_OK); |
| 123 } | 123 } |
| 124 | 124 |
| 125 void DirectoryImpl::Rename(const mojo::String& raw_old_path, | 125 void DirectoryImpl::Rename(const mojo::String& raw_old_path, |
| 126 const mojo::String& raw_new_path, | 126 const mojo::String& raw_new_path, |
| 127 const RenameCallback& callback) { | 127 const RenameCallback& callback) { |
| 128 base::FilePath old_path; | 128 base::FilePath old_path; |
| 129 if (FileError error = | 129 if (Error error = ValidatePath(raw_old_path, directory_path_, &old_path)) { |
| 130 ValidatePath(raw_old_path, directory_path_, &old_path)) { | |
| 131 callback.Run(error); | 130 callback.Run(error); |
| 132 return; | 131 return; |
| 133 } | 132 } |
| 134 | 133 |
| 135 base::FilePath new_path; | 134 base::FilePath new_path; |
| 136 if (FileError error = | 135 if (Error error = ValidatePath(raw_new_path, directory_path_, &new_path)) { |
| 137 ValidatePath(raw_new_path, directory_path_, &new_path)) { | |
| 138 callback.Run(error); | 136 callback.Run(error); |
| 139 return; | 137 return; |
| 140 } | 138 } |
| 141 | 139 |
| 142 if (!base::Move(old_path, new_path)) { | 140 if (!base::Move(old_path, new_path)) { |
| 143 callback.Run(FILE_ERROR_FAILED); | 141 callback.Run(ERROR_FAILED); |
| 144 return; | 142 return; |
| 145 } | 143 } |
| 146 | 144 |
| 147 callback.Run(FILE_ERROR_OK); | 145 callback.Run(ERROR_OK); |
| 148 } | 146 } |
| 149 | 147 |
| 150 void DirectoryImpl::Delete(const mojo::String& raw_path, | 148 void DirectoryImpl::Delete(const mojo::String& raw_path, |
| 151 uint32_t delete_flags, | 149 uint32_t delete_flags, |
| 152 const DeleteCallback& callback) { | 150 const DeleteCallback& callback) { |
| 153 base::FilePath path; | 151 base::FilePath path; |
| 154 if (FileError error = ValidatePath(raw_path, directory_path_, &path)) { | 152 if (Error error = ValidatePath(raw_path, directory_path_, &path)) { |
| 155 callback.Run(error); | 153 callback.Run(error); |
| 156 return; | 154 return; |
| 157 } | 155 } |
| 158 | 156 |
| 159 bool recursive = delete_flags & kDeleteFlagRecursive; | 157 bool recursive = delete_flags & kDeleteFlagRecursive; |
| 160 if (!base::DeleteFile(path, recursive)) { | 158 if (!base::DeleteFile(path, recursive)) { |
| 161 callback.Run(FILE_ERROR_FAILED); | 159 callback.Run(ERROR_FAILED); |
| 162 return; | 160 return; |
| 163 } | 161 } |
| 164 | 162 |
| 165 callback.Run(FILE_ERROR_OK); | 163 callback.Run(ERROR_OK); |
| 166 } | 164 } |
| 167 | 165 |
| 168 } // namespace filesystem | 166 } // namespace filesystem |
| OLD | NEW |