Chromium Code Reviews| 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_system_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" |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 | 34 |
| 35 void InitFileInfo( | 35 void InitFileInfo( |
| 36 fileapi::FileSystemDirectoryDatabase::FileInfo* file_info, | 36 fileapi::FileSystemDirectoryDatabase::FileInfo* file_info, |
| 37 fileapi::FileSystemDirectoryDatabase::FileId parent_id, | 37 fileapi::FileSystemDirectoryDatabase::FileId parent_id, |
| 38 const FilePath::StringType& file_name) { | 38 const FilePath::StringType& file_name) { |
| 39 DCHECK(file_info); | 39 DCHECK(file_info); |
| 40 file_info->parent_id = parent_id; | 40 file_info->parent_id = parent_id; |
| 41 file_info->name = file_name; | 41 file_info->name = file_name; |
| 42 } | 42 } |
| 43 | 43 |
| 44 bool IsRootDirectory(const FilePath& virtual_path) { | |
| 45 return (virtual_path.empty() || | |
| 46 virtual_path.value() == FILE_PATH_LITERAL("/")); | |
| 47 } | |
| 48 | |
| 44 const FilePath::CharType kLegacyDataDirectory[] = FILE_PATH_LITERAL("Legacy"); | 49 const FilePath::CharType kLegacyDataDirectory[] = FILE_PATH_LITERAL("Legacy"); |
| 45 | 50 |
| 46 const FilePath::CharType kTemporaryDirectoryName[] = FILE_PATH_LITERAL("t"); | 51 const FilePath::CharType kTemporaryDirectoryName[] = FILE_PATH_LITERAL("t"); |
| 47 const FilePath::CharType kPersistentDirectoryName[] = FILE_PATH_LITERAL("p"); | 52 const FilePath::CharType kPersistentDirectoryName[] = FILE_PATH_LITERAL("p"); |
| 48 | 53 |
| 49 } // namespace | 54 } // namespace |
| 50 | 55 |
| 51 namespace fileapi { | 56 namespace fileapi { |
| 52 | 57 |
| 53 using base::PlatformFile; | 58 using base::PlatformFile; |
| 54 using base::PlatformFileError; | 59 using base::PlatformFileError; |
| 55 | 60 |
| 56 ObfuscatedFileSystemFileUtil::ObfuscatedFileSystemFileUtil( | 61 ObfuscatedFileSystemFileUtil::ObfuscatedFileSystemFileUtil( |
| 57 const FilePath& file_system_directory) | 62 const FilePath& file_system_directory) |
| 58 : file_system_directory_(file_system_directory) { | 63 : file_system_directory_(file_system_directory) { |
| 59 } | 64 } |
| 60 | 65 |
| 61 ObfuscatedFileSystemFileUtil::~ObfuscatedFileSystemFileUtil() { | 66 ObfuscatedFileSystemFileUtil::~ObfuscatedFileSystemFileUtil() { |
| 62 DropDatabases(); | 67 DropDatabases(); |
| 63 } | 68 } |
| 64 | 69 |
| 65 PlatformFileError ObfuscatedFileSystemFileUtil::CreateOrOpen( | 70 PlatformFileError ObfuscatedFileSystemFileUtil::CreateOrOpen( |
| 66 FileSystemOperationContext* context, | 71 FileSystemOperationContext* context, |
| 67 const FilePath& virtual_path, int file_flags, | 72 const FilePath& virtual_path, int file_flags, |
| 68 PlatformFile* file_handle, bool* created) { | 73 PlatformFile* file_handle, bool* created) { |
| 69 DCHECK(!(file_flags & (base::PLATFORM_FILE_DELETE_ON_CLOSE | | 74 DCHECK(!(file_flags & (base::PLATFORM_FILE_DELETE_ON_CLOSE | |
| 70 base::PLATFORM_FILE_HIDDEN | base::PLATFORM_FILE_EXCLUSIVE_READ | | 75 base::PLATFORM_FILE_HIDDEN | base::PLATFORM_FILE_EXCLUSIVE_READ | |
| 71 base::PLATFORM_FILE_EXCLUSIVE_WRITE))); | 76 base::PLATFORM_FILE_EXCLUSIVE_WRITE))); |
| 72 FileSystemDirectoryDatabase* db = | 77 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 73 GetDirectoryDatabase(context->src_origin_url(), context->src_type()); | 78 context->src_origin_url(), context->src_type(), true); |
| 74 if (!db) | 79 if (!db) |
| 75 return base::PLATFORM_FILE_ERROR_FAILED; | 80 return base::PLATFORM_FILE_ERROR_FAILED; |
| 76 FileId file_id; | 81 FileId file_id; |
| 77 if (!db->GetFileWithPath(virtual_path, &file_id)) { | 82 if (!db->GetFileWithPath(virtual_path, &file_id)) { |
| 78 // The file doesn't exist. | 83 // The file doesn't exist. |
| 79 if (!(file_flags & (base::PLATFORM_FILE_CREATE | | 84 if (!(file_flags & (base::PLATFORM_FILE_CREATE | |
| 80 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_OPEN_ALWAYS))) | 85 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_OPEN_ALWAYS))) |
| 81 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 86 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 82 FileId parent_id; | 87 FileId parent_id; |
| 83 if (!db->GetFileWithPath(virtual_path.DirName(), &parent_id)) | 88 if (!db->GetFileWithPath(virtual_path.DirName(), &parent_id)) |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 104 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), | 109 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), |
| 105 context->src_type(), file_info.data_path); | 110 context->src_type(), file_info.data_path); |
| 106 return QuotaFileUtil::GetInstance()->CreateOrOpen( | 111 return QuotaFileUtil::GetInstance()->CreateOrOpen( |
| 107 context, data_path, file_flags, file_handle, created); | 112 context, data_path, file_flags, file_handle, created); |
| 108 } | 113 } |
| 109 | 114 |
| 110 PlatformFileError ObfuscatedFileSystemFileUtil::EnsureFileExists( | 115 PlatformFileError ObfuscatedFileSystemFileUtil::EnsureFileExists( |
| 111 FileSystemOperationContext* context, | 116 FileSystemOperationContext* context, |
| 112 const FilePath& virtual_path, | 117 const FilePath& virtual_path, |
| 113 bool* created) { | 118 bool* created) { |
| 114 FileSystemDirectoryDatabase* db = | 119 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 115 GetDirectoryDatabase(context->src_origin_url(), context->src_type()); | 120 context->src_origin_url(), context->src_type(), true); |
| 116 if (!db) | 121 if (!db) |
| 117 return base::PLATFORM_FILE_ERROR_FAILED; | 122 return base::PLATFORM_FILE_ERROR_FAILED; |
| 118 FileId file_id; | 123 FileId file_id; |
| 119 if (db->GetFileWithPath(virtual_path, &file_id)) { | 124 if (db->GetFileWithPath(virtual_path, &file_id)) { |
| 120 FileInfo file_info; | 125 FileInfo file_info; |
| 121 if (!db->GetFileInfo(file_id, &file_info)) { | 126 if (!db->GetFileInfo(file_id, &file_info)) { |
| 122 NOTREACHED(); | 127 NOTREACHED(); |
| 123 return base::PLATFORM_FILE_ERROR_FAILED; | 128 return base::PLATFORM_FILE_ERROR_FAILED; |
| 124 } | 129 } |
| 125 if (file_info.is_directory()) | 130 if (file_info.is_directory()) |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 153 | 158 |
| 154 *local_path = path; | 159 *local_path = path; |
| 155 return base::PLATFORM_FILE_OK; | 160 return base::PLATFORM_FILE_OK; |
| 156 } | 161 } |
| 157 | 162 |
| 158 PlatformFileError ObfuscatedFileSystemFileUtil::GetFileInfo( | 163 PlatformFileError ObfuscatedFileSystemFileUtil::GetFileInfo( |
| 159 FileSystemOperationContext* context, | 164 FileSystemOperationContext* context, |
| 160 const FilePath& virtual_path, | 165 const FilePath& virtual_path, |
| 161 base::PlatformFileInfo* file_info, | 166 base::PlatformFileInfo* file_info, |
| 162 FilePath* platform_file_path) { | 167 FilePath* platform_file_path) { |
| 163 FileSystemDirectoryDatabase* db = | 168 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 164 GetDirectoryDatabase(context->src_origin_url(), context->src_type()); | 169 context->src_origin_url(), context->src_type(), false); |
| 165 if (!db) | 170 if (!db) |
| 166 return base::PLATFORM_FILE_ERROR_FAILED; | 171 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 167 FileId file_id; | 172 FileId file_id; |
| 168 if (!db->GetFileWithPath(virtual_path, &file_id)) | 173 if (!db->GetFileWithPath(virtual_path, &file_id)) |
| 169 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 174 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 170 FileInfo local_info; | 175 FileInfo local_info; |
| 171 if (!db->GetFileInfo(file_id, &local_info)) { | 176 if (!db->GetFileInfo(file_id, &local_info)) { |
| 172 NOTREACHED(); | 177 NOTREACHED(); |
| 173 return base::PLATFORM_FILE_ERROR_FAILED; | 178 return base::PLATFORM_FILE_ERROR_FAILED; |
| 174 } | 179 } |
| 175 if (local_info.is_directory()) { | 180 if (local_info.is_directory()) { |
| 176 file_info->is_directory = true; | 181 file_info->is_directory = true; |
| 177 file_info->is_symbolic_link = false; | 182 file_info->is_symbolic_link = false; |
| 178 file_info->last_modified = local_info.modification_time; | 183 file_info->last_modified = local_info.modification_time; |
| 179 *platform_file_path = FilePath(); | 184 *platform_file_path = FilePath(); |
| 180 // We don't fill in ctime or atime. | 185 // We don't fill in ctime or atime. |
| 181 return base::PLATFORM_FILE_OK; | 186 return base::PLATFORM_FILE_OK; |
| 182 } | 187 } |
| 183 if (local_info.data_path.empty()) | 188 if (local_info.data_path.empty()) |
| 184 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 189 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
| 185 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), | 190 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), |
| 186 context->src_type(), local_info.data_path); | 191 context->src_type(), local_info.data_path); |
| 187 return QuotaFileUtil::GetInstance()->GetFileInfo( | 192 return QuotaFileUtil::GetInstance()->GetFileInfo( |
| 188 context, data_path, file_info, platform_file_path); | 193 context, data_path, file_info, platform_file_path); |
| 189 } | 194 } |
| 190 | 195 |
| 191 PlatformFileError ObfuscatedFileSystemFileUtil::ReadDirectory( | 196 PlatformFileError ObfuscatedFileSystemFileUtil::ReadDirectory( |
| 192 FileSystemOperationContext* context, | 197 FileSystemOperationContext* context, |
| 193 const FilePath& virtual_path, | 198 const FilePath& virtual_path, |
| 194 std::vector<base::FileUtilProxy::Entry>* entries) { | 199 std::vector<base::FileUtilProxy::Entry>* entries) { |
| 195 // TODO(kkanetkar): Implement directory read in multiple chunks. | 200 // TODO(kkanetkar): Implement directory read in multiple chunks. |
| 196 FileSystemDirectoryDatabase* db = | 201 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 197 GetDirectoryDatabase(context->src_origin_url(), context->src_type()); | 202 context->src_origin_url(), context->src_type(), false); |
| 198 if (!db) | 203 if (!db) { |
| 199 return base::PLATFORM_FILE_ERROR_FAILED; | 204 if (IsRootDirectory(virtual_path)) { |
| 205 // It's the root directory and the database hasn't been initialized yet. | |
| 206 entries->clear(); | |
| 207 return base::PLATFORM_FILE_OK; | |
| 208 } | |
| 209 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 210 } | |
| 200 FileId file_id; | 211 FileId file_id; |
| 201 if (!db->GetFileWithPath(virtual_path, &file_id)) | 212 if (!db->GetFileWithPath(virtual_path, &file_id)) |
| 202 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 213 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 203 FileInfo file_info; | 214 FileInfo file_info; |
| 204 if (!db->GetFileInfo(file_id, &file_info)) { | 215 if (!db->GetFileInfo(file_id, &file_info)) { |
| 205 DCHECK(!file_id); | 216 DCHECK(!file_id); |
| 206 // It's the root directory and the database hasn't been initialized yet. | 217 // It's the root directory and the database hasn't been initialized yet. |
| 207 entries->clear(); | 218 entries->clear(); |
| 208 return base::PLATFORM_FILE_OK; | 219 return base::PLATFORM_FILE_OK; |
| 209 } | 220 } |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 226 entries->push_back(entry); | 237 entries->push_back(entry); |
| 227 } | 238 } |
| 228 return base::PLATFORM_FILE_OK; | 239 return base::PLATFORM_FILE_OK; |
| 229 } | 240 } |
| 230 | 241 |
| 231 PlatformFileError ObfuscatedFileSystemFileUtil::CreateDirectory( | 242 PlatformFileError ObfuscatedFileSystemFileUtil::CreateDirectory( |
| 232 FileSystemOperationContext* context, | 243 FileSystemOperationContext* context, |
| 233 const FilePath& virtual_path, | 244 const FilePath& virtual_path, |
| 234 bool exclusive, | 245 bool exclusive, |
| 235 bool recursive) { | 246 bool recursive) { |
| 236 FileSystemDirectoryDatabase* db = | 247 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 237 GetDirectoryDatabase(context->src_origin_url(), context->src_type()); | 248 context->src_origin_url(), context->src_type(), true); |
| 238 if (!db) | 249 if (!db) |
| 239 return base::PLATFORM_FILE_ERROR_FAILED; | 250 return base::PLATFORM_FILE_ERROR_FAILED; |
| 240 FileId file_id; | 251 FileId file_id; |
| 241 if (db->GetFileWithPath(virtual_path, &file_id)) { | 252 if (db->GetFileWithPath(virtual_path, &file_id)) { |
| 242 FileInfo file_info; | 253 FileInfo file_info; |
| 243 if (exclusive) | 254 if (exclusive) |
| 244 return base::PLATFORM_FILE_ERROR_EXISTS; | 255 return base::PLATFORM_FILE_ERROR_EXISTS; |
| 245 if (!db->GetFileInfo(file_id, &file_info)) { | 256 if (!db->GetFileInfo(file_id, &file_info)) { |
| 246 NOTREACHED(); | 257 NOTREACHED(); |
| 247 return base::PLATFORM_FILE_ERROR_FAILED; | 258 return base::PLATFORM_FILE_ERROR_FAILED; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 277 } | 288 } |
| 278 } | 289 } |
| 279 return base::PLATFORM_FILE_OK; | 290 return base::PLATFORM_FILE_OK; |
| 280 } | 291 } |
| 281 | 292 |
| 282 PlatformFileError ObfuscatedFileSystemFileUtil::CopyOrMoveFile( | 293 PlatformFileError ObfuscatedFileSystemFileUtil::CopyOrMoveFile( |
| 283 FileSystemOperationContext* context, | 294 FileSystemOperationContext* context, |
| 284 const FilePath& src_file_path, | 295 const FilePath& src_file_path, |
| 285 const FilePath& dest_file_path, | 296 const FilePath& dest_file_path, |
| 286 bool copy) { | 297 bool copy) { |
| 287 FileSystemDirectoryDatabase* db = | 298 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 288 GetDirectoryDatabase(context->src_origin_url(), context->src_type()); | 299 context->src_origin_url(), context->src_type(), true); |
| 289 if (!db) | 300 if (!db) |
| 290 return base::PLATFORM_FILE_ERROR_FAILED; | 301 return base::PLATFORM_FILE_ERROR_FAILED; |
| 291 FileId src_file_id; | 302 FileId src_file_id; |
| 292 if (!db->GetFileWithPath(src_file_path, &src_file_id)) | 303 if (!db->GetFileWithPath(src_file_path, &src_file_id)) |
| 293 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 304 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 294 FileId dest_file_id; | 305 FileId dest_file_id; |
| 295 bool overwrite = db->GetFileWithPath(dest_file_path, &dest_file_id); | 306 bool overwrite = db->GetFileWithPath(dest_file_path, &dest_file_id); |
| 296 FileInfo src_file_info; | 307 FileInfo src_file_info; |
| 297 FileInfo dest_file_info; | 308 FileInfo dest_file_info; |
| 298 if (!db->GetFileInfo(src_file_id, &src_file_info) || | 309 if (!db->GetFileInfo(src_file_id, &src_file_info) || |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 365 } | 376 } |
| 366 } | 377 } |
| 367 NOTREACHED(); | 378 NOTREACHED(); |
| 368 return base::PLATFORM_FILE_ERROR_FAILED; | 379 return base::PLATFORM_FILE_ERROR_FAILED; |
| 369 } | 380 } |
| 370 | 381 |
| 371 PlatformFileError ObfuscatedFileSystemFileUtil::CopyInForeignFile( | 382 PlatformFileError ObfuscatedFileSystemFileUtil::CopyInForeignFile( |
| 372 FileSystemOperationContext* context, | 383 FileSystemOperationContext* context, |
| 373 const FilePath& src_file_path, | 384 const FilePath& src_file_path, |
| 374 const FilePath& dest_file_path) { | 385 const FilePath& dest_file_path) { |
| 375 FileSystemDirectoryDatabase* db = | 386 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 376 GetDirectoryDatabase(context->dest_origin_url(), context->dest_type()); | 387 context->dest_origin_url(), context->dest_type(), true); |
| 377 if (!db) | 388 if (!db) |
| 378 return base::PLATFORM_FILE_ERROR_FAILED; | 389 return base::PLATFORM_FILE_ERROR_FAILED; |
| 379 FileId dest_file_id; | 390 FileId dest_file_id; |
| 380 bool overwrite = db->GetFileWithPath(dest_file_path, &dest_file_id); | 391 bool overwrite = db->GetFileWithPath(dest_file_path, &dest_file_id); |
| 381 FileInfo dest_file_info; | 392 FileInfo dest_file_info; |
| 382 if (overwrite) { | 393 if (overwrite) { |
| 383 if (!db->GetFileInfo(dest_file_id, &dest_file_info) || | 394 if (!db->GetFileInfo(dest_file_id, &dest_file_info) || |
| 384 dest_file_info.is_directory()) { | 395 dest_file_info.is_directory()) { |
| 385 NOTREACHED(); | 396 NOTREACHED(); |
| 386 return base::PLATFORM_FILE_ERROR_FAILED; | 397 return base::PLATFORM_FILE_ERROR_FAILED; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 399 dest_file_path.BaseName().value()); | 410 dest_file_path.BaseName().value()); |
| 400 return CreateFile(context, context->dest_origin_url(), | 411 return CreateFile(context, context->dest_origin_url(), |
| 401 context->dest_type(), src_file_path, &dest_file_info, 0, NULL); | 412 context->dest_type(), src_file_path, &dest_file_info, 0, NULL); |
| 402 } | 413 } |
| 403 return base::PLATFORM_FILE_ERROR_FAILED; | 414 return base::PLATFORM_FILE_ERROR_FAILED; |
| 404 } | 415 } |
| 405 | 416 |
| 406 PlatformFileError ObfuscatedFileSystemFileUtil::DeleteFile( | 417 PlatformFileError ObfuscatedFileSystemFileUtil::DeleteFile( |
| 407 FileSystemOperationContext* context, | 418 FileSystemOperationContext* context, |
| 408 const FilePath& virtual_path) { | 419 const FilePath& virtual_path) { |
| 409 FileSystemDirectoryDatabase* db = | 420 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 410 GetDirectoryDatabase(context->src_origin_url(), context->src_type()); | 421 context->src_origin_url(), context->src_type(), true); |
| 411 if (!db) | 422 if (!db) |
| 412 return base::PLATFORM_FILE_ERROR_FAILED; | 423 return base::PLATFORM_FILE_ERROR_FAILED; |
| 413 FileId file_id; | 424 FileId file_id; |
| 414 if (!db->GetFileWithPath(virtual_path, &file_id)) | 425 if (!db->GetFileWithPath(virtual_path, &file_id)) |
| 415 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 426 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 416 FileInfo file_info; | 427 FileInfo file_info; |
| 417 if (!db->GetFileInfo(file_id, &file_info) || file_info.is_directory()) { | 428 if (!db->GetFileInfo(file_id, &file_info) || file_info.is_directory()) { |
| 418 NOTREACHED(); | 429 NOTREACHED(); |
| 419 return base::PLATFORM_FILE_ERROR_FAILED; | 430 return base::PLATFORM_FILE_ERROR_FAILED; |
| 420 } | 431 } |
| 421 if (!db->RemoveFileInfo(file_id)) { | 432 if (!db->RemoveFileInfo(file_id)) { |
| 422 NOTREACHED(); | 433 NOTREACHED(); |
| 423 return base::PLATFORM_FILE_ERROR_FAILED; | 434 return base::PLATFORM_FILE_ERROR_FAILED; |
| 424 } | 435 } |
| 425 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), | 436 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), |
| 426 context->src_type(), file_info.data_path); | 437 context->src_type(), file_info.data_path); |
| 427 if (base::PLATFORM_FILE_OK != | 438 if (base::PLATFORM_FILE_OK != |
| 428 QuotaFileUtil::GetInstance()->DeleteFile(context, data_path)) | 439 QuotaFileUtil::GetInstance()->DeleteFile(context, data_path)) |
| 429 LOG(WARNING) << "Leaked a backing file."; | 440 LOG(WARNING) << "Leaked a backing file."; |
| 430 return base::PLATFORM_FILE_OK; | 441 return base::PLATFORM_FILE_OK; |
| 431 } | 442 } |
| 432 | 443 |
| 433 PlatformFileError ObfuscatedFileSystemFileUtil::DeleteSingleDirectory( | 444 PlatformFileError ObfuscatedFileSystemFileUtil::DeleteSingleDirectory( |
| 434 FileSystemOperationContext* context, | 445 FileSystemOperationContext* context, |
| 435 const FilePath& virtual_path) { | 446 const FilePath& virtual_path) { |
| 436 FileSystemDirectoryDatabase* db = | 447 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 437 GetDirectoryDatabase(context->src_origin_url(), context->src_type()); | 448 context->src_origin_url(), context->src_type(), true); |
| 438 if (!db) | 449 if (!db) |
| 439 return base::PLATFORM_FILE_ERROR_FAILED; | 450 return base::PLATFORM_FILE_ERROR_FAILED; |
| 440 FileId file_id; | 451 FileId file_id; |
| 441 if (!db->GetFileWithPath(virtual_path, &file_id)) | 452 if (!db->GetFileWithPath(virtual_path, &file_id)) |
| 442 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 453 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 443 FileInfo file_info; | 454 FileInfo file_info; |
| 444 if (!db->GetFileInfo(file_id, &file_info) || !file_info.is_directory()) { | 455 if (!db->GetFileInfo(file_id, &file_info) || !file_info.is_directory()) { |
| 445 NOTREACHED(); | 456 NOTREACHED(); |
| 446 return base::PLATFORM_FILE_ERROR_FAILED; | 457 return base::PLATFORM_FILE_ERROR_FAILED; |
| 447 } | 458 } |
| 448 if (!db->RemoveFileInfo(file_id)) | 459 if (!db->RemoveFileInfo(file_id)) |
| 449 return base::PLATFORM_FILE_ERROR_NOT_EMPTY; | 460 return base::PLATFORM_FILE_ERROR_NOT_EMPTY; |
| 450 return base::PLATFORM_FILE_OK; | 461 return base::PLATFORM_FILE_OK; |
| 451 } | 462 } |
| 452 | 463 |
| 453 PlatformFileError ObfuscatedFileSystemFileUtil::Touch( | 464 PlatformFileError ObfuscatedFileSystemFileUtil::Touch( |
| 454 FileSystemOperationContext* context, | 465 FileSystemOperationContext* context, |
| 455 const FilePath& virtual_path, | 466 const FilePath& virtual_path, |
| 456 const base::Time& last_access_time, | 467 const base::Time& last_access_time, |
| 457 const base::Time& last_modified_time) { | 468 const base::Time& last_modified_time) { |
| 458 FileSystemDirectoryDatabase* db = | 469 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 459 GetDirectoryDatabase(context->src_origin_url(), context->src_type()); | 470 context->src_origin_url(), context->src_type(), true); |
| 460 if (!db) | 471 if (!db) |
| 461 return base::PLATFORM_FILE_ERROR_FAILED; | 472 return base::PLATFORM_FILE_ERROR_FAILED; |
| 462 FileId file_id; | 473 FileId file_id; |
| 463 if (db->GetFileWithPath(virtual_path, &file_id)) { | 474 if (db->GetFileWithPath(virtual_path, &file_id)) { |
| 464 FileInfo file_info; | 475 FileInfo file_info; |
| 465 if (!db->GetFileInfo(file_id, &file_info)) { | 476 if (!db->GetFileInfo(file_id, &file_info)) { |
| 466 NOTREACHED(); | 477 NOTREACHED(); |
| 467 return base::PLATFORM_FILE_ERROR_FAILED; | 478 return base::PLATFORM_FILE_ERROR_FAILED; |
| 468 } | 479 } |
| 469 if (file_info.is_directory()) { | 480 if (file_info.is_directory()) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 505 virtual_path); | 516 virtual_path); |
| 506 if (local_path.empty()) | 517 if (local_path.empty()) |
| 507 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 518 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 508 return QuotaFileUtil::GetInstance()->Truncate( | 519 return QuotaFileUtil::GetInstance()->Truncate( |
| 509 context, local_path, length); | 520 context, local_path, length); |
| 510 } | 521 } |
| 511 | 522 |
| 512 bool ObfuscatedFileSystemFileUtil::PathExists( | 523 bool ObfuscatedFileSystemFileUtil::PathExists( |
| 513 FileSystemOperationContext* context, | 524 FileSystemOperationContext* context, |
| 514 const FilePath& virtual_path) { | 525 const FilePath& virtual_path) { |
| 515 FileSystemDirectoryDatabase* db = | 526 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 516 GetDirectoryDatabase(context->src_origin_url(), context->src_type()); | 527 context->src_origin_url(), context->src_type(), false); |
| 517 if (!db) | 528 if (!db) |
| 518 return false; | 529 return false; |
| 519 FileId file_id; | 530 FileId file_id; |
| 520 return db->GetFileWithPath(virtual_path, &file_id); | 531 return db->GetFileWithPath(virtual_path, &file_id); |
| 521 } | 532 } |
| 522 | 533 |
| 523 bool ObfuscatedFileSystemFileUtil::DirectoryExists( | 534 bool ObfuscatedFileSystemFileUtil::DirectoryExists( |
| 524 FileSystemOperationContext* context, | 535 FileSystemOperationContext* context, |
| 525 const FilePath& virtual_path) { | 536 const FilePath& virtual_path) { |
| 526 FileSystemDirectoryDatabase* db = | 537 if (IsRootDirectory(virtual_path)) { |
| 527 GetDirectoryDatabase(context->src_origin_url(), context->src_type()); | 538 // It's questionable whether we should return true or false for the |
| 539 // root directory of nonexistent origin, but here we return true | |
| 540 // as the current implementation of ReadDirectory always returns an empty | |
| 541 // array (rather than erroring out with NOT_FOUND_ERR even) for | |
| 542 // nonexistent origins. | |
| 543 // Note: if you're going to change this behavior please also consider | |
| 544 // changiing the ReadDirectory's behavior! | |
| 545 return true; | |
| 546 } | |
| 547 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 548 context->src_origin_url(), context->src_type(), false); | |
| 528 if (!db) | 549 if (!db) |
| 529 return false; | 550 return false; |
| 530 FileId file_id; | 551 FileId file_id; |
| 531 if (!db->GetFileWithPath(virtual_path, &file_id)) | 552 if (!db->GetFileWithPath(virtual_path, &file_id)) |
| 532 return false; | 553 return false; |
| 533 FileInfo file_info; | 554 FileInfo file_info; |
| 534 if (!db->GetFileInfo(file_id, &file_info)) { | 555 if (!db->GetFileInfo(file_id, &file_info)) { |
| 535 NOTREACHED(); | 556 NOTREACHED(); |
| 536 return false; | 557 return false; |
| 537 } | 558 } |
| 538 return file_info.is_directory(); | 559 return file_info.is_directory(); |
| 539 } | 560 } |
| 540 | 561 |
| 541 bool ObfuscatedFileSystemFileUtil::IsDirectoryEmpty( | 562 bool ObfuscatedFileSystemFileUtil::IsDirectoryEmpty( |
| 542 FileSystemOperationContext* context, | 563 FileSystemOperationContext* context, |
| 543 const FilePath& virtual_path) { | 564 const FilePath& virtual_path) { |
| 544 FileSystemDirectoryDatabase* db = | 565 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 545 GetDirectoryDatabase(context->src_origin_url(), context->src_type()); | 566 context->src_origin_url(), context->src_type(), false); |
| 546 if (!db) | 567 if (!db) |
| 547 return false; | 568 return true; // Not a great answer, but it's what others do. |
| 548 FileId file_id; | 569 FileId file_id; |
| 549 if (!db->GetFileWithPath(virtual_path, &file_id)) | 570 if (!db->GetFileWithPath(virtual_path, &file_id)) |
| 550 return true; // Not a great answer, but it's what others do. | 571 return true; // Ditto. |
| 551 FileInfo file_info; | 572 FileInfo file_info; |
| 552 if (!db->GetFileInfo(file_id, &file_info)) { | 573 if (!db->GetFileInfo(file_id, &file_info)) { |
| 553 DCHECK(!file_id); | 574 DCHECK(!file_id); |
| 554 // It's the root directory and the database hasn't been initialized yet. | 575 // It's the root directory and the database hasn't been initialized yet. |
| 555 return true; | 576 return true; |
| 556 } | 577 } |
| 557 if (!file_info.is_directory()) | 578 if (!file_info.is_directory()) |
| 558 return true; | 579 return true; |
| 559 std::vector<FileId> children; | 580 std::vector<FileId> children; |
| 560 // TODO(ericu): This could easily be made faster with help from the database. | 581 // TODO(ericu): This could easily be made faster with help from the database. |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 685 | 706 |
| 686 InitOriginDatabase(false); | 707 InitOriginDatabase(false); |
| 687 return new ObfuscatedFileSystemOriginEnumerator( | 708 return new ObfuscatedFileSystemOriginEnumerator( |
| 688 origin_database_.get(), file_system_directory_); | 709 origin_database_.get(), file_system_directory_); |
| 689 } | 710 } |
| 690 | 711 |
| 691 FileSystemFileUtil::AbstractFileEnumerator* | 712 FileSystemFileUtil::AbstractFileEnumerator* |
| 692 ObfuscatedFileSystemFileUtil::CreateFileEnumerator( | 713 ObfuscatedFileSystemFileUtil::CreateFileEnumerator( |
| 693 FileSystemOperationContext* context, | 714 FileSystemOperationContext* context, |
| 694 const FilePath& root_path) { | 715 const FilePath& root_path) { |
| 695 FileSystemDirectoryDatabase* db = | 716 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 696 GetDirectoryDatabase(context->src_origin_url(), context->src_type()); | 717 context->src_origin_url(), context->src_type(), false); |
| 697 if (!db) | 718 if (!db) |
| 698 return new FileSystemFileUtil::EmptyFileEnumerator(); | 719 return new FileSystemFileUtil::EmptyFileEnumerator(); |
| 699 return new ObfuscatedFileSystemFileEnumerator(db, root_path); | 720 return new ObfuscatedFileSystemFileEnumerator(db, root_path); |
| 700 } | 721 } |
| 701 | 722 |
| 702 PlatformFileError ObfuscatedFileSystemFileUtil::CreateFile( | 723 PlatformFileError ObfuscatedFileSystemFileUtil::CreateFile( |
| 703 FileSystemOperationContext* context, | 724 FileSystemOperationContext* context, |
| 704 const GURL& origin_url, FileSystemType type, const FilePath& source_path, | 725 const GURL& origin_url, FileSystemType type, const FilePath& source_path, |
| 705 FileInfo* file_info, int file_flags, PlatformFile* handle) { | 726 FileInfo* file_info, int file_flags, PlatformFile* handle) { |
| 706 if (handle) | 727 if (handle) |
| 707 *handle = base::kInvalidPlatformFileValue; | 728 *handle = base::kInvalidPlatformFileValue; |
| 708 FileSystemDirectoryDatabase* db = GetDirectoryDatabase(origin_url, type); | 729 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 730 origin_url, type, true); | |
| 709 int64 number; | 731 int64 number; |
| 710 if (!db || !db->GetNextInteger(&number)) | 732 if (!db || !db->GetNextInteger(&number)) |
| 711 return base::PLATFORM_FILE_ERROR_FAILED; | 733 return base::PLATFORM_FILE_ERROR_FAILED; |
| 712 // We use the third- and fourth-to-last digits as the directory. | 734 // We use the third- and fourth-to-last digits as the directory. |
| 713 int64 directory_number = number % 10000 / 100; | 735 int64 directory_number = number % 10000 / 100; |
| 714 FilePath path = | 736 FilePath path = |
| 715 GetDirectoryForOriginAndType(origin_url, type, false); | 737 GetDirectoryForOriginAndType(origin_url, type, false); |
| 716 if (path.empty()) | 738 if (path.empty()) |
| 717 return base::PLATFORM_FILE_ERROR_FAILED; | 739 return base::PLATFORM_FILE_ERROR_FAILED; |
| 718 | 740 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 764 return base::PLATFORM_FILE_ERROR_FAILED; | 786 return base::PLATFORM_FILE_ERROR_FAILED; |
| 765 } | 787 } |
| 766 | 788 |
| 767 return base::PLATFORM_FILE_OK; | 789 return base::PLATFORM_FILE_OK; |
| 768 } | 790 } |
| 769 | 791 |
| 770 FilePath ObfuscatedFileSystemFileUtil::GetLocalPath( | 792 FilePath ObfuscatedFileSystemFileUtil::GetLocalPath( |
| 771 const GURL& origin_url, | 793 const GURL& origin_url, |
| 772 FileSystemType type, | 794 FileSystemType type, |
| 773 const FilePath& virtual_path) { | 795 const FilePath& virtual_path) { |
| 774 FileSystemDirectoryDatabase* db = GetDirectoryDatabase(origin_url, type); | 796 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 797 origin_url, type, false); | |
| 775 if (!db) | 798 if (!db) |
| 776 return FilePath(); | 799 return FilePath(); |
| 777 FileId file_id; | 800 FileId file_id; |
| 778 if (!db->GetFileWithPath(virtual_path, &file_id)) | 801 if (!db->GetFileWithPath(virtual_path, &file_id)) |
| 779 return FilePath(); | 802 return FilePath(); |
| 780 FileInfo file_info; | 803 FileInfo file_info; |
| 781 if (!db->GetFileInfo(file_id, &file_info) || file_info.is_directory()) { | 804 if (!db->GetFileInfo(file_id, &file_info) || file_info.is_directory()) { |
| 782 NOTREACHED(); | 805 NOTREACHED(); |
| 783 return FilePath(); // Directories have no local path. | 806 return FilePath(); // Directories have no local path. |
| 784 } | 807 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 819 return path; | 842 return path; |
| 820 } | 843 } |
| 821 | 844 |
| 822 bool ObfuscatedFileSystemFileUtil::MigrateFromOldSandbox( | 845 bool ObfuscatedFileSystemFileUtil::MigrateFromOldSandbox( |
| 823 const GURL& origin_url, FileSystemType type, const FilePath& src_root) { | 846 const GURL& origin_url, FileSystemType type, const FilePath& src_root) { |
| 824 if (!DestroyDirectoryDatabase(origin_url, type)) | 847 if (!DestroyDirectoryDatabase(origin_url, type)) |
| 825 return false; | 848 return false; |
| 826 FilePath dest_root = GetDirectoryForOriginAndType(origin_url, type, true); | 849 FilePath dest_root = GetDirectoryForOriginAndType(origin_url, type, true); |
| 827 if (dest_root.empty()) | 850 if (dest_root.empty()) |
| 828 return false; | 851 return false; |
| 829 FileSystemDirectoryDatabase* db = GetDirectoryDatabase(origin_url, type); | 852 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 853 origin_url, type, false); | |
|
ericu
2011/08/01 22:53:27
While this code won't actually fail [due to the "t
| |
| 830 if (!db) | 854 if (!db) |
| 831 return false; | 855 return false; |
| 832 | 856 |
| 833 file_util::FileEnumerator file_enum(src_root, true, | 857 file_util::FileEnumerator file_enum(src_root, true, |
| 834 static_cast<file_util::FileEnumerator::FILE_TYPE>( | 858 static_cast<file_util::FileEnumerator::FILE_TYPE>( |
| 835 file_util::FileEnumerator::FILES | | 859 file_util::FileEnumerator::FILES | |
| 836 file_util::FileEnumerator::DIRECTORIES)); | 860 file_util::FileEnumerator::DIRECTORIES)); |
| 837 FilePath src_full_path; | 861 FilePath src_full_path; |
| 838 size_t root_path_length = src_root.value().length() + 1; // +1 for the slash | 862 size_t root_path_length = src_root.value().length() + 1; // +1 for the slash |
| 839 while (!(src_full_path = file_enum.Next()).empty()) { | 863 while (!(src_full_path = file_enum.Next()).empty()) { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 918 return root; | 942 return root; |
| 919 // This removes the root, including the trailing slash, leaving a relative | 943 // This removes the root, including the trailing slash, leaving a relative |
| 920 // path. | 944 // path. |
| 921 return FilePath(local_path.value().substr(root.value().length() + 1)); | 945 return FilePath(local_path.value().substr(root.value().length() + 1)); |
| 922 } | 946 } |
| 923 | 947 |
| 924 // TODO: How to do the whole validation-without-creation thing? We may not have | 948 // TODO: How to do the whole validation-without-creation thing? We may not have |
| 925 // quota even to create the database. Ah, in that case don't even get here? | 949 // quota even to create the database. Ah, in that case don't even get here? |
| 926 // Still doesn't answer the quota issue, though. | 950 // Still doesn't answer the quota issue, though. |
| 927 FileSystemDirectoryDatabase* ObfuscatedFileSystemFileUtil::GetDirectoryDatabase( | 951 FileSystemDirectoryDatabase* ObfuscatedFileSystemFileUtil::GetDirectoryDatabase( |
| 928 const GURL& origin, FileSystemType type) { | 952 const GURL& origin, FileSystemType type, bool create) { |
| 929 | |
| 930 MarkUsed(); | |
| 931 std::string type_string = | 953 std::string type_string = |
| 932 FileSystemPathManager::GetFileSystemTypeString(type); | 954 FileSystemPathManager::GetFileSystemTypeString(type); |
| 933 if (type_string.empty()) { | 955 if (type_string.empty()) { |
| 934 LOG(WARNING) << "Unknown filesystem type requested:" << type; | 956 LOG(WARNING) << "Unknown filesystem type requested:" << type; |
| 935 return NULL; | 957 return NULL; |
| 936 } | 958 } |
| 937 std::string key = GetOriginIdentifierFromURL(origin) + type_string; | 959 std::string key = GetOriginIdentifierFromURL(origin) + type_string; |
| 938 DirectoryMap::iterator iter = directories_.find(key); | 960 DirectoryMap::iterator iter = directories_.find(key); |
| 939 if (iter != directories_.end()) | 961 if (iter != directories_.end()) { |
| 962 MarkUsed(); | |
| 940 return iter->second; | 963 return iter->second; |
| 964 } | |
| 941 | 965 |
| 942 FilePath path = GetDirectoryForOriginAndType(origin, type, true); | 966 FilePath path = GetDirectoryForOriginAndType(origin, type, create); |
| 943 if (path.empty()) | 967 if (path.empty()) |
| 944 return NULL; | 968 return NULL; |
| 945 if (!file_util::DirectoryExists(path)) { | 969 if (!file_util::DirectoryExists(path)) { |
| 946 if (!file_util::CreateDirectory(path)) { | 970 if (!file_util::CreateDirectory(path)) { |
| 947 LOG(WARNING) << "Failed to origin+type directory: " << path.value(); | 971 LOG(WARNING) << "Failed to origin+type directory: " << path.value(); |
| 948 return NULL; | 972 return NULL; |
| 949 } | 973 } |
| 950 } | 974 } |
| 975 MarkUsed(); | |
| 951 path = path.AppendASCII(kDirectoryDatabaseName); | 976 path = path.AppendASCII(kDirectoryDatabaseName); |
| 952 FileSystemDirectoryDatabase* database = new FileSystemDirectoryDatabase(path); | 977 FileSystemDirectoryDatabase* database = new FileSystemDirectoryDatabase(path); |
| 953 directories_[key] = database; | 978 directories_[key] = database; |
| 954 return database; | 979 return database; |
| 955 } | 980 } |
| 956 | 981 |
| 957 void ObfuscatedFileSystemFileUtil::MarkUsed() { | 982 void ObfuscatedFileSystemFileUtil::MarkUsed() { |
| 958 if (timer_.IsRunning()) | 983 if (timer_.IsRunning()) |
| 959 timer_.Reset(); | 984 timer_.Reset(); |
| 960 else | 985 else |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1001 return false; | 1026 return false; |
| 1002 } | 1027 } |
| 1003 origin_database_.reset( | 1028 origin_database_.reset( |
| 1004 new FileSystemOriginDatabase( | 1029 new FileSystemOriginDatabase( |
| 1005 file_system_directory_.AppendASCII(kOriginDatabaseName))); | 1030 file_system_directory_.AppendASCII(kOriginDatabaseName))); |
| 1006 } | 1031 } |
| 1007 return true; | 1032 return true; |
| 1008 } | 1033 } |
| 1009 | 1034 |
| 1010 } // namespace fileapi | 1035 } // namespace fileapi |
| OLD | NEW |