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