[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 5 | #include "chrome/browser/chromeos/drive/file_cache.h" |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 6 | |
| 7 | #include <vector> |
| 8 | |
lukasza | 037c10b1 | 2015-06-12 04:21:25 | [diff] [blame^] | 9 | #include "base/bind.h" |
| 10 | #include "base/bind_helpers.h" |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 11 | #include "base/callback_helpers.h" |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 12 | #include "base/files/file_enumerator.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 13 | #include "base/files/file_util.h" |
lukasza | 037c10b1 | 2015-06-12 04:21:25 | [diff] [blame^] | 14 | #include "base/location.h" |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 15 | #include "base/logging.h" |
[email protected] | d105b3ad | 2013-11-01 05:33:13 | [diff] [blame] | 16 | #include "base/metrics/histogram.h" |
[email protected] | 5c07332 | 2013-06-11 08:03:30 | [diff] [blame] | 17 | #include "base/strings/string_util.h" |
| 18 | #include "base/strings/stringprintf.h" |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 19 | #include "base/sys_info.h" |
[email protected] | 15de814 | 2012-10-11 06:00:54 | [diff] [blame] | 20 | #include "chrome/browser/chromeos/drive/drive.pb.h" |
[email protected] | 4fa2fd5d | 2013-04-26 03:42:52 | [diff] [blame] | 21 | #include "chrome/browser/chromeos/drive/file_system_util.h" |
[email protected] | 2df61e1 | 2013-06-21 16:00:09 | [diff] [blame] | 22 | #include "chrome/browser/chromeos/drive/resource_metadata_storage.h" |
[email protected] | f2731d1 | 2013-10-22 03:23:15 | [diff] [blame] | 23 | #include "chrome/browser/drive/drive_api_util.h" |
[email protected] | e8f3f998 | 2013-05-10 20:59:53 | [diff] [blame] | 24 | #include "chromeos/chromeos_constants.h" |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 25 | #include "google_apis/drive/task_util.h" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 26 | #include "net/base/filename_util.h" |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 27 | #include "net/base/mime_sniffer.h" |
| 28 | #include "net/base/mime_util.h" |
[email protected] | ab3ed7e4a | 2013-12-12 09:18:30 | [diff] [blame] | 29 | #include "third_party/cros_system_api/constants/cryptohome.h" |
[email protected] | 7986b8d | 2012-06-14 15:05:14 | [diff] [blame] | 30 | |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 31 | namespace drive { |
[email protected] | 59c7cdec | 2013-05-07 04:17:13 | [diff] [blame] | 32 | namespace internal { |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 33 | namespace { |
| 34 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 35 | // Returns ID extracted from the path. |
| 36 | std::string GetIdFromPath(const base::FilePath& path) { |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 37 | return util::UnescapeCacheFileName(path.BaseName().AsUTF8Unsafe()); |
| 38 | } |
| 39 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 40 | } // namespace |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 41 | |
[email protected] | 2df61e1 | 2013-06-21 16:00:09 | [diff] [blame] | 42 | FileCache::FileCache(ResourceMetadataStorage* storage, |
[email protected] | e07f7b7b | 2013-06-19 03:43:12 | [diff] [blame] | 43 | const base::FilePath& cache_file_directory, |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 44 | base::SequencedTaskRunner* blocking_task_runner, |
| 45 | FreeDiskSpaceGetterInterface* free_disk_space_getter) |
[email protected] | 2df61e1 | 2013-06-21 16:00:09 | [diff] [blame] | 46 | : cache_file_directory_(cache_file_directory), |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 47 | blocking_task_runner_(blocking_task_runner), |
[email protected] | 2df61e1 | 2013-06-21 16:00:09 | [diff] [blame] | 48 | storage_(storage), |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 49 | free_disk_space_getter_(free_disk_space_getter), |
[email protected] | 9c00909 | 2013-05-01 03:14:09 | [diff] [blame] | 50 | weak_ptr_factory_(this) { |
[email protected] | 144b6c4 | 2013-06-14 07:30:38 | [diff] [blame] | 51 | DCHECK(blocking_task_runner_.get()); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 52 | } |
| 53 | |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 54 | FileCache::~FileCache() { |
[email protected] | 17196ee | 2012-12-13 06:23:51 | [diff] [blame] | 55 | // Must be on the sequenced worker pool, as |metadata_| must be deleted on |
| 56 | // the sequenced worker pool. |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 57 | AssertOnSequencedWorkerPool(); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 58 | } |
| 59 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 60 | base::FilePath FileCache::GetCacheFilePath(const std::string& id) const { |
[email protected] | e07f7b7b | 2013-06-19 03:43:12 | [diff] [blame] | 61 | return cache_file_directory_.Append( |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 62 | base::FilePath::FromUTF8Unsafe(util::EscapeCacheFileName(id))); |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 63 | } |
| 64 | |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 65 | void FileCache::AssertOnSequencedWorkerPool() { |
[email protected] | 8e37b9b | 2013-12-11 09:06:02 | [diff] [blame] | 66 | DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); |
[email protected] | fcc92a5 | 2012-06-08 22:54:16 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 69 | bool FileCache::IsUnderFileCacheDirectory(const base::FilePath& path) const { |
[email protected] | e07f7b7b | 2013-06-19 03:43:12 | [diff] [blame] | 70 | return cache_file_directory_.IsParent(path); |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 71 | } |
| 72 | |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 73 | bool FileCache::FreeDiskSpaceIfNeededFor(int64 num_bytes) { |
| 74 | AssertOnSequencedWorkerPool(); |
| 75 | |
| 76 | // Do nothing and return if we have enough space. |
[email protected] | e07f7b7b | 2013-06-19 03:43:12 | [diff] [blame] | 77 | if (HasEnoughSpaceFor(num_bytes, cache_file_directory_)) |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 78 | return true; |
| 79 | |
| 80 | // Otherwise, try to free up the disk space. |
| 81 | DVLOG(1) << "Freeing up disk space for " << num_bytes; |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 82 | |
[email protected] | 9d14758 | 2013-06-14 06:25:45 | [diff] [blame] | 83 | // Remove all entries unless specially marked. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 84 | scoped_ptr<ResourceMetadataStorage::Iterator> it = storage_->GetIterator(); |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 85 | for (; !it->IsAtEnd(); it->Advance()) { |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 86 | if (it->GetValue().file_specific_info().has_cache_state() && |
| 87 | !it->GetValue().file_specific_info().cache_state().is_pinned() && |
| 88 | !it->GetValue().file_specific_info().cache_state().is_dirty() && |
| 89 | !mounted_files_.count(it->GetID())) { |
| 90 | ResourceEntry entry(it->GetValue()); |
| 91 | entry.mutable_file_specific_info()->clear_cache_state(); |
| 92 | storage_->PutEntry(entry); |
| 93 | } |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 94 | } |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 95 | if (it->HasError()) |
| 96 | return false; |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 97 | |
[email protected] | 9d14758 | 2013-06-14 06:25:45 | [diff] [blame] | 98 | // Remove all files which have no corresponding cache entries. |
[email protected] | e07f7b7b | 2013-06-19 03:43:12 | [diff] [blame] | 99 | base::FileEnumerator enumerator(cache_file_directory_, |
[email protected] | 9d14758 | 2013-06-14 06:25:45 | [diff] [blame] | 100 | false, // not recursive |
| 101 | base::FileEnumerator::FILES); |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 102 | ResourceEntry entry; |
[email protected] | 9d14758 | 2013-06-14 06:25:45 | [diff] [blame] | 103 | for (base::FilePath current = enumerator.Next(); !current.empty(); |
| 104 | current = enumerator.Next()) { |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 105 | std::string id = GetIdFromPath(current); |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 106 | FileError error = storage_->GetEntry(id, &entry); |
| 107 | if (error == FILE_ERROR_NOT_FOUND || |
| 108 | (error == FILE_ERROR_OK && |
| 109 | !entry.file_specific_info().cache_state().is_present())) |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 110 | base::DeleteFile(current, false /* recursive */); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 111 | else if (error != FILE_ERROR_OK) |
| 112 | return false; |
[email protected] | 9d14758 | 2013-06-14 06:25:45 | [diff] [blame] | 113 | } |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 114 | |
| 115 | // Check the disk space again. |
[email protected] | e07f7b7b | 2013-06-19 03:43:12 | [diff] [blame] | 116 | return HasEnoughSpaceFor(num_bytes, cache_file_directory_); |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 119 | FileError FileCache::GetFile(const std::string& id, |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 120 | base::FilePath* cache_file_path) { |
| 121 | AssertOnSequencedWorkerPool(); |
| 122 | DCHECK(cache_file_path); |
| 123 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 124 | ResourceEntry entry; |
| 125 | FileError error = storage_->GetEntry(id, &entry); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 126 | if (error != FILE_ERROR_OK) |
| 127 | return error; |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 128 | if (!entry.file_specific_info().cache_state().is_present()) |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 129 | return FILE_ERROR_NOT_FOUND; |
| 130 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 131 | *cache_file_path = GetCacheFilePath(id); |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 132 | return FILE_ERROR_OK; |
| 133 | } |
| 134 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 135 | FileError FileCache::Store(const std::string& id, |
[email protected] | 82c4eb9 | 2013-05-21 11:25:23 | [diff] [blame] | 136 | const std::string& md5, |
| 137 | const base::FilePath& source_path, |
| 138 | FileOperationType file_operation_type) { |
| 139 | AssertOnSequencedWorkerPool(); |
[email protected] | d8546c9 | 2013-05-02 05:09:59 | [diff] [blame] | 140 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 141 | ResourceEntry entry; |
| 142 | FileError error = storage_->GetEntry(id, &entry); |
| 143 | if (error != FILE_ERROR_OK) |
| 144 | return error; |
| 145 | |
[email protected] | 8e37b9b | 2013-12-11 09:06:02 | [diff] [blame] | 146 | int64 file_size = 0; |
| 147 | if (file_operation_type == FILE_OPERATION_COPY) { |
| 148 | if (!base::GetFileSize(source_path, &file_size)) { |
| 149 | LOG(WARNING) << "Couldn't get file size for: " << source_path.value(); |
| 150 | return FILE_ERROR_FAILED; |
| 151 | } |
| 152 | } |
| 153 | if (!FreeDiskSpaceIfNeededFor(file_size)) |
| 154 | return FILE_ERROR_NO_LOCAL_SPACE; |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 155 | |
[email protected] | 1d0786a | 2014-02-06 12:37:08 | [diff] [blame] | 156 | // If file is mounted, return error. |
| 157 | if (mounted_files_.count(id)) |
[email protected] | 8e37b9b | 2013-12-11 09:06:02 | [diff] [blame] | 158 | return FILE_ERROR_IN_USE; |
| 159 | |
| 160 | base::FilePath dest_path = GetCacheFilePath(id); |
| 161 | bool success = false; |
| 162 | switch (file_operation_type) { |
| 163 | case FILE_OPERATION_MOVE: |
| 164 | success = base::Move(source_path, dest_path); |
| 165 | break; |
| 166 | case FILE_OPERATION_COPY: |
| 167 | success = base::CopyFile(source_path, dest_path); |
| 168 | break; |
| 169 | default: |
| 170 | NOTREACHED(); |
| 171 | } |
| 172 | |
| 173 | if (!success) { |
| 174 | LOG(ERROR) << "Failed to store: " |
| 175 | << "source_path = " << source_path.value() << ", " |
| 176 | << "dest_path = " << dest_path.value() << ", " |
| 177 | << "file_operation_type = " << file_operation_type; |
| 178 | return FILE_ERROR_FAILED; |
| 179 | } |
| 180 | |
| 181 | // Now that file operations have completed, update metadata. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 182 | FileCacheEntry* cache_state = |
| 183 | entry.mutable_file_specific_info()->mutable_cache_state(); |
| 184 | cache_state->set_md5(md5); |
| 185 | cache_state->set_is_present(true); |
[email protected] | bae99ae5 | 2014-01-29 01:13:14 | [diff] [blame] | 186 | if (md5.empty()) |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 187 | cache_state->set_is_dirty(true); |
| 188 | return storage_->PutEntry(entry); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 189 | } |
| 190 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 191 | FileError FileCache::Pin(const std::string& id) { |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 192 | AssertOnSequencedWorkerPool(); |
| 193 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 194 | ResourceEntry entry; |
| 195 | FileError error = storage_->GetEntry(id, &entry); |
| 196 | if (error != FILE_ERROR_OK) |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 197 | return error; |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 198 | entry.mutable_file_specific_info()->mutable_cache_state()->set_is_pinned( |
| 199 | true); |
| 200 | return storage_->PutEntry(entry); |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 201 | } |
| 202 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 203 | FileError FileCache::Unpin(const std::string& id) { |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 204 | AssertOnSequencedWorkerPool(); |
| 205 | |
| 206 | // Unpinning a file means its entry must exist in cache. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 207 | ResourceEntry entry; |
| 208 | FileError error = storage_->GetEntry(id, &entry); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 209 | if (error != FILE_ERROR_OK) |
| 210 | return error; |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 211 | |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 212 | // Now that file operations have completed, update metadata. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 213 | if (entry.file_specific_info().cache_state().is_present()) { |
| 214 | entry.mutable_file_specific_info()->mutable_cache_state()->set_is_pinned( |
| 215 | false); |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 216 | } else { |
| 217 | // Remove the existing entry if we are unpinning a non-present file. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 218 | entry.mutable_file_specific_info()->clear_cache_state(); |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 219 | } |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 220 | error = storage_->PutEntry(entry); |
| 221 | if (error != FILE_ERROR_OK) |
| 222 | return error; |
[email protected] | bd2254d | 2013-06-12 16:00:47 | [diff] [blame] | 223 | |
[email protected] | 9d14758 | 2013-06-14 06:25:45 | [diff] [blame] | 224 | // Now it's a chance to free up space if needed. |
[email protected] | bd2254d | 2013-06-12 16:00:47 | [diff] [blame] | 225 | FreeDiskSpaceIfNeededFor(0); |
| 226 | |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 227 | return FILE_ERROR_OK; |
| 228 | } |
| 229 | |
[email protected] | c3f6564 | 2013-08-28 02:04:33 | [diff] [blame] | 230 | FileError FileCache::MarkAsMounted(const std::string& id, |
| 231 | base::FilePath* cache_file_path) { |
| 232 | AssertOnSequencedWorkerPool(); |
| 233 | DCHECK(cache_file_path); |
| 234 | |
| 235 | // Get cache entry associated with the id and md5 |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 236 | ResourceEntry entry; |
| 237 | FileError error = storage_->GetEntry(id, &entry); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 238 | if (error != FILE_ERROR_OK) |
| 239 | return error; |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 240 | if (!entry.file_specific_info().cache_state().is_present()) |
| 241 | return FILE_ERROR_NOT_FOUND; |
[email protected] | c3f6564 | 2013-08-28 02:04:33 | [diff] [blame] | 242 | |
| 243 | if (mounted_files_.count(id)) |
| 244 | return FILE_ERROR_INVALID_OPERATION; |
| 245 | |
| 246 | // Ensure the file is readable to cros_disks. See crbug.com/236994. |
| 247 | base::FilePath path = GetCacheFilePath(id); |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 248 | if (!base::SetPosixFilePermissions( |
[email protected] | c3f6564 | 2013-08-28 02:04:33 | [diff] [blame] | 249 | path, |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 250 | base::FILE_PERMISSION_READ_BY_USER | |
| 251 | base::FILE_PERMISSION_WRITE_BY_USER | |
| 252 | base::FILE_PERMISSION_READ_BY_GROUP | |
| 253 | base::FILE_PERMISSION_READ_BY_OTHERS)) |
[email protected] | c3f6564 | 2013-08-28 02:04:33 | [diff] [blame] | 254 | return FILE_ERROR_FAILED; |
| 255 | |
| 256 | mounted_files_.insert(id); |
| 257 | |
| 258 | *cache_file_path = path; |
| 259 | return FILE_ERROR_OK; |
| 260 | } |
| 261 | |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 262 | FileError FileCache::OpenForWrite( |
| 263 | const std::string& id, |
| 264 | scoped_ptr<base::ScopedClosureRunner>* file_closer) { |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 265 | AssertOnSequencedWorkerPool(); |
| 266 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 267 | // Marking a file dirty means its entry and actual file blob must exist in |
| 268 | // cache. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 269 | ResourceEntry entry; |
| 270 | FileError error = storage_->GetEntry(id, &entry); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 271 | if (error != FILE_ERROR_OK) |
| 272 | return error; |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 273 | if (!entry.file_specific_info().cache_state().is_present()) { |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 274 | LOG(WARNING) << "Can't mark dirty a file that wasn't cached: " << id; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 275 | return FILE_ERROR_NOT_FOUND; |
| 276 | } |
| 277 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 278 | entry.mutable_file_specific_info()->mutable_cache_state()->set_is_dirty(true); |
| 279 | entry.mutable_file_specific_info()->mutable_cache_state()->clear_md5(); |
| 280 | error = storage_->PutEntry(entry); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 281 | if (error != FILE_ERROR_OK) |
| 282 | return error; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 283 | |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 284 | write_opened_files_[id]++; |
| 285 | file_closer->reset(new base::ScopedClosureRunner( |
[email protected] | c929c93 | 2014-07-23 06:06:05 | [diff] [blame] | 286 | base::Bind(&google_apis::RunTaskWithTaskRunner, |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 287 | blocking_task_runner_, |
| 288 | base::Bind(&FileCache::CloseForWrite, |
| 289 | weak_ptr_factory_.GetWeakPtr(), |
| 290 | id)))); |
| 291 | return FILE_ERROR_OK; |
| 292 | } |
| 293 | |
| 294 | bool FileCache::IsOpenedForWrite(const std::string& id) { |
| 295 | AssertOnSequencedWorkerPool(); |
| 296 | return write_opened_files_.count(id); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 297 | } |
| 298 | |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 299 | FileError FileCache::UpdateMd5(const std::string& id) { |
[email protected] | fcf8eafe0 | 2013-05-28 11:15:39 | [diff] [blame] | 300 | AssertOnSequencedWorkerPool(); |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 301 | |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 302 | if (IsOpenedForWrite(id)) |
| 303 | return FILE_ERROR_IN_USE; |
| 304 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 305 | ResourceEntry entry; |
| 306 | FileError error = storage_->GetEntry(id, &entry); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 307 | if (error != FILE_ERROR_OK) |
| 308 | return error; |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 309 | if (!entry.file_specific_info().cache_state().is_present()) |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 310 | return FILE_ERROR_NOT_FOUND; |
| 311 | |
hashimoto | 246e4a8 | 2015-04-17 07:44:49 | [diff] [blame] | 312 | const std::string& md5 = |
| 313 | util::GetMd5Digest(GetCacheFilePath(id), &in_shutdown_); |
| 314 | if (in_shutdown_.IsSet()) |
| 315 | return FILE_ERROR_ABORT; |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 316 | if (md5.empty()) |
| 317 | return FILE_ERROR_NOT_FOUND; |
| 318 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 319 | entry.mutable_file_specific_info()->mutable_cache_state()->set_md5(md5); |
| 320 | return storage_->PutEntry(entry); |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | FileError FileCache::ClearDirty(const std::string& id) { |
| 324 | AssertOnSequencedWorkerPool(); |
| 325 | |
| 326 | if (IsOpenedForWrite(id)) |
| 327 | return FILE_ERROR_IN_USE; |
| 328 | |
[email protected] | fcf8eafe0 | 2013-05-28 11:15:39 | [diff] [blame] | 329 | // Clearing a dirty file means its entry and actual file blob must exist in |
| 330 | // cache. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 331 | ResourceEntry entry; |
| 332 | FileError error = storage_->GetEntry(id, &entry); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 333 | if (error != FILE_ERROR_OK) |
| 334 | return error; |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 335 | if (!entry.file_specific_info().cache_state().is_present()) { |
[email protected] | fcf8eafe0 | 2013-05-28 11:15:39 | [diff] [blame] | 336 | LOG(WARNING) << "Can't clear dirty state of a file that wasn't cached: " |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 337 | << id; |
[email protected] | fcf8eafe0 | 2013-05-28 11:15:39 | [diff] [blame] | 338 | return FILE_ERROR_NOT_FOUND; |
| 339 | } |
| 340 | |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 341 | // If a file is not dirty (it should have been marked dirty via OpenForWrite), |
| 342 | // clearing its dirty state is an invalid operation. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 343 | if (!entry.file_specific_info().cache_state().is_dirty()) { |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 344 | LOG(WARNING) << "Can't clear dirty state of a non-dirty file: " << id; |
[email protected] | fcf8eafe0 | 2013-05-28 11:15:39 | [diff] [blame] | 345 | return FILE_ERROR_INVALID_OPERATION; |
| 346 | } |
| 347 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 348 | entry.mutable_file_specific_info()->mutable_cache_state()->set_is_dirty( |
| 349 | false); |
| 350 | return storage_->PutEntry(entry); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 351 | } |
| 352 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 353 | FileError FileCache::Remove(const std::string& id) { |
[email protected] | 3361a54 | 2013-05-22 17:38:27 | [diff] [blame] | 354 | AssertOnSequencedWorkerPool(); |
| 355 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 356 | ResourceEntry entry; |
[email protected] | 3361a54 | 2013-05-22 17:38:27 | [diff] [blame] | 357 | |
[email protected] | 4b60a25f | 2013-06-17 09:43:11 | [diff] [blame] | 358 | // If entry doesn't exist, nothing to do. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 359 | FileError error = storage_->GetEntry(id, &entry); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 360 | if (error == FILE_ERROR_NOT_FOUND) |
[email protected] | 3361a54 | 2013-05-22 17:38:27 | [diff] [blame] | 361 | return FILE_ERROR_OK; |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 362 | if (error != FILE_ERROR_OK) |
| 363 | return error; |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 364 | if (!entry.file_specific_info().has_cache_state()) |
| 365 | return FILE_ERROR_OK; |
[email protected] | 3361a54 | 2013-05-22 17:38:27 | [diff] [blame] | 366 | |
[email protected] | d1ad8fa | 2013-07-11 13:23:20 | [diff] [blame] | 367 | // Cannot delete a mounted file. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 368 | if (mounted_files_.count(id)) |
[email protected] | 4b60a25f | 2013-06-17 09:43:11 | [diff] [blame] | 369 | return FILE_ERROR_IN_USE; |
| 370 | |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 371 | // Delete the file. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 372 | base::FilePath path = GetCacheFilePath(id); |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 373 | if (!base::DeleteFile(path, false /* recursive */)) |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 374 | return FILE_ERROR_FAILED; |
[email protected] | 3361a54 | 2013-05-22 17:38:27 | [diff] [blame] | 375 | |
| 376 | // Now that all file operations have completed, remove from metadata. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 377 | entry.mutable_file_specific_info()->clear_cache_state(); |
| 378 | return storage_->PutEntry(entry); |
[email protected] | 3361a54 | 2013-05-22 17:38:27 | [diff] [blame] | 379 | } |
| 380 | |
[email protected] | 823ca971 | 2013-09-13 10:09:09 | [diff] [blame] | 381 | bool FileCache::ClearAll() { |
| 382 | AssertOnSequencedWorkerPool(); |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 383 | |
[email protected] | 823ca971 | 2013-09-13 10:09:09 | [diff] [blame] | 384 | // Remove files. |
| 385 | base::FileEnumerator enumerator(cache_file_directory_, |
| 386 | false, // not recursive |
| 387 | base::FileEnumerator::FILES); |
| 388 | for (base::FilePath file = enumerator.Next(); !file.empty(); |
| 389 | file = enumerator.Next()) |
| 390 | base::DeleteFile(file, false /* recursive */); |
| 391 | |
| 392 | return true; |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 393 | } |
| 394 | |
[email protected] | 34a1bbf3 | 2013-06-17 07:24:02 | [diff] [blame] | 395 | bool FileCache::Initialize() { |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 396 | AssertOnSequencedWorkerPool(); |
| 397 | |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 398 | // Older versions do not clear MD5 when marking entries dirty. |
| 399 | // Clear MD5 of all dirty entries to deal with old data. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 400 | scoped_ptr<ResourceMetadataStorage::Iterator> it = storage_->GetIterator(); |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 401 | for (; !it->IsAtEnd(); it->Advance()) { |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 402 | if (it->GetValue().file_specific_info().cache_state().is_dirty()) { |
| 403 | ResourceEntry new_entry(it->GetValue()); |
| 404 | new_entry.mutable_file_specific_info()->mutable_cache_state()-> |
| 405 | clear_md5(); |
| 406 | if (storage_->PutEntry(new_entry) != FILE_ERROR_OK) |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 407 | return false; |
| 408 | } |
| 409 | } |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 410 | if (it->HasError()) |
| 411 | return false; |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 412 | |
[email protected] | f2731d1 | 2013-10-22 03:23:15 | [diff] [blame] | 413 | if (!RenameCacheFilesToNewFormat()) |
| 414 | return false; |
[email protected] | e8842b19 | 2013-06-11 04:05:14 | [diff] [blame] | 415 | return true; |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 416 | } |
| 417 | |
[email protected] | 34a1bbf3 | 2013-06-17 07:24:02 | [diff] [blame] | 418 | void FileCache::Destroy() { |
lukasza | 037c10b1 | 2015-06-12 04:21:25 | [diff] [blame^] | 419 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 34a1bbf3 | 2013-06-17 07:24:02 | [diff] [blame] | 420 | |
hashimoto | 246e4a8 | 2015-04-17 07:44:49 | [diff] [blame] | 421 | in_shutdown_.Set(); |
| 422 | |
[email protected] | 34a1bbf3 | 2013-06-17 07:24:02 | [diff] [blame] | 423 | // Destroy myself on the blocking pool. |
| 424 | // Note that base::DeletePointer<> cannot be used as the destructor of this |
| 425 | // class is private. |
| 426 | blocking_task_runner_->PostTask( |
| 427 | FROM_HERE, |
| 428 | base::Bind(&FileCache::DestroyOnBlockingPool, base::Unretained(this))); |
| 429 | } |
| 430 | |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 431 | void FileCache::DestroyOnBlockingPool() { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 432 | AssertOnSequencedWorkerPool(); |
| 433 | delete this; |
| 434 | } |
| 435 | |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 436 | bool FileCache::RecoverFilesFromCacheDirectory( |
[email protected] | 760abc3 | 2013-11-01 05:13:01 | [diff] [blame] | 437 | const base::FilePath& dest_directory, |
[email protected] | 026d4a52 | 2013-11-05 14:22:18 | [diff] [blame] | 438 | const ResourceMetadataStorage::RecoveredCacheInfoMap& |
| 439 | recovered_cache_info) { |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 440 | int file_number = 1; |
| 441 | |
| 442 | base::FileEnumerator enumerator(cache_file_directory_, |
| 443 | false, // not recursive |
| 444 | base::FileEnumerator::FILES); |
| 445 | for (base::FilePath current = enumerator.Next(); !current.empty(); |
| 446 | current = enumerator.Next()) { |
| 447 | const std::string& id = GetIdFromPath(current); |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 448 | ResourceEntry entry; |
| 449 | FileError error = storage_->GetEntry(id, &entry); |
| 450 | if (error != FILE_ERROR_OK && error != FILE_ERROR_NOT_FOUND) |
| 451 | return false; |
| 452 | if (error == FILE_ERROR_OK && |
| 453 | entry.file_specific_info().cache_state().is_present()) { |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 454 | // This file is managed by FileCache, no need to recover it. |
| 455 | continue; |
| 456 | } |
| 457 | |
[email protected] | 760abc3 | 2013-11-01 05:13:01 | [diff] [blame] | 458 | // If a cache entry which is non-dirty and has matching MD5 is found in |
| 459 | // |recovered_cache_entries|, it means the current file is already uploaded |
| 460 | // to the server. Just delete it instead of recovering it. |
[email protected] | 026d4a52 | 2013-11-05 14:22:18 | [diff] [blame] | 461 | ResourceMetadataStorage::RecoveredCacheInfoMap::const_iterator it = |
| 462 | recovered_cache_info.find(id); |
| 463 | if (it != recovered_cache_info.end()) { |
| 464 | // Due to the DB corruption, cache info might be recovered from old |
| 465 | // revision. Perform MD5 check even when is_dirty is false just in case. |
| 466 | if (!it->second.is_dirty && |
hashimoto | 246e4a8 | 2015-04-17 07:44:49 | [diff] [blame] | 467 | it->second.md5 == util::GetMd5Digest(current, &in_shutdown_)) { |
[email protected] | 760abc3 | 2013-11-01 05:13:01 | [diff] [blame] | 468 | base::DeleteFile(current, false /* recursive */); |
| 469 | continue; |
| 470 | } |
| 471 | } |
| 472 | |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 473 | // Read file contents to sniff mime type. |
| 474 | std::vector<char> content(net::kMaxBytesToSniff); |
| 475 | const int read_result = |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 476 | base::ReadFile(current, &content[0], content.size()); |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 477 | if (read_result < 0) { |
| 478 | LOG(WARNING) << "Cannot read: " << current.value(); |
| 479 | return false; |
| 480 | } |
| 481 | if (read_result == 0) // Skip empty files. |
| 482 | continue; |
| 483 | |
[email protected] | 026d4a52 | 2013-11-05 14:22:18 | [diff] [blame] | 484 | // Use recovered file name if available, otherwise decide file name with |
| 485 | // sniffed mime type. |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 486 | base::FilePath dest_base_name(FILE_PATH_LITERAL("file")); |
| 487 | std::string mime_type; |
[email protected] | 026d4a52 | 2013-11-05 14:22:18 | [diff] [blame] | 488 | if (it != recovered_cache_info.end() && !it->second.title.empty()) { |
| 489 | // We can use a file name recovered from the trashed DB. |
| 490 | dest_base_name = base::FilePath::FromUTF8Unsafe(it->second.title); |
| 491 | } else if (net::SniffMimeType(&content[0], read_result, |
| 492 | net::FilePathToFileURL(current), |
| 493 | std::string(), &mime_type) || |
| 494 | net::SniffMimeTypeFromLocalData(&content[0], read_result, |
| 495 | &mime_type)) { |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 496 | // Change base name for common mime types. |
| 497 | if (net::MatchesMimeType("image/*", mime_type)) { |
| 498 | dest_base_name = base::FilePath(FILE_PATH_LITERAL("image")); |
| 499 | } else if (net::MatchesMimeType("video/*", mime_type)) { |
| 500 | dest_base_name = base::FilePath(FILE_PATH_LITERAL("video")); |
| 501 | } else if (net::MatchesMimeType("audio/*", mime_type)) { |
| 502 | dest_base_name = base::FilePath(FILE_PATH_LITERAL("audio")); |
| 503 | } |
| 504 | |
| 505 | // Estimate extension from mime type. |
| 506 | std::vector<base::FilePath::StringType> extensions; |
| 507 | base::FilePath::StringType extension; |
| 508 | if (net::GetPreferredExtensionForMimeType(mime_type, &extension)) |
| 509 | extensions.push_back(extension); |
| 510 | else |
| 511 | net::GetExtensionsForMimeType(mime_type, &extensions); |
| 512 | |
| 513 | // Add extension if possible. |
| 514 | if (!extensions.empty()) |
| 515 | dest_base_name = dest_base_name.AddExtension(extensions[0]); |
| 516 | } |
| 517 | |
| 518 | // Add file number to the file name and move. |
| 519 | const base::FilePath& dest_path = dest_directory.Append(dest_base_name) |
| 520 | .InsertBeforeExtensionASCII(base::StringPrintf("%08d", file_number++)); |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 521 | if (!base::CreateDirectory(dest_directory) || |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 522 | !base::Move(current, dest_path)) { |
| 523 | LOG(WARNING) << "Failed to move: " << current.value() |
| 524 | << " to " << dest_path.value(); |
| 525 | return false; |
| 526 | } |
| 527 | } |
[email protected] | d105b3ad | 2013-11-01 05:33:13 | [diff] [blame] | 528 | UMA_HISTOGRAM_COUNTS("Drive.NumberOfCacheFilesRecoveredAfterDBCorruption", |
| 529 | file_number - 1); |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 530 | return true; |
| 531 | } |
| 532 | |
[email protected] | 54ba3750 | 2013-05-09 08:43:40 | [diff] [blame] | 533 | FileError FileCache::MarkAsUnmounted(const base::FilePath& file_path) { |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 534 | AssertOnSequencedWorkerPool(); |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 535 | DCHECK(IsUnderFileCacheDirectory(file_path)); |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 536 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 537 | std::string id = GetIdFromPath(file_path); |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 538 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 539 | // Get the entry associated with the id. |
| 540 | ResourceEntry entry; |
| 541 | FileError error = storage_->GetEntry(id, &entry); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 542 | if (error != FILE_ERROR_OK) |
| 543 | return error; |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 544 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 545 | std::set<std::string>::iterator it = mounted_files_.find(id); |
[email protected] | 4b60a25f | 2013-06-17 09:43:11 | [diff] [blame] | 546 | if (it == mounted_files_.end()) |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 547 | return FILE_ERROR_INVALID_OPERATION; |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 548 | |
[email protected] | 4b60a25f | 2013-06-17 09:43:11 | [diff] [blame] | 549 | mounted_files_.erase(it); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 550 | return FILE_ERROR_OK; |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 551 | } |
| 552 | |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 553 | bool FileCache::HasEnoughSpaceFor(int64 num_bytes, |
| 554 | const base::FilePath& path) { |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 555 | int64 free_space = 0; |
| 556 | if (free_disk_space_getter_) |
| 557 | free_space = free_disk_space_getter_->AmountOfFreeDiskSpace(); |
| 558 | else |
| 559 | free_space = base::SysInfo::AmountOfFreeDiskSpace(path); |
| 560 | |
| 561 | // Subtract this as if this portion does not exist. |
[email protected] | ab3ed7e4a | 2013-12-12 09:18:30 | [diff] [blame] | 562 | free_space -= cryptohome::kMinFreeSpaceInBytes; |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 563 | return (free_space >= num_bytes); |
| 564 | } |
| 565 | |
[email protected] | f2731d1 | 2013-10-22 03:23:15 | [diff] [blame] | 566 | bool FileCache::RenameCacheFilesToNewFormat() { |
| 567 | base::FileEnumerator enumerator(cache_file_directory_, |
| 568 | false, // not recursive |
| 569 | base::FileEnumerator::FILES); |
| 570 | for (base::FilePath current = enumerator.Next(); !current.empty(); |
| 571 | current = enumerator.Next()) { |
| 572 | base::FilePath new_path = current.RemoveExtension(); |
| 573 | if (!new_path.Extension().empty()) { |
| 574 | // Delete files with multiple extensions. |
| 575 | if (!base::DeleteFile(current, false /* recursive */)) |
| 576 | return false; |
| 577 | continue; |
| 578 | } |
| 579 | const std::string& id = GetIdFromPath(new_path); |
| 580 | new_path = GetCacheFilePath(util::CanonicalizeResourceId(id)); |
| 581 | if (new_path != current && !base::Move(current, new_path)) |
| 582 | return false; |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 583 | } |
[email protected] | f2731d1 | 2013-10-22 03:23:15 | [diff] [blame] | 584 | return true; |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 585 | } |
| 586 | |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 587 | void FileCache::CloseForWrite(const std::string& id) { |
| 588 | AssertOnSequencedWorkerPool(); |
| 589 | |
| 590 | std::map<std::string, int>::iterator it = write_opened_files_.find(id); |
| 591 | if (it == write_opened_files_.end()) |
| 592 | return; |
| 593 | |
| 594 | DCHECK_LT(0, it->second); |
| 595 | --it->second; |
| 596 | if (it->second == 0) |
| 597 | write_opened_files_.erase(it); |
[email protected] | f92367ae | 2014-06-02 07:35:43 | [diff] [blame] | 598 | |
| 599 | // Update last modified date. |
| 600 | ResourceEntry entry; |
| 601 | FileError error = storage_->GetEntry(id, &entry); |
| 602 | if (error != FILE_ERROR_OK) { |
| 603 | LOG(ERROR) << "Failed to get entry: " << id << ", " |
| 604 | << FileErrorToString(error); |
| 605 | return; |
| 606 | } |
| 607 | entry.mutable_file_info()->set_last_modified( |
| 608 | base::Time::Now().ToInternalValue()); |
| 609 | error = storage_->PutEntry(entry); |
| 610 | if (error != FILE_ERROR_OK) { |
| 611 | LOG(ERROR) << "Failed to put entry: " << id << ", " |
| 612 | << FileErrorToString(error); |
| 613 | } |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 614 | } |
| 615 | |
[email protected] | 59c7cdec | 2013-05-07 04:17:13 | [diff] [blame] | 616 | } // namespace internal |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 617 | } // namespace drive |