| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webkit/fileapi/file_system_file_util.h" |
| 6 |
| 7 #include "base/file_util_proxy.h" |
| 8 #include "webkit/fileapi/file_system_file_util_proxy.h" |
| 9 |
| 10 // This also removes the destination directory if it's non-empty and all other |
| 11 // checks are passed (so that the copy/move correctly overwrites the |
| 12 // destination). |
| 13 // TODO(ericu): This isn't exactly what we want for FileSystem, but we can make |
| 14 // it fit with minimal changes. The call to Delete will have to be virtualized |
| 15 // somehow. Not sure about the other file_util calls; it will depend on whether |
| 16 // or not we obfuscate before calling in [if needed]. |
| 17 static base::PlatformFileError PerformCommonCheckAndPreparationForMoveAndCopy( |
| 18 const FilePath& src_file_path, |
| 19 const FilePath& dest_file_path) { |
| 20 // Exits earlier if the source path does not exist. |
| 21 if (!file_util::PathExists(src_file_path)) |
| 22 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 23 |
| 24 // The parent of the |dest_file_path| does not exist. |
| 25 if (!file_util::DirectoryExists(dest_file_path.DirName())) |
| 26 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 27 |
| 28 // It is an error to try to copy/move an entry into its child. |
| 29 if (src_file_path.IsParent(dest_file_path)) |
| 30 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
| 31 |
| 32 // Now it is ok to return if the |dest_file_path| does not exist. |
| 33 if (!file_util::PathExists(dest_file_path)) |
| 34 return base::PLATFORM_FILE_OK; |
| 35 |
| 36 // |src_file_path| exists and is a directory. |
| 37 // |dest_file_path| exists and is a file. |
| 38 bool src_is_directory = file_util::DirectoryExists(src_file_path); |
| 39 bool dest_is_directory = file_util::DirectoryExists(dest_file_path); |
| 40 if (src_is_directory && !dest_is_directory) |
| 41 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; |
| 42 |
| 43 // |src_file_path| exists and is a file. |
| 44 // |dest_file_path| exists and is a directory. |
| 45 if (!src_is_directory && dest_is_directory) |
| 46 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; |
| 47 |
| 48 // It is an error to copy/move an entry into the same path. |
| 49 if (src_file_path.value() == dest_file_path.value()) |
| 50 return base::PLATFORM_FILE_ERROR_EXISTS; |
| 51 |
| 52 if (dest_is_directory) { |
| 53 // It is an error to copy/move an entry to a non-empty directory. |
| 54 // Otherwise the copy/move attempt must overwrite the destination, but |
| 55 // the file_util's Copy or Move method doesn't perform overwrite |
| 56 // on all platforms, so we delete the destination directory here. |
| 57 // TODO(kinuko): may be better to change the file_util::{Copy,Move}. |
| 58 if (!file_util::Delete(dest_file_path, false /* recursive */)) { |
| 59 if (!file_util::IsDirectoryEmpty(dest_file_path)) |
| 60 return base::PLATFORM_FILE_ERROR_NOT_EMPTY; |
| 61 return base::PLATFORM_FILE_ERROR_FAILED; |
| 62 } |
| 63 } |
| 64 return base::PLATFORM_FILE_OK; |
| 65 } |
| 66 |
| 67 namespace fileapi { |
| 68 |
| 69 FileSystemFileUtil* FileSystemFileUtil::GetInstance() { |
| 70 return Singleton<FileSystemFileUtil>::get(); |
| 71 } |
| 72 |
| 73 |
| 74 PlatformFileError FileSystemFileUtil::CreateOrOpen( |
| 75 FileSystemOperationContext* unused, |
| 76 const FilePath& file_path, int file_flags, |
| 77 PlatformFile* file_handle, bool* created) { |
| 78 if (!file_util::DirectoryExists(file_path.DirName())) { |
| 79 // If its parent does not exist, should return NOT_FOUND error. |
| 80 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 81 } |
| 82 PlatformFileError error_code = base::PLATFORM_FILE_OK; |
| 83 *file_handle = base::CreatePlatformFile(file_path, file_flags, |
| 84 created, &error_code); |
| 85 return error_code; |
| 86 } |
| 87 |
| 88 PlatformFileError FileSystemFileUtil::Close( |
| 89 FileSystemOperationContext* unused, |
| 90 PlatformFile file_handle) { |
| 91 if (!base::ClosePlatformFile(file_handle)) |
| 92 return base::PLATFORM_FILE_ERROR_FAILED; |
| 93 return base::PLATFORM_FILE_OK; |
| 94 } |
| 95 |
| 96 PlatformFileError FileSystemFileUtil::EnsureFileExists( |
| 97 FileSystemOperationContext* unused, |
| 98 const FilePath& file_path, |
| 99 bool* created) { |
| 100 if (!file_util::DirectoryExists(file_path.DirName())) |
| 101 // If its parent does not exist, should return NOT_FOUND error. |
| 102 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 103 PlatformFileError error_code = base::PLATFORM_FILE_OK; |
| 104 // Tries to create the |file_path| exclusively. This should fail |
| 105 // with base::PLATFORM_FILE_ERROR_EXISTS if the path already exists. |
| 106 PlatformFile handle = base::CreatePlatformFile( |
| 107 file_path, |
| 108 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, |
| 109 created, &error_code); |
| 110 if (error_code == base::PLATFORM_FILE_ERROR_EXISTS) { |
| 111 // Make sure created_ is false. |
| 112 *created = false; |
| 113 error_code = base::PLATFORM_FILE_OK; |
| 114 } |
| 115 if (handle != base::kInvalidPlatformFileValue) |
| 116 base::ClosePlatformFile(handle); |
| 117 return error_code; |
| 118 } |
| 119 |
| 120 PlatformFileError FileSystemFileUtil::GetFileInfo( |
| 121 FileSystemOperationContext* unused, |
| 122 const FilePath& file_path, |
| 123 base::PlatformFileInfo* file_info) { |
| 124 if (!file_util::PathExists(file_path)) |
| 125 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 126 if (!file_util::GetFileInfo(file_path, file_info)) |
| 127 return base::PLATFORM_FILE_ERROR_FAILED; |
| 128 return base::PLATFORM_FILE_OK; |
| 129 } |
| 130 |
| 131 PlatformFileError FileSystemFileUtil::ReadDirectory( |
| 132 FileSystemOperationContext* unused, |
| 133 const FilePath& file_path, |
| 134 std::vector<base::FileUtilProxy::Entry>* entries) { |
| 135 // TODO(kkanetkar): Implement directory read in multiple chunks. |
| 136 if (!file_util::DirectoryExists(file_path)) |
| 137 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 138 |
| 139 file_util::FileEnumerator file_enum( |
| 140 file_path, false, static_cast<file_util::FileEnumerator::FILE_TYPE>( |
| 141 file_util::FileEnumerator::FILES | |
| 142 file_util::FileEnumerator::DIRECTORIES)); |
| 143 FilePath current; |
| 144 while (!(current = file_enum.Next()).empty()) { |
| 145 fileapi::FileSystemFileUtilProxy::Entry entry; |
| 146 file_util::FileEnumerator::FindInfo info; |
| 147 file_enum.GetFindInfo(&info); |
| 148 entry.is_directory = file_enum.IsDirectory(info); |
| 149 // This will just give the entry's name instead of entire path |
| 150 // if we use current.value(). |
| 151 entry.name = file_util::FileEnumerator::GetFilename(info).value(); |
| 152 entries->push_back(entry); |
| 153 } |
| 154 return base::PLATFORM_FILE_OK; |
| 155 } |
| 156 |
| 157 PlatformFileError FileSystemFileUtil::CreateDirectory( |
| 158 FileSystemOperationContext* unused, |
| 159 const FilePath& file_path, |
| 160 bool exclusive) { |
| 161 bool path_exists = file_util::PathExists(file_path); |
| 162 // If parent dir of file doesn't exist. |
| 163 if (exclusive && path_exists) |
| 164 return base::PLATFORM_FILE_ERROR_EXISTS; |
| 165 // If file exists at the path. |
| 166 if (path_exists && !file_util::DirectoryExists(file_path)) |
| 167 return base::PLATFORM_FILE_ERROR_EXISTS; |
| 168 if (!file_util::CreateDirectory(file_path)) |
| 169 return base::PLATFORM_FILE_ERROR_FAILED; |
| 170 return base::PLATFORM_FILE_OK; |
| 171 } |
| 172 |
| 173 PlatformFileError FileSystemFileUtil::Copy( |
| 174 FileSystemOperationContext* unused, |
| 175 const FilePath& src_file_path, |
| 176 const FilePath& dest_file_path) { |
| 177 PlatformFileError error_code; |
| 178 error_code = |
| 179 PerformCommonCheckAndPreparationForMoveAndCopy( |
| 180 src_file_path, dest_file_path); |
| 181 if (error_code != base::PLATFORM_FILE_OK) |
| 182 return error_code; |
| 183 if (!file_util::CopyDirectory(src_file_path, dest_file_path, |
| 184 true /* recursive */)) |
| 185 return base::PLATFORM_FILE_ERROR_FAILED; |
| 186 return base::PLATFORM_FILE_OK; |
| 187 } |
| 188 |
| 189 PlatformFileError FileSystemFileUtil::Move( |
| 190 FileSystemOperationContext* unused, |
| 191 const FilePath& src_file_path, |
| 192 const FilePath& dest_file_path) { |
| 193 PlatformFileError error_code; |
| 194 error_code = |
| 195 PerformCommonCheckAndPreparationForMoveAndCopy( |
| 196 src_file_path, dest_file_path); |
| 197 if (error_code != base::PLATFORM_FILE_OK) |
| 198 return error_code; |
| 199 if (!file_util::Move(src_file_path, dest_file_path)) |
| 200 return base::PLATFORM_FILE_ERROR_FAILED; |
| 201 return base::PLATFORM_FILE_OK; |
| 202 } |
| 203 |
| 204 PlatformFileError FileSystemFileUtil::Delete( |
| 205 FileSystemOperationContext* unused, |
| 206 const FilePath& file_path, |
| 207 bool recursive) { |
| 208 if (!file_util::PathExists(file_path)) { |
| 209 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 210 } |
| 211 if (!file_util::Delete(file_path, recursive)) { |
| 212 if (!recursive && !file_util::IsDirectoryEmpty(file_path)) { |
| 213 return base::PLATFORM_FILE_ERROR_NOT_EMPTY; |
| 214 } |
| 215 return base::PLATFORM_FILE_ERROR_FAILED; |
| 216 } |
| 217 return base::PLATFORM_FILE_OK; |
| 218 } |
| 219 |
| 220 PlatformFileError FileSystemFileUtil::Touch( |
| 221 FileSystemOperationContext* unused, |
| 222 const FilePath& file_path, |
| 223 const base::Time& last_access_time, |
| 224 const base::Time& last_modified_time) { |
| 225 if (!file_util::TouchFile( |
| 226 file_path, last_access_time, last_modified_time)) |
| 227 return base::PLATFORM_FILE_ERROR_FAILED; |
| 228 return base::PLATFORM_FILE_OK; |
| 229 } |
| 230 |
| 231 PlatformFileError FileSystemFileUtil::Truncate( |
| 232 FileSystemOperationContext* unused, |
| 233 const FilePath& file_path, |
| 234 int64 length) { |
| 235 PlatformFileError error_code(base::PLATFORM_FILE_ERROR_FAILED); |
| 236 PlatformFile file = |
| 237 base::CreatePlatformFile( |
| 238 file_path, |
| 239 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
| 240 NULL, |
| 241 &error_code); |
| 242 if (error_code != base::PLATFORM_FILE_OK) { |
| 243 return error_code; |
| 244 } |
| 245 if (!base::TruncatePlatformFile(file, length)) |
| 246 error_code = base::PLATFORM_FILE_ERROR_FAILED; |
| 247 base::ClosePlatformFile(file); |
| 248 return error_code; |
| 249 } |
| 250 |
| 251 } // namespace fileapi |
| OLD | NEW |