| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "webkit/fileapi/obfuscated_file_system_file_util.h" | |
| 6 | |
| 7 #include <queue> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/file_util.h" | |
| 11 #include "base/format_macros.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/message_loop.h" | |
| 14 #include "base/stl_util.h" | |
| 15 #include "base/string_number_conversions.h" | |
| 16 #include "base/stringprintf.h" | |
| 17 #include "base/sys_string_conversions.h" | |
| 18 #include "googleurl/src/gurl.h" | |
| 19 #include "webkit/fileapi/file_system_context.h" | |
| 20 #include "webkit/fileapi/file_system_operation_context.h" | |
| 21 #include "webkit/fileapi/file_system_path_manager.h" | |
| 22 #include "webkit/fileapi/file_system_quota_util.h" | |
| 23 #include "webkit/fileapi/file_system_util.h" | |
| 24 #include "webkit/fileapi/sandbox_mount_point_provider.h" | |
| 25 #include "webkit/quota/quota_manager.h" | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 const int64 kFlushDelaySeconds = 10 * 60; // 10 minutes | |
| 30 | |
| 31 const char kOriginDatabaseName[] = "Origins"; | |
| 32 const char kDirectoryDatabaseName[] = "Paths"; | |
| 33 | |
| 34 void InitFileInfo( | |
| 35 fileapi::FileSystemDirectoryDatabase::FileInfo* file_info, | |
| 36 fileapi::FileSystemDirectoryDatabase::FileId parent_id, | |
| 37 const FilePath::StringType& file_name) { | |
| 38 DCHECK(file_info); | |
| 39 file_info->parent_id = parent_id; | |
| 40 file_info->name = file_name; | |
| 41 } | |
| 42 | |
| 43 bool IsRootDirectory(const FilePath& virtual_path) { | |
| 44 return (virtual_path.empty() || | |
| 45 virtual_path.value() == FILE_PATH_LITERAL("/")); | |
| 46 } | |
| 47 | |
| 48 // Costs computed as per crbug.com/86114, based on the LevelDB implementation of | |
| 49 // path storage under Linux. It's not clear if that will differ on Windows, on | |
| 50 // which FilePath uses wide chars [since they're converted to UTF-8 for storage | |
| 51 // anyway], but as long as the cost is high enough that one can't cheat on quota | |
| 52 // by storing data in paths, it doesn't need to be all that accurate. | |
| 53 const int64 kPathCreationQuotaCost = 146; // Bytes per inode, basically. | |
| 54 const int64 kPathByteQuotaCost = 2; // Bytes per byte of path length in UTF-8. | |
| 55 | |
| 56 int64 GetPathQuotaUsage( | |
| 57 int growth_in_number_of_paths, | |
| 58 int64 growth_in_bytes_of_path_length) { | |
| 59 return growth_in_number_of_paths * kPathCreationQuotaCost + | |
| 60 growth_in_bytes_of_path_length * kPathByteQuotaCost; | |
| 61 } | |
| 62 | |
| 63 bool AllocateQuotaForPath( | |
| 64 fileapi::FileSystemOperationContext* context, | |
| 65 int growth_in_number_of_paths, | |
| 66 int64 growth_in_bytes_of_path_length) { | |
| 67 int64 growth = GetPathQuotaUsage(growth_in_number_of_paths, | |
| 68 growth_in_bytes_of_path_length); | |
| 69 int64 new_quota = context->allowed_bytes_growth() - growth; | |
| 70 | |
| 71 if (growth <= 0 || new_quota >= 0) { | |
| 72 context->set_allowed_bytes_growth(new_quota); | |
| 73 return true; | |
| 74 } | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 void UpdatePathQuotaUsage( | |
| 79 fileapi::FileSystemOperationContext* context, | |
| 80 const GURL& origin_url, | |
| 81 fileapi::FileSystemType type, | |
| 82 int growth_in_number_of_paths, // -1, 0, or 1 | |
| 83 int64 growth_in_bytes_of_path_length) { | |
| 84 int64 growth = GetPathQuotaUsage(growth_in_number_of_paths, | |
| 85 growth_in_bytes_of_path_length); | |
| 86 fileapi::FileSystemQuotaUtil* quota_util = | |
| 87 context->file_system_context()->GetQuotaUtil(type); | |
| 88 quota::QuotaManagerProxy* quota_manager_proxy = | |
| 89 context->file_system_context()->quota_manager_proxy(); | |
| 90 quota_util->UpdateOriginUsageOnFileThread(quota_manager_proxy, origin_url, | |
| 91 type, growth); | |
| 92 } | |
| 93 | |
| 94 const FilePath::CharType kLegacyDataDirectory[] = FILE_PATH_LITERAL("Legacy"); | |
| 95 | |
| 96 const FilePath::CharType kTemporaryDirectoryName[] = FILE_PATH_LITERAL("t"); | |
| 97 const FilePath::CharType kPersistentDirectoryName[] = FILE_PATH_LITERAL("p"); | |
| 98 | |
| 99 } // namespace | |
| 100 | |
| 101 namespace fileapi { | |
| 102 | |
| 103 using base::PlatformFile; | |
| 104 using base::PlatformFileError; | |
| 105 | |
| 106 ObfuscatedFileSystemFileUtil::ObfuscatedFileSystemFileUtil( | |
| 107 const FilePath& file_system_directory, | |
| 108 FileSystemFileUtil* underlying_file_util) | |
| 109 : file_system_directory_(file_system_directory), | |
| 110 underlying_file_util_(underlying_file_util) { | |
| 111 } | |
| 112 | |
| 113 ObfuscatedFileSystemFileUtil::~ObfuscatedFileSystemFileUtil() { | |
| 114 DropDatabases(); | |
| 115 } | |
| 116 | |
| 117 PlatformFileError ObfuscatedFileSystemFileUtil::CreateOrOpen( | |
| 118 FileSystemOperationContext* context, | |
| 119 const FilePath& virtual_path, int file_flags, | |
| 120 PlatformFile* file_handle, bool* created) { | |
| 121 DCHECK(!(file_flags & (base::PLATFORM_FILE_DELETE_ON_CLOSE | | |
| 122 base::PLATFORM_FILE_HIDDEN | base::PLATFORM_FILE_EXCLUSIVE_READ | | |
| 123 base::PLATFORM_FILE_EXCLUSIVE_WRITE))); | |
| 124 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 125 context->src_origin_url(), context->src_type(), true); | |
| 126 if (!db) | |
| 127 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 128 FileId file_id; | |
| 129 if (!db->GetFileWithPath(virtual_path, &file_id)) { | |
| 130 // The file doesn't exist. | |
| 131 if (!(file_flags & (base::PLATFORM_FILE_CREATE | | |
| 132 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_OPEN_ALWAYS))) | |
| 133 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 134 FileId parent_id; | |
| 135 if (!db->GetFileWithPath(virtual_path.DirName(), &parent_id)) | |
| 136 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 137 FileInfo file_info; | |
| 138 InitFileInfo(&file_info, parent_id, virtual_path.BaseName().value()); | |
| 139 if (!AllocateQuotaForPath(context, 1, file_info.name.size())) | |
| 140 return base::PLATFORM_FILE_ERROR_NO_SPACE; | |
| 141 PlatformFileError error = CreateFile( | |
| 142 context, context->src_origin_url(), context->src_type(), FilePath(), | |
| 143 &file_info, file_flags, file_handle); | |
| 144 if (created && base::PLATFORM_FILE_OK == error) | |
| 145 *created = true; | |
| 146 return error; | |
| 147 } | |
| 148 if (file_flags & base::PLATFORM_FILE_CREATE) | |
| 149 return base::PLATFORM_FILE_ERROR_EXISTS; | |
| 150 | |
| 151 FileInfo file_info; | |
| 152 if (!db->GetFileInfo(file_id, &file_info)) { | |
| 153 NOTREACHED(); | |
| 154 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 155 } | |
| 156 if (file_info.is_directory()) | |
| 157 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; | |
| 158 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), | |
| 159 context->src_type(), file_info.data_path); | |
| 160 return underlying_file_util_->CreateOrOpen( | |
| 161 context, data_path, file_flags, file_handle, created); | |
| 162 } | |
| 163 | |
| 164 PlatformFileError ObfuscatedFileSystemFileUtil::EnsureFileExists( | |
| 165 FileSystemOperationContext* context, | |
| 166 const FilePath& virtual_path, | |
| 167 bool* created) { | |
| 168 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 169 context->src_origin_url(), context->src_type(), true); | |
| 170 if (!db) | |
| 171 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 172 FileId file_id; | |
| 173 if (db->GetFileWithPath(virtual_path, &file_id)) { | |
| 174 FileInfo file_info; | |
| 175 if (!db->GetFileInfo(file_id, &file_info)) { | |
| 176 NOTREACHED(); | |
| 177 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 178 } | |
| 179 if (file_info.is_directory()) | |
| 180 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; | |
| 181 if (created) | |
| 182 *created = false; | |
| 183 return base::PLATFORM_FILE_OK; | |
| 184 } | |
| 185 FileId parent_id; | |
| 186 if (!db->GetFileWithPath(virtual_path.DirName(), &parent_id)) | |
| 187 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 188 | |
| 189 FileInfo file_info; | |
| 190 InitFileInfo(&file_info, parent_id, virtual_path.BaseName().value()); | |
| 191 if (!AllocateQuotaForPath(context, 1, file_info.name.size())) | |
| 192 return base::PLATFORM_FILE_ERROR_NO_SPACE; | |
| 193 PlatformFileError error = CreateFile(context, context->src_origin_url(), | |
| 194 context->src_type(), FilePath(), &file_info, 0, NULL); | |
| 195 if (created && base::PLATFORM_FILE_OK == error) | |
| 196 *created = true; | |
| 197 return error; | |
| 198 } | |
| 199 | |
| 200 PlatformFileError ObfuscatedFileSystemFileUtil::GetLocalFilePath( | |
| 201 FileSystemOperationContext* context, | |
| 202 const FilePath& virtual_path, | |
| 203 FilePath* local_path) { | |
| 204 FilePath path = | |
| 205 GetLocalPath(context->src_origin_url(), context->src_type(), | |
| 206 virtual_path); | |
| 207 if (path.empty()) | |
| 208 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 209 | |
| 210 *local_path = path; | |
| 211 return base::PLATFORM_FILE_OK; | |
| 212 } | |
| 213 | |
| 214 PlatformFileError ObfuscatedFileSystemFileUtil::GetFileInfo( | |
| 215 FileSystemOperationContext* context, | |
| 216 const FilePath& virtual_path, | |
| 217 base::PlatformFileInfo* file_info, | |
| 218 FilePath* platform_file_path) { | |
| 219 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 220 context->src_origin_url(), context->src_type(), false); | |
| 221 if (!db) | |
| 222 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 223 FileId file_id; | |
| 224 if (!db->GetFileWithPath(virtual_path, &file_id)) | |
| 225 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 226 FileInfo local_info; | |
| 227 return GetFileInfoInternal(db, context, file_id, | |
| 228 &local_info, file_info, platform_file_path); | |
| 229 } | |
| 230 | |
| 231 PlatformFileError ObfuscatedFileSystemFileUtil::ReadDirectory( | |
| 232 FileSystemOperationContext* context, | |
| 233 const FilePath& virtual_path, | |
| 234 std::vector<base::FileUtilProxy::Entry>* entries) { | |
| 235 // TODO(kkanetkar): Implement directory read in multiple chunks. | |
| 236 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 237 context->src_origin_url(), context->src_type(), false); | |
| 238 if (!db) { | |
| 239 if (IsRootDirectory(virtual_path)) { | |
| 240 // It's the root directory and the database hasn't been initialized yet. | |
| 241 entries->clear(); | |
| 242 return base::PLATFORM_FILE_OK; | |
| 243 } | |
| 244 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 245 } | |
| 246 FileId file_id; | |
| 247 if (!db->GetFileWithPath(virtual_path, &file_id)) | |
| 248 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 249 FileInfo file_info; | |
| 250 if (!db->GetFileInfo(file_id, &file_info)) { | |
| 251 DCHECK(!file_id); | |
| 252 // It's the root directory and the database hasn't been initialized yet. | |
| 253 entries->clear(); | |
| 254 return base::PLATFORM_FILE_OK; | |
| 255 } | |
| 256 if (!file_info.is_directory()) | |
| 257 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 258 std::vector<FileId> children; | |
| 259 if (!db->ListChildren(file_id, &children)) { | |
| 260 NOTREACHED(); | |
| 261 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 262 } | |
| 263 std::vector<FileId>::iterator iter; | |
| 264 for (iter = children.begin(); iter != children.end(); ++iter) { | |
| 265 base::PlatformFileInfo platform_file_info; | |
| 266 FilePath file_path; | |
| 267 if (GetFileInfoInternal(db, context, *iter, | |
| 268 &file_info, &platform_file_info, &file_path) != | |
| 269 base::PLATFORM_FILE_OK) { | |
| 270 NOTREACHED(); | |
| 271 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 272 } | |
| 273 | |
| 274 base::FileUtilProxy::Entry entry; | |
| 275 entry.name = file_info.name; | |
| 276 entry.is_directory = file_info.is_directory(); | |
| 277 entry.size = entry.is_directory ? 0 : platform_file_info.size; | |
| 278 entry.last_modified_time = platform_file_info.last_modified; | |
| 279 entries->push_back(entry); | |
| 280 } | |
| 281 return base::PLATFORM_FILE_OK; | |
| 282 } | |
| 283 | |
| 284 PlatformFileError ObfuscatedFileSystemFileUtil::CreateDirectory( | |
| 285 FileSystemOperationContext* context, | |
| 286 const FilePath& virtual_path, | |
| 287 bool exclusive, | |
| 288 bool recursive) { | |
| 289 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 290 context->src_origin_url(), context->src_type(), true); | |
| 291 if (!db) | |
| 292 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 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 | |
| 307 std::vector<FilePath::StringType> components; | |
| 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; | |
| 337 } | |
| 338 | |
| 339 PlatformFileError ObfuscatedFileSystemFileUtil::CopyOrMoveFile( | |
| 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, | |
| 540 const FilePath& virtual_path, | |
| 541 const base::Time& last_access_time, | |
| 542 const base::Time& last_modified_time) { | |
| 543 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 544 context->src_origin_url(), context->src_type(), false); | |
| 545 if (!db) | |
| 546 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 547 FileId file_id; | |
| 548 if (!db->GetFileWithPath(virtual_path, &file_id)) | |
| 549 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 550 | |
| 551 FileInfo file_info; | |
| 552 if (!db->GetFileInfo(file_id, &file_info)) { | |
| 553 NOTREACHED(); | |
| 554 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 555 } | |
| 556 if (file_info.is_directory()) { | |
| 557 file_info.modification_time = last_modified_time; | |
| 558 if (!db->UpdateFileInfo(file_id, file_info)) | |
| 559 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 560 return base::PLATFORM_FILE_OK; | |
| 561 } | |
| 562 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), | |
| 563 context->src_type(), file_info.data_path); | |
| 564 return underlying_file_util_->Touch( | |
| 565 context, data_path, last_access_time, last_modified_time); | |
| 566 } | |
| 567 | |
| 568 PlatformFileError ObfuscatedFileSystemFileUtil::Truncate( | |
| 569 FileSystemOperationContext* context, | |
| 570 const FilePath& virtual_path, | |
| 571 int64 length) { | |
| 572 FilePath local_path = | |
| 573 GetLocalPath(context->src_origin_url(), context->src_type(), | |
| 574 virtual_path); | |
| 575 if (local_path.empty()) | |
| 576 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 577 return underlying_file_util_->Truncate( | |
| 578 context, local_path, length); | |
| 579 } | |
| 580 | |
| 581 bool ObfuscatedFileSystemFileUtil::PathExists( | |
| 582 FileSystemOperationContext* context, | |
| 583 const FilePath& virtual_path) { | |
| 584 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 585 context->src_origin_url(), context->src_type(), false); | |
| 586 if (!db) | |
| 587 return false; | |
| 588 FileId file_id; | |
| 589 return db->GetFileWithPath(virtual_path, &file_id); | |
| 590 } | |
| 591 | |
| 592 bool ObfuscatedFileSystemFileUtil::DirectoryExists( | |
| 593 FileSystemOperationContext* context, | |
| 594 const FilePath& virtual_path) { | |
| 595 if (IsRootDirectory(virtual_path)) { | |
| 596 // It's questionable whether we should return true or false for the | |
| 597 // root directory of nonexistent origin, but here we return true | |
| 598 // as the current implementation of ReadDirectory always returns an empty | |
| 599 // array (rather than erroring out with NOT_FOUND_ERR even) for | |
| 600 // nonexistent origins. | |
| 601 // Note: if you're going to change this behavior please also consider | |
| 602 // changiing the ReadDirectory's behavior! | |
| 603 return true; | |
| 604 } | |
| 605 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 606 context->src_origin_url(), context->src_type(), false); | |
| 607 if (!db) | |
| 608 return false; | |
| 609 FileId file_id; | |
| 610 if (!db->GetFileWithPath(virtual_path, &file_id)) | |
| 611 return false; | |
| 612 FileInfo file_info; | |
| 613 if (!db->GetFileInfo(file_id, &file_info)) { | |
| 614 NOTREACHED(); | |
| 615 return false; | |
| 616 } | |
| 617 return file_info.is_directory(); | |
| 618 } | |
| 619 | |
| 620 bool ObfuscatedFileSystemFileUtil::IsDirectoryEmpty( | |
| 621 FileSystemOperationContext* context, | |
| 622 const FilePath& virtual_path) { | |
| 623 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 624 context->src_origin_url(), context->src_type(), false); | |
| 625 if (!db) | |
| 626 return true; // Not a great answer, but it's what others do. | |
| 627 FileId file_id; | |
| 628 if (!db->GetFileWithPath(virtual_path, &file_id)) | |
| 629 return true; // Ditto. | |
| 630 FileInfo file_info; | |
| 631 if (!db->GetFileInfo(file_id, &file_info)) { | |
| 632 DCHECK(!file_id); | |
| 633 // It's the root directory and the database hasn't been initialized yet. | |
| 634 return true; | |
| 635 } | |
| 636 if (!file_info.is_directory()) | |
| 637 return true; | |
| 638 std::vector<FileId> children; | |
| 639 // TODO(ericu): This could easily be made faster with help from the database. | |
| 640 if (!db->ListChildren(file_id, &children)) | |
| 641 return true; | |
| 642 return children.empty(); | |
| 643 } | |
| 644 | |
| 645 class ObfuscatedFileSystemFileEnumerator | |
| 646 : public FileSystemFileUtil::AbstractFileEnumerator { | |
| 647 public: | |
| 648 ObfuscatedFileSystemFileEnumerator( | |
| 649 FileSystemDirectoryDatabase* db, const FilePath& virtual_root_path) | |
| 650 : db_(db) { | |
| 651 FileId file_id; | |
| 652 FileInfo file_info; | |
| 653 if (!db_->GetFileWithPath(virtual_root_path, &file_id)) | |
| 654 return; | |
| 655 if (!db_->GetFileInfo(file_id, &file_info)) | |
| 656 return; | |
| 657 if (!file_info.is_directory()) | |
| 658 return; | |
| 659 FileRecord record = { file_id, file_info, virtual_root_path }; | |
| 660 display_queue_.push(record); | |
| 661 Next(); // Enumerators don't include the directory itself. | |
| 662 } | |
| 663 | |
| 664 ~ObfuscatedFileSystemFileEnumerator() {} | |
| 665 | |
| 666 virtual FilePath Next() { | |
| 667 ProcessRecurseQueue(); | |
| 668 if (display_queue_.empty()) | |
| 669 return FilePath(); | |
| 670 current_ = display_queue_.front(); | |
| 671 display_queue_.pop(); | |
| 672 if (current_.file_info.is_directory()) | |
| 673 recurse_queue_.push(current_); | |
| 674 return current_.file_path; | |
| 675 } | |
| 676 | |
| 677 virtual bool IsDirectory() { | |
| 678 return current_.file_info.is_directory(); | |
| 679 } | |
| 680 | |
| 681 private: | |
| 682 typedef FileSystemDirectoryDatabase::FileId FileId; | |
| 683 typedef FileSystemDirectoryDatabase::FileInfo FileInfo; | |
| 684 | |
| 685 struct FileRecord { | |
| 686 FileId file_id; | |
| 687 FileInfo file_info; | |
| 688 FilePath file_path; | |
| 689 }; | |
| 690 | |
| 691 void ProcessRecurseQueue() { | |
| 692 while (display_queue_.empty() && !recurse_queue_.empty()) { | |
| 693 FileRecord directory = recurse_queue_.front(); | |
| 694 std::vector<FileId> children; | |
| 695 recurse_queue_.pop(); | |
| 696 if (!db_->ListChildren(directory.file_id, &children)) | |
| 697 return; | |
| 698 std::vector<FileId>::iterator iter; | |
| 699 for (iter = children.begin(); iter != children.end(); ++iter) { | |
| 700 FileRecord child; | |
| 701 child.file_id = *iter; | |
| 702 if (!db_->GetFileInfo(child.file_id, &child.file_info)) | |
| 703 return; | |
| 704 child.file_path = directory.file_path.Append(child.file_info.name); | |
| 705 display_queue_.push(child); | |
| 706 } | |
| 707 } | |
| 708 } | |
| 709 | |
| 710 std::queue<FileRecord> display_queue_; | |
| 711 std::queue<FileRecord> recurse_queue_; | |
| 712 FileRecord current_; | |
| 713 FileSystemDirectoryDatabase* db_; | |
| 714 }; | |
| 715 | |
| 716 class ObfuscatedFileSystemOriginEnumerator | |
| 717 : public ObfuscatedFileSystemFileUtil::AbstractOriginEnumerator { | |
| 718 public: | |
| 719 typedef FileSystemOriginDatabase::OriginRecord OriginRecord; | |
| 720 ObfuscatedFileSystemOriginEnumerator( | |
| 721 FileSystemOriginDatabase* origin_database, | |
| 722 const FilePath& base_path) | |
| 723 : base_path_(base_path) { | |
| 724 if (origin_database) | |
| 725 origin_database->ListAllOrigins(&origins_); | |
| 726 } | |
| 727 | |
| 728 ~ObfuscatedFileSystemOriginEnumerator() {} | |
| 729 | |
| 730 // Returns the next origin. Returns empty if there are no more origins. | |
| 731 virtual GURL Next() OVERRIDE { | |
| 732 OriginRecord record; | |
| 733 if (!origins_.empty()) { | |
| 734 record = origins_.back(); | |
| 735 origins_.pop_back(); | |
| 736 } | |
| 737 current_ = record; | |
| 738 return GetOriginURLFromIdentifier(record.origin); | |
| 739 } | |
| 740 | |
| 741 // Returns the current origin's information. | |
| 742 virtual bool HasFileSystemType(FileSystemType type) const OVERRIDE { | |
| 743 if (current_.path.empty()) | |
| 744 return false; | |
| 745 FilePath::StringType type_string = | |
| 746 ObfuscatedFileSystemFileUtil::GetDirectoryNameForType(type); | |
| 747 if (type_string.empty()) { | |
| 748 NOTREACHED(); | |
| 749 return false; | |
| 750 } | |
| 751 FilePath path = base_path_.Append(current_.path).Append(type_string); | |
| 752 return file_util::DirectoryExists(path); | |
| 753 } | |
| 754 | |
| 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 { | |
| 850 if (handle) { | |
| 851 error = underlying_file_util_->CreateOrOpen( | |
| 852 context, path, file_flags, handle, &created); | |
| 853 // If this succeeds, we must close handle on any subsequent error. | |
| 854 } else { | |
| 855 DCHECK(!file_flags); // file_flags is only used by CreateOrOpen. | |
| 856 error = underlying_file_util_->EnsureFileExists( | |
| 857 context, path, &created); | |
| 858 } | |
| 859 } | |
| 860 if (error != base::PLATFORM_FILE_OK) | |
| 861 return error; | |
| 862 | |
| 863 if (!created) { | |
| 864 NOTREACHED(); | |
| 865 if (handle) { | |
| 866 DCHECK_NE(base::kInvalidPlatformFileValue, *handle); | |
| 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) { | |
| 891 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 892 origin_url, type, false); | |
| 893 if (!db) | |
| 894 return FilePath(); | |
| 895 FileId file_id; | |
| 896 if (!db->GetFileWithPath(virtual_path, &file_id)) | |
| 897 return FilePath(); | |
| 898 FileInfo file_info; | |
| 899 if (!db->GetFileInfo(file_id, &file_info) || file_info.is_directory()) { | |
| 900 NOTREACHED(); | |
| 901 return FilePath(); // Directories have no local path. | |
| 902 } | |
| 903 return DataPathToLocalPath(origin_url, type, file_info.data_path); | |
| 904 } | |
| 905 | |
| 906 FilePath ObfuscatedFileSystemFileUtil::GetDirectoryForOriginAndType( | |
| 907 const GURL& origin, FileSystemType type, bool create) { | |
| 908 FilePath origin_dir = GetDirectoryForOrigin(origin, create); | |
| 909 if (origin_dir.empty()) | |
| 910 return FilePath(); | |
| 911 FilePath::StringType type_string = GetDirectoryNameForType(type); | |
| 912 if (type_string.empty()) { | |
| 913 LOG(WARNING) << "Unknown filesystem type requested:" << type; | |
| 914 return FilePath(); | |
| 915 } | |
| 916 FilePath path = origin_dir.Append(type_string); | |
| 917 if (!file_util::DirectoryExists(path) && | |
| 918 (!create || !file_util::CreateDirectory(path))) | |
| 919 return FilePath(); | |
| 920 return path; | |
| 921 } | |
| 922 | |
| 923 bool ObfuscatedFileSystemFileUtil::DeleteDirectoryForOriginAndType( | |
| 924 const GURL& origin, FileSystemType type) { | |
| 925 FilePath origin_type_path = GetDirectoryForOriginAndType(origin, type, false); | |
| 926 if (!file_util::PathExists(origin_type_path)) | |
| 927 return true; | |
| 928 | |
| 929 // TODO(dmikurube): Consider the return value of DestroyDirectoryDatabase. | |
| 930 // 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. | |
| 932 // Information about failure would be useful for debugging. | |
| 933 DestroyDirectoryDatabase(origin, type); | |
| 934 if (!file_util::Delete(origin_type_path, true /* recursive */)) | |
| 935 return false; | |
| 936 | |
| 937 FilePath origin_path = origin_type_path.DirName(); | |
| 938 DCHECK_EQ(origin_path.value(), GetDirectoryForOrigin(origin, false).value()); | |
| 939 | |
| 940 // Delete the origin directory if the deleted one was the last remaining | |
| 941 // type for the origin. | |
| 942 if (file_util::Delete(origin_path, false /* recursive */)) { | |
| 943 InitOriginDatabase(false); | |
| 944 if (origin_database_.get()) | |
| 945 origin_database_->RemovePathForOrigin(GetOriginIdentifierFromURL(origin)); | |
| 946 } | |
| 947 | |
| 948 // At this point we are sure we had successfully deleted the origin/type | |
| 949 // directory, so just returning true here. | |
| 950 return true; | |
| 951 } | |
| 952 | |
| 953 bool ObfuscatedFileSystemFileUtil::MigrateFromOldSandbox( | |
| 954 const GURL& origin_url, FileSystemType type, const FilePath& src_root) { | |
| 955 if (!DestroyDirectoryDatabase(origin_url, type)) | |
| 956 return false; | |
| 957 FilePath dest_root = GetDirectoryForOriginAndType(origin_url, type, true); | |
| 958 if (dest_root.empty()) | |
| 959 return false; | |
| 960 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
| 961 origin_url, type, true); | |
| 962 if (!db) | |
| 963 return false; | |
| 964 | |
| 965 file_util::FileEnumerator file_enum(src_root, true, | |
| 966 static_cast<file_util::FileEnumerator::FileType>( | |
| 967 file_util::FileEnumerator::FILES | | |
| 968 file_util::FileEnumerator::DIRECTORIES)); | |
| 969 FilePath src_full_path; | |
| 970 size_t root_path_length = src_root.value().length() + 1; // +1 for the slash | |
| 971 while (!(src_full_path = file_enum.Next()).empty()) { | |
| 972 file_util::FileEnumerator::FindInfo info; | |
| 973 file_enum.GetFindInfo(&info); | |
| 974 FilePath relative_virtual_path = | |
| 975 FilePath(src_full_path.value().substr(root_path_length)); | |
| 976 if (relative_virtual_path.empty()) { | |
| 977 LOG(WARNING) << "Failed to convert path to relative: " << | |
| 978 src_full_path.value(); | |
| 979 return false; | |
| 980 } | |
| 981 FileId file_id; | |
| 982 if (db->GetFileWithPath(relative_virtual_path, &file_id)) { | |
| 983 NOTREACHED(); // File already exists. | |
| 984 return false; | |
| 985 } | |
| 986 if (!db->GetFileWithPath(relative_virtual_path.DirName(), &file_id)) { | |
| 987 NOTREACHED(); // Parent doesn't exist. | |
| 988 return false; | |
| 989 } | |
| 990 | |
| 991 FileInfo file_info; | |
| 992 file_info.name = src_full_path.BaseName().value(); | |
| 993 if (file_util::FileEnumerator::IsDirectory(info)) { | |
| 994 #if defined(OS_WIN) | |
| 995 file_info.modification_time = | |
| 996 base::Time::FromFileTime(info.ftLastWriteTime); | |
| 997 #elif defined(OS_POSIX) | |
| 998 file_info.modification_time = base::Time::FromTimeT(info.stat.st_mtime); | |
| 999 #endif | |
| 1000 } else { | |
| 1001 file_info.data_path = | |
| 1002 FilePath(kLegacyDataDirectory).Append(relative_virtual_path); | |
| 1003 } | |
| 1004 file_info.parent_id = file_id; | |
| 1005 if (!db->AddFileInfo(file_info, &file_id)) { | |
| 1006 NOTREACHED(); | |
| 1007 return false; | |
| 1008 } | |
| 1009 } | |
| 1010 // TODO(ericu): Should we adjust the mtime of the root directory to match as | |
| 1011 // well? | |
| 1012 FilePath legacy_dest_dir = dest_root.Append(kLegacyDataDirectory); | |
| 1013 | |
| 1014 if (!file_util::Move(src_root, legacy_dest_dir)) { | |
| 1015 LOG(WARNING) << | |
| 1016 "The final step of a migration failed; I'll try to clean up."; | |
| 1017 db = NULL; | |
| 1018 DestroyDirectoryDatabase(origin_url, type); | |
| 1019 return false; | |
| 1020 } | |
| 1021 return true; | |
| 1022 } | |
| 1023 | |
| 1024 // static | |
| 1025 FilePath::StringType ObfuscatedFileSystemFileUtil::GetDirectoryNameForType( | |
| 1026 FileSystemType type) { | |
| 1027 switch (type) { | |
| 1028 case kFileSystemTypeTemporary: | |
| 1029 return kTemporaryDirectoryName; | |
| 1030 case kFileSystemTypePersistent: | |
| 1031 return kPersistentDirectoryName; | |
| 1032 case kFileSystemTypeUnknown: | |
| 1033 default: | |
| 1034 return FilePath::StringType(); | |
| 1035 } | |
| 1036 } | |
| 1037 | |
| 1038 FilePath ObfuscatedFileSystemFileUtil::DataPathToLocalPath( | |
| 1039 const GURL& origin, FileSystemType type, const FilePath& data_path) { | |
| 1040 FilePath root = GetDirectoryForOriginAndType(origin, type, false); | |
| 1041 if (root.empty()) | |
| 1042 return root; | |
| 1043 return root.Append(data_path); | |
| 1044 } | |
| 1045 | |
| 1046 FilePath ObfuscatedFileSystemFileUtil::LocalPathToDataPath( | |
| 1047 const GURL& origin, FileSystemType type, const FilePath& local_path) { | |
| 1048 FilePath root = GetDirectoryForOriginAndType(origin, type, false); | |
| 1049 if (root.empty()) | |
| 1050 return root; | |
| 1051 // This removes the root, including the trailing slash, leaving a relative | |
| 1052 // path. | |
| 1053 return FilePath(local_path.value().substr(root.value().length() + 1)); | |
| 1054 } | |
| 1055 | |
| 1056 // 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? | |
| 1058 // Still doesn't answer the quota issue, though. | |
| 1059 FileSystemDirectoryDatabase* ObfuscatedFileSystemFileUtil::GetDirectoryDatabase( | |
| 1060 const GURL& origin, FileSystemType type, bool create) { | |
| 1061 std::string type_string = | |
| 1062 FileSystemPathManager::GetFileSystemTypeString(type); | |
| 1063 if (type_string.empty()) { | |
| 1064 LOG(WARNING) << "Unknown filesystem type requested:" << type; | |
| 1065 return NULL; | |
| 1066 } | |
| 1067 std::string key = GetOriginIdentifierFromURL(origin) + type_string; | |
| 1068 DirectoryMap::iterator iter = directories_.find(key); | |
| 1069 if (iter != directories_.end()) { | |
| 1070 MarkUsed(); | |
| 1071 return iter->second; | |
| 1072 } | |
| 1073 | |
| 1074 FilePath path = GetDirectoryForOriginAndType(origin, type, create); | |
| 1075 if (path.empty()) | |
| 1076 return NULL; | |
| 1077 if (!file_util::DirectoryExists(path)) { | |
| 1078 if (!file_util::CreateDirectory(path)) { | |
| 1079 LOG(WARNING) << "Failed to origin+type directory: " << path.value(); | |
| 1080 return NULL; | |
| 1081 } | |
| 1082 } | |
| 1083 MarkUsed(); | |
| 1084 path = path.AppendASCII(kDirectoryDatabaseName); | |
| 1085 FileSystemDirectoryDatabase* database = new FileSystemDirectoryDatabase(path); | |
| 1086 directories_[key] = database; | |
| 1087 return database; | |
| 1088 } | |
| 1089 | |
| 1090 FilePath ObfuscatedFileSystemFileUtil::GetDirectoryForOrigin( | |
| 1091 const GURL& origin, bool create) { | |
| 1092 if (!InitOriginDatabase(create)) | |
| 1093 return FilePath(); | |
| 1094 FilePath directory_name; | |
| 1095 std::string id = GetOriginIdentifierFromURL(origin); | |
| 1096 if (!create && !origin_database_->HasOriginPath(id)) | |
| 1097 return FilePath(); | |
| 1098 if (!origin_database_->GetPathForOrigin(id, &directory_name)) | |
| 1099 return FilePath(); | |
| 1100 FilePath path = file_system_directory_.Append(directory_name); | |
| 1101 if (!file_util::DirectoryExists(path) && | |
| 1102 (!create || !file_util::CreateDirectory(path))) | |
| 1103 return FilePath(); | |
| 1104 return path; | |
| 1105 } | |
| 1106 | |
| 1107 void ObfuscatedFileSystemFileUtil::MarkUsed() { | |
| 1108 if (timer_.IsRunning()) | |
| 1109 timer_.Reset(); | |
| 1110 else | |
| 1111 timer_.Start(base::TimeDelta::FromSeconds(kFlushDelaySeconds), this, | |
| 1112 &ObfuscatedFileSystemFileUtil::DropDatabases); | |
| 1113 } | |
| 1114 | |
| 1115 void ObfuscatedFileSystemFileUtil::DropDatabases() { | |
| 1116 origin_database_.reset(); | |
| 1117 STLDeleteContainerPairSecondPointers( | |
| 1118 directories_.begin(), directories_.end()); | |
| 1119 directories_.clear(); | |
| 1120 } | |
| 1121 | |
| 1122 // static | |
| 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()) { | |
| 1154 if (!create && !file_util::DirectoryExists(file_system_directory_)) | |
| 1155 return false; | |
| 1156 if (!file_util::CreateDirectory(file_system_directory_)) { | |
| 1157 LOG(WARNING) << "Failed to create FileSystem directory: " << | |
| 1158 file_system_directory_.value(); | |
| 1159 return false; | |
| 1160 } | |
| 1161 origin_database_.reset( | |
| 1162 new FileSystemOriginDatabase( | |
| 1163 file_system_directory_.AppendASCII(kOriginDatabaseName))); | |
| 1164 } | |
| 1165 return true; | |
| 1166 } | |
| 1167 | |
| 1168 } // namespace fileapi | |
| OLD | NEW |