OLD | NEW |
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 "chrome/browser/chromeos/drive/file_cache.h" | 5 #include "chrome/browser/chromeos/drive/file_cache.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 DCHECK(!callback.is_null()); | 544 DCHECK(!callback.is_null()); |
545 | 545 |
546 base::PostTaskAndReplyWithResult( | 546 base::PostTaskAndReplyWithResult( |
547 blocking_task_runner_, | 547 blocking_task_runner_, |
548 FROM_HERE, | 548 FROM_HERE, |
549 base::Bind(&FileCache::ClearDirty, | 549 base::Bind(&FileCache::ClearDirty, |
550 base::Unretained(this), resource_id, md5), | 550 base::Unretained(this), resource_id, md5), |
551 callback); | 551 callback); |
552 } | 552 } |
553 | 553 |
| 554 FileError FileCache::ClearDirty(const std::string& resource_id, |
| 555 const std::string& md5) { |
| 556 AssertOnSequencedWorkerPool(); |
| 557 |
| 558 // |md5| is the new .<md5> extension to rename the file to. |
| 559 // So, search for entry in cache without comparing md5. |
| 560 FileCacheEntry cache_entry; |
| 561 |
| 562 // Clearing a dirty file means its entry and actual file blob must exist in |
| 563 // cache. |
| 564 if (!GetCacheEntry(resource_id, std::string(), &cache_entry) || |
| 565 !cache_entry.is_present()) { |
| 566 LOG(WARNING) << "Can't clear dirty state of a file that wasn't cached: " |
| 567 << "res_id=" << resource_id |
| 568 << ", md5=" << md5; |
| 569 return FILE_ERROR_NOT_FOUND; |
| 570 } |
| 571 |
| 572 // If a file is not dirty (it should have been marked dirty via |
| 573 // MarkDirtyInCache), clearing its dirty state is an invalid operation. |
| 574 if (!cache_entry.is_dirty()) { |
| 575 LOG(WARNING) << "Can't clear dirty state of a non-dirty file: res_id=" |
| 576 << resource_id |
| 577 << ", md5=" << md5; |
| 578 return FILE_ERROR_INVALID_OPERATION; |
| 579 } |
| 580 |
| 581 // File must be dirty and hence in persistent dir. |
| 582 DCHECK(cache_entry.is_persistent()); |
| 583 |
| 584 // Get the current path of the file in cache. |
| 585 base::FilePath source_path = |
| 586 GetCacheFilePath(resource_id, |
| 587 md5, |
| 588 GetSubDirectoryType(cache_entry), |
| 589 CACHED_FILE_LOCALLY_MODIFIED); |
| 590 |
| 591 // Determine destination path. |
| 592 // If file is pinned, move it to persistent dir with .md5 extension; |
| 593 // otherwise, move it to tmp dir with .md5 extension. |
| 594 const CacheSubDirectoryType sub_dir_type = |
| 595 cache_entry.is_pinned() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; |
| 596 base::FilePath dest_path = GetCacheFilePath(resource_id, |
| 597 md5, |
| 598 sub_dir_type, |
| 599 CACHED_FILE_FROM_SERVER); |
| 600 |
| 601 if (!MoveFile(source_path, dest_path)) |
| 602 return FILE_ERROR_FAILED; |
| 603 |
| 604 // Now that file operations have completed, update metadata. |
| 605 cache_entry.set_md5(md5); |
| 606 cache_entry.set_is_dirty(false); |
| 607 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); |
| 608 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); |
| 609 return FILE_ERROR_OK; |
| 610 } |
| 611 |
554 void FileCache::RemoveOnUIThread(const std::string& resource_id, | 612 void FileCache::RemoveOnUIThread(const std::string& resource_id, |
555 const FileOperationCallback& callback) { | 613 const FileOperationCallback& callback) { |
556 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 614 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
557 DCHECK(!callback.is_null()); | 615 DCHECK(!callback.is_null()); |
558 | 616 |
559 base::PostTaskAndReplyWithResult( | 617 base::PostTaskAndReplyWithResult( |
560 blocking_task_runner_, | 618 blocking_task_runner_, |
561 FROM_HERE, | 619 FROM_HERE, |
562 base::Bind(&FileCache::Remove, | 620 base::Bind(&FileCache::Remove, |
563 base::Unretained(this), resource_id), | 621 base::Unretained(this), resource_id), |
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
952 return FILE_ERROR_FAILED; | 1010 return FILE_ERROR_FAILED; |
953 | 1011 |
954 // Now that file operations have completed, update metadata. | 1012 // Now that file operations have completed, update metadata. |
955 cache_entry.set_md5(md5); | 1013 cache_entry.set_md5(md5); |
956 cache_entry.set_is_dirty(true); | 1014 cache_entry.set_is_dirty(true); |
957 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); | 1015 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); |
958 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); | 1016 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); |
959 return FILE_ERROR_OK; | 1017 return FILE_ERROR_OK; |
960 } | 1018 } |
961 | 1019 |
962 FileError FileCache::ClearDirty(const std::string& resource_id, | |
963 const std::string& md5) { | |
964 AssertOnSequencedWorkerPool(); | |
965 | |
966 // |md5| is the new .<md5> extension to rename the file to. | |
967 // So, search for entry in cache without comparing md5. | |
968 FileCacheEntry cache_entry; | |
969 | |
970 // Clearing a dirty file means its entry and actual file blob must exist in | |
971 // cache. | |
972 if (!GetCacheEntry(resource_id, std::string(), &cache_entry) || | |
973 !cache_entry.is_present()) { | |
974 LOG(WARNING) << "Can't clear dirty state of a file that wasn't cached: " | |
975 << "res_id=" << resource_id | |
976 << ", md5=" << md5; | |
977 return FILE_ERROR_NOT_FOUND; | |
978 } | |
979 | |
980 // If a file is not dirty (it should have been marked dirty via | |
981 // MarkDirtyInCache), clearing its dirty state is an invalid operation. | |
982 if (!cache_entry.is_dirty()) { | |
983 LOG(WARNING) << "Can't clear dirty state of a non-dirty file: res_id=" | |
984 << resource_id | |
985 << ", md5=" << md5; | |
986 return FILE_ERROR_INVALID_OPERATION; | |
987 } | |
988 | |
989 // File must be dirty and hence in persistent dir. | |
990 DCHECK(cache_entry.is_persistent()); | |
991 | |
992 // Get the current path of the file in cache. | |
993 base::FilePath source_path = | |
994 GetCacheFilePath(resource_id, | |
995 md5, | |
996 GetSubDirectoryType(cache_entry), | |
997 CACHED_FILE_LOCALLY_MODIFIED); | |
998 | |
999 // Determine destination path. | |
1000 // If file is pinned, move it to persistent dir with .md5 extension; | |
1001 // otherwise, move it to tmp dir with .md5 extension. | |
1002 const CacheSubDirectoryType sub_dir_type = | |
1003 cache_entry.is_pinned() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; | |
1004 base::FilePath dest_path = GetCacheFilePath(resource_id, | |
1005 md5, | |
1006 sub_dir_type, | |
1007 CACHED_FILE_FROM_SERVER); | |
1008 | |
1009 if (!MoveFile(source_path, dest_path)) | |
1010 return FILE_ERROR_FAILED; | |
1011 | |
1012 // Now that file operations have completed, update metadata. | |
1013 cache_entry.set_md5(md5); | |
1014 cache_entry.set_is_dirty(false); | |
1015 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); | |
1016 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); | |
1017 return FILE_ERROR_OK; | |
1018 } | |
1019 | |
1020 bool FileCache::ClearAll() { | 1020 bool FileCache::ClearAll() { |
1021 AssertOnSequencedWorkerPool(); | 1021 AssertOnSequencedWorkerPool(); |
1022 | 1022 |
1023 if (!file_util::Delete(cache_root_path_, true)) { | 1023 if (!file_util::Delete(cache_root_path_, true)) { |
1024 LOG(WARNING) << "Failed to delete the cache directory"; | 1024 LOG(WARNING) << "Failed to delete the cache directory"; |
1025 return false; | 1025 return false; |
1026 } | 1026 } |
1027 | 1027 |
1028 if (!InitializeOnBlockingPool()) { | 1028 if (!InitializeOnBlockingPool()) { |
1029 LOG(WARNING) << "Failed to initialize the cache"; | 1029 LOG(WARNING) << "Failed to initialize the cache"; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1131 } | 1131 } |
1132 | 1132 |
1133 // static | 1133 // static |
1134 FileCache::CacheSubDirectoryType FileCache::GetSubDirectoryType( | 1134 FileCache::CacheSubDirectoryType FileCache::GetSubDirectoryType( |
1135 const FileCacheEntry& cache_entry) { | 1135 const FileCacheEntry& cache_entry) { |
1136 return cache_entry.is_persistent() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; | 1136 return cache_entry.is_persistent() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; |
1137 } | 1137 } |
1138 | 1138 |
1139 } // namespace internal | 1139 } // namespace internal |
1140 } // namespace drive | 1140 } // namespace drive |
OLD | NEW |