[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 | |
yawano | 3513e14 | 2016-04-20 00:42:42 | [diff] [blame] | 5 | #include "components/drive/chromeos/file_cache.h" |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 6 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 7 | #include <linux/fs.h> |
| 8 | #include <sys/ioctl.h> |
| 9 | #include <sys/xattr.h> |
yawano | 1c325bf | 2016-04-20 06:37:03 | [diff] [blame] | 10 | |
Stuart Langley | be403574 | 2018-05-10 05:32:27 | [diff] [blame] | 11 | #include <memory> |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 12 | #include <queue> |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
lukasza | 037c10b1 | 2015-06-12 04:21:25 | [diff] [blame] | 15 | #include "base/bind.h" |
| 16 | #include "base/bind_helpers.h" |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 17 | #include "base/callback_helpers.h" |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 18 | #include "base/files/file.h" |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 19 | #include "base/files/file_enumerator.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 20 | #include "base/files/file_util.h" |
lukasza | 037c10b1 | 2015-06-12 04:21:25 | [diff] [blame] | 21 | #include "base/location.h" |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 22 | #include "base/logging.h" |
asvitkine | 776f9db | 2017-01-25 21:39:29 | [diff] [blame] | 23 | #include "base/metrics/histogram_macros.h" |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 24 | #include "base/stl_util.h" |
[email protected] | 5c07332 | 2013-06-11 08:03:30 | [diff] [blame] | 25 | #include "base/strings/string_util.h" |
| 26 | #include "base/strings/stringprintf.h" |
Sebastien Marchand | 75a7cdf | 2018-11-13 23:47:03 | [diff] [blame] | 27 | #include "base/system/sys_info.h" |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 28 | #include "build/build_config.h" |
lukasza | 01b9d55a | 2015-07-21 15:19:25 | [diff] [blame] | 29 | #include "components/drive/drive.pb.h" |
lukasza | 8acc4eb | 2015-07-20 20:57:20 | [diff] [blame] | 30 | #include "components/drive/drive_api_util.h" |
lukasza | 6364a02 | 2015-08-21 01:13:24 | [diff] [blame] | 31 | #include "components/drive/file_system_core_util.h" |
| 32 | #include "components/drive/resource_metadata_storage.h" |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 33 | #include "google_apis/drive/task_util.h" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 34 | #include "net/base/filename_util.h" |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 35 | #include "net/base/mime_sniffer.h" |
| 36 | #include "net/base/mime_util.h" |
[email protected] | 7986b8d | 2012-06-14 15:05:14 | [diff] [blame] | 37 | |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 38 | namespace drive { |
[email protected] | 59c7cdec | 2013-05-07 04:17:13 | [diff] [blame] | 39 | namespace internal { |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 40 | namespace { |
| 41 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 42 | typedef std::pair<base::File::Info, ResourceEntry> CacheInfo; |
| 43 | typedef long FileAttributes; // NOLINT(runtime/int) |
| 44 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 45 | // Returns ID extracted from the path. |
| 46 | std::string GetIdFromPath(const base::FilePath& path) { |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 47 | return util::UnescapeCacheFileName(path.BaseName().AsUTF8Unsafe()); |
| 48 | } |
| 49 | |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 50 | base::FilePath GetPathForId(const base::FilePath& cache_directory, |
| 51 | const std::string& id) { |
| 52 | return cache_directory.Append( |
| 53 | base::FilePath::FromUTF8Unsafe(util::EscapeCacheFileName(id))); |
| 54 | } |
| 55 | |
oka | 7bd26bbf | 2016-10-14 06:59:06 | [diff] [blame] | 56 | // Returns if the filesystem backing |path| supports file attributes. |
| 57 | // This will return false if the filesystem is for example tmpfs, which is used |
| 58 | // for ephemeral mode. |
| 59 | bool IsFileAttributesSupported(const base::FilePath& path) { |
| 60 | if (getxattr(path.value().c_str(), "user.foo", nullptr, 0) >= 0) { |
| 61 | return true; |
| 62 | } |
| 63 | return errno != ENOTSUP; |
| 64 | } |
| 65 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 66 | // Sets extended file attribute as |name| |value| pair. |
| 67 | bool SetExtendedFileAttributes(const base::FilePath& path, |
| 68 | const std::string& name, const std::string& value) { |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 69 | if (setxattr(path.value().c_str(), name.c_str(), value.c_str(), |
| 70 | value.size() + 1, 0) != 0) { |
| 71 | PLOG(ERROR) << "setxattr: " << path.value(); |
| 72 | return false; |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | // Remove extended file attribute with |name|. |
| 78 | bool UnsetExtendedFileAttributes(const base::FilePath& path, |
| 79 | const std::string& name) { |
| 80 | if (removexattr(path.value().c_str(), name.c_str()) != 0) { |
Sergei Datsenko | 50132dc | 2018-09-11 04:27:22 | [diff] [blame] | 81 | PLOG_IF(ERROR, errno != ENODATA) << "removexattr: " << path.value(); |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | return true; |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Changes attributes of the file with |flags|, e.g. FS_NODUMP_FL (cryptohome |
| 88 | // will remove Drive caches with this attribute). |
| 89 | // See linux/fs.h for available flags, and chattr(1) which does similar thing. |
| 90 | // Returns whether operation succeeded. |
| 91 | bool SetFileAttributes(const base::FilePath& path, FileAttributes flags) { |
| 92 | base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 93 | if (!file.IsValid()) { |
| 94 | PLOG(ERROR) << "Failed to open file: " << path.value(); |
| 95 | return false; |
| 96 | } |
| 97 | if (ioctl(file.GetPlatformFile(), FS_IOC_SETFLAGS, &flags) < 0) { |
| 98 | PLOG(ERROR) << "ioctl: " << path.value(); |
| 99 | return false; |
| 100 | } |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | // Gets file attributes similarly to lsattr(1). Returns flags or -1 on error. |
| 105 | // See linux/fs.h for the definition of the returned flags e.g. FS_NODUMP_FL. |
| 106 | FileAttributes GetFileAttributes(const base::FilePath& path) { |
| 107 | base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 108 | if (!file.IsValid()) { |
| 109 | PLOG(ERROR) << "Failed to open file: " << path.value(); |
| 110 | return -1; |
| 111 | } |
| 112 | FileAttributes flags = 0; |
| 113 | if (ioctl(file.GetPlatformFile(), FS_IOC_GETFLAGS, &flags) < 0) { |
| 114 | PLOG(ERROR) << "ioctl: " << path.value(); |
| 115 | return -1; |
| 116 | } |
| 117 | return flags; |
| 118 | } |
| 119 | |
oka | 7bd26bbf | 2016-10-14 06:59:06 | [diff] [blame] | 120 | // Marks the cache file to be removable by cryptohome, or do nothing if |
| 121 | // underlying filesystem doesn't support file attributes, as tmpfs for ephemeral |
| 122 | // mode. |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 123 | bool SetRemovable(const base::FilePath& path) { |
oka | 7bd26bbf | 2016-10-14 06:59:06 | [diff] [blame] | 124 | // For ephemeral mode. |
| 125 | if (!IsFileAttributesSupported(path)) { |
| 126 | return true; |
| 127 | } |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 128 | FileAttributes flags = GetFileAttributes(path); |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 129 | bool xattr = flags >= 0 && SetFileAttributes(path, flags | FS_NODUMP_FL); |
| 130 | bool fattr = SetExtendedFileAttributes( |
| 131 | path, FileCache::kGCacheRemovableAttribute, "1"); |
| 132 | return xattr || fattr; |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 133 | } |
| 134 | |
oka | 7bd26bbf | 2016-10-14 06:59:06 | [diff] [blame] | 135 | // Marks the cache file to be unremovable by cryptohome, or do nothing if |
| 136 | // underlying filesystem doesn't support file attributes, as tmpfs for ephemeral |
| 137 | // mode. |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 138 | bool UnsetRemovable(const base::FilePath& path) { |
oka | 7bd26bbf | 2016-10-14 06:59:06 | [diff] [blame] | 139 | // For ephemeral mode. |
| 140 | if (!IsFileAttributesSupported(path)) { |
| 141 | return true; |
| 142 | } |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 143 | FileAttributes flags = GetFileAttributes(path); |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 144 | bool xattr = flags >= 0 && SetFileAttributes(path, flags & ~FS_NODUMP_FL); |
| 145 | bool fattr = |
| 146 | UnsetExtendedFileAttributes(path, FileCache::kGCacheRemovableAttribute); |
| 147 | return xattr || fattr; |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 148 | } |
| 149 | |
oka | 7bd26bbf | 2016-10-14 06:59:06 | [diff] [blame] | 150 | // Marks |path| as drive cache dir, or do nothing if underlying filesystem |
| 151 | // doesn't support file attributes, as tmpfs for ephemeral mode. Returns if the |
| 152 | // operation succeeded. |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 153 | bool MarkAsDriveCacheDir(const base::FilePath& path) { |
oka | 7bd26bbf | 2016-10-14 06:59:06 | [diff] [blame] | 154 | // For ephemeral mode. |
| 155 | if (!IsFileAttributesSupported(path)) { |
| 156 | return true; |
| 157 | } |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 158 | return SetRemovable(path) |
| 159 | && SetExtendedFileAttributes(path, FileCache::kGCacheFilesAttribute, ""); |
| 160 | } |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 161 | |
| 162 | class CacheInfoLatestCompare { |
| 163 | public: |
| 164 | bool operator()(const CacheInfo& info_a, const CacheInfo& info_b) { |
| 165 | return info_a.first.last_accessed < info_b.first.last_accessed; |
| 166 | } |
| 167 | }; |
| 168 | |
fukino | 6380c07c | 2016-06-09 07:28:29 | [diff] [blame] | 169 | // Returns true if the cache file is present. |
| 170 | bool IsPresent(const ResourceEntry& entry) { |
| 171 | return entry.has_file_specific_info() && |
| 172 | entry.file_specific_info().has_cache_state() && |
| 173 | entry.file_specific_info().cache_state().is_present(); |
| 174 | } |
| 175 | |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 176 | const size_t kMaxNumOfEvictedCacheFiles = 30000; |
| 177 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 178 | } // namespace |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 179 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 180 | // static |
| 181 | const char FileCache::kGCacheFilesAttribute[] = "user.GCacheFiles"; |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 182 | const char FileCache::kGCacheRemovableAttribute[] = "user.GCacheRemovable"; |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 183 | |
[email protected] | 2df61e1 | 2013-06-21 16:00:09 | [diff] [blame] | 184 | FileCache::FileCache(ResourceMetadataStorage* storage, |
[email protected] | e07f7b7b | 2013-06-19 03:43:12 | [diff] [blame] | 185 | const base::FilePath& cache_file_directory, |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 186 | base::SequencedTaskRunner* blocking_task_runner, |
| 187 | FreeDiskSpaceGetterInterface* free_disk_space_getter) |
[email protected] | 2df61e1 | 2013-06-21 16:00:09 | [diff] [blame] | 188 | : cache_file_directory_(cache_file_directory), |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 189 | blocking_task_runner_(blocking_task_runner), |
[email protected] | 2df61e1 | 2013-06-21 16:00:09 | [diff] [blame] | 190 | storage_(storage), |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 191 | free_disk_space_getter_(free_disk_space_getter), |
Jeremy Roman | 47d432e | 2019-08-20 14:24:00 | [diff] [blame] | 192 | max_num_of_evicted_cache_files_(kMaxNumOfEvictedCacheFiles) { |
[email protected] | 144b6c4 | 2013-06-14 07:30:38 | [diff] [blame] | 193 | DCHECK(blocking_task_runner_.get()); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 194 | } |
| 195 | |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 196 | FileCache::~FileCache() { |
[email protected] | 17196ee | 2012-12-13 06:23:51 | [diff] [blame] | 197 | // Must be on the sequenced worker pool, as |metadata_| must be deleted on |
| 198 | // the sequenced worker pool. |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 199 | AssertOnSequencedWorkerPool(); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 200 | } |
| 201 | |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 202 | void FileCache::SetMaxNumOfEvictedCacheFilesForTest( |
| 203 | size_t max_num_of_evicted_cache_files) { |
| 204 | max_num_of_evicted_cache_files_ = max_num_of_evicted_cache_files; |
| 205 | } |
| 206 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 207 | base::FilePath FileCache::GetCacheFilePath(const std::string& id) const { |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 208 | return GetPathForId(cache_file_directory_, id); |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 209 | } |
| 210 | |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 211 | void FileCache::AssertOnSequencedWorkerPool() { |
peary2 | ac76448 | 2017-06-25 14:39:53 | [diff] [blame] | 212 | DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); |
[email protected] | fcc92a5 | 2012-06-08 22:54:16 | [diff] [blame] | 213 | } |
| 214 | |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 215 | bool FileCache::IsUnderFileCacheDirectory(const base::FilePath& path) const { |
[email protected] | e07f7b7b | 2013-06-19 03:43:12 | [diff] [blame] | 216 | return cache_file_directory_.IsParent(path); |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 217 | } |
| 218 | |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 219 | bool FileCache::FreeDiskSpaceIfNeededFor(int64_t num_bytes) { |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 220 | AssertOnSequencedWorkerPool(); |
| 221 | |
| 222 | // Do nothing and return if we have enough space. |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 223 | if (GetAvailableSpace() >= num_bytes) |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 224 | return true; |
| 225 | |
| 226 | // Otherwise, try to free up the disk space. |
| 227 | DVLOG(1) << "Freeing up disk space for " << num_bytes; |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 228 | |
[email protected] | 9d14758 | 2013-06-14 06:25:45 | [diff] [blame] | 229 | // Remove all files which have no corresponding cache entries. |
[email protected] | e07f7b7b | 2013-06-19 03:43:12 | [diff] [blame] | 230 | base::FileEnumerator enumerator(cache_file_directory_, |
[email protected] | 9d14758 | 2013-06-14 06:25:45 | [diff] [blame] | 231 | false, // not recursive |
| 232 | base::FileEnumerator::FILES); |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 233 | ResourceEntry entry; |
[email protected] | 9d14758 | 2013-06-14 06:25:45 | [diff] [blame] | 234 | for (base::FilePath current = enumerator.Next(); !current.empty(); |
| 235 | current = enumerator.Next()) { |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 236 | const std::string id = GetIdFromPath(current); |
| 237 | const FileError error = storage_->GetEntry(id, &entry); |
| 238 | |
| 239 | if (error == FILE_ERROR_NOT_FOUND) |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 240 | base::DeleteFile(current, false /* recursive */); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 241 | else if (error != FILE_ERROR_OK) |
| 242 | return false; |
[email protected] | 9d14758 | 2013-06-14 06:25:45 | [diff] [blame] | 243 | } |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 244 | |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 245 | // Check available space again. If we have enough space here, do nothing. |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 246 | const int64_t available_space = GetAvailableSpace(); |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 247 | if (available_space >= num_bytes) |
| 248 | return true; |
| 249 | |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 250 | const int64_t requested_space = num_bytes - available_space; |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 251 | |
| 252 | // Put all entries in priority queue where latest entry becomes top. |
| 253 | std::priority_queue<CacheInfo, std::vector<CacheInfo>, CacheInfoLatestCompare> |
| 254 | cache_info_queue; |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 255 | std::unique_ptr<ResourceMetadataStorage::Iterator> it = |
| 256 | storage_->GetIterator(); |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 257 | for (; !it->IsAtEnd(); it->Advance()) { |
| 258 | if (IsEvictable(it->GetID(), it->GetValue())) { |
| 259 | const ResourceEntry& entry = it->GetValue(); |
| 260 | |
| 261 | const base::FilePath& cache_path = GetCacheFilePath(entry.local_id()); |
| 262 | base::File::Info info; |
| 263 | // If it fails to get file info of |cache_path|, use default value as its |
| 264 | // file info. i.e. the file becomes least recently used one. |
| 265 | base::GetFileInfo(cache_path, &info); |
| 266 | |
| 267 | CacheInfo cache_info = std::make_pair(info, entry); |
| 268 | |
| 269 | if (cache_info_queue.size() < max_num_of_evicted_cache_files_) { |
| 270 | cache_info_queue.push(cache_info); |
| 271 | } else if (cache_info_queue.size() >= max_num_of_evicted_cache_files_ && |
| 272 | cache_info.first.last_accessed < |
| 273 | cache_info_queue.top().first.last_accessed) { |
| 274 | // Do not enqueue more than max_num_of_evicted_cache_files_ not to use |
| 275 | // up memory with this queue. |
| 276 | cache_info_queue.pop(); |
| 277 | cache_info_queue.push(cache_info); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | if (it->HasError()) |
| 282 | return false; |
| 283 | |
| 284 | // Copy entries to the vector. This becomes last-accessed desc order. |
| 285 | std::vector<CacheInfo> cache_info_list; |
| 286 | while (!cache_info_queue.empty()) { |
| 287 | cache_info_list.push_back(cache_info_queue.top()); |
| 288 | cache_info_queue.pop(); |
| 289 | } |
| 290 | |
| 291 | // Update DB and delete files with accessing to the vector in ascending order. |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 292 | int64_t evicted_cache_size = 0; |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 293 | auto iter = cache_info_list.rbegin(); |
| 294 | while (evicted_cache_size < requested_space && |
| 295 | iter != cache_info_list.rend()) { |
| 296 | const CacheInfo& cache_info = *iter; |
| 297 | |
| 298 | // Update DB. |
| 299 | ResourceEntry entry = cache_info.second; |
| 300 | entry.mutable_file_specific_info()->clear_cache_state(); |
| 301 | storage_->PutEntry(entry); |
| 302 | |
| 303 | // Delete cache file. |
| 304 | const base::FilePath& path = GetCacheFilePath(entry.local_id()); |
| 305 | |
| 306 | if (base::DeleteFile(path, false /* recursive */)) |
| 307 | evicted_cache_size += cache_info.first.size; |
| 308 | |
| 309 | ++iter; |
| 310 | } |
| 311 | |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 312 | // Check the disk space again. |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 313 | return GetAvailableSpace() >= num_bytes; |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 314 | } |
| 315 | |
fukino | 6380c07c | 2016-06-09 07:28:29 | [diff] [blame] | 316 | int64_t FileCache::CalculateCacheSize() { |
yawano | 8578abf | 2015-08-26 09:15:50 | [diff] [blame] | 317 | AssertOnSequencedWorkerPool(); |
| 318 | |
fukino | 6380c07c | 2016-06-09 07:28:29 | [diff] [blame] | 319 | int64_t total_cache_size = 0; |
| 320 | int64_t cache_size = 0; |
| 321 | |
| 322 | std::unique_ptr<ResourceMetadataStorage::Iterator> it = |
| 323 | storage_->GetIterator(); |
| 324 | for (; !it->IsAtEnd(); it->Advance()) { |
| 325 | if (IsPresent(it->GetValue()) && |
| 326 | base::GetFileSize(GetCacheFilePath(it->GetID()), &cache_size)) { |
| 327 | DCHECK_GE(cache_size, 0); |
| 328 | total_cache_size += cache_size; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | if (it->HasError()) |
| 333 | return 0; |
| 334 | |
| 335 | return total_cache_size; |
| 336 | } |
| 337 | |
| 338 | int64_t FileCache::CalculateEvictableCacheSize() { |
| 339 | AssertOnSequencedWorkerPool(); |
| 340 | |
| 341 | int64_t evictable_cache_size = 0; |
battre | a5f64bd | 2015-08-26 10:47:41 | [diff] [blame] | 342 | int64_t cache_size = 0; |
yawano | 8578abf | 2015-08-26 09:15:50 | [diff] [blame] | 343 | |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 344 | std::unique_ptr<ResourceMetadataStorage::Iterator> it = |
| 345 | storage_->GetIterator(); |
yawano | 8578abf | 2015-08-26 09:15:50 | [diff] [blame] | 346 | for (; !it->IsAtEnd(); it->Advance()) { |
| 347 | if (IsEvictable(it->GetID(), it->GetValue()) && |
| 348 | base::GetFileSize(GetCacheFilePath(it->GetID()), &cache_size)) { |
| 349 | DCHECK_GE(cache_size, 0); |
| 350 | evictable_cache_size += cache_size; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | if (it->HasError()) |
| 355 | return 0; |
| 356 | |
| 357 | return evictable_cache_size; |
| 358 | } |
| 359 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 360 | FileError FileCache::GetFile(const std::string& id, |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 361 | base::FilePath* cache_file_path) { |
| 362 | AssertOnSequencedWorkerPool(); |
| 363 | DCHECK(cache_file_path); |
| 364 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 365 | ResourceEntry entry; |
| 366 | FileError error = storage_->GetEntry(id, &entry); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 367 | if (error != FILE_ERROR_OK) |
| 368 | return error; |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 369 | if (!entry.file_specific_info().cache_state().is_present()) |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 370 | return FILE_ERROR_NOT_FOUND; |
| 371 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 372 | *cache_file_path = GetCacheFilePath(id); |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 373 | return FILE_ERROR_OK; |
| 374 | } |
| 375 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 376 | FileError FileCache::Store(const std::string& id, |
[email protected] | 82c4eb9 | 2013-05-21 11:25:23 | [diff] [blame] | 377 | const std::string& md5, |
| 378 | const base::FilePath& source_path, |
| 379 | FileOperationType file_operation_type) { |
| 380 | AssertOnSequencedWorkerPool(); |
[email protected] | d8546c9 | 2013-05-02 05:09:59 | [diff] [blame] | 381 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 382 | ResourceEntry entry; |
| 383 | FileError error = storage_->GetEntry(id, &entry); |
| 384 | if (error != FILE_ERROR_OK) |
| 385 | return error; |
| 386 | |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 387 | int64_t file_size = 0; |
[email protected] | 8e37b9b | 2013-12-11 09:06:02 | [diff] [blame] | 388 | if (file_operation_type == FILE_OPERATION_COPY) { |
| 389 | if (!base::GetFileSize(source_path, &file_size)) { |
| 390 | LOG(WARNING) << "Couldn't get file size for: " << source_path.value(); |
| 391 | return FILE_ERROR_FAILED; |
| 392 | } |
| 393 | } |
| 394 | if (!FreeDiskSpaceIfNeededFor(file_size)) |
| 395 | return FILE_ERROR_NO_LOCAL_SPACE; |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 396 | |
[email protected] | 1d0786a | 2014-02-06 12:37:08 | [diff] [blame] | 397 | // If file is mounted, return error. |
| 398 | if (mounted_files_.count(id)) |
[email protected] | 8e37b9b | 2013-12-11 09:06:02 | [diff] [blame] | 399 | return FILE_ERROR_IN_USE; |
| 400 | |
| 401 | base::FilePath dest_path = GetCacheFilePath(id); |
| 402 | bool success = false; |
| 403 | switch (file_operation_type) { |
| 404 | case FILE_OPERATION_MOVE: |
| 405 | success = base::Move(source_path, dest_path); |
| 406 | break; |
| 407 | case FILE_OPERATION_COPY: |
| 408 | success = base::CopyFile(source_path, dest_path); |
| 409 | break; |
| 410 | default: |
| 411 | NOTREACHED(); |
| 412 | } |
| 413 | |
| 414 | if (!success) { |
| 415 | LOG(ERROR) << "Failed to store: " |
| 416 | << "source_path = " << source_path.value() << ", " |
| 417 | << "dest_path = " << dest_path.value() << ", " |
| 418 | << "file_operation_type = " << file_operation_type; |
| 419 | return FILE_ERROR_FAILED; |
| 420 | } |
| 421 | |
| 422 | // Now that file operations have completed, update metadata. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 423 | FileCacheEntry* cache_state = |
| 424 | entry.mutable_file_specific_info()->mutable_cache_state(); |
| 425 | cache_state->set_md5(md5); |
| 426 | cache_state->set_is_present(true); |
[email protected] | bae99ae5 | 2014-01-29 01:13:14 | [diff] [blame] | 427 | if (md5.empty()) |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 428 | cache_state->set_is_dirty(true); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 429 | |
| 430 | if (!cache_state->is_pinned() && !cache_state->is_dirty()) { |
| 431 | if (!SetRemovable(dest_path)) |
| 432 | return FILE_ERROR_FAILED; |
| 433 | } else { |
| 434 | if (!UnsetRemovable(dest_path)) |
| 435 | return FILE_ERROR_FAILED; |
| 436 | } |
| 437 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 438 | return storage_->PutEntry(entry); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 439 | } |
| 440 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 441 | FileError FileCache::Pin(const std::string& id) { |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 442 | AssertOnSequencedWorkerPool(); |
| 443 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 444 | ResourceEntry entry; |
| 445 | FileError error = storage_->GetEntry(id, &entry); |
| 446 | if (error != FILE_ERROR_OK) |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 447 | return error; |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 448 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 449 | entry.mutable_file_specific_info()->mutable_cache_state()->set_is_pinned( |
| 450 | true); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 451 | |
| 452 | base::FilePath file_path = GetCacheFilePath(entry.local_id()); |
| 453 | // Cache file can be created later. |
| 454 | if (entry.file_specific_info().cache_state().is_present()) { |
| 455 | if (!UnsetRemovable(file_path)) |
| 456 | return FILE_ERROR_FAILED; |
| 457 | } |
| 458 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 459 | return storage_->PutEntry(entry); |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 460 | } |
| 461 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 462 | FileError FileCache::Unpin(const std::string& id) { |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 463 | AssertOnSequencedWorkerPool(); |
| 464 | |
| 465 | // Unpinning a file means its entry must exist in cache. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 466 | ResourceEntry entry; |
| 467 | FileError error = storage_->GetEntry(id, &entry); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 468 | if (error != FILE_ERROR_OK) |
| 469 | return error; |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 470 | |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 471 | // Now that file operations have completed, update metadata. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 472 | if (entry.file_specific_info().cache_state().is_present()) { |
| 473 | entry.mutable_file_specific_info()->mutable_cache_state()->set_is_pinned( |
| 474 | false); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 475 | if (!entry.file_specific_info().cache_state().is_dirty()) { |
| 476 | if (!SetRemovable(GetCacheFilePath(entry.local_id()))) |
| 477 | return FILE_ERROR_FAILED; |
| 478 | } |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 479 | } else { |
| 480 | // Remove the existing entry if we are unpinning a non-present file. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 481 | entry.mutable_file_specific_info()->clear_cache_state(); |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 482 | } |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 483 | error = storage_->PutEntry(entry); |
| 484 | if (error != FILE_ERROR_OK) |
| 485 | return error; |
[email protected] | bd2254d | 2013-06-12 16:00:47 | [diff] [blame] | 486 | |
[email protected] | 9d14758 | 2013-06-14 06:25:45 | [diff] [blame] | 487 | // Now it's a chance to free up space if needed. |
[email protected] | bd2254d | 2013-06-12 16:00:47 | [diff] [blame] | 488 | FreeDiskSpaceIfNeededFor(0); |
| 489 | |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 490 | return FILE_ERROR_OK; |
| 491 | } |
| 492 | |
Tatsuhisa Yamaguchi | 4810128 | 2018-03-20 02:18:39 | [diff] [blame] | 493 | bool FileCache::IsMarkedAsMounted(const std::string& id) { |
| 494 | AssertOnSequencedWorkerPool(); |
| 495 | return mounted_files_.count(id); |
| 496 | } |
| 497 | |
[email protected] | c3f6564 | 2013-08-28 02:04:33 | [diff] [blame] | 498 | FileError FileCache::MarkAsMounted(const std::string& id, |
| 499 | base::FilePath* cache_file_path) { |
| 500 | AssertOnSequencedWorkerPool(); |
| 501 | DCHECK(cache_file_path); |
| 502 | |
| 503 | // Get cache entry associated with the id and md5 |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 504 | ResourceEntry entry; |
| 505 | FileError error = storage_->GetEntry(id, &entry); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 506 | if (error != FILE_ERROR_OK) |
| 507 | return error; |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 508 | if (!entry.file_specific_info().cache_state().is_present()) |
| 509 | return FILE_ERROR_NOT_FOUND; |
[email protected] | c3f6564 | 2013-08-28 02:04:33 | [diff] [blame] | 510 | |
| 511 | if (mounted_files_.count(id)) |
| 512 | return FILE_ERROR_INVALID_OPERATION; |
| 513 | |
[email protected] | c3f6564 | 2013-08-28 02:04:33 | [diff] [blame] | 514 | base::FilePath path = GetCacheFilePath(id); |
lukasza | 6364a02 | 2015-08-21 01:13:24 | [diff] [blame] | 515 | |
| 516 | #if defined(OS_CHROMEOS) |
| 517 | // Ensure the file is readable to cros_disks. See crbug.com/236994. |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 518 | if (!base::SetPosixFilePermissions( |
[email protected] | c3f6564 | 2013-08-28 02:04:33 | [diff] [blame] | 519 | path, |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 520 | base::FILE_PERMISSION_READ_BY_USER | |
| 521 | base::FILE_PERMISSION_WRITE_BY_USER | |
| 522 | base::FILE_PERMISSION_READ_BY_GROUP | |
| 523 | base::FILE_PERMISSION_READ_BY_OTHERS)) |
[email protected] | c3f6564 | 2013-08-28 02:04:33 | [diff] [blame] | 524 | return FILE_ERROR_FAILED; |
lukasza | 6364a02 | 2015-08-21 01:13:24 | [diff] [blame] | 525 | #endif |
[email protected] | c3f6564 | 2013-08-28 02:04:33 | [diff] [blame] | 526 | |
| 527 | mounted_files_.insert(id); |
| 528 | |
| 529 | *cache_file_path = path; |
| 530 | return FILE_ERROR_OK; |
| 531 | } |
| 532 | |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 533 | FileError FileCache::OpenForWrite( |
| 534 | const std::string& id, |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 535 | std::unique_ptr<base::ScopedClosureRunner>* file_closer) { |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 536 | AssertOnSequencedWorkerPool(); |
| 537 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 538 | // Marking a file dirty means its entry and actual file blob must exist in |
| 539 | // cache. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 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] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 544 | if (!entry.file_specific_info().cache_state().is_present()) { |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 545 | LOG(WARNING) << "Can't mark dirty a file that wasn't cached: " << id; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 546 | return FILE_ERROR_NOT_FOUND; |
| 547 | } |
| 548 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 549 | entry.mutable_file_specific_info()->mutable_cache_state()->set_is_dirty(true); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 550 | if (!UnsetRemovable(GetCacheFilePath(entry.local_id()))) |
| 551 | return FILE_ERROR_FAILED; |
| 552 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 553 | entry.mutable_file_specific_info()->mutable_cache_state()->clear_md5(); |
| 554 | error = storage_->PutEntry(entry); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 555 | if (error != FILE_ERROR_OK) |
| 556 | return error; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 557 | |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 558 | write_opened_files_[id]++; |
Stuart Langley | be403574 | 2018-05-10 05:32:27 | [diff] [blame] | 559 | *file_closer = std::make_unique<base::ScopedClosureRunner>( |
| 560 | base::Bind(&google_apis::RunTaskWithTaskRunner, blocking_task_runner_, |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 561 | base::Bind(&FileCache::CloseForWrite, |
Stuart Langley | be403574 | 2018-05-10 05:32:27 | [diff] [blame] | 562 | weak_ptr_factory_.GetWeakPtr(), id))); |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 563 | return FILE_ERROR_OK; |
| 564 | } |
| 565 | |
| 566 | bool FileCache::IsOpenedForWrite(const std::string& id) { |
| 567 | AssertOnSequencedWorkerPool(); |
lukasza | 81be975 | 2015-06-17 00:14:35 | [diff] [blame] | 568 | return write_opened_files_.count(id) != 0; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 569 | } |
| 570 | |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 571 | FileError FileCache::UpdateMd5(const std::string& id) { |
[email protected] | fcf8eafe0 | 2013-05-28 11:15:39 | [diff] [blame] | 572 | AssertOnSequencedWorkerPool(); |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 573 | |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 574 | if (IsOpenedForWrite(id)) |
| 575 | return FILE_ERROR_IN_USE; |
| 576 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 577 | ResourceEntry entry; |
| 578 | FileError error = storage_->GetEntry(id, &entry); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 579 | if (error != FILE_ERROR_OK) |
| 580 | return error; |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 581 | if (!entry.file_specific_info().cache_state().is_present()) |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 582 | return FILE_ERROR_NOT_FOUND; |
| 583 | |
hashimoto | 246e4a8 | 2015-04-17 07:44:49 | [diff] [blame] | 584 | const std::string& md5 = |
| 585 | util::GetMd5Digest(GetCacheFilePath(id), &in_shutdown_); |
| 586 | if (in_shutdown_.IsSet()) |
| 587 | return FILE_ERROR_ABORT; |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 588 | if (md5.empty()) |
| 589 | return FILE_ERROR_NOT_FOUND; |
| 590 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 591 | entry.mutable_file_specific_info()->mutable_cache_state()->set_md5(md5); |
| 592 | return storage_->PutEntry(entry); |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | FileError FileCache::ClearDirty(const std::string& id) { |
| 596 | AssertOnSequencedWorkerPool(); |
| 597 | |
| 598 | if (IsOpenedForWrite(id)) |
| 599 | return FILE_ERROR_IN_USE; |
| 600 | |
[email protected] | fcf8eafe0 | 2013-05-28 11:15:39 | [diff] [blame] | 601 | // Clearing a dirty file means its entry and actual file blob must exist in |
| 602 | // cache. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 603 | ResourceEntry entry; |
| 604 | FileError error = storage_->GetEntry(id, &entry); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 605 | if (error != FILE_ERROR_OK) |
| 606 | return error; |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 607 | if (!entry.file_specific_info().cache_state().is_present()) { |
[email protected] | fcf8eafe0 | 2013-05-28 11:15:39 | [diff] [blame] | 608 | 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] | 609 | << id; |
[email protected] | fcf8eafe0 | 2013-05-28 11:15:39 | [diff] [blame] | 610 | return FILE_ERROR_NOT_FOUND; |
| 611 | } |
| 612 | |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 613 | // If a file is not dirty (it should have been marked dirty via OpenForWrite), |
| 614 | // clearing its dirty state is an invalid operation. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 615 | if (!entry.file_specific_info().cache_state().is_dirty()) { |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 616 | LOG(WARNING) << "Can't clear dirty state of a non-dirty file: " << id; |
[email protected] | fcf8eafe0 | 2013-05-28 11:15:39 | [diff] [blame] | 617 | return FILE_ERROR_INVALID_OPERATION; |
| 618 | } |
| 619 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 620 | entry.mutable_file_specific_info()->mutable_cache_state()->set_is_dirty( |
| 621 | false); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 622 | if (!entry.file_specific_info().cache_state().is_pinned()) { |
| 623 | if (!SetRemovable(GetCacheFilePath(entry.local_id()))) |
| 624 | return FILE_ERROR_FAILED; |
| 625 | } |
| 626 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 627 | return storage_->PutEntry(entry); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 628 | } |
| 629 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 630 | FileError FileCache::Remove(const std::string& id) { |
[email protected] | 3361a54 | 2013-05-22 17:38:27 | [diff] [blame] | 631 | AssertOnSequencedWorkerPool(); |
| 632 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 633 | ResourceEntry entry; |
[email protected] | 3361a54 | 2013-05-22 17:38:27 | [diff] [blame] | 634 | |
[email protected] | 4b60a25f | 2013-06-17 09:43:11 | [diff] [blame] | 635 | // If entry doesn't exist, nothing to do. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 636 | FileError error = storage_->GetEntry(id, &entry); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 637 | if (error == FILE_ERROR_NOT_FOUND) |
[email protected] | 3361a54 | 2013-05-22 17:38:27 | [diff] [blame] | 638 | return FILE_ERROR_OK; |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 639 | if (error != FILE_ERROR_OK) |
| 640 | return error; |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 641 | if (!entry.file_specific_info().has_cache_state()) |
| 642 | return FILE_ERROR_OK; |
[email protected] | 3361a54 | 2013-05-22 17:38:27 | [diff] [blame] | 643 | |
[email protected] | d1ad8fa | 2013-07-11 13:23:20 | [diff] [blame] | 644 | // Cannot delete a mounted file. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 645 | if (mounted_files_.count(id)) |
[email protected] | 4b60a25f | 2013-06-17 09:43:11 | [diff] [blame] | 646 | return FILE_ERROR_IN_USE; |
| 647 | |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 648 | // Delete the file. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 649 | base::FilePath path = GetCacheFilePath(id); |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 650 | if (!base::DeleteFile(path, false /* recursive */)) |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 651 | return FILE_ERROR_FAILED; |
[email protected] | 3361a54 | 2013-05-22 17:38:27 | [diff] [blame] | 652 | |
| 653 | // Now that all file operations have completed, remove from metadata. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 654 | entry.mutable_file_specific_info()->clear_cache_state(); |
| 655 | return storage_->PutEntry(entry); |
[email protected] | 3361a54 | 2013-05-22 17:38:27 | [diff] [blame] | 656 | } |
| 657 | |
[email protected] | 823ca971 | 2013-09-13 10:09:09 | [diff] [blame] | 658 | bool FileCache::ClearAll() { |
| 659 | AssertOnSequencedWorkerPool(); |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 660 | |
[email protected] | 823ca971 | 2013-09-13 10:09:09 | [diff] [blame] | 661 | // Remove files. |
| 662 | base::FileEnumerator enumerator(cache_file_directory_, |
| 663 | false, // not recursive |
| 664 | base::FileEnumerator::FILES); |
| 665 | for (base::FilePath file = enumerator.Next(); !file.empty(); |
| 666 | file = enumerator.Next()) |
| 667 | base::DeleteFile(file, false /* recursive */); |
| 668 | |
| 669 | return true; |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 670 | } |
| 671 | |
[email protected] | 34a1bbf3 | 2013-06-17 07:24:02 | [diff] [blame] | 672 | bool FileCache::Initialize() { |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 673 | AssertOnSequencedWorkerPool(); |
| 674 | |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 675 | // Older versions do not clear MD5 when marking entries dirty. |
| 676 | // Clear MD5 of all dirty entries to deal with old data. |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 677 | std::unique_ptr<ResourceMetadataStorage::Iterator> it = |
| 678 | storage_->GetIterator(); |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 679 | for (; !it->IsAtEnd(); it->Advance()) { |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 680 | if (it->GetValue().file_specific_info().cache_state().is_dirty()) { |
| 681 | ResourceEntry new_entry(it->GetValue()); |
| 682 | new_entry.mutable_file_specific_info()->mutable_cache_state()-> |
| 683 | clear_md5(); |
| 684 | if (storage_->PutEntry(new_entry) != FILE_ERROR_OK) |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 685 | return false; |
| 686 | } |
| 687 | } |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 688 | if (it->HasError()) |
| 689 | return false; |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 690 | |
[email protected] | f2731d1 | 2013-10-22 03:23:15 | [diff] [blame] | 691 | if (!RenameCacheFilesToNewFormat()) |
| 692 | return false; |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 693 | |
| 694 | // Run this every time to resolve inconsistency between metadata |
| 695 | // and file attributes which possibly occurs on abrupt power failure. |
| 696 | if (!FixMetadataAndFileAttributes()) { |
| 697 | return false; |
| 698 | } |
| 699 | |
[email protected] | e8842b19 | 2013-06-11 04:05:14 | [diff] [blame] | 700 | return true; |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 701 | } |
| 702 | |
[email protected] | 34a1bbf3 | 2013-06-17 07:24:02 | [diff] [blame] | 703 | void FileCache::Destroy() { |
Stuart Langley | be403574 | 2018-05-10 05:32:27 | [diff] [blame] | 704 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
[email protected] | 34a1bbf3 | 2013-06-17 07:24:02 | [diff] [blame] | 705 | |
hashimoto | 246e4a8 | 2015-04-17 07:44:49 | [diff] [blame] | 706 | in_shutdown_.Set(); |
| 707 | |
[email protected] | 34a1bbf3 | 2013-06-17 07:24:02 | [diff] [blame] | 708 | // Destroy myself on the blocking pool. |
| 709 | // Note that base::DeletePointer<> cannot be used as the destructor of this |
| 710 | // class is private. |
| 711 | blocking_task_runner_->PostTask( |
tzik | 2bcf8e4 | 2018-07-31 11:22:15 | [diff] [blame] | 712 | FROM_HERE, base::BindOnce(&FileCache::DestroyOnBlockingPool, |
| 713 | base::Unretained(this))); |
[email protected] | 34a1bbf3 | 2013-06-17 07:24:02 | [diff] [blame] | 714 | } |
| 715 | |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 716 | void FileCache::DestroyOnBlockingPool() { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 717 | AssertOnSequencedWorkerPool(); |
| 718 | delete this; |
| 719 | } |
| 720 | |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 721 | bool FileCache::RecoverFilesFromCacheDirectory( |
[email protected] | 760abc3 | 2013-11-01 05:13:01 | [diff] [blame] | 722 | const base::FilePath& dest_directory, |
[email protected] | 026d4a52 | 2013-11-05 14:22:18 | [diff] [blame] | 723 | const ResourceMetadataStorage::RecoveredCacheInfoMap& |
| 724 | recovered_cache_info) { |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 725 | int file_number = 1; |
| 726 | |
| 727 | base::FileEnumerator enumerator(cache_file_directory_, |
| 728 | false, // not recursive |
| 729 | base::FileEnumerator::FILES); |
| 730 | for (base::FilePath current = enumerator.Next(); !current.empty(); |
| 731 | current = enumerator.Next()) { |
| 732 | const std::string& id = GetIdFromPath(current); |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 733 | ResourceEntry entry; |
| 734 | FileError error = storage_->GetEntry(id, &entry); |
| 735 | if (error != FILE_ERROR_OK && error != FILE_ERROR_NOT_FOUND) |
| 736 | return false; |
| 737 | if (error == FILE_ERROR_OK && |
| 738 | entry.file_specific_info().cache_state().is_present()) { |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 739 | // This file is managed by FileCache, no need to recover it. |
| 740 | continue; |
| 741 | } |
| 742 | |
[email protected] | 760abc3 | 2013-11-01 05:13:01 | [diff] [blame] | 743 | // If a cache entry which is non-dirty and has matching MD5 is found in |
| 744 | // |recovered_cache_entries|, it means the current file is already uploaded |
| 745 | // to the server. Just delete it instead of recovering it. |
[email protected] | 026d4a52 | 2013-11-05 14:22:18 | [diff] [blame] | 746 | ResourceMetadataStorage::RecoveredCacheInfoMap::const_iterator it = |
| 747 | recovered_cache_info.find(id); |
| 748 | if (it != recovered_cache_info.end()) { |
| 749 | // Due to the DB corruption, cache info might be recovered from old |
| 750 | // revision. Perform MD5 check even when is_dirty is false just in case. |
| 751 | if (!it->second.is_dirty && |
hashimoto | 246e4a8 | 2015-04-17 07:44:49 | [diff] [blame] | 752 | it->second.md5 == util::GetMd5Digest(current, &in_shutdown_)) { |
[email protected] | 760abc3 | 2013-11-01 05:13:01 | [diff] [blame] | 753 | base::DeleteFile(current, false /* recursive */); |
| 754 | continue; |
| 755 | } |
| 756 | } |
| 757 | |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 758 | // Read file contents to sniff mime type. |
| 759 | std::vector<char> content(net::kMaxBytesToSniff); |
| 760 | const int read_result = |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 761 | base::ReadFile(current, &content[0], content.size()); |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 762 | if (read_result < 0) { |
| 763 | LOG(WARNING) << "Cannot read: " << current.value(); |
| 764 | return false; |
| 765 | } |
| 766 | if (read_result == 0) // Skip empty files. |
| 767 | continue; |
| 768 | |
[email protected] | 026d4a52 | 2013-11-05 14:22:18 | [diff] [blame] | 769 | // Use recovered file name if available, otherwise decide file name with |
| 770 | // sniffed mime type. |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 771 | base::FilePath dest_base_name(FILE_PATH_LITERAL("file")); |
| 772 | std::string mime_type; |
[email protected] | 026d4a52 | 2013-11-05 14:22:18 | [diff] [blame] | 773 | if (it != recovered_cache_info.end() && !it->second.title.empty()) { |
| 774 | // We can use a file name recovered from the trashed DB. |
| 775 | dest_base_name = base::FilePath::FromUTF8Unsafe(it->second.title); |
Matt Menke | fcbb1bd7 | 2018-01-31 21:53:12 | [diff] [blame] | 776 | } else if (net::SniffMimeType( |
| 777 | &content[0], read_result, net::FilePathToFileURL(current), |
| 778 | std::string(), net::ForceSniffFileUrlsForHtml::kDisabled, |
| 779 | &mime_type) || |
[email protected] | 026d4a52 | 2013-11-05 14:22:18 | [diff] [blame] | 780 | net::SniffMimeTypeFromLocalData(&content[0], read_result, |
| 781 | &mime_type)) { |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 782 | // Change base name for common mime types. |
| 783 | if (net::MatchesMimeType("image/*", mime_type)) { |
| 784 | dest_base_name = base::FilePath(FILE_PATH_LITERAL("image")); |
| 785 | } else if (net::MatchesMimeType("video/*", mime_type)) { |
| 786 | dest_base_name = base::FilePath(FILE_PATH_LITERAL("video")); |
| 787 | } else if (net::MatchesMimeType("audio/*", mime_type)) { |
| 788 | dest_base_name = base::FilePath(FILE_PATH_LITERAL("audio")); |
| 789 | } |
| 790 | |
| 791 | // Estimate extension from mime type. |
| 792 | std::vector<base::FilePath::StringType> extensions; |
| 793 | base::FilePath::StringType extension; |
| 794 | if (net::GetPreferredExtensionForMimeType(mime_type, &extension)) |
| 795 | extensions.push_back(extension); |
| 796 | else |
| 797 | net::GetExtensionsForMimeType(mime_type, &extensions); |
| 798 | |
| 799 | // Add extension if possible. |
| 800 | if (!extensions.empty()) |
| 801 | dest_base_name = dest_base_name.AddExtension(extensions[0]); |
| 802 | } |
| 803 | |
| 804 | // Add file number to the file name and move. |
| 805 | const base::FilePath& dest_path = dest_directory.Append(dest_base_name) |
| 806 | .InsertBeforeExtensionASCII(base::StringPrintf("%08d", file_number++)); |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 807 | if (!base::CreateDirectory(dest_directory) || |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 808 | !base::Move(current, dest_path)) { |
| 809 | LOG(WARNING) << "Failed to move: " << current.value() |
| 810 | << " to " << dest_path.value(); |
| 811 | return false; |
| 812 | } |
| 813 | } |
Steven Holte | 9592222 | 2018-09-14 20:06:23 | [diff] [blame] | 814 | UMA_HISTOGRAM_COUNTS_1M("Drive.NumberOfCacheFilesRecoveredAfterDBCorruption", |
| 815 | file_number - 1); |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 816 | return true; |
| 817 | } |
| 818 | |
[email protected] | 54ba3750 | 2013-05-09 08:43:40 | [diff] [blame] | 819 | FileError FileCache::MarkAsUnmounted(const base::FilePath& file_path) { |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 820 | AssertOnSequencedWorkerPool(); |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 821 | DCHECK(IsUnderFileCacheDirectory(file_path)); |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 822 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 823 | std::string id = GetIdFromPath(file_path); |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 824 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 825 | // Get the entry associated with the id. |
| 826 | ResourceEntry entry; |
| 827 | FileError error = storage_->GetEntry(id, &entry); |
[email protected] | 99613941 | 2014-05-10 06:19:50 | [diff] [blame] | 828 | if (error != FILE_ERROR_OK) |
| 829 | return error; |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 830 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 831 | std::set<std::string>::iterator it = mounted_files_.find(id); |
[email protected] | 4b60a25f | 2013-06-17 09:43:11 | [diff] [blame] | 832 | if (it == mounted_files_.end()) |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 833 | return FILE_ERROR_INVALID_OPERATION; |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 834 | |
[email protected] | 4b60a25f | 2013-06-17 09:43:11 | [diff] [blame] | 835 | mounted_files_.erase(it); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 836 | return FILE_ERROR_OK; |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 837 | } |
| 838 | |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 839 | int64_t FileCache::GetAvailableSpace() { |
| 840 | int64_t free_space = 0; |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 841 | if (free_disk_space_getter_) |
| 842 | free_space = free_disk_space_getter_->AmountOfFreeDiskSpace(); |
| 843 | else |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 844 | free_space = base::SysInfo::AmountOfFreeDiskSpace(cache_file_directory_); |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 845 | |
| 846 | // Subtract this as if this portion does not exist. |
lukasza | 3fb2262 | 2015-08-27 21:04:34 | [diff] [blame] | 847 | free_space -= drive::internal::kMinFreeSpaceInBytes; |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 848 | return free_space; |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 849 | } |
| 850 | |
[email protected] | f2731d1 | 2013-10-22 03:23:15 | [diff] [blame] | 851 | bool FileCache::RenameCacheFilesToNewFormat() { |
| 852 | base::FileEnumerator enumerator(cache_file_directory_, |
| 853 | false, // not recursive |
| 854 | base::FileEnumerator::FILES); |
| 855 | for (base::FilePath current = enumerator.Next(); !current.empty(); |
| 856 | current = enumerator.Next()) { |
| 857 | base::FilePath new_path = current.RemoveExtension(); |
| 858 | if (!new_path.Extension().empty()) { |
| 859 | // Delete files with multiple extensions. |
| 860 | if (!base::DeleteFile(current, false /* recursive */)) |
| 861 | return false; |
| 862 | continue; |
| 863 | } |
| 864 | const std::string& id = GetIdFromPath(new_path); |
| 865 | new_path = GetCacheFilePath(util::CanonicalizeResourceId(id)); |
| 866 | if (new_path != current && !base::Move(current, new_path)) |
| 867 | return false; |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 868 | } |
[email protected] | f2731d1 | 2013-10-22 03:23:15 | [diff] [blame] | 869 | return true; |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 870 | } |
| 871 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 872 | bool FileCache::FixMetadataAndFileAttributes() { |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 873 | std::unique_ptr<ResourceMetadataStorage::Iterator> it = |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 874 | storage_->GetIterator(); |
| 875 | |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 876 | for (; !it->IsAtEnd(); it->Advance()) { |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 877 | ResourceEntry entry = it->GetValue(); |
| 878 | FileCacheEntry* file_cache_entry = |
| 879 | entry.mutable_file_specific_info()->mutable_cache_state(); |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 880 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 881 | const base::FilePath filepath = GetPathForId(cache_file_directory_, |
| 882 | entry.local_id()); |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 883 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 884 | if (base::PathExists(filepath)) { |
| 885 | if (file_cache_entry->is_present()) { |
| 886 | // Update file attribues for cryptohome. |
| 887 | if (file_cache_entry->is_pinned() || file_cache_entry->is_dirty()) { |
| 888 | if (!UnsetRemovable(filepath)) return false; |
| 889 | } else { |
| 890 | if (!SetRemovable(filepath)) return false; |
| 891 | } |
| 892 | } else { |
| 893 | // Delete file if the file is present but metadata says not. |
| 894 | // It happens only on abrupt shutdown. |
| 895 | LOG(WARNING) |
| 896 | << "File is present but metadata's state was inconsistent."; |
| 897 | |
| 898 | if (!base::DeleteFile(filepath, false /* recursive */)) |
| 899 | return false; |
yawano | 1c325bf | 2016-04-20 06:37:03 | [diff] [blame] | 900 | } |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 901 | } else { |
| 902 | // Update metatadata if there is no file but metadata says there is. |
| 903 | // It happens when cryptohome removed the file. |
| 904 | // We don't clear is_pinned here, so that file download is restarted on |
| 905 | // the following scenario: |
| 906 | // 1. The file was pinned but not present. |
| 907 | // 2. Then the file was downloaded and became present. |
| 908 | // 3. Unclean shutdown happens, metadata update was saved to the disk, |
| 909 | // but the file move was not. |
| 910 | if (file_cache_entry->is_present()) { |
| 911 | file_cache_entry->set_is_present(false); |
| 912 | file_cache_entry->set_is_dirty(false); |
| 913 | file_cache_entry->clear_md5(); |
| 914 | if (storage_->PutEntry(entry) != FILE_ERROR_OK) |
| 915 | return false; |
| 916 | } |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 917 | } |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 918 | } |
| 919 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 920 | return MarkAsDriveCacheDir(cache_file_directory_); |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 921 | } |
| 922 | |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 923 | void FileCache::CloseForWrite(const std::string& id) { |
| 924 | AssertOnSequencedWorkerPool(); |
| 925 | |
| 926 | std::map<std::string, int>::iterator it = write_opened_files_.find(id); |
| 927 | if (it == write_opened_files_.end()) |
| 928 | return; |
| 929 | |
| 930 | DCHECK_LT(0, it->second); |
| 931 | --it->second; |
| 932 | if (it->second == 0) |
| 933 | write_opened_files_.erase(it); |
[email protected] | f92367ae | 2014-06-02 07:35:43 | [diff] [blame] | 934 | |
| 935 | // Update last modified date. |
| 936 | ResourceEntry entry; |
| 937 | FileError error = storage_->GetEntry(id, &entry); |
| 938 | if (error != FILE_ERROR_OK) { |
| 939 | LOG(ERROR) << "Failed to get entry: " << id << ", " |
| 940 | << FileErrorToString(error); |
| 941 | return; |
| 942 | } |
Shuhei Takahashi | a25ace56 | 2017-09-15 09:22:17 | [diff] [blame] | 943 | int64_t now = base::Time::Now().ToInternalValue(); |
| 944 | entry.mutable_file_info()->set_last_modified(now); |
| 945 | entry.set_last_modified_by_me(now); |
[email protected] | f92367ae | 2014-06-02 07:35:43 | [diff] [blame] | 946 | error = storage_->PutEntry(entry); |
| 947 | if (error != FILE_ERROR_OK) { |
| 948 | LOG(ERROR) << "Failed to put entry: " << id << ", " |
| 949 | << FileErrorToString(error); |
| 950 | } |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 951 | } |
| 952 | |
yawano | 8578abf | 2015-08-26 09:15:50 | [diff] [blame] | 953 | bool FileCache::IsEvictable(const std::string& id, const ResourceEntry& entry) { |
fukino | 6380c07c | 2016-06-09 07:28:29 | [diff] [blame] | 954 | return IsPresent(entry) && |
yawano | 8578abf | 2015-08-26 09:15:50 | [diff] [blame] | 955 | !entry.file_specific_info().cache_state().is_pinned() && |
| 956 | !entry.file_specific_info().cache_state().is_dirty() && |
| 957 | !mounted_files_.count(id); |
| 958 | } |
| 959 | |
[email protected] | 59c7cdec | 2013-05-07 04:17:13 | [diff] [blame] | 960 | } // namespace internal |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 961 | } // namespace drive |