| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "webkit/fileapi/obfuscated_file_system_file_util.h" | 5 #include "webkit/fileapi/obfuscated_file_util.h" |
| 6 | 6 |
| 7 #include <queue> | 7 #include <queue> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/string_number_conversions.h" | 15 #include "base/string_number_conversions.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 const FilePath::CharType kTemporaryDirectoryName[] = FILE_PATH_LITERAL("t"); | 96 const FilePath::CharType kTemporaryDirectoryName[] = FILE_PATH_LITERAL("t"); |
| 97 const FilePath::CharType kPersistentDirectoryName[] = FILE_PATH_LITERAL("p"); | 97 const FilePath::CharType kPersistentDirectoryName[] = FILE_PATH_LITERAL("p"); |
| 98 | 98 |
| 99 } // namespace | 99 } // namespace |
| 100 | 100 |
| 101 namespace fileapi { | 101 namespace fileapi { |
| 102 | 102 |
| 103 using base::PlatformFile; | 103 using base::PlatformFile; |
| 104 using base::PlatformFileError; | 104 using base::PlatformFileError; |
| 105 | 105 |
| 106 ObfuscatedFileSystemFileUtil::ObfuscatedFileSystemFileUtil( | 106 class ObfuscatedFileEnumerator |
| 107 : public FileSystemFileUtil::AbstractFileEnumerator { |
| 108 public: |
| 109 ObfuscatedFileEnumerator( |
| 110 FileSystemDirectoryDatabase* db, const FilePath& virtual_root_path) |
| 111 : db_(db) { |
| 112 FileId file_id; |
| 113 FileInfo file_info; |
| 114 if (!db_->GetFileWithPath(virtual_root_path, &file_id)) |
| 115 return; |
| 116 if (!db_->GetFileInfo(file_id, &file_info)) |
| 117 return; |
| 118 if (!file_info.is_directory()) |
| 119 return; |
| 120 FileRecord record = { file_id, file_info, virtual_root_path }; |
| 121 display_queue_.push(record); |
| 122 Next(); // Enumerators don't include the directory itself. |
| 123 } |
| 124 |
| 125 ~ObfuscatedFileEnumerator() {} |
| 126 |
| 127 virtual FilePath Next() { |
| 128 ProcessRecurseQueue(); |
| 129 if (display_queue_.empty()) |
| 130 return FilePath(); |
| 131 current_ = display_queue_.front(); |
| 132 display_queue_.pop(); |
| 133 if (current_.file_info.is_directory()) |
| 134 recurse_queue_.push(current_); |
| 135 return current_.file_path; |
| 136 } |
| 137 |
| 138 virtual bool IsDirectory() { |
| 139 return current_.file_info.is_directory(); |
| 140 } |
| 141 |
| 142 private: |
| 143 typedef FileSystemDirectoryDatabase::FileId FileId; |
| 144 typedef FileSystemDirectoryDatabase::FileInfo FileInfo; |
| 145 |
| 146 struct FileRecord { |
| 147 FileId file_id; |
| 148 FileInfo file_info; |
| 149 FilePath file_path; |
| 150 }; |
| 151 |
| 152 void ProcessRecurseQueue() { |
| 153 while (display_queue_.empty() && !recurse_queue_.empty()) { |
| 154 FileRecord directory = recurse_queue_.front(); |
| 155 std::vector<FileId> children; |
| 156 recurse_queue_.pop(); |
| 157 if (!db_->ListChildren(directory.file_id, &children)) |
| 158 return; |
| 159 std::vector<FileId>::iterator iter; |
| 160 for (iter = children.begin(); iter != children.end(); ++iter) { |
| 161 FileRecord child; |
| 162 child.file_id = *iter; |
| 163 if (!db_->GetFileInfo(child.file_id, &child.file_info)) |
| 164 return; |
| 165 child.file_path = directory.file_path.Append(child.file_info.name); |
| 166 display_queue_.push(child); |
| 167 } |
| 168 } |
| 169 } |
| 170 |
| 171 std::queue<FileRecord> display_queue_; |
| 172 std::queue<FileRecord> recurse_queue_; |
| 173 FileRecord current_; |
| 174 FileSystemDirectoryDatabase* db_; |
| 175 }; |
| 176 |
| 177 class ObfuscatedOriginEnumerator |
| 178 : public ObfuscatedFileUtil::AbstractOriginEnumerator { |
| 179 public: |
| 180 typedef FileSystemOriginDatabase::OriginRecord OriginRecord; |
| 181 ObfuscatedOriginEnumerator( |
| 182 FileSystemOriginDatabase* origin_database, |
| 183 const FilePath& base_path) |
| 184 : base_path_(base_path) { |
| 185 if (origin_database) |
| 186 origin_database->ListAllOrigins(&origins_); |
| 187 } |
| 188 |
| 189 ~ObfuscatedOriginEnumerator() {} |
| 190 |
| 191 // Returns the next origin. Returns empty if there are no more origins. |
| 192 virtual GURL Next() OVERRIDE { |
| 193 OriginRecord record; |
| 194 if (!origins_.empty()) { |
| 195 record = origins_.back(); |
| 196 origins_.pop_back(); |
| 197 } |
| 198 current_ = record; |
| 199 return GetOriginURLFromIdentifier(record.origin); |
| 200 } |
| 201 |
| 202 // Returns the current origin's information. |
| 203 virtual bool HasFileSystemType(FileSystemType type) const OVERRIDE { |
| 204 if (current_.path.empty()) |
| 205 return false; |
| 206 FilePath::StringType type_string = |
| 207 ObfuscatedFileUtil::GetDirectoryNameForType(type); |
| 208 if (type_string.empty()) { |
| 209 NOTREACHED(); |
| 210 return false; |
| 211 } |
| 212 FilePath path = base_path_.Append(current_.path).Append(type_string); |
| 213 return file_util::DirectoryExists(path); |
| 214 } |
| 215 |
| 216 private: |
| 217 std::vector<OriginRecord> origins_; |
| 218 OriginRecord current_; |
| 219 FilePath base_path_; |
| 220 }; |
| 221 |
| 222 ObfuscatedFileUtil::ObfuscatedFileUtil( |
| 107 const FilePath& file_system_directory, | 223 const FilePath& file_system_directory, |
| 108 FileSystemFileUtil* underlying_file_util) | 224 FileSystemFileUtil* underlying_file_util) |
| 109 : file_system_directory_(file_system_directory), | 225 : FileSystemFileUtil(underlying_file_util), |
| 110 underlying_file_util_(underlying_file_util) { | 226 file_system_directory_(file_system_directory) { |
| 111 } | 227 } |
| 112 | 228 |
| 113 ObfuscatedFileSystemFileUtil::~ObfuscatedFileSystemFileUtil() { | 229 ObfuscatedFileUtil::~ObfuscatedFileUtil() { |
| 114 DropDatabases(); | 230 DropDatabases(); |
| 115 } | 231 } |
| 116 | 232 |
| 117 PlatformFileError ObfuscatedFileSystemFileUtil::CreateOrOpen( | 233 PlatformFileError ObfuscatedFileUtil::CreateOrOpen( |
| 118 FileSystemOperationContext* context, | 234 FileSystemOperationContext* context, |
| 119 const FilePath& virtual_path, int file_flags, | 235 const FilePath& virtual_path, int file_flags, |
| 120 PlatformFile* file_handle, bool* created) { | 236 PlatformFile* file_handle, bool* created) { |
| 121 DCHECK(!(file_flags & (base::PLATFORM_FILE_DELETE_ON_CLOSE | | 237 DCHECK(!(file_flags & (base::PLATFORM_FILE_DELETE_ON_CLOSE | |
| 122 base::PLATFORM_FILE_HIDDEN | base::PLATFORM_FILE_EXCLUSIVE_READ | | 238 base::PLATFORM_FILE_HIDDEN | base::PLATFORM_FILE_EXCLUSIVE_READ | |
| 123 base::PLATFORM_FILE_EXCLUSIVE_WRITE))); | 239 base::PLATFORM_FILE_EXCLUSIVE_WRITE))); |
| 124 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | 240 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 125 context->src_origin_url(), context->src_type(), true); | 241 context->src_origin_url(), context->src_type(), true); |
| 126 if (!db) | 242 if (!db) |
| 127 return base::PLATFORM_FILE_ERROR_FAILED; | 243 return base::PLATFORM_FILE_ERROR_FAILED; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 150 | 266 |
| 151 FileInfo file_info; | 267 FileInfo file_info; |
| 152 if (!db->GetFileInfo(file_id, &file_info)) { | 268 if (!db->GetFileInfo(file_id, &file_info)) { |
| 153 NOTREACHED(); | 269 NOTREACHED(); |
| 154 return base::PLATFORM_FILE_ERROR_FAILED; | 270 return base::PLATFORM_FILE_ERROR_FAILED; |
| 155 } | 271 } |
| 156 if (file_info.is_directory()) | 272 if (file_info.is_directory()) |
| 157 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; | 273 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; |
| 158 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), | 274 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), |
| 159 context->src_type(), file_info.data_path); | 275 context->src_type(), file_info.data_path); |
| 160 return underlying_file_util_->CreateOrOpen( | 276 return underlying_file_util()->CreateOrOpen( |
| 161 context, data_path, file_flags, file_handle, created); | 277 context, data_path, file_flags, file_handle, created); |
| 162 } | 278 } |
| 163 | 279 |
| 164 PlatformFileError ObfuscatedFileSystemFileUtil::EnsureFileExists( | 280 PlatformFileError ObfuscatedFileUtil::EnsureFileExists( |
| 165 FileSystemOperationContext* context, | 281 FileSystemOperationContext* context, |
| 166 const FilePath& virtual_path, | 282 const FilePath& virtual_path, |
| 167 bool* created) { | 283 bool* created) { |
| 168 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | 284 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 169 context->src_origin_url(), context->src_type(), true); | 285 context->src_origin_url(), context->src_type(), true); |
| 170 if (!db) | 286 if (!db) |
| 171 return base::PLATFORM_FILE_ERROR_FAILED; | 287 return base::PLATFORM_FILE_ERROR_FAILED; |
| 172 FileId file_id; | 288 FileId file_id; |
| 173 if (db->GetFileWithPath(virtual_path, &file_id)) { | 289 if (db->GetFileWithPath(virtual_path, &file_id)) { |
| 174 FileInfo file_info; | 290 FileInfo file_info; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 190 InitFileInfo(&file_info, parent_id, virtual_path.BaseName().value()); | 306 InitFileInfo(&file_info, parent_id, virtual_path.BaseName().value()); |
| 191 if (!AllocateQuotaForPath(context, 1, file_info.name.size())) | 307 if (!AllocateQuotaForPath(context, 1, file_info.name.size())) |
| 192 return base::PLATFORM_FILE_ERROR_NO_SPACE; | 308 return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 193 PlatformFileError error = CreateFile(context, context->src_origin_url(), | 309 PlatformFileError error = CreateFile(context, context->src_origin_url(), |
| 194 context->src_type(), FilePath(), &file_info, 0, NULL); | 310 context->src_type(), FilePath(), &file_info, 0, NULL); |
| 195 if (created && base::PLATFORM_FILE_OK == error) | 311 if (created && base::PLATFORM_FILE_OK == error) |
| 196 *created = true; | 312 *created = true; |
| 197 return error; | 313 return error; |
| 198 } | 314 } |
| 199 | 315 |
| 200 PlatformFileError ObfuscatedFileSystemFileUtil::GetLocalFilePath( | 316 PlatformFileError ObfuscatedFileUtil::CreateDirectory( |
| 201 FileSystemOperationContext* context, | 317 FileSystemOperationContext* context, |
| 202 const FilePath& virtual_path, | 318 const FilePath& virtual_path, |
| 203 FilePath* local_path) { | 319 bool exclusive, |
| 204 FilePath path = | 320 bool recursive) { |
| 205 GetLocalPath(context->src_origin_url(), context->src_type(), | 321 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 206 virtual_path); | 322 context->src_origin_url(), context->src_type(), true); |
| 207 if (path.empty()) | 323 if (!db) |
| 324 return base::PLATFORM_FILE_ERROR_FAILED; |
| 325 FileId file_id; |
| 326 if (db->GetFileWithPath(virtual_path, &file_id)) { |
| 327 FileInfo file_info; |
| 328 if (exclusive) |
| 329 return base::PLATFORM_FILE_ERROR_EXISTS; |
| 330 if (!db->GetFileInfo(file_id, &file_info)) { |
| 331 NOTREACHED(); |
| 332 return base::PLATFORM_FILE_ERROR_FAILED; |
| 333 } |
| 334 if (!file_info.is_directory()) |
| 335 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; |
| 336 return base::PLATFORM_FILE_OK; |
| 337 } |
| 338 |
| 339 std::vector<FilePath::StringType> components; |
| 340 virtual_path.GetComponents(&components); |
| 341 FileId parent_id = 0; |
| 342 size_t index; |
| 343 for (index = 0; index < components.size(); ++index) { |
| 344 FilePath::StringType name = components[index]; |
| 345 if (name == FILE_PATH_LITERAL("/")) |
| 346 continue; |
| 347 if (!db->GetChildWithName(parent_id, name, &parent_id)) |
| 348 break; |
| 349 } |
| 350 if (!recursive && components.size() - index > 1) |
| 208 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 351 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 209 | 352 for (; index < components.size(); ++index) { |
| 210 *local_path = path; | 353 FileInfo file_info; |
| 354 file_info.name = components[index]; |
| 355 if (file_info.name == FILE_PATH_LITERAL("/")) |
| 356 continue; |
| 357 file_info.modification_time = base::Time::Now(); |
| 358 file_info.parent_id = parent_id; |
| 359 if (!AllocateQuotaForPath(context, 1, file_info.name.size())) |
| 360 return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 361 if (!db->AddFileInfo(file_info, &parent_id)) { |
| 362 NOTREACHED(); |
| 363 return base::PLATFORM_FILE_ERROR_FAILED; |
| 364 } |
| 365 UpdatePathQuotaUsage(context, context->src_origin_url(), |
| 366 context->src_type(), 1, file_info.name.size()); |
| 367 } |
| 211 return base::PLATFORM_FILE_OK; | 368 return base::PLATFORM_FILE_OK; |
| 212 } | 369 } |
| 213 | 370 |
| 214 PlatformFileError ObfuscatedFileSystemFileUtil::GetFileInfo( | 371 PlatformFileError ObfuscatedFileUtil::GetFileInfo( |
| 215 FileSystemOperationContext* context, | 372 FileSystemOperationContext* context, |
| 216 const FilePath& virtual_path, | 373 const FilePath& virtual_path, |
| 217 base::PlatformFileInfo* file_info, | 374 base::PlatformFileInfo* file_info, |
| 218 FilePath* platform_file_path) { | 375 FilePath* platform_file_path) { |
| 219 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | 376 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 220 context->src_origin_url(), context->src_type(), false); | 377 context->src_origin_url(), context->src_type(), false); |
| 221 if (!db) | 378 if (!db) |
| 222 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 379 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 223 FileId file_id; | 380 FileId file_id; |
| 224 if (!db->GetFileWithPath(virtual_path, &file_id)) | 381 if (!db->GetFileWithPath(virtual_path, &file_id)) |
| 225 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 382 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 226 FileInfo local_info; | 383 FileInfo local_info; |
| 227 return GetFileInfoInternal(db, context, file_id, | 384 return GetFileInfoInternal(db, context, file_id, |
| 228 &local_info, file_info, platform_file_path); | 385 &local_info, file_info, platform_file_path); |
| 229 } | 386 } |
| 230 | 387 |
| 231 PlatformFileError ObfuscatedFileSystemFileUtil::ReadDirectory( | 388 PlatformFileError ObfuscatedFileUtil::ReadDirectory( |
| 232 FileSystemOperationContext* context, | 389 FileSystemOperationContext* context, |
| 233 const FilePath& virtual_path, | 390 const FilePath& virtual_path, |
| 234 std::vector<base::FileUtilProxy::Entry>* entries) { | 391 std::vector<base::FileUtilProxy::Entry>* entries) { |
| 235 // TODO(kkanetkar): Implement directory read in multiple chunks. | 392 // TODO(kkanetkar): Implement directory read in multiple chunks. |
| 236 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | 393 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 237 context->src_origin_url(), context->src_type(), false); | 394 context->src_origin_url(), context->src_type(), false); |
| 238 if (!db) { | 395 if (!db) { |
| 239 if (IsRootDirectory(virtual_path)) { | 396 if (IsRootDirectory(virtual_path)) { |
| 240 // It's the root directory and the database hasn't been initialized yet. | 397 // It's the root directory and the database hasn't been initialized yet. |
| 241 entries->clear(); | 398 entries->clear(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 base::FileUtilProxy::Entry entry; | 431 base::FileUtilProxy::Entry entry; |
| 275 entry.name = file_info.name; | 432 entry.name = file_info.name; |
| 276 entry.is_directory = file_info.is_directory(); | 433 entry.is_directory = file_info.is_directory(); |
| 277 entry.size = entry.is_directory ? 0 : platform_file_info.size; | 434 entry.size = entry.is_directory ? 0 : platform_file_info.size; |
| 278 entry.last_modified_time = platform_file_info.last_modified; | 435 entry.last_modified_time = platform_file_info.last_modified; |
| 279 entries->push_back(entry); | 436 entries->push_back(entry); |
| 280 } | 437 } |
| 281 return base::PLATFORM_FILE_OK; | 438 return base::PLATFORM_FILE_OK; |
| 282 } | 439 } |
| 283 | 440 |
| 284 PlatformFileError ObfuscatedFileSystemFileUtil::CreateDirectory( | 441 FileSystemFileUtil::AbstractFileEnumerator* |
| 442 ObfuscatedFileUtil::CreateFileEnumerator( |
| 443 FileSystemOperationContext* context, |
| 444 const FilePath& root_path) { |
| 445 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 446 context->src_origin_url(), context->src_type(), false); |
| 447 if (!db) |
| 448 return new FileSystemFileUtil::EmptyFileEnumerator(); |
| 449 return new ObfuscatedFileEnumerator(db, root_path); |
| 450 } |
| 451 |
| 452 PlatformFileError ObfuscatedFileUtil::GetLocalFilePath( |
| 285 FileSystemOperationContext* context, | 453 FileSystemOperationContext* context, |
| 286 const FilePath& virtual_path, | 454 const FilePath& virtual_path, |
| 287 bool exclusive, | 455 FilePath* local_path) { |
| 288 bool recursive) { | 456 FilePath path = |
| 289 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | 457 GetLocalPath(context->src_origin_url(), context->src_type(), |
| 290 context->src_origin_url(), context->src_type(), true); | 458 virtual_path); |
| 291 if (!db) | 459 if (path.empty()) |
| 292 return base::PLATFORM_FILE_ERROR_FAILED; | 460 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 293 FileId file_id; | |
| 294 if (db->GetFileWithPath(virtual_path, &file_id)) { | |
| 295 FileInfo file_info; | |
| 296 if (exclusive) | |
| 297 return base::PLATFORM_FILE_ERROR_EXISTS; | |
| 298 if (!db->GetFileInfo(file_id, &file_info)) { | |
| 299 NOTREACHED(); | |
| 300 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 301 } | |
| 302 if (!file_info.is_directory()) | |
| 303 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; | |
| 304 return base::PLATFORM_FILE_OK; | |
| 305 } | |
| 306 | 461 |
| 307 std::vector<FilePath::StringType> components; | 462 *local_path = path; |
| 308 virtual_path.GetComponents(&components); | |
| 309 FileId parent_id = 0; | |
| 310 size_t index; | |
| 311 for (index = 0; index < components.size(); ++index) { | |
| 312 FilePath::StringType name = components[index]; | |
| 313 if (name == FILE_PATH_LITERAL("/")) | |
| 314 continue; | |
| 315 if (!db->GetChildWithName(parent_id, name, &parent_id)) | |
| 316 break; | |
| 317 } | |
| 318 if (!recursive && components.size() - index > 1) | |
| 319 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 320 for (; index < components.size(); ++index) { | |
| 321 FileInfo file_info; | |
| 322 file_info.name = components[index]; | |
| 323 if (file_info.name == FILE_PATH_LITERAL("/")) | |
| 324 continue; | |
| 325 file_info.modification_time = base::Time::Now(); | |
| 326 file_info.parent_id = parent_id; | |
| 327 if (!AllocateQuotaForPath(context, 1, file_info.name.size())) | |
| 328 return base::PLATFORM_FILE_ERROR_NO_SPACE; | |
| 329 if (!db->AddFileInfo(file_info, &parent_id)) { | |
| 330 NOTREACHED(); | |
| 331 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 332 } | |
| 333 UpdatePathQuotaUsage(context, context->src_origin_url(), | |
| 334 context->src_type(), 1, file_info.name.size()); | |
| 335 } | |
| 336 return base::PLATFORM_FILE_OK; | 463 return base::PLATFORM_FILE_OK; |
| 337 } | 464 } |
| 338 | 465 |
| 339 PlatformFileError ObfuscatedFileSystemFileUtil::CopyOrMoveFile( | 466 PlatformFileError ObfuscatedFileUtil::Touch( |
| 340 FileSystemOperationContext* context, | |
| 341 const FilePath& src_file_path, | |
| 342 const FilePath& dest_file_path, | |
| 343 bool copy) { | |
| 344 // Cross-filesystem copies and moves should be handled via CopyInForeignFile. | |
| 345 DCHECK(context->src_origin_url() == context->dest_origin_url()); | |
| 346 DCHECK(context->src_type() == context->dest_type()); | |
| 347 | |
| 348 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 349 context->src_origin_url(), context->src_type(), true); | |
| 350 if (!db) | |
| 351 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 352 FileId src_file_id; | |
| 353 if (!db->GetFileWithPath(src_file_path, &src_file_id)) | |
| 354 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 355 FileId dest_file_id; | |
| 356 bool overwrite = db->GetFileWithPath(dest_file_path, &dest_file_id); | |
| 357 FileInfo src_file_info; | |
| 358 FileInfo dest_file_info; | |
| 359 if (!db->GetFileInfo(src_file_id, &src_file_info) || | |
| 360 src_file_info.is_directory()) { | |
| 361 NOTREACHED(); | |
| 362 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 363 } | |
| 364 if (overwrite) { | |
| 365 if (!db->GetFileInfo(dest_file_id, &dest_file_info) || | |
| 366 dest_file_info.is_directory()) { | |
| 367 NOTREACHED(); | |
| 368 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 369 } | |
| 370 } | |
| 371 /* | |
| 372 * Copy-with-overwrite | |
| 373 * Just overwrite data file | |
| 374 * Copy-without-overwrite | |
| 375 * Copy backing file | |
| 376 * Create new metadata pointing to new backing file. | |
| 377 * Move-with-overwrite | |
| 378 * transaction: | |
| 379 * Remove source entry. | |
| 380 * Point target entry to source entry's backing file. | |
| 381 * Delete target entry's old backing file | |
| 382 * Move-without-overwrite | |
| 383 * Just update metadata | |
| 384 */ | |
| 385 if (copy) { | |
| 386 FilePath src_data_path = DataPathToLocalPath(context->src_origin_url(), | |
| 387 context->src_type(), src_file_info.data_path); | |
| 388 if (overwrite) { | |
| 389 FilePath dest_data_path = DataPathToLocalPath(context->src_origin_url(), | |
| 390 context->src_type(), dest_file_info.data_path); | |
| 391 return underlying_file_util_->CopyOrMoveFile(context, | |
| 392 src_data_path, dest_data_path, copy); | |
| 393 } else { | |
| 394 FileId dest_parent_id; | |
| 395 if (!db->GetFileWithPath(dest_file_path.DirName(), &dest_parent_id)) { | |
| 396 NOTREACHED(); // We shouldn't be called in this case. | |
| 397 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 398 } | |
| 399 InitFileInfo(&dest_file_info, dest_parent_id, | |
| 400 dest_file_path.BaseName().value()); | |
| 401 if (!AllocateQuotaForPath(context, 1, dest_file_info.name.size())) | |
| 402 return base::PLATFORM_FILE_ERROR_NO_SPACE; | |
| 403 return CreateFile(context, context->dest_origin_url(), | |
| 404 context->dest_type(), src_data_path, &dest_file_info, 0, | |
| 405 NULL); | |
| 406 } | |
| 407 } else { // It's a move. | |
| 408 if (overwrite) { | |
| 409 AllocateQuotaForPath(context, -1, | |
| 410 -static_cast<int64>(src_file_info.name.size())); | |
| 411 if (!db->OverwritingMoveFile(src_file_id, dest_file_id)) | |
| 412 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 413 FilePath dest_data_path = DataPathToLocalPath(context->src_origin_url(), | |
| 414 context->src_type(), dest_file_info.data_path); | |
| 415 if (base::PLATFORM_FILE_OK != | |
| 416 underlying_file_util_->DeleteFile(context, dest_data_path)) | |
| 417 LOG(WARNING) << "Leaked a backing file."; | |
| 418 UpdatePathQuotaUsage(context, context->src_origin_url(), | |
| 419 context->src_type(), -1, | |
| 420 -static_cast<int64>(src_file_info.name.size())); | |
| 421 return base::PLATFORM_FILE_OK; | |
| 422 } else { | |
| 423 FileId dest_parent_id; | |
| 424 if (!db->GetFileWithPath(dest_file_path.DirName(), &dest_parent_id)) { | |
| 425 NOTREACHED(); | |
| 426 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 427 } | |
| 428 if (!AllocateQuotaForPath( | |
| 429 context, 0, | |
| 430 static_cast<int64>(dest_file_path.BaseName().value().size()) | |
| 431 - static_cast<int64>(src_file_info.name.size()))) | |
| 432 return base::PLATFORM_FILE_ERROR_NO_SPACE; | |
| 433 src_file_info.parent_id = dest_parent_id; | |
| 434 src_file_info.name = dest_file_path.BaseName().value(); | |
| 435 if (!db->UpdateFileInfo(src_file_id, src_file_info)) | |
| 436 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 437 UpdatePathQuotaUsage( | |
| 438 context, context->src_origin_url(), context->src_type(), 0, | |
| 439 static_cast<int64>(dest_file_path.BaseName().value().size()) - | |
| 440 static_cast<int64>(src_file_path.BaseName().value().size())); | |
| 441 return base::PLATFORM_FILE_OK; | |
| 442 } | |
| 443 } | |
| 444 NOTREACHED(); | |
| 445 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 446 } | |
| 447 | |
| 448 PlatformFileError ObfuscatedFileSystemFileUtil::CopyInForeignFile( | |
| 449 FileSystemOperationContext* context, | |
| 450 const FilePath& src_file_path, | |
| 451 const FilePath& dest_file_path) { | |
| 452 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 453 context->dest_origin_url(), context->dest_type(), true); | |
| 454 if (!db) | |
| 455 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 456 FileId dest_file_id; | |
| 457 bool overwrite = db->GetFileWithPath(dest_file_path, &dest_file_id); | |
| 458 FileInfo dest_file_info; | |
| 459 if (overwrite) { | |
| 460 if (!db->GetFileInfo(dest_file_id, &dest_file_info) || | |
| 461 dest_file_info.is_directory()) { | |
| 462 NOTREACHED(); | |
| 463 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 464 } | |
| 465 FilePath dest_data_path = DataPathToLocalPath(context->dest_origin_url(), | |
| 466 context->dest_type(), dest_file_info.data_path); | |
| 467 return underlying_file_util_->CopyOrMoveFile(context, | |
| 468 src_file_path, dest_data_path, true /* copy */); | |
| 469 } else { | |
| 470 FileId dest_parent_id; | |
| 471 if (!db->GetFileWithPath(dest_file_path.DirName(), &dest_parent_id)) { | |
| 472 NOTREACHED(); // We shouldn't be called in this case. | |
| 473 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 474 } | |
| 475 InitFileInfo(&dest_file_info, dest_parent_id, | |
| 476 dest_file_path.BaseName().value()); | |
| 477 if (!AllocateQuotaForPath(context, 1, dest_file_info.name.size())) | |
| 478 return base::PLATFORM_FILE_ERROR_NO_SPACE; | |
| 479 return CreateFile(context, context->dest_origin_url(), | |
| 480 context->dest_type(), src_file_path, &dest_file_info, 0, NULL); | |
| 481 } | |
| 482 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 483 } | |
| 484 | |
| 485 PlatformFileError ObfuscatedFileSystemFileUtil::DeleteFile( | |
| 486 FileSystemOperationContext* context, | |
| 487 const FilePath& virtual_path) { | |
| 488 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 489 context->src_origin_url(), context->src_type(), true); | |
| 490 if (!db) | |
| 491 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 492 FileId file_id; | |
| 493 if (!db->GetFileWithPath(virtual_path, &file_id)) | |
| 494 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 495 FileInfo file_info; | |
| 496 if (!db->GetFileInfo(file_id, &file_info) || file_info.is_directory()) { | |
| 497 NOTREACHED(); | |
| 498 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 499 } | |
| 500 if (!db->RemoveFileInfo(file_id)) { | |
| 501 NOTREACHED(); | |
| 502 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 503 } | |
| 504 AllocateQuotaForPath(context, -1, -static_cast<int64>(file_info.name.size())); | |
| 505 UpdatePathQuotaUsage(context, context->src_origin_url(), context->src_type(), | |
| 506 -1, -static_cast<int64>(file_info.name.size())); | |
| 507 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), | |
| 508 context->src_type(), file_info.data_path); | |
| 509 if (base::PLATFORM_FILE_OK != | |
| 510 underlying_file_util_->DeleteFile(context, data_path)) | |
| 511 LOG(WARNING) << "Leaked a backing file."; | |
| 512 return base::PLATFORM_FILE_OK; | |
| 513 } | |
| 514 | |
| 515 PlatformFileError ObfuscatedFileSystemFileUtil::DeleteSingleDirectory( | |
| 516 FileSystemOperationContext* context, | |
| 517 const FilePath& virtual_path) { | |
| 518 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 519 context->src_origin_url(), context->src_type(), true); | |
| 520 if (!db) | |
| 521 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 522 FileId file_id; | |
| 523 if (!db->GetFileWithPath(virtual_path, &file_id)) | |
| 524 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 525 FileInfo file_info; | |
| 526 if (!db->GetFileInfo(file_id, &file_info) || !file_info.is_directory()) { | |
| 527 NOTREACHED(); | |
| 528 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 529 } | |
| 530 if (!db->RemoveFileInfo(file_id)) | |
| 531 return base::PLATFORM_FILE_ERROR_NOT_EMPTY; | |
| 532 AllocateQuotaForPath(context, -1, -static_cast<int64>(file_info.name.size())); | |
| 533 UpdatePathQuotaUsage(context, context->src_origin_url(), context->src_type(), | |
| 534 -1, -static_cast<int64>(file_info.name.size())); | |
| 535 return base::PLATFORM_FILE_OK; | |
| 536 } | |
| 537 | |
| 538 PlatformFileError ObfuscatedFileSystemFileUtil::Touch( | |
| 539 FileSystemOperationContext* context, | 467 FileSystemOperationContext* context, |
| 540 const FilePath& virtual_path, | 468 const FilePath& virtual_path, |
| 541 const base::Time& last_access_time, | 469 const base::Time& last_access_time, |
| 542 const base::Time& last_modified_time) { | 470 const base::Time& last_modified_time) { |
| 543 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | 471 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 544 context->src_origin_url(), context->src_type(), false); | 472 context->src_origin_url(), context->src_type(), false); |
| 545 if (!db) | 473 if (!db) |
| 546 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 474 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 547 FileId file_id; | 475 FileId file_id; |
| 548 if (!db->GetFileWithPath(virtual_path, &file_id)) | 476 if (!db->GetFileWithPath(virtual_path, &file_id)) |
| 549 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 477 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 550 | 478 |
| 551 FileInfo file_info; | 479 FileInfo file_info; |
| 552 if (!db->GetFileInfo(file_id, &file_info)) { | 480 if (!db->GetFileInfo(file_id, &file_info)) { |
| 553 NOTREACHED(); | 481 NOTREACHED(); |
| 554 return base::PLATFORM_FILE_ERROR_FAILED; | 482 return base::PLATFORM_FILE_ERROR_FAILED; |
| 555 } | 483 } |
| 556 if (file_info.is_directory()) { | 484 if (file_info.is_directory()) { |
| 557 file_info.modification_time = last_modified_time; | 485 file_info.modification_time = last_modified_time; |
| 558 if (!db->UpdateFileInfo(file_id, file_info)) | 486 if (!db->UpdateFileInfo(file_id, file_info)) |
| 559 return base::PLATFORM_FILE_ERROR_FAILED; | 487 return base::PLATFORM_FILE_ERROR_FAILED; |
| 560 return base::PLATFORM_FILE_OK; | 488 return base::PLATFORM_FILE_OK; |
| 561 } | 489 } |
| 562 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), | 490 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), |
| 563 context->src_type(), file_info.data_path); | 491 context->src_type(), file_info.data_path); |
| 564 return underlying_file_util_->Touch( | 492 return underlying_file_util()->Touch( |
| 565 context, data_path, last_access_time, last_modified_time); | 493 context, data_path, last_access_time, last_modified_time); |
| 566 } | 494 } |
| 567 | 495 |
| 568 PlatformFileError ObfuscatedFileSystemFileUtil::Truncate( | 496 PlatformFileError ObfuscatedFileUtil::Truncate( |
| 569 FileSystemOperationContext* context, | 497 FileSystemOperationContext* context, |
| 570 const FilePath& virtual_path, | 498 const FilePath& virtual_path, |
| 571 int64 length) { | 499 int64 length) { |
| 572 FilePath local_path = | 500 FilePath local_path = |
| 573 GetLocalPath(context->src_origin_url(), context->src_type(), | 501 GetLocalPath(context->src_origin_url(), context->src_type(), |
| 574 virtual_path); | 502 virtual_path); |
| 575 if (local_path.empty()) | 503 if (local_path.empty()) |
| 576 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 504 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 577 return underlying_file_util_->Truncate( | 505 return underlying_file_util()->Truncate( |
| 578 context, local_path, length); | 506 context, local_path, length); |
| 579 } | 507 } |
| 580 | 508 |
| 581 bool ObfuscatedFileSystemFileUtil::PathExists( | 509 bool ObfuscatedFileUtil::PathExists( |
| 582 FileSystemOperationContext* context, | 510 FileSystemOperationContext* context, |
| 583 const FilePath& virtual_path) { | 511 const FilePath& virtual_path) { |
| 584 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | 512 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 585 context->src_origin_url(), context->src_type(), false); | 513 context->src_origin_url(), context->src_type(), false); |
| 586 if (!db) | 514 if (!db) |
| 587 return false; | 515 return false; |
| 588 FileId file_id; | 516 FileId file_id; |
| 589 return db->GetFileWithPath(virtual_path, &file_id); | 517 return db->GetFileWithPath(virtual_path, &file_id); |
| 590 } | 518 } |
| 591 | 519 |
| 592 bool ObfuscatedFileSystemFileUtil::DirectoryExists( | 520 bool ObfuscatedFileUtil::DirectoryExists( |
| 593 FileSystemOperationContext* context, | 521 FileSystemOperationContext* context, |
| 594 const FilePath& virtual_path) { | 522 const FilePath& virtual_path) { |
| 595 if (IsRootDirectory(virtual_path)) { | 523 if (IsRootDirectory(virtual_path)) { |
| 596 // It's questionable whether we should return true or false for the | 524 // It's questionable whether we should return true or false for the |
| 597 // root directory of nonexistent origin, but here we return true | 525 // root directory of nonexistent origin, but here we return true |
| 598 // as the current implementation of ReadDirectory always returns an empty | 526 // as the current implementation of ReadDirectory always returns an empty |
| 599 // array (rather than erroring out with NOT_FOUND_ERR even) for | 527 // array (rather than erroring out with NOT_FOUND_ERR even) for |
| 600 // nonexistent origins. | 528 // nonexistent origins. |
| 601 // Note: if you're going to change this behavior please also consider | 529 // Note: if you're going to change this behavior please also consider |
| 602 // changiing the ReadDirectory's behavior! | 530 // changiing the ReadDirectory's behavior! |
| 603 return true; | 531 return true; |
| 604 } | 532 } |
| 605 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | 533 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 606 context->src_origin_url(), context->src_type(), false); | 534 context->src_origin_url(), context->src_type(), false); |
| 607 if (!db) | 535 if (!db) |
| 608 return false; | 536 return false; |
| 609 FileId file_id; | 537 FileId file_id; |
| 610 if (!db->GetFileWithPath(virtual_path, &file_id)) | 538 if (!db->GetFileWithPath(virtual_path, &file_id)) |
| 611 return false; | 539 return false; |
| 612 FileInfo file_info; | 540 FileInfo file_info; |
| 613 if (!db->GetFileInfo(file_id, &file_info)) { | 541 if (!db->GetFileInfo(file_id, &file_info)) { |
| 614 NOTREACHED(); | 542 NOTREACHED(); |
| 615 return false; | 543 return false; |
| 616 } | 544 } |
| 617 return file_info.is_directory(); | 545 return file_info.is_directory(); |
| 618 } | 546 } |
| 619 | 547 |
| 620 bool ObfuscatedFileSystemFileUtil::IsDirectoryEmpty( | 548 bool ObfuscatedFileUtil::IsDirectoryEmpty( |
| 621 FileSystemOperationContext* context, | 549 FileSystemOperationContext* context, |
| 622 const FilePath& virtual_path) { | 550 const FilePath& virtual_path) { |
| 623 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | 551 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 624 context->src_origin_url(), context->src_type(), false); | 552 context->src_origin_url(), context->src_type(), false); |
| 625 if (!db) | 553 if (!db) |
| 626 return true; // Not a great answer, but it's what others do. | 554 return true; // Not a great answer, but it's what others do. |
| 627 FileId file_id; | 555 FileId file_id; |
| 628 if (!db->GetFileWithPath(virtual_path, &file_id)) | 556 if (!db->GetFileWithPath(virtual_path, &file_id)) |
| 629 return true; // Ditto. | 557 return true; // Ditto. |
| 630 FileInfo file_info; | 558 FileInfo file_info; |
| 631 if (!db->GetFileInfo(file_id, &file_info)) { | 559 if (!db->GetFileInfo(file_id, &file_info)) { |
| 632 DCHECK(!file_id); | 560 DCHECK(!file_id); |
| 633 // It's the root directory and the database hasn't been initialized yet. | 561 // It's the root directory and the database hasn't been initialized yet. |
| 634 return true; | 562 return true; |
| 635 } | 563 } |
| 636 if (!file_info.is_directory()) | 564 if (!file_info.is_directory()) |
| 637 return true; | 565 return true; |
| 638 std::vector<FileId> children; | 566 std::vector<FileId> children; |
| 639 // TODO(ericu): This could easily be made faster with help from the database. | 567 // TODO(ericu): This could easily be made faster with help from the database. |
| 640 if (!db->ListChildren(file_id, &children)) | 568 if (!db->ListChildren(file_id, &children)) |
| 641 return true; | 569 return true; |
| 642 return children.empty(); | 570 return children.empty(); |
| 643 } | 571 } |
| 644 | 572 |
| 645 class ObfuscatedFileSystemFileEnumerator | 573 PlatformFileError ObfuscatedFileUtil::CopyOrMoveFile( |
| 646 : public FileSystemFileUtil::AbstractFileEnumerator { | 574 FileSystemOperationContext* context, |
| 647 public: | 575 const FilePath& src_file_path, |
| 648 ObfuscatedFileSystemFileEnumerator( | 576 const FilePath& dest_file_path, |
| 649 FileSystemDirectoryDatabase* db, const FilePath& virtual_root_path) | 577 bool copy) { |
| 650 : db_(db) { | 578 // Cross-filesystem copies and moves should be handled via CopyInForeignFile. |
| 651 FileId file_id; | 579 DCHECK(context->src_origin_url() == context->dest_origin_url()); |
| 652 FileInfo file_info; | 580 DCHECK(context->src_type() == context->dest_type()); |
| 653 if (!db_->GetFileWithPath(virtual_root_path, &file_id)) | 581 |
| 654 return; | 582 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 655 if (!db_->GetFileInfo(file_id, &file_info)) | 583 context->src_origin_url(), context->src_type(), true); |
| 656 return; | 584 if (!db) |
| 657 if (!file_info.is_directory()) | 585 return base::PLATFORM_FILE_ERROR_FAILED; |
| 658 return; | 586 FileId src_file_id; |
| 659 FileRecord record = { file_id, file_info, virtual_root_path }; | 587 if (!db->GetFileWithPath(src_file_path, &src_file_id)) |
| 660 display_queue_.push(record); | 588 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 661 Next(); // Enumerators don't include the directory itself. | 589 FileId dest_file_id; |
| 662 } | 590 bool overwrite = db->GetFileWithPath(dest_file_path, &dest_file_id); |
| 663 | 591 FileInfo src_file_info; |
| 664 ~ObfuscatedFileSystemFileEnumerator() {} | 592 FileInfo dest_file_info; |
| 665 | 593 if (!db->GetFileInfo(src_file_id, &src_file_info) || |
| 666 virtual FilePath Next() { | 594 src_file_info.is_directory()) { |
| 667 ProcessRecurseQueue(); | 595 NOTREACHED(); |
| 668 if (display_queue_.empty()) | 596 return base::PLATFORM_FILE_ERROR_FAILED; |
| 669 return FilePath(); | 597 } |
| 670 current_ = display_queue_.front(); | 598 if (overwrite) { |
| 671 display_queue_.pop(); | 599 if (!db->GetFileInfo(dest_file_id, &dest_file_info) || |
| 672 if (current_.file_info.is_directory()) | 600 dest_file_info.is_directory()) { |
| 673 recurse_queue_.push(current_); | 601 NOTREACHED(); |
| 674 return current_.file_path; | 602 return base::PLATFORM_FILE_ERROR_FAILED; |
| 675 } | 603 } |
| 676 | 604 } |
| 677 virtual bool IsDirectory() { | 605 /* |
| 678 return current_.file_info.is_directory(); | 606 * Copy-with-overwrite |
| 679 } | 607 * Just overwrite data file |
| 680 | 608 * Copy-without-overwrite |
| 681 private: | 609 * Copy backing file |
| 682 typedef FileSystemDirectoryDatabase::FileId FileId; | 610 * Create new metadata pointing to new backing file. |
| 683 typedef FileSystemDirectoryDatabase::FileInfo FileInfo; | 611 * Move-with-overwrite |
| 684 | 612 * transaction: |
| 685 struct FileRecord { | 613 * Remove source entry. |
| 686 FileId file_id; | 614 * Point target entry to source entry's backing file. |
| 687 FileInfo file_info; | 615 * Delete target entry's old backing file |
| 688 FilePath file_path; | 616 * Move-without-overwrite |
| 689 }; | 617 * Just update metadata |
| 690 | 618 */ |
| 691 void ProcessRecurseQueue() { | 619 if (copy) { |
| 692 while (display_queue_.empty() && !recurse_queue_.empty()) { | 620 FilePath src_data_path = DataPathToLocalPath(context->src_origin_url(), |
| 693 FileRecord directory = recurse_queue_.front(); | 621 context->src_type(), src_file_info.data_path); |
| 694 std::vector<FileId> children; | 622 if (overwrite) { |
| 695 recurse_queue_.pop(); | 623 FilePath dest_data_path = DataPathToLocalPath(context->src_origin_url(), |
| 696 if (!db_->ListChildren(directory.file_id, &children)) | 624 context->src_type(), dest_file_info.data_path); |
| 697 return; | 625 return underlying_file_util()->CopyOrMoveFile(context, |
| 698 std::vector<FileId>::iterator iter; | 626 src_data_path, dest_data_path, copy); |
| 699 for (iter = children.begin(); iter != children.end(); ++iter) { | 627 } else { |
| 700 FileRecord child; | 628 FileId dest_parent_id; |
| 701 child.file_id = *iter; | 629 if (!db->GetFileWithPath(dest_file_path.DirName(), &dest_parent_id)) { |
| 702 if (!db_->GetFileInfo(child.file_id, &child.file_info)) | 630 NOTREACHED(); // We shouldn't be called in this case. |
| 703 return; | 631 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 704 child.file_path = directory.file_path.Append(child.file_info.name); | |
| 705 display_queue_.push(child); | |
| 706 } | 632 } |
| 707 } | 633 InitFileInfo(&dest_file_info, dest_parent_id, |
| 708 } | 634 dest_file_path.BaseName().value()); |
| 709 | 635 if (!AllocateQuotaForPath(context, 1, dest_file_info.name.size())) |
| 710 std::queue<FileRecord> display_queue_; | 636 return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 711 std::queue<FileRecord> recurse_queue_; | 637 return CreateFile(context, context->dest_origin_url(), |
| 712 FileRecord current_; | 638 context->dest_type(), src_data_path, &dest_file_info, 0, |
| 713 FileSystemDirectoryDatabase* db_; | 639 NULL); |
| 714 }; | 640 } |
| 715 | 641 } else { // It's a move. |
| 716 class ObfuscatedFileSystemOriginEnumerator | 642 if (overwrite) { |
| 717 : public ObfuscatedFileSystemFileUtil::AbstractOriginEnumerator { | 643 AllocateQuotaForPath(context, -1, |
| 718 public: | 644 -static_cast<int64>(src_file_info.name.size())); |
| 719 typedef FileSystemOriginDatabase::OriginRecord OriginRecord; | 645 if (!db->OverwritingMoveFile(src_file_id, dest_file_id)) |
| 720 ObfuscatedFileSystemOriginEnumerator( | 646 return base::PLATFORM_FILE_ERROR_FAILED; |
| 721 FileSystemOriginDatabase* origin_database, | 647 FilePath dest_data_path = DataPathToLocalPath(context->src_origin_url(), |
| 722 const FilePath& base_path) | 648 context->src_type(), dest_file_info.data_path); |
| 723 : base_path_(base_path) { | 649 if (base::PLATFORM_FILE_OK != |
| 724 if (origin_database) | 650 underlying_file_util()->DeleteFile(context, dest_data_path)) |
| 725 origin_database->ListAllOrigins(&origins_); | 651 LOG(WARNING) << "Leaked a backing file."; |
| 726 } | 652 UpdatePathQuotaUsage(context, context->src_origin_url(), |
| 727 | 653 context->src_type(), -1, |
| 728 ~ObfuscatedFileSystemOriginEnumerator() {} | 654 -static_cast<int64>(src_file_info.name.size())); |
| 729 | 655 return base::PLATFORM_FILE_OK; |
| 730 // Returns the next origin. Returns empty if there are no more origins. | 656 } else { |
| 731 virtual GURL Next() OVERRIDE { | 657 FileId dest_parent_id; |
| 732 OriginRecord record; | 658 if (!db->GetFileWithPath(dest_file_path.DirName(), &dest_parent_id)) { |
| 733 if (!origins_.empty()) { | 659 NOTREACHED(); |
| 734 record = origins_.back(); | 660 return base::PLATFORM_FILE_ERROR_FAILED; |
| 735 origins_.pop_back(); | 661 } |
| 736 } | 662 if (!AllocateQuotaForPath( |
| 737 current_ = record; | 663 context, 0, |
| 738 return GetOriginURLFromIdentifier(record.origin); | 664 static_cast<int64>(dest_file_path.BaseName().value().size()) |
| 739 } | 665 - static_cast<int64>(src_file_info.name.size()))) |
| 740 | 666 return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 741 // Returns the current origin's information. | 667 src_file_info.parent_id = dest_parent_id; |
| 742 virtual bool HasFileSystemType(FileSystemType type) const OVERRIDE { | 668 src_file_info.name = dest_file_path.BaseName().value(); |
| 743 if (current_.path.empty()) | 669 if (!db->UpdateFileInfo(src_file_id, src_file_info)) |
| 744 return false; | 670 return base::PLATFORM_FILE_ERROR_FAILED; |
| 745 FilePath::StringType type_string = | 671 UpdatePathQuotaUsage( |
| 746 ObfuscatedFileSystemFileUtil::GetDirectoryNameForType(type); | 672 context, context->src_origin_url(), context->src_type(), 0, |
| 747 if (type_string.empty()) { | 673 static_cast<int64>(dest_file_path.BaseName().value().size()) - |
| 674 static_cast<int64>(src_file_path.BaseName().value().size())); |
| 675 return base::PLATFORM_FILE_OK; |
| 676 } |
| 677 } |
| 678 NOTREACHED(); |
| 679 return base::PLATFORM_FILE_ERROR_FAILED; |
| 680 } |
| 681 |
| 682 PlatformFileError ObfuscatedFileUtil::CopyInForeignFile( |
| 683 FileSystemOperationContext* context, |
| 684 const FilePath& src_file_path, |
| 685 const FilePath& dest_file_path) { |
| 686 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 687 context->dest_origin_url(), context->dest_type(), true); |
| 688 if (!db) |
| 689 return base::PLATFORM_FILE_ERROR_FAILED; |
| 690 FileId dest_file_id; |
| 691 bool overwrite = db->GetFileWithPath(dest_file_path, &dest_file_id); |
| 692 FileInfo dest_file_info; |
| 693 if (overwrite) { |
| 694 if (!db->GetFileInfo(dest_file_id, &dest_file_info) || |
| 695 dest_file_info.is_directory()) { |
| 748 NOTREACHED(); | 696 NOTREACHED(); |
| 749 return false; | 697 return base::PLATFORM_FILE_ERROR_FAILED; |
| 750 } | 698 } |
| 751 FilePath path = base_path_.Append(current_.path).Append(type_string); | 699 FilePath dest_data_path = DataPathToLocalPath(context->dest_origin_url(), |
| 752 return file_util::DirectoryExists(path); | 700 context->dest_type(), dest_file_info.data_path); |
| 753 } | 701 return underlying_file_util()->CopyOrMoveFile(context, |
| 754 | 702 src_file_path, dest_data_path, true /* copy */); |
| 755 private: | |
| 756 std::vector<OriginRecord> origins_; | |
| 757 OriginRecord current_; | |
| 758 FilePath base_path_; | |
| 759 }; | |
| 760 | |
| 761 ObfuscatedFileSystemFileUtil::AbstractOriginEnumerator* | |
| 762 ObfuscatedFileSystemFileUtil::CreateOriginEnumerator() { | |
| 763 std::vector<FileSystemOriginDatabase::OriginRecord> origins; | |
| 764 | |
| 765 InitOriginDatabase(false); | |
| 766 return new ObfuscatedFileSystemOriginEnumerator( | |
| 767 origin_database_.get(), file_system_directory_); | |
| 768 } | |
| 769 | |
| 770 FileSystemFileUtil::AbstractFileEnumerator* | |
| 771 ObfuscatedFileSystemFileUtil::CreateFileEnumerator( | |
| 772 FileSystemOperationContext* context, | |
| 773 const FilePath& root_path) { | |
| 774 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 775 context->src_origin_url(), context->src_type(), false); | |
| 776 if (!db) | |
| 777 return new FileSystemFileUtil::EmptyFileEnumerator(); | |
| 778 return new ObfuscatedFileSystemFileEnumerator(db, root_path); | |
| 779 } | |
| 780 | |
| 781 PlatformFileError ObfuscatedFileSystemFileUtil::GetFileInfoInternal( | |
| 782 FileSystemDirectoryDatabase* db, | |
| 783 FileSystemOperationContext* context, | |
| 784 FileId file_id, | |
| 785 FileInfo* local_info, | |
| 786 base::PlatformFileInfo* file_info, | |
| 787 FilePath* platform_file_path) { | |
| 788 DCHECK(db); | |
| 789 DCHECK(context); | |
| 790 DCHECK(file_info); | |
| 791 DCHECK(platform_file_path); | |
| 792 | |
| 793 if (!db->GetFileInfo(file_id, local_info)) { | |
| 794 NOTREACHED(); | |
| 795 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 796 } | |
| 797 | |
| 798 if (local_info->is_directory()) { | |
| 799 file_info->is_directory = true; | |
| 800 file_info->is_symbolic_link = false; | |
| 801 file_info->last_modified = local_info->modification_time; | |
| 802 *platform_file_path = FilePath(); | |
| 803 // We don't fill in ctime or atime. | |
| 804 return base::PLATFORM_FILE_OK; | |
| 805 } | |
| 806 if (local_info->data_path.empty()) | |
| 807 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 808 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), | |
| 809 context->src_type(), local_info->data_path); | |
| 810 return underlying_file_util_->GetFileInfo( | |
| 811 context, data_path, file_info, platform_file_path); | |
| 812 } | |
| 813 | |
| 814 PlatformFileError ObfuscatedFileSystemFileUtil::CreateFile( | |
| 815 FileSystemOperationContext* context, | |
| 816 const GURL& origin_url, FileSystemType type, const FilePath& source_path, | |
| 817 FileInfo* file_info, int file_flags, PlatformFile* handle) { | |
| 818 if (handle) | |
| 819 *handle = base::kInvalidPlatformFileValue; | |
| 820 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 821 origin_url, type, true); | |
| 822 int64 number; | |
| 823 if (!db || !db->GetNextInteger(&number)) | |
| 824 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 825 // We use the third- and fourth-to-last digits as the directory. | |
| 826 int64 directory_number = number % 10000 / 100; | |
| 827 FilePath path = | |
| 828 GetDirectoryForOriginAndType(origin_url, type, false); | |
| 829 if (path.empty()) | |
| 830 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 831 | |
| 832 path = path.AppendASCII(StringPrintf("%02" PRIu64, directory_number)); | |
| 833 PlatformFileError error; | |
| 834 error = underlying_file_util_->CreateDirectory( | |
| 835 context, path, false /* exclusive */, false /* recursive */); | |
| 836 if (base::PLATFORM_FILE_OK != error) | |
| 837 return error; | |
| 838 path = path.AppendASCII(StringPrintf("%08" PRIu64, number)); | |
| 839 FilePath data_path = LocalPathToDataPath(origin_url, type, path); | |
| 840 if (data_path.empty()) | |
| 841 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 842 bool created = false; | |
| 843 if (!source_path.empty()) { | |
| 844 DCHECK(!file_flags); | |
| 845 DCHECK(!handle); | |
| 846 error = underlying_file_util_->CopyOrMoveFile( | |
| 847 context, source_path, path, true /* copy */); | |
| 848 created = true; | |
| 849 } else { | 703 } else { |
| 850 if (handle) { | 704 FileId dest_parent_id; |
| 851 error = underlying_file_util_->CreateOrOpen( | 705 if (!db->GetFileWithPath(dest_file_path.DirName(), &dest_parent_id)) { |
| 852 context, path, file_flags, handle, &created); | 706 NOTREACHED(); // We shouldn't be called in this case. |
| 853 // If this succeeds, we must close handle on any subsequent error. | 707 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 854 } else { | 708 } |
| 855 DCHECK(!file_flags); // file_flags is only used by CreateOrOpen. | 709 InitFileInfo(&dest_file_info, dest_parent_id, |
| 856 error = underlying_file_util_->EnsureFileExists( | 710 dest_file_path.BaseName().value()); |
| 857 context, path, &created); | 711 if (!AllocateQuotaForPath(context, 1, dest_file_info.name.size())) |
| 858 } | 712 return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 859 } | 713 return CreateFile(context, context->dest_origin_url(), |
| 860 if (error != base::PLATFORM_FILE_OK) | 714 context->dest_type(), src_file_path, &dest_file_info, 0, NULL); |
| 861 return error; | 715 } |
| 862 | 716 return base::PLATFORM_FILE_ERROR_FAILED; |
| 863 if (!created) { | 717 } |
| 864 NOTREACHED(); | 718 |
| 865 if (handle) { | 719 PlatformFileError ObfuscatedFileUtil::DeleteFile( |
| 866 DCHECK_NE(base::kInvalidPlatformFileValue, *handle); | 720 FileSystemOperationContext* context, |
| 867 base::ClosePlatformFile(*handle); | |
| 868 underlying_file_util_->DeleteFile(context, path); | |
| 869 } | |
| 870 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 871 } | |
| 872 file_info->data_path = data_path; | |
| 873 FileId file_id; | |
| 874 if (!db->AddFileInfo(*file_info, &file_id)) { | |
| 875 if (handle) { | |
| 876 DCHECK_NE(base::kInvalidPlatformFileValue, *handle); | |
| 877 base::ClosePlatformFile(*handle); | |
| 878 } | |
| 879 underlying_file_util_->DeleteFile(context, path); | |
| 880 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 881 } | |
| 882 UpdatePathQuotaUsage(context, origin_url, type, 1, file_info->name.size()); | |
| 883 | |
| 884 return base::PLATFORM_FILE_OK; | |
| 885 } | |
| 886 | |
| 887 FilePath ObfuscatedFileSystemFileUtil::GetLocalPath( | |
| 888 const GURL& origin_url, | |
| 889 FileSystemType type, | |
| 890 const FilePath& virtual_path) { | 721 const FilePath& virtual_path) { |
| 891 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | 722 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 892 origin_url, type, false); | 723 context->src_origin_url(), context->src_type(), true); |
| 893 if (!db) | 724 if (!db) |
| 894 return FilePath(); | 725 return base::PLATFORM_FILE_ERROR_FAILED; |
| 895 FileId file_id; | 726 FileId file_id; |
| 896 if (!db->GetFileWithPath(virtual_path, &file_id)) | 727 if (!db->GetFileWithPath(virtual_path, &file_id)) |
| 897 return FilePath(); | 728 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 898 FileInfo file_info; | 729 FileInfo file_info; |
| 899 if (!db->GetFileInfo(file_id, &file_info) || file_info.is_directory()) { | 730 if (!db->GetFileInfo(file_id, &file_info) || file_info.is_directory()) { |
| 900 NOTREACHED(); | 731 NOTREACHED(); |
| 901 return FilePath(); // Directories have no local path. | 732 return base::PLATFORM_FILE_ERROR_FAILED; |
| 902 } | 733 } |
| 903 return DataPathToLocalPath(origin_url, type, file_info.data_path); | 734 if (!db->RemoveFileInfo(file_id)) { |
| 904 } | 735 NOTREACHED(); |
| 905 | 736 return base::PLATFORM_FILE_ERROR_FAILED; |
| 906 FilePath ObfuscatedFileSystemFileUtil::GetDirectoryForOriginAndType( | 737 } |
| 738 AllocateQuotaForPath(context, -1, -static_cast<int64>(file_info.name.size())); |
| 739 UpdatePathQuotaUsage(context, context->src_origin_url(), context->src_type(), |
| 740 -1, -static_cast<int64>(file_info.name.size())); |
| 741 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), |
| 742 context->src_type(), file_info.data_path); |
| 743 if (base::PLATFORM_FILE_OK != |
| 744 underlying_file_util()->DeleteFile(context, data_path)) |
| 745 LOG(WARNING) << "Leaked a backing file."; |
| 746 return base::PLATFORM_FILE_OK; |
| 747 } |
| 748 |
| 749 PlatformFileError ObfuscatedFileUtil::DeleteSingleDirectory( |
| 750 FileSystemOperationContext* context, |
| 751 const FilePath& virtual_path) { |
| 752 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 753 context->src_origin_url(), context->src_type(), true); |
| 754 if (!db) |
| 755 return base::PLATFORM_FILE_ERROR_FAILED; |
| 756 FileId file_id; |
| 757 if (!db->GetFileWithPath(virtual_path, &file_id)) |
| 758 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 759 FileInfo file_info; |
| 760 if (!db->GetFileInfo(file_id, &file_info) || !file_info.is_directory()) { |
| 761 NOTREACHED(); |
| 762 return base::PLATFORM_FILE_ERROR_FAILED; |
| 763 } |
| 764 if (!db->RemoveFileInfo(file_id)) |
| 765 return base::PLATFORM_FILE_ERROR_NOT_EMPTY; |
| 766 AllocateQuotaForPath(context, -1, -static_cast<int64>(file_info.name.size())); |
| 767 UpdatePathQuotaUsage(context, context->src_origin_url(), context->src_type(), |
| 768 -1, -static_cast<int64>(file_info.name.size())); |
| 769 return base::PLATFORM_FILE_OK; |
| 770 } |
| 771 |
| 772 FilePath ObfuscatedFileUtil::GetDirectoryForOriginAndType( |
| 907 const GURL& origin, FileSystemType type, bool create) { | 773 const GURL& origin, FileSystemType type, bool create) { |
| 908 FilePath origin_dir = GetDirectoryForOrigin(origin, create); | 774 FilePath origin_dir = GetDirectoryForOrigin(origin, create); |
| 909 if (origin_dir.empty()) | 775 if (origin_dir.empty()) |
| 910 return FilePath(); | 776 return FilePath(); |
| 911 FilePath::StringType type_string = GetDirectoryNameForType(type); | 777 FilePath::StringType type_string = GetDirectoryNameForType(type); |
| 912 if (type_string.empty()) { | 778 if (type_string.empty()) { |
| 913 LOG(WARNING) << "Unknown filesystem type requested:" << type; | 779 LOG(WARNING) << "Unknown filesystem type requested:" << type; |
| 914 return FilePath(); | 780 return FilePath(); |
| 915 } | 781 } |
| 916 FilePath path = origin_dir.Append(type_string); | 782 FilePath path = origin_dir.Append(type_string); |
| 917 if (!file_util::DirectoryExists(path) && | 783 if (!file_util::DirectoryExists(path) && |
| 918 (!create || !file_util::CreateDirectory(path))) | 784 (!create || !file_util::CreateDirectory(path))) |
| 919 return FilePath(); | 785 return FilePath(); |
| 920 return path; | 786 return path; |
| 921 } | 787 } |
| 922 | 788 |
| 923 bool ObfuscatedFileSystemFileUtil::DeleteDirectoryForOriginAndType( | 789 bool ObfuscatedFileUtil::DeleteDirectoryForOriginAndType( |
| 924 const GURL& origin, FileSystemType type) { | 790 const GURL& origin, FileSystemType type) { |
| 925 FilePath origin_type_path = GetDirectoryForOriginAndType(origin, type, false); | 791 FilePath origin_type_path = GetDirectoryForOriginAndType(origin, type, false); |
| 926 if (!file_util::PathExists(origin_type_path)) | 792 if (!file_util::PathExists(origin_type_path)) |
| 927 return true; | 793 return true; |
| 928 | 794 |
| 929 // TODO(dmikurube): Consider the return value of DestroyDirectoryDatabase. | 795 // TODO(dmikurube): Consider the return value of DestroyDirectoryDatabase. |
| 930 // We ignore its error now since 1) it doesn't matter the final result, and | 796 // We ignore its error now since 1) it doesn't matter the final result, and |
| 931 // 2) it always returns false in Windows because of LevelDB's implementation. | 797 // 2) it always returns false in Windows because of LevelDB's implementation. |
| 932 // Information about failure would be useful for debugging. | 798 // Information about failure would be useful for debugging. |
| 933 DestroyDirectoryDatabase(origin, type); | 799 DestroyDirectoryDatabase(origin, type); |
| 934 if (!file_util::Delete(origin_type_path, true /* recursive */)) | 800 if (!file_util::Delete(origin_type_path, true /* recursive */)) |
| 935 return false; | 801 return false; |
| 936 | 802 |
| 937 FilePath origin_path = origin_type_path.DirName(); | 803 FilePath origin_path = origin_type_path.DirName(); |
| 938 DCHECK_EQ(origin_path.value(), GetDirectoryForOrigin(origin, false).value()); | 804 DCHECK_EQ(origin_path.value(), GetDirectoryForOrigin(origin, false).value()); |
| 939 | 805 |
| 940 // Delete the origin directory if the deleted one was the last remaining | 806 // Delete the origin directory if the deleted one was the last remaining |
| 941 // type for the origin. | 807 // type for the origin. |
| 942 if (file_util::Delete(origin_path, false /* recursive */)) { | 808 if (file_util::Delete(origin_path, false /* recursive */)) { |
| 943 InitOriginDatabase(false); | 809 InitOriginDatabase(false); |
| 944 if (origin_database_.get()) | 810 if (origin_database_.get()) |
| 945 origin_database_->RemovePathForOrigin(GetOriginIdentifierFromURL(origin)); | 811 origin_database_->RemovePathForOrigin(GetOriginIdentifierFromURL(origin)); |
| 946 } | 812 } |
| 947 | 813 |
| 948 // At this point we are sure we had successfully deleted the origin/type | 814 // At this point we are sure we had successfully deleted the origin/type |
| 949 // directory, so just returning true here. | 815 // directory, so just returning true here. |
| 950 return true; | 816 return true; |
| 951 } | 817 } |
| 952 | 818 |
| 953 bool ObfuscatedFileSystemFileUtil::MigrateFromOldSandbox( | 819 bool ObfuscatedFileUtil::MigrateFromOldSandbox( |
| 954 const GURL& origin_url, FileSystemType type, const FilePath& src_root) { | 820 const GURL& origin_url, FileSystemType type, const FilePath& src_root) { |
| 955 if (!DestroyDirectoryDatabase(origin_url, type)) | 821 if (!DestroyDirectoryDatabase(origin_url, type)) |
| 956 return false; | 822 return false; |
| 957 FilePath dest_root = GetDirectoryForOriginAndType(origin_url, type, true); | 823 FilePath dest_root = GetDirectoryForOriginAndType(origin_url, type, true); |
| 958 if (dest_root.empty()) | 824 if (dest_root.empty()) |
| 959 return false; | 825 return false; |
| 960 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | 826 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 961 origin_url, type, true); | 827 origin_url, type, true); |
| 962 if (!db) | 828 if (!db) |
| 963 return false; | 829 return false; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1015 LOG(WARNING) << | 881 LOG(WARNING) << |
| 1016 "The final step of a migration failed; I'll try to clean up."; | 882 "The final step of a migration failed; I'll try to clean up."; |
| 1017 db = NULL; | 883 db = NULL; |
| 1018 DestroyDirectoryDatabase(origin_url, type); | 884 DestroyDirectoryDatabase(origin_url, type); |
| 1019 return false; | 885 return false; |
| 1020 } | 886 } |
| 1021 return true; | 887 return true; |
| 1022 } | 888 } |
| 1023 | 889 |
| 1024 // static | 890 // static |
| 1025 FilePath::StringType ObfuscatedFileSystemFileUtil::GetDirectoryNameForType( | 891 FilePath::StringType ObfuscatedFileUtil::GetDirectoryNameForType( |
| 1026 FileSystemType type) { | 892 FileSystemType type) { |
| 1027 switch (type) { | 893 switch (type) { |
| 1028 case kFileSystemTypeTemporary: | 894 case kFileSystemTypeTemporary: |
| 1029 return kTemporaryDirectoryName; | 895 return kTemporaryDirectoryName; |
| 1030 case kFileSystemTypePersistent: | 896 case kFileSystemTypePersistent: |
| 1031 return kPersistentDirectoryName; | 897 return kPersistentDirectoryName; |
| 1032 case kFileSystemTypeUnknown: | 898 case kFileSystemTypeUnknown: |
| 1033 default: | 899 default: |
| 1034 return FilePath::StringType(); | 900 return FilePath::StringType(); |
| 1035 } | 901 } |
| 1036 } | 902 } |
| 1037 | 903 |
| 1038 FilePath ObfuscatedFileSystemFileUtil::DataPathToLocalPath( | 904 ObfuscatedFileUtil::AbstractOriginEnumerator* |
| 905 ObfuscatedFileUtil::CreateOriginEnumerator() { |
| 906 std::vector<FileSystemOriginDatabase::OriginRecord> origins; |
| 907 |
| 908 InitOriginDatabase(false); |
| 909 return new ObfuscatedOriginEnumerator( |
| 910 origin_database_.get(), file_system_directory_); |
| 911 } |
| 912 |
| 913 bool ObfuscatedFileUtil::DestroyDirectoryDatabase( |
| 914 const GURL& origin, FileSystemType type) { |
| 915 std::string type_string = |
| 916 FileSystemPathManager::GetFileSystemTypeString(type); |
| 917 if (type_string.empty()) { |
| 918 LOG(WARNING) << "Unknown filesystem type requested:" << type; |
| 919 return true; |
| 920 } |
| 921 std::string key = GetOriginIdentifierFromURL(origin) + type_string; |
| 922 DirectoryMap::iterator iter = directories_.find(key); |
| 923 if (iter != directories_.end()) { |
| 924 FileSystemDirectoryDatabase* database = iter->second; |
| 925 directories_.erase(iter); |
| 926 delete database; |
| 927 } |
| 928 |
| 929 FilePath path = GetDirectoryForOriginAndType(origin, type, false); |
| 930 if (path.empty()) |
| 931 return true; |
| 932 if (!file_util::DirectoryExists(path)) |
| 933 return true; |
| 934 path = path.AppendASCII(kDirectoryDatabaseName); |
| 935 return FileSystemDirectoryDatabase::DestroyDatabase(path); |
| 936 } |
| 937 |
| 938 // static |
| 939 int64 ObfuscatedFileUtil::ComputeFilePathCost(const FilePath& path) { |
| 940 return GetPathQuotaUsage(1, path.BaseName().value().size()); |
| 941 } |
| 942 |
| 943 PlatformFileError ObfuscatedFileUtil::GetFileInfoInternal( |
| 944 FileSystemDirectoryDatabase* db, |
| 945 FileSystemOperationContext* context, |
| 946 FileId file_id, |
| 947 FileInfo* local_info, |
| 948 base::PlatformFileInfo* file_info, |
| 949 FilePath* platform_file_path) { |
| 950 DCHECK(db); |
| 951 DCHECK(context); |
| 952 DCHECK(file_info); |
| 953 DCHECK(platform_file_path); |
| 954 |
| 955 if (!db->GetFileInfo(file_id, local_info)) { |
| 956 NOTREACHED(); |
| 957 return base::PLATFORM_FILE_ERROR_FAILED; |
| 958 } |
| 959 |
| 960 if (local_info->is_directory()) { |
| 961 file_info->is_directory = true; |
| 962 file_info->is_symbolic_link = false; |
| 963 file_info->last_modified = local_info->modification_time; |
| 964 *platform_file_path = FilePath(); |
| 965 // We don't fill in ctime or atime. |
| 966 return base::PLATFORM_FILE_OK; |
| 967 } |
| 968 if (local_info->data_path.empty()) |
| 969 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
| 970 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), |
| 971 context->src_type(), local_info->data_path); |
| 972 return underlying_file_util()->GetFileInfo( |
| 973 context, data_path, file_info, platform_file_path); |
| 974 } |
| 975 |
| 976 PlatformFileError ObfuscatedFileUtil::CreateFile( |
| 977 FileSystemOperationContext* context, |
| 978 const GURL& origin_url, FileSystemType type, const FilePath& source_path, |
| 979 FileInfo* file_info, int file_flags, PlatformFile* handle) { |
| 980 if (handle) |
| 981 *handle = base::kInvalidPlatformFileValue; |
| 982 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 983 origin_url, type, true); |
| 984 int64 number; |
| 985 if (!db || !db->GetNextInteger(&number)) |
| 986 return base::PLATFORM_FILE_ERROR_FAILED; |
| 987 // We use the third- and fourth-to-last digits as the directory. |
| 988 int64 directory_number = number % 10000 / 100; |
| 989 FilePath path = |
| 990 GetDirectoryForOriginAndType(origin_url, type, false); |
| 991 if (path.empty()) |
| 992 return base::PLATFORM_FILE_ERROR_FAILED; |
| 993 |
| 994 path = path.AppendASCII(StringPrintf("%02" PRIu64, directory_number)); |
| 995 PlatformFileError error; |
| 996 error = underlying_file_util()->CreateDirectory( |
| 997 context, path, false /* exclusive */, false /* recursive */); |
| 998 if (base::PLATFORM_FILE_OK != error) |
| 999 return error; |
| 1000 path = path.AppendASCII(StringPrintf("%08" PRIu64, number)); |
| 1001 FilePath data_path = LocalPathToDataPath(origin_url, type, path); |
| 1002 if (data_path.empty()) |
| 1003 return base::PLATFORM_FILE_ERROR_FAILED; |
| 1004 bool created = false; |
| 1005 if (!source_path.empty()) { |
| 1006 DCHECK(!file_flags); |
| 1007 DCHECK(!handle); |
| 1008 error = underlying_file_util()->CopyOrMoveFile( |
| 1009 context, source_path, path, true /* copy */); |
| 1010 created = true; |
| 1011 } else { |
| 1012 if (handle) { |
| 1013 error = underlying_file_util()->CreateOrOpen( |
| 1014 context, path, file_flags, handle, &created); |
| 1015 // If this succeeds, we must close handle on any subsequent error. |
| 1016 } else { |
| 1017 DCHECK(!file_flags); // file_flags is only used by CreateOrOpen. |
| 1018 error = underlying_file_util()->EnsureFileExists( |
| 1019 context, path, &created); |
| 1020 } |
| 1021 } |
| 1022 if (error != base::PLATFORM_FILE_OK) |
| 1023 return error; |
| 1024 |
| 1025 if (!created) { |
| 1026 NOTREACHED(); |
| 1027 if (handle) { |
| 1028 DCHECK_NE(base::kInvalidPlatformFileValue, *handle); |
| 1029 base::ClosePlatformFile(*handle); |
| 1030 underlying_file_util()->DeleteFile(context, path); |
| 1031 } |
| 1032 return base::PLATFORM_FILE_ERROR_FAILED; |
| 1033 } |
| 1034 file_info->data_path = data_path; |
| 1035 FileId file_id; |
| 1036 if (!db->AddFileInfo(*file_info, &file_id)) { |
| 1037 if (handle) { |
| 1038 DCHECK_NE(base::kInvalidPlatformFileValue, *handle); |
| 1039 base::ClosePlatformFile(*handle); |
| 1040 } |
| 1041 underlying_file_util()->DeleteFile(context, path); |
| 1042 return base::PLATFORM_FILE_ERROR_FAILED; |
| 1043 } |
| 1044 UpdatePathQuotaUsage(context, origin_url, type, 1, file_info->name.size()); |
| 1045 |
| 1046 return base::PLATFORM_FILE_OK; |
| 1047 } |
| 1048 |
| 1049 FilePath ObfuscatedFileUtil::GetLocalPath( |
| 1050 const GURL& origin_url, |
| 1051 FileSystemType type, |
| 1052 const FilePath& virtual_path) { |
| 1053 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 1054 origin_url, type, false); |
| 1055 if (!db) |
| 1056 return FilePath(); |
| 1057 FileId file_id; |
| 1058 if (!db->GetFileWithPath(virtual_path, &file_id)) |
| 1059 return FilePath(); |
| 1060 FileInfo file_info; |
| 1061 if (!db->GetFileInfo(file_id, &file_info) || file_info.is_directory()) { |
| 1062 NOTREACHED(); |
| 1063 return FilePath(); // Directories have no local path. |
| 1064 } |
| 1065 return DataPathToLocalPath(origin_url, type, file_info.data_path); |
| 1066 } |
| 1067 |
| 1068 FilePath ObfuscatedFileUtil::DataPathToLocalPath( |
| 1039 const GURL& origin, FileSystemType type, const FilePath& data_path) { | 1069 const GURL& origin, FileSystemType type, const FilePath& data_path) { |
| 1040 FilePath root = GetDirectoryForOriginAndType(origin, type, false); | 1070 FilePath root = GetDirectoryForOriginAndType(origin, type, false); |
| 1041 if (root.empty()) | 1071 if (root.empty()) |
| 1042 return root; | 1072 return root; |
| 1043 return root.Append(data_path); | 1073 return root.Append(data_path); |
| 1044 } | 1074 } |
| 1045 | 1075 |
| 1046 FilePath ObfuscatedFileSystemFileUtil::LocalPathToDataPath( | 1076 FilePath ObfuscatedFileUtil::LocalPathToDataPath( |
| 1047 const GURL& origin, FileSystemType type, const FilePath& local_path) { | 1077 const GURL& origin, FileSystemType type, const FilePath& local_path) { |
| 1048 FilePath root = GetDirectoryForOriginAndType(origin, type, false); | 1078 FilePath root = GetDirectoryForOriginAndType(origin, type, false); |
| 1049 if (root.empty()) | 1079 if (root.empty()) |
| 1050 return root; | 1080 return root; |
| 1051 // This removes the root, including the trailing slash, leaving a relative | 1081 // This removes the root, including the trailing slash, leaving a relative |
| 1052 // path. | 1082 // path. |
| 1053 return FilePath(local_path.value().substr(root.value().length() + 1)); | 1083 return FilePath(local_path.value().substr(root.value().length() + 1)); |
| 1054 } | 1084 } |
| 1055 | 1085 |
| 1056 // TODO: How to do the whole validation-without-creation thing? We may not have | 1086 // TODO: How to do the whole validation-without-creation thing? We may not have |
| 1057 // quota even to create the database. Ah, in that case don't even get here? | 1087 // quota even to create the database. Ah, in that case don't even get here? |
| 1058 // Still doesn't answer the quota issue, though. | 1088 // Still doesn't answer the quota issue, though. |
| 1059 FileSystemDirectoryDatabase* ObfuscatedFileSystemFileUtil::GetDirectoryDatabase( | 1089 FileSystemDirectoryDatabase* ObfuscatedFileUtil::GetDirectoryDatabase( |
| 1060 const GURL& origin, FileSystemType type, bool create) { | 1090 const GURL& origin, FileSystemType type, bool create) { |
| 1061 std::string type_string = | 1091 std::string type_string = |
| 1062 FileSystemPathManager::GetFileSystemTypeString(type); | 1092 FileSystemPathManager::GetFileSystemTypeString(type); |
| 1063 if (type_string.empty()) { | 1093 if (type_string.empty()) { |
| 1064 LOG(WARNING) << "Unknown filesystem type requested:" << type; | 1094 LOG(WARNING) << "Unknown filesystem type requested:" << type; |
| 1065 return NULL; | 1095 return NULL; |
| 1066 } | 1096 } |
| 1067 std::string key = GetOriginIdentifierFromURL(origin) + type_string; | 1097 std::string key = GetOriginIdentifierFromURL(origin) + type_string; |
| 1068 DirectoryMap::iterator iter = directories_.find(key); | 1098 DirectoryMap::iterator iter = directories_.find(key); |
| 1069 if (iter != directories_.end()) { | 1099 if (iter != directories_.end()) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1080 return NULL; | 1110 return NULL; |
| 1081 } | 1111 } |
| 1082 } | 1112 } |
| 1083 MarkUsed(); | 1113 MarkUsed(); |
| 1084 path = path.AppendASCII(kDirectoryDatabaseName); | 1114 path = path.AppendASCII(kDirectoryDatabaseName); |
| 1085 FileSystemDirectoryDatabase* database = new FileSystemDirectoryDatabase(path); | 1115 FileSystemDirectoryDatabase* database = new FileSystemDirectoryDatabase(path); |
| 1086 directories_[key] = database; | 1116 directories_[key] = database; |
| 1087 return database; | 1117 return database; |
| 1088 } | 1118 } |
| 1089 | 1119 |
| 1090 FilePath ObfuscatedFileSystemFileUtil::GetDirectoryForOrigin( | 1120 FilePath ObfuscatedFileUtil::GetDirectoryForOrigin( |
| 1091 const GURL& origin, bool create) { | 1121 const GURL& origin, bool create) { |
| 1092 if (!InitOriginDatabase(create)) | 1122 if (!InitOriginDatabase(create)) |
| 1093 return FilePath(); | 1123 return FilePath(); |
| 1094 FilePath directory_name; | 1124 FilePath directory_name; |
| 1095 std::string id = GetOriginIdentifierFromURL(origin); | 1125 std::string id = GetOriginIdentifierFromURL(origin); |
| 1096 if (!create && !origin_database_->HasOriginPath(id)) | 1126 if (!create && !origin_database_->HasOriginPath(id)) |
| 1097 return FilePath(); | 1127 return FilePath(); |
| 1098 if (!origin_database_->GetPathForOrigin(id, &directory_name)) | 1128 if (!origin_database_->GetPathForOrigin(id, &directory_name)) |
| 1099 return FilePath(); | 1129 return FilePath(); |
| 1100 FilePath path = file_system_directory_.Append(directory_name); | 1130 FilePath path = file_system_directory_.Append(directory_name); |
| 1101 if (!file_util::DirectoryExists(path) && | 1131 if (!file_util::DirectoryExists(path) && |
| 1102 (!create || !file_util::CreateDirectory(path))) | 1132 (!create || !file_util::CreateDirectory(path))) |
| 1103 return FilePath(); | 1133 return FilePath(); |
| 1104 return path; | 1134 return path; |
| 1105 } | 1135 } |
| 1106 | 1136 |
| 1107 void ObfuscatedFileSystemFileUtil::MarkUsed() { | 1137 void ObfuscatedFileUtil::MarkUsed() { |
| 1108 if (timer_.IsRunning()) | 1138 if (timer_.IsRunning()) |
| 1109 timer_.Reset(); | 1139 timer_.Reset(); |
| 1110 else | 1140 else |
| 1111 timer_.Start(base::TimeDelta::FromSeconds(kFlushDelaySeconds), this, | 1141 timer_.Start(base::TimeDelta::FromSeconds(kFlushDelaySeconds), this, |
| 1112 &ObfuscatedFileSystemFileUtil::DropDatabases); | 1142 &ObfuscatedFileUtil::DropDatabases); |
| 1113 } | 1143 } |
| 1114 | 1144 |
| 1115 void ObfuscatedFileSystemFileUtil::DropDatabases() { | 1145 void ObfuscatedFileUtil::DropDatabases() { |
| 1116 origin_database_.reset(); | 1146 origin_database_.reset(); |
| 1117 STLDeleteContainerPairSecondPointers( | 1147 STLDeleteContainerPairSecondPointers( |
| 1118 directories_.begin(), directories_.end()); | 1148 directories_.begin(), directories_.end()); |
| 1119 directories_.clear(); | 1149 directories_.clear(); |
| 1120 } | 1150 } |
| 1121 | 1151 |
| 1122 // static | 1152 bool ObfuscatedFileUtil::InitOriginDatabase(bool create) { |
| 1123 int64 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(const FilePath& path) { | |
| 1124 return GetPathQuotaUsage(1, path.BaseName().value().size()); | |
| 1125 } | |
| 1126 | |
| 1127 bool ObfuscatedFileSystemFileUtil::DestroyDirectoryDatabase( | |
| 1128 const GURL& origin, FileSystemType type) { | |
| 1129 std::string type_string = | |
| 1130 FileSystemPathManager::GetFileSystemTypeString(type); | |
| 1131 if (type_string.empty()) { | |
| 1132 LOG(WARNING) << "Unknown filesystem type requested:" << type; | |
| 1133 return true; | |
| 1134 } | |
| 1135 std::string key = GetOriginIdentifierFromURL(origin) + type_string; | |
| 1136 DirectoryMap::iterator iter = directories_.find(key); | |
| 1137 if (iter != directories_.end()) { | |
| 1138 FileSystemDirectoryDatabase* database = iter->second; | |
| 1139 directories_.erase(iter); | |
| 1140 delete database; | |
| 1141 } | |
| 1142 | |
| 1143 FilePath path = GetDirectoryForOriginAndType(origin, type, false); | |
| 1144 if (path.empty()) | |
| 1145 return true; | |
| 1146 if (!file_util::DirectoryExists(path)) | |
| 1147 return true; | |
| 1148 path = path.AppendASCII(kDirectoryDatabaseName); | |
| 1149 return FileSystemDirectoryDatabase::DestroyDatabase(path); | |
| 1150 } | |
| 1151 | |
| 1152 bool ObfuscatedFileSystemFileUtil::InitOriginDatabase(bool create) { | |
| 1153 if (!origin_database_.get()) { | 1153 if (!origin_database_.get()) { |
| 1154 if (!create && !file_util::DirectoryExists(file_system_directory_)) | 1154 if (!create && !file_util::DirectoryExists(file_system_directory_)) |
| 1155 return false; | 1155 return false; |
| 1156 if (!file_util::CreateDirectory(file_system_directory_)) { | 1156 if (!file_util::CreateDirectory(file_system_directory_)) { |
| 1157 LOG(WARNING) << "Failed to create FileSystem directory: " << | 1157 LOG(WARNING) << "Failed to create FileSystem directory: " << |
| 1158 file_system_directory_.value(); | 1158 file_system_directory_.value(); |
| 1159 return false; | 1159 return false; |
| 1160 } | 1160 } |
| 1161 origin_database_.reset( | 1161 origin_database_.reset( |
| 1162 new FileSystemOriginDatabase( | 1162 new FileSystemOriginDatabase( |
| 1163 file_system_directory_.AppendASCII(kOriginDatabaseName))); | 1163 file_system_directory_.AppendASCII(kOriginDatabaseName))); |
| 1164 } | 1164 } |
| 1165 return true; | 1165 return true; |
| 1166 } | 1166 } |
| 1167 | 1167 |
| 1168 } // namespace fileapi | 1168 } // namespace fileapi |
| OLD | NEW |