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