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 #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]. | |
|
ericu
2011/03/03 01:05:11
This comment is somewhat obsolete. We will need t
Dai Mikurube (google.com)
2011/03/03 20:24:52
Agreed. I've rewritten the TODO. Could you check w
ericu
2011/03/03 21:27:48
Yes, that looks good. Thanks.
| |
| 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) { | |
|
ericu
2011/03/03 01:05:11
In this method I mixed FileUtilProxy::Entry and Fi
Dai Mikurube (google.com)
2011/03/03 20:24:52
Agreed. Actually, I did the same and found that in
| |
| 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 (!file_util::PathExists(file_path.DirName())) | |
| 163 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 164 // If parent dir of file doesn't exist. | |
| 165 if (exclusive && path_exists) | |
| 166 return base::PLATFORM_FILE_ERROR_EXISTS; | |
| 167 // If file exists at the path. | |
| 168 if (path_exists && !file_util::DirectoryExists(file_path)) | |
| 169 return base::PLATFORM_FILE_ERROR_EXISTS; | |
| 170 if (!file_util::CreateDirectory(file_path)) | |
| 171 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 172 return base::PLATFORM_FILE_OK; | |
| 173 } | |
| 174 | |
| 175 PlatformFileError FileSystemFileUtil::Copy( | |
| 176 FileSystemOperationContext* unused, | |
| 177 const FilePath& src_file_path, | |
| 178 const FilePath& dest_file_path) { | |
| 179 PlatformFileError error_code; | |
| 180 error_code = | |
| 181 PerformCommonCheckAndPreparationForMoveAndCopy( | |
| 182 src_file_path, dest_file_path); | |
| 183 if (error_code != base::PLATFORM_FILE_OK) | |
| 184 return error_code; | |
| 185 if (!file_util::CopyDirectory(src_file_path, dest_file_path, | |
| 186 true /* recursive */)) | |
| 187 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 188 return base::PLATFORM_FILE_OK; | |
| 189 } | |
| 190 | |
| 191 PlatformFileError FileSystemFileUtil::Move( | |
| 192 FileSystemOperationContext* unused, | |
| 193 const FilePath& src_file_path, | |
| 194 const FilePath& dest_file_path) { | |
| 195 PlatformFileError error_code; | |
| 196 error_code = | |
| 197 PerformCommonCheckAndPreparationForMoveAndCopy( | |
| 198 src_file_path, dest_file_path); | |
| 199 if (error_code != base::PLATFORM_FILE_OK) | |
| 200 return error_code; | |
| 201 if (!file_util::Move(src_file_path, dest_file_path)) | |
| 202 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 203 return base::PLATFORM_FILE_OK; | |
| 204 } | |
| 205 | |
| 206 PlatformFileError FileSystemFileUtil::Delete( | |
| 207 FileSystemOperationContext* unused, | |
| 208 const FilePath& file_path, | |
| 209 bool recursive) { | |
| 210 if (!file_util::PathExists(file_path)) { | |
| 211 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 212 } | |
| 213 if (!file_util::Delete(file_path, recursive)) { | |
| 214 if (!recursive && !file_util::IsDirectoryEmpty(file_path)) { | |
| 215 return base::PLATFORM_FILE_ERROR_NOT_EMPTY; | |
| 216 } | |
| 217 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 218 } | |
| 219 return base::PLATFORM_FILE_OK; | |
| 220 } | |
| 221 | |
| 222 PlatformFileError FileSystemFileUtil::Touch( | |
| 223 FileSystemOperationContext* unused, | |
| 224 const FilePath& file_path, | |
| 225 const base::Time& last_access_time, | |
| 226 const base::Time& last_modified_time) { | |
| 227 if (!file_util::TouchFile( | |
| 228 file_path, last_access_time, last_modified_time)) | |
| 229 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 230 return base::PLATFORM_FILE_OK; | |
| 231 } | |
| 232 | |
| 233 PlatformFileError FileSystemFileUtil::Truncate( | |
| 234 FileSystemOperationContext* unused, | |
| 235 const FilePath& file_path, | |
| 236 int64 length) { | |
| 237 PlatformFileError error_code(base::PLATFORM_FILE_ERROR_FAILED); | |
| 238 PlatformFile file = | |
| 239 base::CreatePlatformFile( | |
| 240 file_path, | |
| 241 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, | |
| 242 NULL, | |
| 243 &error_code); | |
| 244 if (error_code != base::PLATFORM_FILE_OK) { | |
| 245 return error_code; | |
| 246 } | |
| 247 if (!base::TruncatePlatformFile(file, length)) | |
| 248 error_code = base::PLATFORM_FILE_ERROR_FAILED; | |
| 249 base::ClosePlatformFile(file); | |
| 250 return error_code; | |
| 251 } | |
| 252 | |
| 253 } // namespace fileapi | |
| OLD | NEW |