Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: webkit/browser/fileapi/obfuscated_file_util.cc

Issue 145303002: Convert Media Galleries to use base::File (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/browser/fileapi/obfuscated_file_util.h" 5 #include "webkit/browser/fileapi/obfuscated_file_util.h"
6 6
7 #include <queue> 7 #include <queue>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 99
100 enum IsolatedOriginStatus { 100 enum IsolatedOriginStatus {
101 kIsolatedOriginMatch, 101 kIsolatedOriginMatch,
102 kIsolatedOriginDontMatch, 102 kIsolatedOriginDontMatch,
103 kIsolatedOriginStatusMax, 103 kIsolatedOriginStatusMax,
104 }; 104 };
105 105
106 } // namespace 106 } // namespace
107 107
108 using base::PlatformFile; 108 using base::PlatformFile;
109 using base::PlatformFileError;
110 109
111 class ObfuscatedFileEnumerator 110 class ObfuscatedFileEnumerator
112 : public FileSystemFileUtil::AbstractFileEnumerator { 111 : public FileSystemFileUtil::AbstractFileEnumerator {
113 public: 112 public:
114 ObfuscatedFileEnumerator( 113 ObfuscatedFileEnumerator(
115 SandboxDirectoryDatabase* db, 114 SandboxDirectoryDatabase* db,
116 FileSystemOperationContext* context, 115 FileSystemOperationContext* context,
117 ObfuscatedFileUtil* obfuscated_file_util, 116 ObfuscatedFileUtil* obfuscated_file_util,
118 const FileSystemURL& root_url, 117 const FileSystemURL& root_url,
119 bool recursive) 118 bool recursive)
(...skipping 18 matching lines...) Expand all
138 virtual base::FilePath Next() OVERRIDE { 137 virtual base::FilePath Next() OVERRIDE {
139 ProcessRecurseQueue(); 138 ProcessRecurseQueue();
140 if (display_stack_.empty()) 139 if (display_stack_.empty())
141 return base::FilePath(); 140 return base::FilePath();
142 141
143 current_file_id_ = display_stack_.back(); 142 current_file_id_ = display_stack_.back();
144 display_stack_.pop_back(); 143 display_stack_.pop_back();
145 144
146 FileInfo file_info; 145 FileInfo file_info;
147 base::FilePath platform_file_path; 146 base::FilePath platform_file_path;
148 base::PlatformFileError error = 147 base::File::Error error =
149 obfuscated_file_util_->GetFileInfoInternal( 148 obfuscated_file_util_->GetFileInfoInternal(
150 db_, context_, root_url_, current_file_id_, 149 db_, context_, root_url_, current_file_id_,
151 &file_info, &current_platform_file_info_, &platform_file_path); 150 &file_info, &current_platform_file_info_, &platform_file_path);
152 if (error != base::PLATFORM_FILE_OK) 151 if (error != base::File::FILE_OK)
153 return Next(); 152 return Next();
154 153
155 base::FilePath virtual_path = 154 base::FilePath virtual_path =
156 current_parent_virtual_path_.Append(file_info.name); 155 current_parent_virtual_path_.Append(file_info.name);
157 if (recursive_ && file_info.is_directory()) { 156 if (recursive_ && file_info.is_directory()) {
158 FileRecord record = { current_file_id_, virtual_path }; 157 FileRecord record = { current_file_id_, virtual_path };
159 recurse_queue_.push(record); 158 recurse_queue_.push(record);
160 } 159 }
161 return virtual_path; 160 return virtual_path;
162 } 161 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 FileSystemOperationContext* context_; 197 FileSystemOperationContext* context_;
199 ObfuscatedFileUtil* obfuscated_file_util_; 198 ObfuscatedFileUtil* obfuscated_file_util_;
200 FileSystemURL root_url_; 199 FileSystemURL root_url_;
201 bool recursive_; 200 bool recursive_;
202 201
203 std::queue<FileRecord> recurse_queue_; 202 std::queue<FileRecord> recurse_queue_;
204 std::vector<FileId> display_stack_; 203 std::vector<FileId> display_stack_;
205 base::FilePath current_parent_virtual_path_; 204 base::FilePath current_parent_virtual_path_;
206 205
207 FileId current_file_id_; 206 FileId current_file_id_;
208 base::PlatformFileInfo current_platform_file_info_; 207 base::File::Info current_platform_file_info_;
209 }; 208 };
210 209
211 class ObfuscatedOriginEnumerator 210 class ObfuscatedOriginEnumerator
212 : public ObfuscatedFileUtil::AbstractOriginEnumerator { 211 : public ObfuscatedFileUtil::AbstractOriginEnumerator {
213 public: 212 public:
214 typedef SandboxOriginDatabase::OriginRecord OriginRecord; 213 typedef SandboxOriginDatabase::OriginRecord OriginRecord;
215 ObfuscatedOriginEnumerator( 214 ObfuscatedOriginEnumerator(
216 SandboxOriginDatabaseInterface* origin_database, 215 SandboxOriginDatabaseInterface* origin_database,
217 const base::FilePath& base_file_path) 216 const base::FilePath& base_file_path)
218 : base_file_path_(base_file_path) { 217 : base_file_path_(base_file_path) {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 file_task_runner_(file_task_runner), 264 file_task_runner_(file_task_runner),
266 get_type_string_for_url_(get_type_string_for_url), 265 get_type_string_for_url_(get_type_string_for_url),
267 known_type_strings_(known_type_strings), 266 known_type_strings_(known_type_strings),
268 sandbox_delegate_(sandbox_delegate) { 267 sandbox_delegate_(sandbox_delegate) {
269 } 268 }
270 269
271 ObfuscatedFileUtil::~ObfuscatedFileUtil() { 270 ObfuscatedFileUtil::~ObfuscatedFileUtil() {
272 DropDatabases(); 271 DropDatabases();
273 } 272 }
274 273
275 PlatformFileError ObfuscatedFileUtil::CreateOrOpen( 274 base::File::Error ObfuscatedFileUtil::CreateOrOpen(
276 FileSystemOperationContext* context, 275 FileSystemOperationContext* context,
277 const FileSystemURL& url, int file_flags, 276 const FileSystemURL& url, int file_flags,
278 PlatformFile* file_handle, bool* created) { 277 PlatformFile* file_handle, bool* created) {
279 PlatformFileError error = CreateOrOpenInternal(context, url, file_flags, 278 base::File::Error error = CreateOrOpenInternal(context, url, file_flags,
280 file_handle, created); 279 file_handle, created);
281 if (*file_handle != base::kInvalidPlatformFileValue && 280 if (*file_handle != base::kInvalidPlatformFileValue &&
282 file_flags & base::PLATFORM_FILE_WRITE && 281 file_flags & base::PLATFORM_FILE_WRITE &&
283 context->quota_limit_type() == quota::kQuotaLimitTypeUnlimited && 282 context->quota_limit_type() == quota::kQuotaLimitTypeUnlimited &&
284 sandbox_delegate_) { 283 sandbox_delegate_) {
285 DCHECK_EQ(base::PLATFORM_FILE_OK, error); 284 DCHECK_EQ(base::File::FILE_OK, error);
286 sandbox_delegate_->StickyInvalidateUsageCache(url.origin(), url.type()); 285 sandbox_delegate_->StickyInvalidateUsageCache(url.origin(), url.type());
287 } 286 }
288 return error; 287 return error;
289 } 288 }
290 289
291 PlatformFileError ObfuscatedFileUtil::Close( 290 base::File::Error ObfuscatedFileUtil::Close(
292 FileSystemOperationContext* context, 291 FileSystemOperationContext* context,
293 base::PlatformFile file) { 292 base::PlatformFile file) {
294 return NativeFileUtil::Close(file); 293 return NativeFileUtil::Close(file);
295 } 294 }
296 295
297 PlatformFileError ObfuscatedFileUtil::EnsureFileExists( 296 base::File::Error ObfuscatedFileUtil::EnsureFileExists(
298 FileSystemOperationContext* context, 297 FileSystemOperationContext* context,
299 const FileSystemURL& url, 298 const FileSystemURL& url,
300 bool* created) { 299 bool* created) {
301 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, true); 300 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, true);
302 if (!db) 301 if (!db)
303 return base::PLATFORM_FILE_ERROR_FAILED; 302 return base::File::FILE_ERROR_FAILED;
304 303
305 FileId file_id; 304 FileId file_id;
306 if (db->GetFileWithPath(url.path(), &file_id)) { 305 if (db->GetFileWithPath(url.path(), &file_id)) {
307 FileInfo file_info; 306 FileInfo file_info;
308 if (!db->GetFileInfo(file_id, &file_info)) { 307 if (!db->GetFileInfo(file_id, &file_info)) {
309 NOTREACHED(); 308 NOTREACHED();
310 return base::PLATFORM_FILE_ERROR_FAILED; 309 return base::File::FILE_ERROR_FAILED;
311 } 310 }
312 if (file_info.is_directory()) 311 if (file_info.is_directory())
313 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; 312 return base::File::FILE_ERROR_NOT_A_FILE;
314 if (created) 313 if (created)
315 *created = false; 314 *created = false;
316 return base::PLATFORM_FILE_OK; 315 return base::File::FILE_OK;
317 } 316 }
318 FileId parent_id; 317 FileId parent_id;
319 if (!db->GetFileWithPath(VirtualPath::DirName(url.path()), &parent_id)) 318 if (!db->GetFileWithPath(VirtualPath::DirName(url.path()), &parent_id))
320 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 319 return base::File::FILE_ERROR_NOT_FOUND;
321 320
322 FileInfo file_info; 321 FileInfo file_info;
323 InitFileInfo(&file_info, parent_id, 322 InitFileInfo(&file_info, parent_id,
324 VirtualPath::BaseName(url.path()).value()); 323 VirtualPath::BaseName(url.path()).value());
325 324
326 int64 growth = UsageForPath(file_info.name.size()); 325 int64 growth = UsageForPath(file_info.name.size());
327 if (!AllocateQuota(context, growth)) 326 if (!AllocateQuota(context, growth))
328 return base::PLATFORM_FILE_ERROR_NO_SPACE; 327 return base::File::FILE_ERROR_NO_SPACE;
329 PlatformFileError error = CreateFile( 328 base::File::Error error = CreateFile(
330 context, base::FilePath(), url, &file_info, 0, NULL); 329 context, base::FilePath(), url, &file_info, 0, NULL);
331 if (created && base::PLATFORM_FILE_OK == error) { 330 if (created && base::File::FILE_OK == error) {
332 *created = true; 331 *created = true;
333 UpdateUsage(context, url, growth); 332 UpdateUsage(context, url, growth);
334 context->change_observers()->Notify( 333 context->change_observers()->Notify(
335 &FileChangeObserver::OnCreateFile, MakeTuple(url)); 334 &FileChangeObserver::OnCreateFile, MakeTuple(url));
336 } 335 }
337 return error; 336 return error;
338 } 337 }
339 338
340 PlatformFileError ObfuscatedFileUtil::CreateDirectory( 339 base::File::Error ObfuscatedFileUtil::CreateDirectory(
341 FileSystemOperationContext* context, 340 FileSystemOperationContext* context,
342 const FileSystemURL& url, 341 const FileSystemURL& url,
343 bool exclusive, 342 bool exclusive,
344 bool recursive) { 343 bool recursive) {
345 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, true); 344 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, true);
346 if (!db) 345 if (!db)
347 return base::PLATFORM_FILE_ERROR_FAILED; 346 return base::File::FILE_ERROR_FAILED;
348 347
349 FileId file_id; 348 FileId file_id;
350 if (db->GetFileWithPath(url.path(), &file_id)) { 349 if (db->GetFileWithPath(url.path(), &file_id)) {
351 FileInfo file_info; 350 FileInfo file_info;
352 if (exclusive) 351 if (exclusive)
353 return base::PLATFORM_FILE_ERROR_EXISTS; 352 return base::File::FILE_ERROR_EXISTS;
354 if (!db->GetFileInfo(file_id, &file_info)) { 353 if (!db->GetFileInfo(file_id, &file_info)) {
355 NOTREACHED(); 354 NOTREACHED();
356 return base::PLATFORM_FILE_ERROR_FAILED; 355 return base::File::FILE_ERROR_FAILED;
357 } 356 }
358 if (!file_info.is_directory()) 357 if (!file_info.is_directory())
359 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; 358 return base::File::FILE_ERROR_NOT_A_DIRECTORY;
360 return base::PLATFORM_FILE_OK; 359 return base::File::FILE_OK;
361 } 360 }
362 361
363 std::vector<base::FilePath::StringType> components; 362 std::vector<base::FilePath::StringType> components;
364 VirtualPath::GetComponents(url.path(), &components); 363 VirtualPath::GetComponents(url.path(), &components);
365 FileId parent_id = 0; 364 FileId parent_id = 0;
366 size_t index; 365 size_t index;
367 for (index = 0; index < components.size(); ++index) { 366 for (index = 0; index < components.size(); ++index) {
368 base::FilePath::StringType name = components[index]; 367 base::FilePath::StringType name = components[index];
369 if (name == FILE_PATH_LITERAL("/")) 368 if (name == FILE_PATH_LITERAL("/"))
370 continue; 369 continue;
371 if (!db->GetChildWithName(parent_id, name, &parent_id)) 370 if (!db->GetChildWithName(parent_id, name, &parent_id))
372 break; 371 break;
373 } 372 }
374 if (!db->IsDirectory(parent_id)) 373 if (!db->IsDirectory(parent_id))
375 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; 374 return base::File::FILE_ERROR_NOT_A_DIRECTORY;
376 if (!recursive && components.size() - index > 1) 375 if (!recursive && components.size() - index > 1)
377 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 376 return base::File::FILE_ERROR_NOT_FOUND;
378 bool first = true; 377 bool first = true;
379 for (; index < components.size(); ++index) { 378 for (; index < components.size(); ++index) {
380 FileInfo file_info; 379 FileInfo file_info;
381 file_info.name = components[index]; 380 file_info.name = components[index];
382 if (file_info.name == FILE_PATH_LITERAL("/")) 381 if (file_info.name == FILE_PATH_LITERAL("/"))
383 continue; 382 continue;
384 file_info.modification_time = base::Time::Now(); 383 file_info.modification_time = base::Time::Now();
385 file_info.parent_id = parent_id; 384 file_info.parent_id = parent_id;
386 int64 growth = UsageForPath(file_info.name.size()); 385 int64 growth = UsageForPath(file_info.name.size());
387 if (!AllocateQuota(context, growth)) 386 if (!AllocateQuota(context, growth))
388 return base::PLATFORM_FILE_ERROR_NO_SPACE; 387 return base::File::FILE_ERROR_NO_SPACE;
389 base::PlatformFileError error = db->AddFileInfo(file_info, &parent_id); 388 base::File::Error error = db->AddFileInfo(file_info, &parent_id);
390 if (error != base::PLATFORM_FILE_OK) 389 if (error != base::File::FILE_OK)
391 return error; 390 return error;
392 UpdateUsage(context, url, growth); 391 UpdateUsage(context, url, growth);
393 context->change_observers()->Notify( 392 context->change_observers()->Notify(
394 &FileChangeObserver::OnCreateDirectory, MakeTuple(url)); 393 &FileChangeObserver::OnCreateDirectory, MakeTuple(url));
395 if (first) { 394 if (first) {
396 first = false; 395 first = false;
397 TouchDirectory(db, file_info.parent_id); 396 TouchDirectory(db, file_info.parent_id);
398 } 397 }
399 } 398 }
400 return base::PLATFORM_FILE_OK; 399 return base::File::FILE_OK;
401 } 400 }
402 401
403 PlatformFileError ObfuscatedFileUtil::GetFileInfo( 402 base::File::Error ObfuscatedFileUtil::GetFileInfo(
404 FileSystemOperationContext* context, 403 FileSystemOperationContext* context,
405 const FileSystemURL& url, 404 const FileSystemURL& url,
406 base::PlatformFileInfo* file_info, 405 base::File::Info* file_info,
407 base::FilePath* platform_file_path) { 406 base::FilePath* platform_file_path) {
408 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, false); 407 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, false);
409 if (!db) 408 if (!db)
410 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 409 return base::File::FILE_ERROR_NOT_FOUND;
411 FileId file_id; 410 FileId file_id;
412 if (!db->GetFileWithPath(url.path(), &file_id)) 411 if (!db->GetFileWithPath(url.path(), &file_id))
413 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 412 return base::File::FILE_ERROR_NOT_FOUND;
414 FileInfo local_info; 413 FileInfo local_info;
415 return GetFileInfoInternal(db, context, url, 414 return GetFileInfoInternal(db, context, url,
416 file_id, &local_info, 415 file_id, &local_info,
417 file_info, platform_file_path); 416 file_info, platform_file_path);
418 } 417 }
419 418
420 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> 419 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator>
421 ObfuscatedFileUtil::CreateFileEnumerator( 420 ObfuscatedFileUtil::CreateFileEnumerator(
422 FileSystemOperationContext* context, 421 FileSystemOperationContext* context,
423 const FileSystemURL& root_url) { 422 const FileSystemURL& root_url) {
424 return CreateFileEnumerator(context, root_url, false /* recursive */); 423 return CreateFileEnumerator(context, root_url, false /* recursive */);
425 } 424 }
426 425
427 PlatformFileError ObfuscatedFileUtil::GetLocalFilePath( 426 base::File::Error ObfuscatedFileUtil::GetLocalFilePath(
428 FileSystemOperationContext* context, 427 FileSystemOperationContext* context,
429 const FileSystemURL& url, 428 const FileSystemURL& url,
430 base::FilePath* local_path) { 429 base::FilePath* local_path) {
431 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, false); 430 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, false);
432 if (!db) 431 if (!db)
433 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 432 return base::File::FILE_ERROR_NOT_FOUND;
434 FileId file_id; 433 FileId file_id;
435 if (!db->GetFileWithPath(url.path(), &file_id)) 434 if (!db->GetFileWithPath(url.path(), &file_id))
436 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 435 return base::File::FILE_ERROR_NOT_FOUND;
437 FileInfo file_info; 436 FileInfo file_info;
438 if (!db->GetFileInfo(file_id, &file_info) || file_info.is_directory()) { 437 if (!db->GetFileInfo(file_id, &file_info) || file_info.is_directory()) {
439 NOTREACHED(); 438 NOTREACHED();
440 // Directories have no local file path. 439 // Directories have no local file path.
441 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 440 return base::File::FILE_ERROR_NOT_FOUND;
442 } 441 }
443 *local_path = DataPathToLocalPath(url, file_info.data_path); 442 *local_path = DataPathToLocalPath(url, file_info.data_path);
444 443
445 if (local_path->empty()) 444 if (local_path->empty())
446 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 445 return base::File::FILE_ERROR_NOT_FOUND;
447 return base::PLATFORM_FILE_OK; 446 return base::File::FILE_OK;
448 } 447 }
449 448
450 PlatformFileError ObfuscatedFileUtil::Touch( 449 base::File::Error ObfuscatedFileUtil::Touch(
451 FileSystemOperationContext* context, 450 FileSystemOperationContext* context,
452 const FileSystemURL& url, 451 const FileSystemURL& url,
453 const base::Time& last_access_time, 452 const base::Time& last_access_time,
454 const base::Time& last_modified_time) { 453 const base::Time& last_modified_time) {
455 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, false); 454 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, false);
456 if (!db) 455 if (!db)
457 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 456 return base::File::FILE_ERROR_NOT_FOUND;
458 FileId file_id; 457 FileId file_id;
459 if (!db->GetFileWithPath(url.path(), &file_id)) 458 if (!db->GetFileWithPath(url.path(), &file_id))
460 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 459 return base::File::FILE_ERROR_NOT_FOUND;
461 460
462 FileInfo file_info; 461 FileInfo file_info;
463 if (!db->GetFileInfo(file_id, &file_info)) { 462 if (!db->GetFileInfo(file_id, &file_info)) {
464 NOTREACHED(); 463 NOTREACHED();
465 return base::PLATFORM_FILE_ERROR_FAILED; 464 return base::File::FILE_ERROR_FAILED;
466 } 465 }
467 if (file_info.is_directory()) { 466 if (file_info.is_directory()) {
468 if (!db->UpdateModificationTime(file_id, last_modified_time)) 467 if (!db->UpdateModificationTime(file_id, last_modified_time))
469 return base::PLATFORM_FILE_ERROR_FAILED; 468 return base::File::FILE_ERROR_FAILED;
470 return base::PLATFORM_FILE_OK; 469 return base::File::FILE_OK;
471 } 470 }
472 return NativeFileUtil::Touch( 471 return NativeFileUtil::Touch(
473 DataPathToLocalPath(url, file_info.data_path), 472 DataPathToLocalPath(url, file_info.data_path),
474 last_access_time, last_modified_time); 473 last_access_time, last_modified_time);
475 } 474 }
476 475
477 PlatformFileError ObfuscatedFileUtil::Truncate( 476 base::File::Error ObfuscatedFileUtil::Truncate(
478 FileSystemOperationContext* context, 477 FileSystemOperationContext* context,
479 const FileSystemURL& url, 478 const FileSystemURL& url,
480 int64 length) { 479 int64 length) {
481 base::PlatformFileInfo file_info; 480 base::File::Info file_info;
482 base::FilePath local_path; 481 base::FilePath local_path;
483 base::PlatformFileError error = 482 base::File::Error error =
484 GetFileInfo(context, url, &file_info, &local_path); 483 GetFileInfo(context, url, &file_info, &local_path);
485 if (error != base::PLATFORM_FILE_OK) 484 if (error != base::File::FILE_OK)
486 return error; 485 return error;
487 486
488 int64 growth = length - file_info.size; 487 int64 growth = length - file_info.size;
489 if (!AllocateQuota(context, growth)) 488 if (!AllocateQuota(context, growth))
490 return base::PLATFORM_FILE_ERROR_NO_SPACE; 489 return base::File::FILE_ERROR_NO_SPACE;
491 error = NativeFileUtil::Truncate(local_path, length); 490 error = NativeFileUtil::Truncate(local_path, length);
492 if (error == base::PLATFORM_FILE_OK) { 491 if (error == base::File::FILE_OK) {
493 UpdateUsage(context, url, growth); 492 UpdateUsage(context, url, growth);
494 context->change_observers()->Notify( 493 context->change_observers()->Notify(
495 &FileChangeObserver::OnModifyFile, MakeTuple(url)); 494 &FileChangeObserver::OnModifyFile, MakeTuple(url));
496 } 495 }
497 return error; 496 return error;
498 } 497 }
499 498
500 PlatformFileError ObfuscatedFileUtil::CopyOrMoveFile( 499 base::File::Error ObfuscatedFileUtil::CopyOrMoveFile(
501 FileSystemOperationContext* context, 500 FileSystemOperationContext* context,
502 const FileSystemURL& src_url, 501 const FileSystemURL& src_url,
503 const FileSystemURL& dest_url, 502 const FileSystemURL& dest_url,
504 CopyOrMoveOption option, 503 CopyOrMoveOption option,
505 bool copy) { 504 bool copy) {
506 // Cross-filesystem copies and moves should be handled via CopyInForeignFile. 505 // Cross-filesystem copies and moves should be handled via CopyInForeignFile.
507 DCHECK(src_url.origin() == dest_url.origin()); 506 DCHECK(src_url.origin() == dest_url.origin());
508 DCHECK(src_url.type() == dest_url.type()); 507 DCHECK(src_url.type() == dest_url.type());
509 508
510 SandboxDirectoryDatabase* db = GetDirectoryDatabase(src_url, true); 509 SandboxDirectoryDatabase* db = GetDirectoryDatabase(src_url, true);
511 if (!db) 510 if (!db)
512 return base::PLATFORM_FILE_ERROR_FAILED; 511 return base::File::FILE_ERROR_FAILED;
513 512
514 FileId src_file_id; 513 FileId src_file_id;
515 if (!db->GetFileWithPath(src_url.path(), &src_file_id)) 514 if (!db->GetFileWithPath(src_url.path(), &src_file_id))
516 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 515 return base::File::FILE_ERROR_NOT_FOUND;
517 516
518 FileId dest_file_id; 517 FileId dest_file_id;
519 bool overwrite = db->GetFileWithPath(dest_url.path(), 518 bool overwrite = db->GetFileWithPath(dest_url.path(),
520 &dest_file_id); 519 &dest_file_id);
521 520
522 FileInfo src_file_info; 521 FileInfo src_file_info;
523 base::PlatformFileInfo src_platform_file_info; 522 base::File::Info src_platform_file_info;
524 base::FilePath src_local_path; 523 base::FilePath src_local_path;
525 base::PlatformFileError error = GetFileInfoInternal( 524 base::File::Error error = GetFileInfoInternal(
526 db, context, src_url, src_file_id, 525 db, context, src_url, src_file_id,
527 &src_file_info, &src_platform_file_info, &src_local_path); 526 &src_file_info, &src_platform_file_info, &src_local_path);
528 if (error != base::PLATFORM_FILE_OK) 527 if (error != base::File::FILE_OK)
529 return error; 528 return error;
530 if (src_file_info.is_directory()) 529 if (src_file_info.is_directory())
531 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; 530 return base::File::FILE_ERROR_NOT_A_FILE;
532 531
533 FileInfo dest_file_info; 532 FileInfo dest_file_info;
534 base::PlatformFileInfo dest_platform_file_info; // overwrite case only 533 base::File::Info dest_platform_file_info; // overwrite case only
535 base::FilePath dest_local_path; // overwrite case only 534 base::FilePath dest_local_path; // overwrite case only
536 if (overwrite) { 535 if (overwrite) {
537 base::PlatformFileError error = GetFileInfoInternal( 536 base::File::Error error = GetFileInfoInternal(
538 db, context, dest_url, dest_file_id, 537 db, context, dest_url, dest_file_id,
539 &dest_file_info, &dest_platform_file_info, &dest_local_path); 538 &dest_file_info, &dest_platform_file_info, &dest_local_path);
540 if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) 539 if (error == base::File::FILE_ERROR_NOT_FOUND)
541 overwrite = false; // fallback to non-overwrite case 540 overwrite = false; // fallback to non-overwrite case
542 else if (error != base::PLATFORM_FILE_OK) 541 else if (error != base::File::FILE_OK)
543 return error; 542 return error;
544 else if (dest_file_info.is_directory()) 543 else if (dest_file_info.is_directory())
545 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; 544 return base::File::FILE_ERROR_INVALID_OPERATION;
546 } 545 }
547 if (!overwrite) { 546 if (!overwrite) {
548 FileId dest_parent_id; 547 FileId dest_parent_id;
549 if (!db->GetFileWithPath(VirtualPath::DirName(dest_url.path()), 548 if (!db->GetFileWithPath(VirtualPath::DirName(dest_url.path()),
550 &dest_parent_id)) { 549 &dest_parent_id)) {
551 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 550 return base::File::FILE_ERROR_NOT_FOUND;
552 } 551 }
553 552
554 dest_file_info = src_file_info; 553 dest_file_info = src_file_info;
555 dest_file_info.parent_id = dest_parent_id; 554 dest_file_info.parent_id = dest_parent_id;
556 dest_file_info.name = 555 dest_file_info.name =
557 VirtualPath::BaseName(dest_url.path()).value(); 556 VirtualPath::BaseName(dest_url.path()).value();
558 } 557 }
559 558
560 int64 growth = 0; 559 int64 growth = 0;
561 if (copy) 560 if (copy)
562 growth += src_platform_file_info.size; 561 growth += src_platform_file_info.size;
563 else 562 else
564 growth -= UsageForPath(src_file_info.name.size()); 563 growth -= UsageForPath(src_file_info.name.size());
565 if (overwrite) 564 if (overwrite)
566 growth -= dest_platform_file_info.size; 565 growth -= dest_platform_file_info.size;
567 else 566 else
568 growth += UsageForPath(dest_file_info.name.size()); 567 growth += UsageForPath(dest_file_info.name.size());
569 if (!AllocateQuota(context, growth)) 568 if (!AllocateQuota(context, growth))
570 return base::PLATFORM_FILE_ERROR_NO_SPACE; 569 return base::File::FILE_ERROR_NO_SPACE;
571 570
572 /* 571 /*
573 * Copy-with-overwrite 572 * Copy-with-overwrite
574 * Just overwrite data file 573 * Just overwrite data file
575 * Copy-without-overwrite 574 * Copy-without-overwrite
576 * Copy backing file 575 * Copy backing file
577 * Create new metadata pointing to new backing file. 576 * Create new metadata pointing to new backing file.
578 * Move-with-overwrite 577 * Move-with-overwrite
579 * transaction: 578 * transaction:
580 * Remove source entry. 579 * Remove source entry.
581 * Point target entry to source entry's backing file. 580 * Point target entry to source entry's backing file.
582 * Delete target entry's old backing file 581 * Delete target entry's old backing file
583 * Move-without-overwrite 582 * Move-without-overwrite
584 * Just update metadata 583 * Just update metadata
585 */ 584 */
586 error = base::PLATFORM_FILE_ERROR_FAILED; 585 error = base::File::FILE_ERROR_FAILED;
587 if (copy) { 586 if (copy) {
588 if (overwrite) { 587 if (overwrite) {
589 error = NativeFileUtil::CopyOrMoveFile( 588 error = NativeFileUtil::CopyOrMoveFile(
590 src_local_path, 589 src_local_path,
591 dest_local_path, 590 dest_local_path,
592 option, 591 option,
593 fileapi::NativeFileUtil::CopyOrMoveModeForDestination( 592 fileapi::NativeFileUtil::CopyOrMoveModeForDestination(
594 dest_url, true /* copy */)); 593 dest_url, true /* copy */));
595 } else { // non-overwrite 594 } else { // non-overwrite
596 error = CreateFile(context, src_local_path, 595 error = CreateFile(context, src_local_path,
597 dest_url, &dest_file_info, 0, NULL); 596 dest_url, &dest_file_info, 0, NULL);
598 } 597 }
599 } else { 598 } else {
600 if (overwrite) { 599 if (overwrite) {
601 if (db->OverwritingMoveFile(src_file_id, dest_file_id)) { 600 if (db->OverwritingMoveFile(src_file_id, dest_file_id)) {
602 if (base::PLATFORM_FILE_OK != 601 if (base::File::FILE_OK !=
603 NativeFileUtil::DeleteFile(dest_local_path)) 602 NativeFileUtil::DeleteFile(dest_local_path))
604 LOG(WARNING) << "Leaked a backing file."; 603 LOG(WARNING) << "Leaked a backing file.";
605 error = base::PLATFORM_FILE_OK; 604 error = base::File::FILE_OK;
606 } else { 605 } else {
607 error = base::PLATFORM_FILE_ERROR_FAILED; 606 error = base::File::FILE_ERROR_FAILED;
608 } 607 }
609 } else { // non-overwrite 608 } else { // non-overwrite
610 if (db->UpdateFileInfo(src_file_id, dest_file_info)) 609 if (db->UpdateFileInfo(src_file_id, dest_file_info))
611 error = base::PLATFORM_FILE_OK; 610 error = base::File::FILE_OK;
612 else 611 else
613 error = base::PLATFORM_FILE_ERROR_FAILED; 612 error = base::File::FILE_ERROR_FAILED;
614 } 613 }
615 } 614 }
616 615
617 if (error != base::PLATFORM_FILE_OK) 616 if (error != base::File::FILE_OK)
618 return error; 617 return error;
619 618
620 if (overwrite) { 619 if (overwrite) {
621 context->change_observers()->Notify( 620 context->change_observers()->Notify(
622 &FileChangeObserver::OnModifyFile, 621 &FileChangeObserver::OnModifyFile,
623 MakeTuple(dest_url)); 622 MakeTuple(dest_url));
624 } else { 623 } else {
625 context->change_observers()->Notify( 624 context->change_observers()->Notify(
626 &FileChangeObserver::OnCreateFileFrom, 625 &FileChangeObserver::OnCreateFileFrom,
627 MakeTuple(dest_url, src_url)); 626 MakeTuple(dest_url, src_url));
628 } 627 }
629 628
630 if (!copy) { 629 if (!copy) {
631 context->change_observers()->Notify( 630 context->change_observers()->Notify(
632 &FileChangeObserver::OnRemoveFile, MakeTuple(src_url)); 631 &FileChangeObserver::OnRemoveFile, MakeTuple(src_url));
633 TouchDirectory(db, src_file_info.parent_id); 632 TouchDirectory(db, src_file_info.parent_id);
634 } 633 }
635 634
636 TouchDirectory(db, dest_file_info.parent_id); 635 TouchDirectory(db, dest_file_info.parent_id);
637 636
638 UpdateUsage(context, dest_url, growth); 637 UpdateUsage(context, dest_url, growth);
639 return error; 638 return error;
640 } 639 }
641 640
642 PlatformFileError ObfuscatedFileUtil::CopyInForeignFile( 641 base::File::Error ObfuscatedFileUtil::CopyInForeignFile(
643 FileSystemOperationContext* context, 642 FileSystemOperationContext* context,
644 const base::FilePath& src_file_path, 643 const base::FilePath& src_file_path,
645 const FileSystemURL& dest_url) { 644 const FileSystemURL& dest_url) {
646 SandboxDirectoryDatabase* db = GetDirectoryDatabase(dest_url, true); 645 SandboxDirectoryDatabase* db = GetDirectoryDatabase(dest_url, true);
647 if (!db) 646 if (!db)
648 return base::PLATFORM_FILE_ERROR_FAILED; 647 return base::File::FILE_ERROR_FAILED;
649 648
650 base::File::Info src_platform_file_info; 649 base::File::Info src_platform_file_info;
651 if (!base::GetFileInfo(src_file_path, &src_platform_file_info)) 650 if (!base::GetFileInfo(src_file_path, &src_platform_file_info))
652 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 651 return base::File::FILE_ERROR_NOT_FOUND;
653 652
654 FileId dest_file_id; 653 FileId dest_file_id;
655 bool overwrite = db->GetFileWithPath(dest_url.path(), 654 bool overwrite = db->GetFileWithPath(dest_url.path(),
656 &dest_file_id); 655 &dest_file_id);
657 656
658 FileInfo dest_file_info; 657 FileInfo dest_file_info;
659 base::PlatformFileInfo dest_platform_file_info; // overwrite case only 658 base::File::Info dest_platform_file_info; // overwrite case only
660 if (overwrite) { 659 if (overwrite) {
661 base::FilePath dest_local_path; 660 base::FilePath dest_local_path;
662 base::PlatformFileError error = GetFileInfoInternal( 661 base::File::Error error = GetFileInfoInternal(
663 db, context, dest_url, dest_file_id, 662 db, context, dest_url, dest_file_id,
664 &dest_file_info, &dest_platform_file_info, &dest_local_path); 663 &dest_file_info, &dest_platform_file_info, &dest_local_path);
665 if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) 664 if (error == base::File::FILE_ERROR_NOT_FOUND)
666 overwrite = false; // fallback to non-overwrite case 665 overwrite = false; // fallback to non-overwrite case
667 else if (error != base::PLATFORM_FILE_OK) 666 else if (error != base::File::FILE_OK)
668 return error; 667 return error;
669 else if (dest_file_info.is_directory()) 668 else if (dest_file_info.is_directory())
670 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; 669 return base::File::FILE_ERROR_INVALID_OPERATION;
671 } 670 }
672 if (!overwrite) { 671 if (!overwrite) {
673 FileId dest_parent_id; 672 FileId dest_parent_id;
674 if (!db->GetFileWithPath(VirtualPath::DirName(dest_url.path()), 673 if (!db->GetFileWithPath(VirtualPath::DirName(dest_url.path()),
675 &dest_parent_id)) { 674 &dest_parent_id)) {
676 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 675 return base::File::FILE_ERROR_NOT_FOUND;
677 } 676 }
678 if (!dest_file_info.is_directory()) 677 if (!dest_file_info.is_directory())
679 return base::PLATFORM_FILE_ERROR_FAILED; 678 return base::File::FILE_ERROR_FAILED;
680 InitFileInfo(&dest_file_info, dest_parent_id, 679 InitFileInfo(&dest_file_info, dest_parent_id,
681 VirtualPath::BaseName(dest_url.path()).value()); 680 VirtualPath::BaseName(dest_url.path()).value());
682 } 681 }
683 682
684 int64 growth = src_platform_file_info.size; 683 int64 growth = src_platform_file_info.size;
685 if (overwrite) 684 if (overwrite)
686 growth -= dest_platform_file_info.size; 685 growth -= dest_platform_file_info.size;
687 else 686 else
688 growth += UsageForPath(dest_file_info.name.size()); 687 growth += UsageForPath(dest_file_info.name.size());
689 if (!AllocateQuota(context, growth)) 688 if (!AllocateQuota(context, growth))
690 return base::PLATFORM_FILE_ERROR_NO_SPACE; 689 return base::File::FILE_ERROR_NO_SPACE;
691 690
692 base::PlatformFileError error; 691 base::File::Error error;
693 if (overwrite) { 692 if (overwrite) {
694 base::FilePath dest_local_path = 693 base::FilePath dest_local_path =
695 DataPathToLocalPath(dest_url, dest_file_info.data_path); 694 DataPathToLocalPath(dest_url, dest_file_info.data_path);
696 error = NativeFileUtil::CopyOrMoveFile( 695 error = NativeFileUtil::CopyOrMoveFile(
697 src_file_path, dest_local_path, 696 src_file_path, dest_local_path,
698 FileSystemOperation::OPTION_NONE, 697 FileSystemOperation::OPTION_NONE,
699 fileapi::NativeFileUtil::CopyOrMoveModeForDestination(dest_url, 698 fileapi::NativeFileUtil::CopyOrMoveModeForDestination(dest_url,
700 true /* copy */)); 699 true /* copy */));
701 } else { 700 } else {
702 error = CreateFile(context, src_file_path, 701 error = CreateFile(context, src_file_path,
703 dest_url, &dest_file_info, 0, NULL); 702 dest_url, &dest_file_info, 0, NULL);
704 } 703 }
705 704
706 if (error != base::PLATFORM_FILE_OK) 705 if (error != base::File::FILE_OK)
707 return error; 706 return error;
708 707
709 if (overwrite) { 708 if (overwrite) {
710 context->change_observers()->Notify( 709 context->change_observers()->Notify(
711 &FileChangeObserver::OnModifyFile, MakeTuple(dest_url)); 710 &FileChangeObserver::OnModifyFile, MakeTuple(dest_url));
712 } else { 711 } else {
713 context->change_observers()->Notify( 712 context->change_observers()->Notify(
714 &FileChangeObserver::OnCreateFile, MakeTuple(dest_url)); 713 &FileChangeObserver::OnCreateFile, MakeTuple(dest_url));
715 } 714 }
716 715
717 UpdateUsage(context, dest_url, growth); 716 UpdateUsage(context, dest_url, growth);
718 TouchDirectory(db, dest_file_info.parent_id); 717 TouchDirectory(db, dest_file_info.parent_id);
719 return base::PLATFORM_FILE_OK; 718 return base::File::FILE_OK;
720 } 719 }
721 720
722 PlatformFileError ObfuscatedFileUtil::DeleteFile( 721 base::File::Error ObfuscatedFileUtil::DeleteFile(
723 FileSystemOperationContext* context, 722 FileSystemOperationContext* context,
724 const FileSystemURL& url) { 723 const FileSystemURL& url) {
725 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, true); 724 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, true);
726 if (!db) 725 if (!db)
727 return base::PLATFORM_FILE_ERROR_FAILED; 726 return base::File::FILE_ERROR_FAILED;
728 FileId file_id; 727 FileId file_id;
729 if (!db->GetFileWithPath(url.path(), &file_id)) 728 if (!db->GetFileWithPath(url.path(), &file_id))
730 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 729 return base::File::FILE_ERROR_NOT_FOUND;
731 730
732 FileInfo file_info; 731 FileInfo file_info;
733 base::PlatformFileInfo platform_file_info; 732 base::File::Info platform_file_info;
734 base::FilePath local_path; 733 base::FilePath local_path;
735 base::PlatformFileError error = GetFileInfoInternal( 734 base::File::Error error = GetFileInfoInternal(
736 db, context, url, file_id, &file_info, &platform_file_info, &local_path); 735 db, context, url, file_id, &file_info, &platform_file_info, &local_path);
737 if (error != base::PLATFORM_FILE_ERROR_NOT_FOUND && 736 if (error != base::File::FILE_ERROR_NOT_FOUND &&
738 error != base::PLATFORM_FILE_OK) 737 error != base::File::FILE_OK)
739 return error; 738 return error;
740 739
741 if (file_info.is_directory()) 740 if (file_info.is_directory())
742 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; 741 return base::File::FILE_ERROR_NOT_A_FILE;
743 742
744 int64 growth = -UsageForPath(file_info.name.size()) - platform_file_info.size; 743 int64 growth = -UsageForPath(file_info.name.size()) - platform_file_info.size;
745 AllocateQuota(context, growth); 744 AllocateQuota(context, growth);
746 if (!db->RemoveFileInfo(file_id)) { 745 if (!db->RemoveFileInfo(file_id)) {
747 NOTREACHED(); 746 NOTREACHED();
748 return base::PLATFORM_FILE_ERROR_FAILED; 747 return base::File::FILE_ERROR_FAILED;
749 } 748 }
750 UpdateUsage(context, url, growth); 749 UpdateUsage(context, url, growth);
751 TouchDirectory(db, file_info.parent_id); 750 TouchDirectory(db, file_info.parent_id);
752 751
753 context->change_observers()->Notify( 752 context->change_observers()->Notify(
754 &FileChangeObserver::OnRemoveFile, MakeTuple(url)); 753 &FileChangeObserver::OnRemoveFile, MakeTuple(url));
755 754
756 if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) 755 if (error == base::File::FILE_ERROR_NOT_FOUND)
757 return base::PLATFORM_FILE_OK; 756 return base::File::FILE_OK;
758 757
759 error = NativeFileUtil::DeleteFile(local_path); 758 error = NativeFileUtil::DeleteFile(local_path);
760 if (base::PLATFORM_FILE_OK != error) 759 if (base::File::FILE_OK != error)
761 LOG(WARNING) << "Leaked a backing file."; 760 LOG(WARNING) << "Leaked a backing file.";
762 return base::PLATFORM_FILE_OK; 761 return base::File::FILE_OK;
763 } 762 }
764 763
765 PlatformFileError ObfuscatedFileUtil::DeleteDirectory( 764 base::File::Error ObfuscatedFileUtil::DeleteDirectory(
766 FileSystemOperationContext* context, 765 FileSystemOperationContext* context,
767 const FileSystemURL& url) { 766 const FileSystemURL& url) {
768 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, true); 767 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, true);
769 if (!db) 768 if (!db)
770 return base::PLATFORM_FILE_ERROR_FAILED; 769 return base::File::FILE_ERROR_FAILED;
771 770
772 FileId file_id; 771 FileId file_id;
773 if (!db->GetFileWithPath(url.path(), &file_id)) 772 if (!db->GetFileWithPath(url.path(), &file_id))
774 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 773 return base::File::FILE_ERROR_NOT_FOUND;
775 FileInfo file_info; 774 FileInfo file_info;
776 if (!db->GetFileInfo(file_id, &file_info)) { 775 if (!db->GetFileInfo(file_id, &file_info)) {
777 NOTREACHED(); 776 NOTREACHED();
778 return base::PLATFORM_FILE_ERROR_FAILED; 777 return base::File::FILE_ERROR_FAILED;
779 } 778 }
780 if (!file_info.is_directory()) 779 if (!file_info.is_directory())
781 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; 780 return base::File::FILE_ERROR_NOT_A_DIRECTORY;
782 if (!db->RemoveFileInfo(file_id)) 781 if (!db->RemoveFileInfo(file_id))
783 return base::PLATFORM_FILE_ERROR_NOT_EMPTY; 782 return base::File::FILE_ERROR_NOT_EMPTY;
784 int64 growth = -UsageForPath(file_info.name.size()); 783 int64 growth = -UsageForPath(file_info.name.size());
785 AllocateQuota(context, growth); 784 AllocateQuota(context, growth);
786 UpdateUsage(context, url, growth); 785 UpdateUsage(context, url, growth);
787 TouchDirectory(db, file_info.parent_id); 786 TouchDirectory(db, file_info.parent_id);
788 context->change_observers()->Notify( 787 context->change_observers()->Notify(
789 &FileChangeObserver::OnRemoveDirectory, MakeTuple(url)); 788 &FileChangeObserver::OnRemoveDirectory, MakeTuple(url));
790 return base::PLATFORM_FILE_OK; 789 return base::File::FILE_OK;
791 } 790 }
792 791
793 webkit_blob::ScopedFile ObfuscatedFileUtil::CreateSnapshotFile( 792 webkit_blob::ScopedFile ObfuscatedFileUtil::CreateSnapshotFile(
794 FileSystemOperationContext* context, 793 FileSystemOperationContext* context,
795 const FileSystemURL& url, 794 const FileSystemURL& url,
796 base::PlatformFileError* error, 795 base::File::Error* error,
797 base::PlatformFileInfo* file_info, 796 base::File::Info* file_info,
798 base::FilePath* platform_path) { 797 base::FilePath* platform_path) {
799 // We're just returning the local file information. 798 // We're just returning the local file information.
800 *error = GetFileInfo(context, url, file_info, platform_path); 799 *error = GetFileInfo(context, url, file_info, platform_path);
801 if (*error == base::PLATFORM_FILE_OK && file_info->is_directory) { 800 if (*error == base::File::FILE_OK && file_info->is_directory) {
802 *file_info = base::PlatformFileInfo(); 801 *file_info = base::File::Info();
803 *error = base::PLATFORM_FILE_ERROR_NOT_A_FILE; 802 *error = base::File::FILE_ERROR_NOT_A_FILE;
804 } 803 }
805 return webkit_blob::ScopedFile(); 804 return webkit_blob::ScopedFile();
806 } 805 }
807 806
808 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> 807 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator>
809 ObfuscatedFileUtil::CreateFileEnumerator( 808 ObfuscatedFileUtil::CreateFileEnumerator(
810 FileSystemOperationContext* context, 809 FileSystemOperationContext* context,
811 const FileSystemURL& root_url, 810 const FileSystemURL& root_url,
812 bool recursive) { 811 bool recursive) {
813 SandboxDirectoryDatabase* db = GetDirectoryDatabase(root_url, false); 812 SandboxDirectoryDatabase* db = GetDirectoryDatabase(root_url, false);
(...skipping 25 matching lines...) Expand all
839 // TODO(ericu): This could easily be made faster with help from the database. 838 // TODO(ericu): This could easily be made faster with help from the database.
840 if (!db->ListChildren(file_id, &children)) 839 if (!db->ListChildren(file_id, &children))
841 return true; 840 return true;
842 return children.empty(); 841 return children.empty();
843 } 842 }
844 843
845 base::FilePath ObfuscatedFileUtil::GetDirectoryForOriginAndType( 844 base::FilePath ObfuscatedFileUtil::GetDirectoryForOriginAndType(
846 const GURL& origin, 845 const GURL& origin,
847 const std::string& type_string, 846 const std::string& type_string,
848 bool create, 847 bool create,
849 base::PlatformFileError* error_code) { 848 base::File::Error* error_code) {
850 base::FilePath origin_dir = GetDirectoryForOrigin(origin, create, error_code); 849 base::FilePath origin_dir = GetDirectoryForOrigin(origin, create, error_code);
851 if (origin_dir.empty()) 850 if (origin_dir.empty())
852 return base::FilePath(); 851 return base::FilePath();
853 if (type_string.empty()) 852 if (type_string.empty())
854 return origin_dir; 853 return origin_dir;
855 base::FilePath path = origin_dir.AppendASCII(type_string); 854 base::FilePath path = origin_dir.AppendASCII(type_string);
856 base::PlatformFileError error = base::PLATFORM_FILE_OK; 855 base::File::Error error = base::File::FILE_OK;
857 if (!base::DirectoryExists(path) && 856 if (!base::DirectoryExists(path) &&
858 (!create || !base::CreateDirectory(path))) { 857 (!create || !base::CreateDirectory(path))) {
859 error = create ? 858 error = create ?
860 base::PLATFORM_FILE_ERROR_FAILED : 859 base::File::FILE_ERROR_FAILED :
861 base::PLATFORM_FILE_ERROR_NOT_FOUND; 860 base::File::FILE_ERROR_NOT_FOUND;
862 } 861 }
863 862
864 if (error_code) 863 if (error_code)
865 *error_code = error; 864 *error_code = error;
866 return path; 865 return path;
867 } 866 }
868 867
869 bool ObfuscatedFileUtil::DeleteDirectoryForOriginAndType( 868 bool ObfuscatedFileUtil::DeleteDirectoryForOriginAndType(
870 const GURL& origin, 869 const GURL& origin,
871 const std::string& type_string) { 870 const std::string& type_string) {
872 base::PlatformFileError error = base::PLATFORM_FILE_OK; 871 base::File::Error error = base::File::FILE_OK;
873 base::FilePath origin_type_path = GetDirectoryForOriginAndType( 872 base::FilePath origin_type_path = GetDirectoryForOriginAndType(
874 origin, type_string, false, &error); 873 origin, type_string, false, &error);
875 if (origin_type_path.empty()) 874 if (origin_type_path.empty())
876 return true; 875 return true;
877 if (error != base::PLATFORM_FILE_ERROR_NOT_FOUND) { 876 if (error != base::File::FILE_ERROR_NOT_FOUND) {
878 // TODO(dmikurube): Consider the return value of DestroyDirectoryDatabase. 877 // TODO(dmikurube): Consider the return value of DestroyDirectoryDatabase.
879 // We ignore its error now since 1) it doesn't matter the final result, and 878 // We ignore its error now since 1) it doesn't matter the final result, and
880 // 2) it always returns false in Windows because of LevelDB's 879 // 2) it always returns false in Windows because of LevelDB's
881 // implementation. 880 // implementation.
882 // Information about failure would be useful for debugging. 881 // Information about failure would be useful for debugging.
883 if (!type_string.empty()) 882 if (!type_string.empty())
884 DestroyDirectoryDatabase(origin, type_string); 883 DestroyDirectoryDatabase(origin, type_string);
885 if (!base::DeleteFile(origin_type_path, true /* recursive */)) 884 if (!base::DeleteFile(origin_type_path, true /* recursive */))
886 return false; 885 return false;
887 } 886 }
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
933 std::string key = GetDirectoryDatabaseKey(origin, type_string); 932 std::string key = GetDirectoryDatabaseKey(origin, type_string);
934 if (key.empty()) 933 if (key.empty())
935 return true; 934 return true;
936 DirectoryMap::iterator iter = directories_.find(key); 935 DirectoryMap::iterator iter = directories_.find(key);
937 if (iter != directories_.end()) { 936 if (iter != directories_.end()) {
938 SandboxDirectoryDatabase* database = iter->second; 937 SandboxDirectoryDatabase* database = iter->second;
939 directories_.erase(iter); 938 directories_.erase(iter);
940 delete database; 939 delete database;
941 } 940 }
942 941
943 PlatformFileError error = base::PLATFORM_FILE_OK; 942 base::File::Error error = base::File::FILE_OK;
944 base::FilePath path = GetDirectoryForOriginAndType( 943 base::FilePath path = GetDirectoryForOriginAndType(
945 origin, type_string, false, &error); 944 origin, type_string, false, &error);
946 if (path.empty() || error == base::PLATFORM_FILE_ERROR_NOT_FOUND) 945 if (path.empty() || error == base::File::FILE_ERROR_NOT_FOUND)
947 return true; 946 return true;
948 return SandboxDirectoryDatabase::DestroyDatabase(path); 947 return SandboxDirectoryDatabase::DestroyDatabase(path);
949 } 948 }
950 949
951 // static 950 // static
952 int64 ObfuscatedFileUtil::ComputeFilePathCost(const base::FilePath& path) { 951 int64 ObfuscatedFileUtil::ComputeFilePathCost(const base::FilePath& path) {
953 return UsageForPath(VirtualPath::BaseName(path).value().size()); 952 return UsageForPath(VirtualPath::BaseName(path).value().size());
954 } 953 }
955 954
956 void ObfuscatedFileUtil::MaybePrepopulateDatabase( 955 void ObfuscatedFileUtil::MaybePrepopulateDatabase(
957 const std::vector<std::string>& type_strings_to_prepopulate) { 956 const std::vector<std::string>& type_strings_to_prepopulate) {
958 SandboxPrioritizedOriginDatabase database(file_system_directory_); 957 SandboxPrioritizedOriginDatabase database(file_system_directory_);
959 std::string origin_string = database.GetPrimaryOrigin(); 958 std::string origin_string = database.GetPrimaryOrigin();
960 if (origin_string.empty() || !database.HasOriginPath(origin_string)) 959 if (origin_string.empty() || !database.HasOriginPath(origin_string))
961 return; 960 return;
962 const GURL origin = webkit_database::GetOriginFromIdentifier(origin_string); 961 const GURL origin = webkit_database::GetOriginFromIdentifier(origin_string);
963 962
964 // Prepopulate the directory database(s) if and only if this instance 963 // Prepopulate the directory database(s) if and only if this instance
965 // has primary origin and the directory database is already there. 964 // has primary origin and the directory database is already there.
966 for (size_t i = 0; i < type_strings_to_prepopulate.size(); ++i) { 965 for (size_t i = 0; i < type_strings_to_prepopulate.size(); ++i) {
967 const std::string type_string = type_strings_to_prepopulate[i]; 966 const std::string type_string = type_strings_to_prepopulate[i];
968 // Only handles known types. 967 // Only handles known types.
969 if (!ContainsKey(known_type_strings_, type_string)) 968 if (!ContainsKey(known_type_strings_, type_string))
970 continue; 969 continue;
971 PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; 970 base::File::Error error = base::File::FILE_ERROR_FAILED;
972 base::FilePath path = GetDirectoryForOriginAndType( 971 base::FilePath path = GetDirectoryForOriginAndType(
973 origin, type_string, false, &error); 972 origin, type_string, false, &error);
974 if (error != base::PLATFORM_FILE_OK) 973 if (error != base::File::FILE_OK)
975 continue; 974 continue;
976 scoped_ptr<SandboxDirectoryDatabase> db(new SandboxDirectoryDatabase(path)); 975 scoped_ptr<SandboxDirectoryDatabase> db(new SandboxDirectoryDatabase(path));
977 if (db->Init(SandboxDirectoryDatabase::FAIL_ON_CORRUPTION)) { 976 if (db->Init(SandboxDirectoryDatabase::FAIL_ON_CORRUPTION)) {
978 directories_[GetDirectoryDatabaseKey(origin, type_string)] = db.release(); 977 directories_[GetDirectoryDatabaseKey(origin, type_string)] = db.release();
979 MarkUsed(); 978 MarkUsed();
980 // Don't populate more than one database, as it may rather hurt 979 // Don't populate more than one database, as it may rather hurt
981 // performance. 980 // performance.
982 break; 981 break;
983 } 982 }
984 } 983 }
985 } 984 }
986 985
987 base::FilePath ObfuscatedFileUtil::GetDirectoryForURL( 986 base::FilePath ObfuscatedFileUtil::GetDirectoryForURL(
988 const FileSystemURL& url, 987 const FileSystemURL& url,
989 bool create, 988 bool create,
990 base::PlatformFileError* error_code) { 989 base::File::Error* error_code) {
991 return GetDirectoryForOriginAndType( 990 return GetDirectoryForOriginAndType(
992 url.origin(), CallGetTypeStringForURL(url), create, error_code); 991 url.origin(), CallGetTypeStringForURL(url), create, error_code);
993 } 992 }
994 993
995 std::string ObfuscatedFileUtil::CallGetTypeStringForURL( 994 std::string ObfuscatedFileUtil::CallGetTypeStringForURL(
996 const FileSystemURL& url) { 995 const FileSystemURL& url) {
997 DCHECK(!get_type_string_for_url_.is_null()); 996 DCHECK(!get_type_string_for_url_.is_null());
998 return get_type_string_for_url_.Run(url); 997 return get_type_string_for_url_.Run(url);
999 } 998 }
1000 999
1001 PlatformFileError ObfuscatedFileUtil::GetFileInfoInternal( 1000 base::File::Error ObfuscatedFileUtil::GetFileInfoInternal(
1002 SandboxDirectoryDatabase* db, 1001 SandboxDirectoryDatabase* db,
1003 FileSystemOperationContext* context, 1002 FileSystemOperationContext* context,
1004 const FileSystemURL& url, 1003 const FileSystemURL& url,
1005 FileId file_id, 1004 FileId file_id,
1006 FileInfo* local_info, 1005 FileInfo* local_info,
1007 base::PlatformFileInfo* file_info, 1006 base::File::Info* file_info,
1008 base::FilePath* platform_file_path) { 1007 base::FilePath* platform_file_path) {
1009 DCHECK(db); 1008 DCHECK(db);
1010 DCHECK(context); 1009 DCHECK(context);
1011 DCHECK(file_info); 1010 DCHECK(file_info);
1012 DCHECK(platform_file_path); 1011 DCHECK(platform_file_path);
1013 1012
1014 if (!db->GetFileInfo(file_id, local_info)) { 1013 if (!db->GetFileInfo(file_id, local_info)) {
1015 NOTREACHED(); 1014 NOTREACHED();
1016 return base::PLATFORM_FILE_ERROR_FAILED; 1015 return base::File::FILE_ERROR_FAILED;
1017 } 1016 }
1018 1017
1019 if (local_info->is_directory()) { 1018 if (local_info->is_directory()) {
1020 file_info->size = 0; 1019 file_info->size = 0;
1021 file_info->is_directory = true; 1020 file_info->is_directory = true;
1022 file_info->is_symbolic_link = false; 1021 file_info->is_symbolic_link = false;
1023 file_info->last_modified = local_info->modification_time; 1022 file_info->last_modified = local_info->modification_time;
1024 *platform_file_path = base::FilePath(); 1023 *platform_file_path = base::FilePath();
1025 // We don't fill in ctime or atime. 1024 // We don't fill in ctime or atime.
1026 return base::PLATFORM_FILE_OK; 1025 return base::File::FILE_OK;
1027 } 1026 }
1028 if (local_info->data_path.empty()) 1027 if (local_info->data_path.empty())
1029 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; 1028 return base::File::FILE_ERROR_INVALID_OPERATION;
1030 base::FilePath local_path = DataPathToLocalPath(url, local_info->data_path); 1029 base::FilePath local_path = DataPathToLocalPath(url, local_info->data_path);
1031 base::PlatformFileError error = NativeFileUtil::GetFileInfo( 1030 base::File::Error error = NativeFileUtil::GetFileInfo(
1032 local_path, file_info); 1031 local_path, file_info);
1033 // We should not follow symbolic links in sandboxed file system. 1032 // We should not follow symbolic links in sandboxed file system.
1034 if (base::IsLink(local_path)) { 1033 if (base::IsLink(local_path)) {
1035 LOG(WARNING) << "Found a symbolic file."; 1034 LOG(WARNING) << "Found a symbolic file.";
1036 error = base::PLATFORM_FILE_ERROR_NOT_FOUND; 1035 error = base::File::FILE_ERROR_NOT_FOUND;
1037 } 1036 }
1038 if (error == base::PLATFORM_FILE_OK) { 1037 if (error == base::File::FILE_OK) {
1039 *platform_file_path = local_path; 1038 *platform_file_path = local_path;
1040 } else if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) { 1039 } else if (error == base::File::FILE_ERROR_NOT_FOUND) {
1041 LOG(WARNING) << "Lost a backing file."; 1040 LOG(WARNING) << "Lost a backing file.";
1042 InvalidateUsageCache(context, url.origin(), url.type()); 1041 InvalidateUsageCache(context, url.origin(), url.type());
1043 if (!db->RemoveFileInfo(file_id)) 1042 if (!db->RemoveFileInfo(file_id))
1044 return base::PLATFORM_FILE_ERROR_FAILED; 1043 return base::File::FILE_ERROR_FAILED;
1045 } 1044 }
1046 return error; 1045 return error;
1047 } 1046 }
1048 1047
1049 PlatformFileError ObfuscatedFileUtil::CreateFile( 1048 base::File::Error ObfuscatedFileUtil::CreateFile(
1050 FileSystemOperationContext* context, 1049 FileSystemOperationContext* context,
1051 const base::FilePath& src_file_path, 1050 const base::FilePath& src_file_path,
1052 const FileSystemURL& dest_url, 1051 const FileSystemURL& dest_url,
1053 FileInfo* dest_file_info, int file_flags, PlatformFile* handle) { 1052 FileInfo* dest_file_info, int file_flags, PlatformFile* handle) {
1054 if (handle) 1053 if (handle)
1055 *handle = base::kInvalidPlatformFileValue; 1054 *handle = base::kInvalidPlatformFileValue;
1056 SandboxDirectoryDatabase* db = GetDirectoryDatabase(dest_url, true); 1055 SandboxDirectoryDatabase* db = GetDirectoryDatabase(dest_url, true);
1057 1056
1058 PlatformFileError error = base::PLATFORM_FILE_OK; 1057 base::File::Error error = base::File::FILE_OK;
1059 base::FilePath root = GetDirectoryForURL(dest_url, false, &error); 1058 base::FilePath root = GetDirectoryForURL(dest_url, false, &error);
1060 if (error != base::PLATFORM_FILE_OK) 1059 if (error != base::File::FILE_OK)
1061 return error; 1060 return error;
1062 1061
1063 base::FilePath dest_local_path; 1062 base::FilePath dest_local_path;
1064 error = GenerateNewLocalPath(db, context, dest_url, &dest_local_path); 1063 error = GenerateNewLocalPath(db, context, dest_url, &dest_local_path);
1065 if (error != base::PLATFORM_FILE_OK) 1064 if (error != base::File::FILE_OK)
1066 return error; 1065 return error;
1067 1066
1068 bool created = false; 1067 bool created = false;
1069 if (!src_file_path.empty()) { 1068 if (!src_file_path.empty()) {
1070 DCHECK(!file_flags); 1069 DCHECK(!file_flags);
1071 DCHECK(!handle); 1070 DCHECK(!handle);
1072 error = NativeFileUtil::CopyOrMoveFile( 1071 error = NativeFileUtil::CopyOrMoveFile(
1073 src_file_path, dest_local_path, 1072 src_file_path, dest_local_path,
1074 FileSystemOperation::OPTION_NONE, 1073 FileSystemOperation::OPTION_NONE,
1075 fileapi::NativeFileUtil::CopyOrMoveModeForDestination(dest_url, 1074 fileapi::NativeFileUtil::CopyOrMoveModeForDestination(dest_url,
1076 true /* copy */)); 1075 true /* copy */));
1077 created = true; 1076 created = true;
1078 } else { 1077 } else {
1079 if (base::PathExists(dest_local_path)) { 1078 if (base::PathExists(dest_local_path)) {
1080 if (!base::DeleteFile(dest_local_path, true /* recursive */)) { 1079 if (!base::DeleteFile(dest_local_path, true /* recursive */)) {
1081 NOTREACHED(); 1080 NOTREACHED();
1082 return base::PLATFORM_FILE_ERROR_FAILED; 1081 return base::File::FILE_ERROR_FAILED;
1083 } 1082 }
1084 LOG(WARNING) << "A stray file detected"; 1083 LOG(WARNING) << "A stray file detected";
1085 InvalidateUsageCache(context, dest_url.origin(), dest_url.type()); 1084 InvalidateUsageCache(context, dest_url.origin(), dest_url.type());
1086 } 1085 }
1087 1086
1088 if (handle) { 1087 if (handle) {
1089 error = NativeFileUtil::CreateOrOpen( 1088 error = NativeFileUtil::CreateOrOpen(
1090 dest_local_path, file_flags, handle, &created); 1089 dest_local_path, file_flags, handle, &created);
1091 // If this succeeds, we must close handle on any subsequent error. 1090 // If this succeeds, we must close handle on any subsequent error.
1092 } else { 1091 } else {
1093 DCHECK(!file_flags); // file_flags is only used by CreateOrOpen. 1092 DCHECK(!file_flags); // file_flags is only used by CreateOrOpen.
1094 error = NativeFileUtil::EnsureFileExists(dest_local_path, &created); 1093 error = NativeFileUtil::EnsureFileExists(dest_local_path, &created);
1095 } 1094 }
1096 } 1095 }
1097 if (error != base::PLATFORM_FILE_OK) 1096 if (error != base::File::FILE_OK)
1098 return error; 1097 return error;
1099 1098
1100 if (!created) { 1099 if (!created) {
1101 NOTREACHED(); 1100 NOTREACHED();
1102 if (handle) { 1101 if (handle) {
1103 DCHECK_NE(base::kInvalidPlatformFileValue, *handle); 1102 DCHECK_NE(base::kInvalidPlatformFileValue, *handle);
1104 base::ClosePlatformFile(*handle); 1103 base::ClosePlatformFile(*handle);
1105 base::DeleteFile(dest_local_path, false /* recursive */); 1104 base::DeleteFile(dest_local_path, false /* recursive */);
1106 *handle = base::kInvalidPlatformFileValue; 1105 *handle = base::kInvalidPlatformFileValue;
1107 } 1106 }
1108 return base::PLATFORM_FILE_ERROR_FAILED; 1107 return base::File::FILE_ERROR_FAILED;
1109 } 1108 }
1110 1109
1111 // This removes the root, including the trailing slash, leaving a relative 1110 // This removes the root, including the trailing slash, leaving a relative
1112 // path. 1111 // path.
1113 dest_file_info->data_path = base::FilePath( 1112 dest_file_info->data_path = base::FilePath(
1114 dest_local_path.value().substr(root.value().length() + 1)); 1113 dest_local_path.value().substr(root.value().length() + 1));
1115 1114
1116 FileId file_id; 1115 FileId file_id;
1117 error = db->AddFileInfo(*dest_file_info, &file_id); 1116 error = db->AddFileInfo(*dest_file_info, &file_id);
1118 if (error != base::PLATFORM_FILE_OK) { 1117 if (error != base::File::FILE_OK) {
1119 if (handle) { 1118 if (handle) {
1120 DCHECK_NE(base::kInvalidPlatformFileValue, *handle); 1119 DCHECK_NE(base::kInvalidPlatformFileValue, *handle);
1121 base::ClosePlatformFile(*handle); 1120 base::ClosePlatformFile(*handle);
1122 *handle = base::kInvalidPlatformFileValue; 1121 *handle = base::kInvalidPlatformFileValue;
1123 } 1122 }
1124 base::DeleteFile(dest_local_path, false /* recursive */); 1123 base::DeleteFile(dest_local_path, false /* recursive */);
1125 return error; 1124 return error;
1126 } 1125 }
1127 TouchDirectory(db, dest_file_info->parent_id); 1126 TouchDirectory(db, dest_file_info->parent_id);
1128 1127
1129 return base::PLATFORM_FILE_OK; 1128 return base::File::FILE_OK;
1130 } 1129 }
1131 1130
1132 base::FilePath ObfuscatedFileUtil::DataPathToLocalPath( 1131 base::FilePath ObfuscatedFileUtil::DataPathToLocalPath(
1133 const FileSystemURL& url, const base::FilePath& data_path) { 1132 const FileSystemURL& url, const base::FilePath& data_path) {
1134 PlatformFileError error = base::PLATFORM_FILE_OK; 1133 base::File::Error error = base::File::FILE_OK;
1135 base::FilePath root = GetDirectoryForURL(url, false, &error); 1134 base::FilePath root = GetDirectoryForURL(url, false, &error);
1136 if (error != base::PLATFORM_FILE_OK) 1135 if (error != base::File::FILE_OK)
1137 return base::FilePath(); 1136 return base::FilePath();
1138 return root.Append(data_path); 1137 return root.Append(data_path);
1139 } 1138 }
1140 1139
1141 std::string ObfuscatedFileUtil::GetDirectoryDatabaseKey( 1140 std::string ObfuscatedFileUtil::GetDirectoryDatabaseKey(
1142 const GURL& origin, const std::string& type_string) { 1141 const GURL& origin, const std::string& type_string) {
1143 if (type_string.empty()) { 1142 if (type_string.empty()) {
1144 LOG(WARNING) << "Unknown filesystem type requested:" << type_string; 1143 LOG(WARNING) << "Unknown filesystem type requested:" << type_string;
1145 return std::string(); 1144 return std::string();
1146 } 1145 }
(...skipping 12 matching lines...) Expand all
1159 url.origin(), CallGetTypeStringForURL(url)); 1158 url.origin(), CallGetTypeStringForURL(url));
1160 if (key.empty()) 1159 if (key.empty())
1161 return NULL; 1160 return NULL;
1162 1161
1163 DirectoryMap::iterator iter = directories_.find(key); 1162 DirectoryMap::iterator iter = directories_.find(key);
1164 if (iter != directories_.end()) { 1163 if (iter != directories_.end()) {
1165 MarkUsed(); 1164 MarkUsed();
1166 return iter->second; 1165 return iter->second;
1167 } 1166 }
1168 1167
1169 PlatformFileError error = base::PLATFORM_FILE_OK; 1168 base::File::Error error = base::File::FILE_OK;
1170 base::FilePath path = GetDirectoryForURL(url, create, &error); 1169 base::FilePath path = GetDirectoryForURL(url, create, &error);
1171 if (error != base::PLATFORM_FILE_OK) { 1170 if (error != base::File::FILE_OK) {
1172 LOG(WARNING) << "Failed to get origin+type directory: " 1171 LOG(WARNING) << "Failed to get origin+type directory: "
1173 << url.DebugString() << " error:" << error; 1172 << url.DebugString() << " error:" << error;
1174 return NULL; 1173 return NULL;
1175 } 1174 }
1176 MarkUsed(); 1175 MarkUsed();
1177 SandboxDirectoryDatabase* database = new SandboxDirectoryDatabase(path); 1176 SandboxDirectoryDatabase* database = new SandboxDirectoryDatabase(path);
1178 directories_[key] = database; 1177 directories_[key] = database;
1179 return database; 1178 return database;
1180 } 1179 }
1181 1180
1182 base::FilePath ObfuscatedFileUtil::GetDirectoryForOrigin( 1181 base::FilePath ObfuscatedFileUtil::GetDirectoryForOrigin(
1183 const GURL& origin, bool create, base::PlatformFileError* error_code) { 1182 const GURL& origin, bool create, base::File::Error* error_code) {
1184 if (!InitOriginDatabase(origin, create)) { 1183 if (!InitOriginDatabase(origin, create)) {
1185 if (error_code) { 1184 if (error_code) {
1186 *error_code = create ? 1185 *error_code = create ?
1187 base::PLATFORM_FILE_ERROR_FAILED : 1186 base::File::FILE_ERROR_FAILED :
1188 base::PLATFORM_FILE_ERROR_NOT_FOUND; 1187 base::File::FILE_ERROR_NOT_FOUND;
1189 } 1188 }
1190 return base::FilePath(); 1189 return base::FilePath();
1191 } 1190 }
1192 base::FilePath directory_name; 1191 base::FilePath directory_name;
1193 std::string id = webkit_database::GetIdentifierFromOrigin(origin); 1192 std::string id = webkit_database::GetIdentifierFromOrigin(origin);
1194 1193
1195 bool exists_in_db = origin_database_->HasOriginPath(id); 1194 bool exists_in_db = origin_database_->HasOriginPath(id);
1196 if (!exists_in_db && !create) { 1195 if (!exists_in_db && !create) {
1197 if (error_code) 1196 if (error_code)
1198 *error_code = base::PLATFORM_FILE_ERROR_NOT_FOUND; 1197 *error_code = base::File::FILE_ERROR_NOT_FOUND;
1199 return base::FilePath(); 1198 return base::FilePath();
1200 } 1199 }
1201 if (!origin_database_->GetPathForOrigin(id, &directory_name)) { 1200 if (!origin_database_->GetPathForOrigin(id, &directory_name)) {
1202 if (error_code) 1201 if (error_code)
1203 *error_code = base::PLATFORM_FILE_ERROR_FAILED; 1202 *error_code = base::File::FILE_ERROR_FAILED;
1204 return base::FilePath(); 1203 return base::FilePath();
1205 } 1204 }
1206 1205
1207 base::FilePath path = file_system_directory_.Append(directory_name); 1206 base::FilePath path = file_system_directory_.Append(directory_name);
1208 bool exists_in_fs = base::DirectoryExists(path); 1207 bool exists_in_fs = base::DirectoryExists(path);
1209 if (!exists_in_db && exists_in_fs) { 1208 if (!exists_in_db && exists_in_fs) {
1210 if (!base::DeleteFile(path, true)) { 1209 if (!base::DeleteFile(path, true)) {
1211 if (error_code) 1210 if (error_code)
1212 *error_code = base::PLATFORM_FILE_ERROR_FAILED; 1211 *error_code = base::File::FILE_ERROR_FAILED;
1213 return base::FilePath(); 1212 return base::FilePath();
1214 } 1213 }
1215 exists_in_fs = false; 1214 exists_in_fs = false;
1216 } 1215 }
1217 1216
1218 if (!exists_in_fs) { 1217 if (!exists_in_fs) {
1219 if (!create || !base::CreateDirectory(path)) { 1218 if (!create || !base::CreateDirectory(path)) {
1220 if (error_code) 1219 if (error_code)
1221 *error_code = create ? 1220 *error_code = create ?
1222 base::PLATFORM_FILE_ERROR_FAILED : 1221 base::File::FILE_ERROR_FAILED :
1223 base::PLATFORM_FILE_ERROR_NOT_FOUND; 1222 base::File::FILE_ERROR_NOT_FOUND;
1224 return base::FilePath(); 1223 return base::FilePath();
1225 } 1224 }
1226 } 1225 }
1227 1226
1228 if (error_code) 1227 if (error_code)
1229 *error_code = base::PLATFORM_FILE_OK; 1228 *error_code = base::File::FILE_OK;
1230 1229
1231 return path; 1230 return path;
1232 } 1231 }
1233 1232
1234 void ObfuscatedFileUtil::InvalidateUsageCache( 1233 void ObfuscatedFileUtil::InvalidateUsageCache(
1235 FileSystemOperationContext* context, 1234 FileSystemOperationContext* context,
1236 const GURL& origin, 1235 const GURL& origin,
1237 FileSystemType type) { 1236 FileSystemType type) {
1238 if (sandbox_delegate_) 1237 if (sandbox_delegate_)
1239 sandbox_delegate_->InvalidateUsageCache(origin, type); 1238 sandbox_delegate_->InvalidateUsageCache(origin, type);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1294 file_system_directory_, 1293 file_system_directory_,
1295 prioritized_origin_database->GetSandboxOriginDatabase()); 1294 prioritized_origin_database->GetSandboxOriginDatabase());
1296 } 1295 }
1297 1296
1298 prioritized_origin_database->InitializePrimaryOrigin( 1297 prioritized_origin_database->InitializePrimaryOrigin(
1299 isolated_origin_string); 1298 isolated_origin_string);
1300 1299
1301 return true; 1300 return true;
1302 } 1301 }
1303 1302
1304 PlatformFileError ObfuscatedFileUtil::GenerateNewLocalPath( 1303 base::File::Error ObfuscatedFileUtil::GenerateNewLocalPath(
1305 SandboxDirectoryDatabase* db, 1304 SandboxDirectoryDatabase* db,
1306 FileSystemOperationContext* context, 1305 FileSystemOperationContext* context,
1307 const FileSystemURL& url, 1306 const FileSystemURL& url,
1308 base::FilePath* local_path) { 1307 base::FilePath* local_path) {
1309 DCHECK(local_path); 1308 DCHECK(local_path);
1310 int64 number; 1309 int64 number;
1311 if (!db || !db->GetNextInteger(&number)) 1310 if (!db || !db->GetNextInteger(&number))
1312 return base::PLATFORM_FILE_ERROR_FAILED; 1311 return base::File::FILE_ERROR_FAILED;
1313 1312
1314 PlatformFileError error = base::PLATFORM_FILE_OK; 1313 base::File::Error error = base::File::FILE_OK;
1315 base::FilePath new_local_path = GetDirectoryForURL(url, false, &error); 1314 base::FilePath new_local_path = GetDirectoryForURL(url, false, &error);
1316 if (error != base::PLATFORM_FILE_OK) 1315 if (error != base::File::FILE_OK)
1317 return base::PLATFORM_FILE_ERROR_FAILED; 1316 return base::File::FILE_ERROR_FAILED;
1318 1317
1319 // We use the third- and fourth-to-last digits as the directory. 1318 // We use the third- and fourth-to-last digits as the directory.
1320 int64 directory_number = number % 10000 / 100; 1319 int64 directory_number = number % 10000 / 100;
1321 new_local_path = new_local_path.AppendASCII( 1320 new_local_path = new_local_path.AppendASCII(
1322 base::StringPrintf("%02" PRId64, directory_number)); 1321 base::StringPrintf("%02" PRId64, directory_number));
1323 1322
1324 error = NativeFileUtil::CreateDirectory( 1323 error = NativeFileUtil::CreateDirectory(
1325 new_local_path, false /* exclusive */, false /* recursive */); 1324 new_local_path, false /* exclusive */, false /* recursive */);
1326 if (error != base::PLATFORM_FILE_OK) 1325 if (error != base::File::FILE_OK)
1327 return error; 1326 return error;
1328 1327
1329 *local_path = 1328 *local_path =
1330 new_local_path.AppendASCII(base::StringPrintf("%08" PRId64, number)); 1329 new_local_path.AppendASCII(base::StringPrintf("%08" PRId64, number));
1331 return base::PLATFORM_FILE_OK; 1330 return base::File::FILE_OK;
1332 } 1331 }
1333 1332
1334 PlatformFileError ObfuscatedFileUtil::CreateOrOpenInternal( 1333 base::File::Error ObfuscatedFileUtil::CreateOrOpenInternal(
1335 FileSystemOperationContext* context, 1334 FileSystemOperationContext* context,
1336 const FileSystemURL& url, int file_flags, 1335 const FileSystemURL& url, int file_flags,
1337 PlatformFile* file_handle, bool* created) { 1336 PlatformFile* file_handle, bool* created) {
1338 DCHECK(!(file_flags & (base::PLATFORM_FILE_DELETE_ON_CLOSE | 1337 DCHECK(!(file_flags & (base::PLATFORM_FILE_DELETE_ON_CLOSE |
1339 base::PLATFORM_FILE_HIDDEN | base::PLATFORM_FILE_EXCLUSIVE_READ | 1338 base::PLATFORM_FILE_HIDDEN | base::PLATFORM_FILE_EXCLUSIVE_READ |
1340 base::PLATFORM_FILE_EXCLUSIVE_WRITE))); 1339 base::PLATFORM_FILE_EXCLUSIVE_WRITE)));
1341 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, true); 1340 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, true);
1342 if (!db) 1341 if (!db)
1343 return base::PLATFORM_FILE_ERROR_FAILED; 1342 return base::File::FILE_ERROR_FAILED;
1344 FileId file_id; 1343 FileId file_id;
1345 if (!db->GetFileWithPath(url.path(), &file_id)) { 1344 if (!db->GetFileWithPath(url.path(), &file_id)) {
1346 // The file doesn't exist. 1345 // The file doesn't exist.
1347 if (!(file_flags & (base::PLATFORM_FILE_CREATE | 1346 if (!(file_flags & (base::PLATFORM_FILE_CREATE |
1348 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_OPEN_ALWAYS))) 1347 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_OPEN_ALWAYS)))
1349 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 1348 return base::File::FILE_ERROR_NOT_FOUND;
1350 FileId parent_id; 1349 FileId parent_id;
1351 if (!db->GetFileWithPath(VirtualPath::DirName(url.path()), 1350 if (!db->GetFileWithPath(VirtualPath::DirName(url.path()),
1352 &parent_id)) 1351 &parent_id))
1353 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 1352 return base::File::FILE_ERROR_NOT_FOUND;
1354 FileInfo file_info; 1353 FileInfo file_info;
1355 InitFileInfo(&file_info, parent_id, 1354 InitFileInfo(&file_info, parent_id,
1356 VirtualPath::BaseName(url.path()).value()); 1355 VirtualPath::BaseName(url.path()).value());
1357 1356
1358 int64 growth = UsageForPath(file_info.name.size()); 1357 int64 growth = UsageForPath(file_info.name.size());
1359 if (!AllocateQuota(context, growth)) 1358 if (!AllocateQuota(context, growth))
1360 return base::PLATFORM_FILE_ERROR_NO_SPACE; 1359 return base::File::FILE_ERROR_NO_SPACE;
1361 PlatformFileError error = CreateFile( 1360 base::File::Error error = CreateFile(
1362 context, base::FilePath(), 1361 context, base::FilePath(),
1363 url, &file_info, file_flags, file_handle); 1362 url, &file_info, file_flags, file_handle);
1364 if (created && base::PLATFORM_FILE_OK == error) { 1363 if (created && base::File::FILE_OK == error) {
1365 *created = true; 1364 *created = true;
1366 UpdateUsage(context, url, growth); 1365 UpdateUsage(context, url, growth);
1367 context->change_observers()->Notify( 1366 context->change_observers()->Notify(
1368 &FileChangeObserver::OnCreateFile, MakeTuple(url)); 1367 &FileChangeObserver::OnCreateFile, MakeTuple(url));
1369 } 1368 }
1370 return error; 1369 return error;
1371 } 1370 }
1372 1371
1373 if (file_flags & base::PLATFORM_FILE_CREATE) 1372 if (file_flags & base::PLATFORM_FILE_CREATE)
1374 return base::PLATFORM_FILE_ERROR_EXISTS; 1373 return base::File::FILE_ERROR_EXISTS;
1375 1374
1376 base::PlatformFileInfo platform_file_info; 1375 base::File::Info platform_file_info;
1377 base::FilePath local_path; 1376 base::FilePath local_path;
1378 FileInfo file_info; 1377 FileInfo file_info;
1379 base::PlatformFileError error = GetFileInfoInternal( 1378 base::File::Error error = GetFileInfoInternal(
1380 db, context, url, file_id, &file_info, &platform_file_info, &local_path); 1379 db, context, url, file_id, &file_info, &platform_file_info, &local_path);
1381 if (error != base::PLATFORM_FILE_OK) 1380 if (error != base::File::FILE_OK)
1382 return error; 1381 return error;
1383 if (file_info.is_directory()) 1382 if (file_info.is_directory())
1384 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; 1383 return base::File::FILE_ERROR_NOT_A_FILE;
1385 1384
1386 int64 delta = 0; 1385 int64 delta = 0;
1387 if (file_flags & (base::PLATFORM_FILE_CREATE_ALWAYS | 1386 if (file_flags & (base::PLATFORM_FILE_CREATE_ALWAYS |
1388 base::PLATFORM_FILE_OPEN_TRUNCATED)) { 1387 base::PLATFORM_FILE_OPEN_TRUNCATED)) {
1389 // The file exists and we're truncating. 1388 // The file exists and we're truncating.
1390 delta = -platform_file_info.size; 1389 delta = -platform_file_info.size;
1391 AllocateQuota(context, delta); 1390 AllocateQuota(context, delta);
1392 } 1391 }
1393 1392
1394 error = NativeFileUtil::CreateOrOpen( 1393 error = NativeFileUtil::CreateOrOpen(
1395 local_path, file_flags, file_handle, created); 1394 local_path, file_flags, file_handle, created);
1396 if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) { 1395 if (error == base::File::FILE_ERROR_NOT_FOUND) {
1397 // TODO(tzik): Also invalidate on-memory usage cache in UsageTracker. 1396 // TODO(tzik): Also invalidate on-memory usage cache in UsageTracker.
1398 // TODO(tzik): Delete database entry after ensuring the file lost. 1397 // TODO(tzik): Delete database entry after ensuring the file lost.
1399 InvalidateUsageCache(context, url.origin(), url.type()); 1398 InvalidateUsageCache(context, url.origin(), url.type());
1400 LOG(WARNING) << "Lost a backing file."; 1399 LOG(WARNING) << "Lost a backing file.";
1401 error = base::PLATFORM_FILE_ERROR_FAILED; 1400 error = base::File::FILE_ERROR_FAILED;
1402 } 1401 }
1403 1402
1404 // If truncating we need to update the usage. 1403 // If truncating we need to update the usage.
1405 if (error == base::PLATFORM_FILE_OK && delta) { 1404 if (error == base::File::FILE_OK && delta) {
1406 UpdateUsage(context, url, delta); 1405 UpdateUsage(context, url, delta);
1407 context->change_observers()->Notify( 1406 context->change_observers()->Notify(
1408 &FileChangeObserver::OnModifyFile, MakeTuple(url)); 1407 &FileChangeObserver::OnModifyFile, MakeTuple(url));
1409 } 1408 }
1410 return error; 1409 return error;
1411 } 1410 }
1412 1411
1413 bool ObfuscatedFileUtil::HasIsolatedStorage(const GURL& origin) { 1412 bool ObfuscatedFileUtil::HasIsolatedStorage(const GURL& origin) {
1414 return special_storage_policy_.get() && 1413 return special_storage_policy_.get() &&
1415 special_storage_policy_->HasIsolatedStorage(origin); 1414 special_storage_policy_->HasIsolatedStorage(origin);
1416 } 1415 }
1417 1416
1418 } // namespace fileapi 1417 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/browser/fileapi/obfuscated_file_util.h ('k') | webkit/browser/fileapi/plugin_private_file_system_backend.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698