blob: 085d6c52d7e132c574d5270b40e75e1d0fd54114 [file] [log] [blame]
[email protected]3653146a2012-05-29 13:41:471// 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]eca3fc92013-05-01 03:53:405#include "chrome/browser/chromeos/drive/file_cache.h"
[email protected]3653146a2012-05-29 13:41:476
7#include <vector>
8
lukasza037c10b12015-06-12 04:21:259#include "base/bind.h"
10#include "base/bind_helpers.h"
[email protected]8b03ab3a2014-01-15 17:52:4511#include "base/callback_helpers.h"
[email protected]25a4c1c2013-06-08 04:53:3612#include "base/files/file_enumerator.h"
thestig18dfb7a52014-08-26 10:44:0413#include "base/files/file_util.h"
lukasza037c10b12015-06-12 04:21:2514#include "base/location.h"
[email protected]3653146a2012-05-29 13:41:4715#include "base/logging.h"
[email protected]d105b3ad2013-11-01 05:33:1316#include "base/metrics/histogram.h"
[email protected]5c073322013-06-11 08:03:3017#include "base/strings/string_util.h"
18#include "base/strings/stringprintf.h"
[email protected]a321b9632012-06-14 03:29:1719#include "base/sys_info.h"
[email protected]15de8142012-10-11 06:00:5420#include "chrome/browser/chromeos/drive/drive.pb.h"
[email protected]4fa2fd5d2013-04-26 03:42:5221#include "chrome/browser/chromeos/drive/file_system_util.h"
[email protected]2df61e12013-06-21 16:00:0922#include "chrome/browser/chromeos/drive/resource_metadata_storage.h"
[email protected]f2731d12013-10-22 03:23:1523#include "chrome/browser/drive/drive_api_util.h"
[email protected]e8f3f9982013-05-10 20:59:5324#include "chromeos/chromeos_constants.h"
[email protected]8b03ab3a2014-01-15 17:52:4525#include "google_apis/drive/task_util.h"
[email protected]d96cf752014-04-09 04:05:2826#include "net/base/filename_util.h"
[email protected]b7af4f12013-10-31 06:57:4527#include "net/base/mime_sniffer.h"
28#include "net/base/mime_util.h"
[email protected]ab3ed7e4a2013-12-12 09:18:3029#include "third_party/cros_system_api/constants/cryptohome.h"
[email protected]7986b8d2012-06-14 15:05:1430
[email protected]d9d04df2012-10-12 07:06:3531namespace drive {
[email protected]59c7cdec2013-05-07 04:17:1332namespace internal {
[email protected]3653146a2012-05-29 13:41:4733namespace {
34
[email protected]c9e4738d2013-08-26 03:04:0735// Returns ID extracted from the path.
36std::string GetIdFromPath(const base::FilePath& path) {
[email protected]91a464e62013-07-10 09:30:0637 return util::UnescapeCacheFileName(path.BaseName().AsUTF8Unsafe());
38}
39
[email protected]a321b9632012-06-14 03:29:1740} // namespace
[email protected]32a7fc852012-06-08 17:25:5041
[email protected]2df61e12013-06-21 16:00:0942FileCache::FileCache(ResourceMetadataStorage* storage,
[email protected]e07f7b7b2013-06-19 03:43:1243 const base::FilePath& cache_file_directory,
[email protected]eca3fc92013-05-01 03:53:4044 base::SequencedTaskRunner* blocking_task_runner,
45 FreeDiskSpaceGetterInterface* free_disk_space_getter)
[email protected]2df61e12013-06-21 16:00:0946 : cache_file_directory_(cache_file_directory),
[email protected]ddbf2052012-07-13 15:07:0247 blocking_task_runner_(blocking_task_runner),
[email protected]2df61e12013-06-21 16:00:0948 storage_(storage),
[email protected]f6fd98a2012-12-14 00:04:0249 free_disk_space_getter_(free_disk_space_getter),
[email protected]9c009092013-05-01 03:14:0950 weak_ptr_factory_(this) {
[email protected]144b6c42013-06-14 07:30:3851 DCHECK(blocking_task_runner_.get());
[email protected]3653146a2012-05-29 13:41:4752}
53
[email protected]eca3fc92013-05-01 03:53:4054FileCache::~FileCache() {
[email protected]17196ee2012-12-13 06:23:5155 // Must be on the sequenced worker pool, as |metadata_| must be deleted on
56 // the sequenced worker pool.
[email protected]73f9c742012-06-15 07:37:1357 AssertOnSequencedWorkerPool();
[email protected]3653146a2012-05-29 13:41:4758}
59
[email protected]c9e4738d2013-08-26 03:04:0760base::FilePath FileCache::GetCacheFilePath(const std::string& id) const {
[email protected]e07f7b7b2013-06-19 03:43:1261 return cache_file_directory_.Append(
[email protected]c9e4738d2013-08-26 03:04:0762 base::FilePath::FromUTF8Unsafe(util::EscapeCacheFileName(id)));
[email protected]32a7fc852012-06-08 17:25:5063}
64
[email protected]eca3fc92013-05-01 03:53:4065void FileCache::AssertOnSequencedWorkerPool() {
[email protected]8e37b9b2013-12-11 09:06:0266 DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread());
[email protected]fcc92a52012-06-08 22:54:1667}
68
[email protected]eca3fc92013-05-01 03:53:4069bool FileCache::IsUnderFileCacheDirectory(const base::FilePath& path) const {
[email protected]e07f7b7b2013-06-19 03:43:1270 return cache_file_directory_.IsParent(path);
[email protected]01ba15f72012-06-09 00:41:0571}
72
[email protected]ec514362013-05-27 17:52:2273bool FileCache::FreeDiskSpaceIfNeededFor(int64 num_bytes) {
74 AssertOnSequencedWorkerPool();
75
76 // Do nothing and return if we have enough space.
[email protected]e07f7b7b2013-06-19 03:43:1277 if (HasEnoughSpaceFor(num_bytes, cache_file_directory_))
[email protected]ec514362013-05-27 17:52:2278 return true;
79
80 // Otherwise, try to free up the disk space.
81 DVLOG(1) << "Freeing up disk space for " << num_bytes;
[email protected]f8b1a532013-06-06 08:35:0882
[email protected]9d147582013-06-14 06:25:4583 // Remove all entries unless specially marked.
[email protected]cd8fd37f2014-05-20 15:45:2184 scoped_ptr<ResourceMetadataStorage::Iterator> it = storage_->GetIterator();
[email protected]f8b1a532013-06-06 08:35:0885 for (; !it->IsAtEnd(); it->Advance()) {
[email protected]cd8fd37f2014-05-20 15:45:2186 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]f8b1a532013-06-06 08:35:0894 }
[email protected]996139412014-05-10 06:19:5095 if (it->HasError())
96 return false;
[email protected]f8b1a532013-06-06 08:35:0897
[email protected]9d147582013-06-14 06:25:4598 // Remove all files which have no corresponding cache entries.
[email protected]e07f7b7b2013-06-19 03:43:1299 base::FileEnumerator enumerator(cache_file_directory_,
[email protected]9d147582013-06-14 06:25:45100 false, // not recursive
101 base::FileEnumerator::FILES);
[email protected]cd8fd37f2014-05-20 15:45:21102 ResourceEntry entry;
[email protected]9d147582013-06-14 06:25:45103 for (base::FilePath current = enumerator.Next(); !current.empty();
104 current = enumerator.Next()) {
[email protected]c9e4738d2013-08-26 03:04:07105 std::string id = GetIdFromPath(current);
[email protected]cd8fd37f2014-05-20 15:45:21106 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]dd3aa792013-07-16 19:10:23110 base::DeleteFile(current, false /* recursive */);
[email protected]996139412014-05-10 06:19:50111 else if (error != FILE_ERROR_OK)
112 return false;
[email protected]9d147582013-06-14 06:25:45113 }
[email protected]ec514362013-05-27 17:52:22114
115 // Check the disk space again.
[email protected]e07f7b7b2013-06-19 03:43:12116 return HasEnoughSpaceFor(num_bytes, cache_file_directory_);
[email protected]ec514362013-05-27 17:52:22117}
118
[email protected]c9e4738d2013-08-26 03:04:07119FileError FileCache::GetFile(const std::string& id,
[email protected]ec514362013-05-27 17:52:22120 base::FilePath* cache_file_path) {
121 AssertOnSequencedWorkerPool();
122 DCHECK(cache_file_path);
123
[email protected]cd8fd37f2014-05-20 15:45:21124 ResourceEntry entry;
125 FileError error = storage_->GetEntry(id, &entry);
[email protected]996139412014-05-10 06:19:50126 if (error != FILE_ERROR_OK)
127 return error;
[email protected]cd8fd37f2014-05-20 15:45:21128 if (!entry.file_specific_info().cache_state().is_present())
[email protected]ec514362013-05-27 17:52:22129 return FILE_ERROR_NOT_FOUND;
130
[email protected]c9e4738d2013-08-26 03:04:07131 *cache_file_path = GetCacheFilePath(id);
[email protected]ec514362013-05-27 17:52:22132 return FILE_ERROR_OK;
133}
134
[email protected]c9e4738d2013-08-26 03:04:07135FileError FileCache::Store(const std::string& id,
[email protected]82c4eb92013-05-21 11:25:23136 const std::string& md5,
137 const base::FilePath& source_path,
138 FileOperationType file_operation_type) {
139 AssertOnSequencedWorkerPool();
[email protected]d8546c92013-05-02 05:09:59140
[email protected]cd8fd37f2014-05-20 15:45:21141 ResourceEntry entry;
142 FileError error = storage_->GetEntry(id, &entry);
143 if (error != FILE_ERROR_OK)
144 return error;
145
[email protected]8e37b9b2013-12-11 09:06:02146 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]73f9c742012-06-15 07:37:13155
[email protected]1d0786a2014-02-06 12:37:08156 // If file is mounted, return error.
157 if (mounted_files_.count(id))
[email protected]8e37b9b2013-12-11 09:06:02158 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]cd8fd37f2014-05-20 15:45:21182 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]bae99ae52014-01-29 01:13:14186 if (md5.empty())
[email protected]cd8fd37f2014-05-20 15:45:21187 cache_state->set_is_dirty(true);
188 return storage_->PutEntry(entry);
[email protected]73f9c742012-06-15 07:37:13189}
190
[email protected]c9e4738d2013-08-26 03:04:07191FileError FileCache::Pin(const std::string& id) {
[email protected]f8b1a532013-06-06 08:35:08192 AssertOnSequencedWorkerPool();
193
[email protected]cd8fd37f2014-05-20 15:45:21194 ResourceEntry entry;
195 FileError error = storage_->GetEntry(id, &entry);
196 if (error != FILE_ERROR_OK)
[email protected]996139412014-05-10 06:19:50197 return error;
[email protected]cd8fd37f2014-05-20 15:45:21198 entry.mutable_file_specific_info()->mutable_cache_state()->set_is_pinned(
199 true);
200 return storage_->PutEntry(entry);
[email protected]f8b1a532013-06-06 08:35:08201}
202
[email protected]c9e4738d2013-08-26 03:04:07203FileError FileCache::Unpin(const std::string& id) {
[email protected]ec514362013-05-27 17:52:22204 AssertOnSequencedWorkerPool();
205
206 // Unpinning a file means its entry must exist in cache.
[email protected]cd8fd37f2014-05-20 15:45:21207 ResourceEntry entry;
208 FileError error = storage_->GetEntry(id, &entry);
[email protected]996139412014-05-10 06:19:50209 if (error != FILE_ERROR_OK)
210 return error;
[email protected]ec514362013-05-27 17:52:22211
[email protected]ec514362013-05-27 17:52:22212 // Now that file operations have completed, update metadata.
[email protected]cd8fd37f2014-05-20 15:45:21213 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]ec514362013-05-27 17:52:22216 } else {
217 // Remove the existing entry if we are unpinning a non-present file.
[email protected]cd8fd37f2014-05-20 15:45:21218 entry.mutable_file_specific_info()->clear_cache_state();
[email protected]ec514362013-05-27 17:52:22219 }
[email protected]cd8fd37f2014-05-20 15:45:21220 error = storage_->PutEntry(entry);
221 if (error != FILE_ERROR_OK)
222 return error;
[email protected]bd2254d2013-06-12 16:00:47223
[email protected]9d147582013-06-14 06:25:45224 // Now it's a chance to free up space if needed.
[email protected]bd2254d2013-06-12 16:00:47225 FreeDiskSpaceIfNeededFor(0);
226
[email protected]ec514362013-05-27 17:52:22227 return FILE_ERROR_OK;
228}
229
[email protected]c3f65642013-08-28 02:04:33230FileError 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]cd8fd37f2014-05-20 15:45:21236 ResourceEntry entry;
237 FileError error = storage_->GetEntry(id, &entry);
[email protected]996139412014-05-10 06:19:50238 if (error != FILE_ERROR_OK)
239 return error;
[email protected]cd8fd37f2014-05-20 15:45:21240 if (!entry.file_specific_info().cache_state().is_present())
241 return FILE_ERROR_NOT_FOUND;
[email protected]c3f65642013-08-28 02:04:33242
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]b264eab2013-11-27 23:22:08248 if (!base::SetPosixFilePermissions(
[email protected]c3f65642013-08-28 02:04:33249 path,
[email protected]b264eab2013-11-27 23:22:08250 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]c3f65642013-08-28 02:04:33254 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]8b03ab3a2014-01-15 17:52:45262FileError FileCache::OpenForWrite(
263 const std::string& id,
264 scoped_ptr<base::ScopedClosureRunner>* file_closer) {
[email protected]b568b882013-06-10 04:38:07265 AssertOnSequencedWorkerPool();
266
[email protected]b568b882013-06-10 04:38:07267 // Marking a file dirty means its entry and actual file blob must exist in
268 // cache.
[email protected]cd8fd37f2014-05-20 15:45:21269 ResourceEntry entry;
270 FileError error = storage_->GetEntry(id, &entry);
[email protected]996139412014-05-10 06:19:50271 if (error != FILE_ERROR_OK)
272 return error;
[email protected]cd8fd37f2014-05-20 15:45:21273 if (!entry.file_specific_info().cache_state().is_present()) {
[email protected]c9e4738d2013-08-26 03:04:07274 LOG(WARNING) << "Can't mark dirty a file that wasn't cached: " << id;
[email protected]b568b882013-06-10 04:38:07275 return FILE_ERROR_NOT_FOUND;
276 }
277
[email protected]cd8fd37f2014-05-20 15:45:21278 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]996139412014-05-10 06:19:50281 if (error != FILE_ERROR_OK)
282 return error;
[email protected]b568b882013-06-10 04:38:07283
[email protected]8b03ab3a2014-01-15 17:52:45284 write_opened_files_[id]++;
285 file_closer->reset(new base::ScopedClosureRunner(
[email protected]c929c932014-07-23 06:06:05286 base::Bind(&google_apis::RunTaskWithTaskRunner,
[email protected]8b03ab3a2014-01-15 17:52:45287 blocking_task_runner_,
288 base::Bind(&FileCache::CloseForWrite,
289 weak_ptr_factory_.GetWeakPtr(),
290 id))));
291 return FILE_ERROR_OK;
292}
293
294bool FileCache::IsOpenedForWrite(const std::string& id) {
295 AssertOnSequencedWorkerPool();
296 return write_opened_files_.count(id);
[email protected]b568b882013-06-10 04:38:07297}
298
[email protected]b1bf19a2014-01-21 04:45:19299FileError FileCache::UpdateMd5(const std::string& id) {
[email protected]fcf8eafe02013-05-28 11:15:39300 AssertOnSequencedWorkerPool();
[email protected]eca3fc92013-05-01 03:53:40301
[email protected]b1bf19a2014-01-21 04:45:19302 if (IsOpenedForWrite(id))
303 return FILE_ERROR_IN_USE;
304
[email protected]cd8fd37f2014-05-20 15:45:21305 ResourceEntry entry;
306 FileError error = storage_->GetEntry(id, &entry);
[email protected]996139412014-05-10 06:19:50307 if (error != FILE_ERROR_OK)
308 return error;
[email protected]cd8fd37f2014-05-20 15:45:21309 if (!entry.file_specific_info().cache_state().is_present())
[email protected]b1bf19a2014-01-21 04:45:19310 return FILE_ERROR_NOT_FOUND;
311
hashimoto246e4a82015-04-17 07:44:49312 const std::string& md5 =
313 util::GetMd5Digest(GetCacheFilePath(id), &in_shutdown_);
314 if (in_shutdown_.IsSet())
315 return FILE_ERROR_ABORT;
[email protected]b1bf19a2014-01-21 04:45:19316 if (md5.empty())
317 return FILE_ERROR_NOT_FOUND;
318
[email protected]cd8fd37f2014-05-20 15:45:21319 entry.mutable_file_specific_info()->mutable_cache_state()->set_md5(md5);
320 return storage_->PutEntry(entry);
[email protected]b1bf19a2014-01-21 04:45:19321}
322
323FileError FileCache::ClearDirty(const std::string& id) {
324 AssertOnSequencedWorkerPool();
325
326 if (IsOpenedForWrite(id))
327 return FILE_ERROR_IN_USE;
328
[email protected]fcf8eafe02013-05-28 11:15:39329 // Clearing a dirty file means its entry and actual file blob must exist in
330 // cache.
[email protected]cd8fd37f2014-05-20 15:45:21331 ResourceEntry entry;
332 FileError error = storage_->GetEntry(id, &entry);
[email protected]996139412014-05-10 06:19:50333 if (error != FILE_ERROR_OK)
334 return error;
[email protected]cd8fd37f2014-05-20 15:45:21335 if (!entry.file_specific_info().cache_state().is_present()) {
[email protected]fcf8eafe02013-05-28 11:15:39336 LOG(WARNING) << "Can't clear dirty state of a file that wasn't cached: "
[email protected]c9e4738d2013-08-26 03:04:07337 << id;
[email protected]fcf8eafe02013-05-28 11:15:39338 return FILE_ERROR_NOT_FOUND;
339 }
340
[email protected]8b03ab3a2014-01-15 17:52:45341 // 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]cd8fd37f2014-05-20 15:45:21343 if (!entry.file_specific_info().cache_state().is_dirty()) {
[email protected]c9e4738d2013-08-26 03:04:07344 LOG(WARNING) << "Can't clear dirty state of a non-dirty file: " << id;
[email protected]fcf8eafe02013-05-28 11:15:39345 return FILE_ERROR_INVALID_OPERATION;
346 }
347
[email protected]cd8fd37f2014-05-20 15:45:21348 entry.mutable_file_specific_info()->mutable_cache_state()->set_is_dirty(
349 false);
350 return storage_->PutEntry(entry);
[email protected]73f9c742012-06-15 07:37:13351}
352
[email protected]c9e4738d2013-08-26 03:04:07353FileError FileCache::Remove(const std::string& id) {
[email protected]3361a542013-05-22 17:38:27354 AssertOnSequencedWorkerPool();
355
[email protected]cd8fd37f2014-05-20 15:45:21356 ResourceEntry entry;
[email protected]3361a542013-05-22 17:38:27357
[email protected]4b60a25f2013-06-17 09:43:11358 // If entry doesn't exist, nothing to do.
[email protected]cd8fd37f2014-05-20 15:45:21359 FileError error = storage_->GetEntry(id, &entry);
[email protected]996139412014-05-10 06:19:50360 if (error == FILE_ERROR_NOT_FOUND)
[email protected]3361a542013-05-22 17:38:27361 return FILE_ERROR_OK;
[email protected]996139412014-05-10 06:19:50362 if (error != FILE_ERROR_OK)
363 return error;
[email protected]cd8fd37f2014-05-20 15:45:21364 if (!entry.file_specific_info().has_cache_state())
365 return FILE_ERROR_OK;
[email protected]3361a542013-05-22 17:38:27366
[email protected]d1ad8fa2013-07-11 13:23:20367 // Cannot delete a mounted file.
[email protected]c9e4738d2013-08-26 03:04:07368 if (mounted_files_.count(id))
[email protected]4b60a25f2013-06-17 09:43:11369 return FILE_ERROR_IN_USE;
370
[email protected]91a464e62013-07-10 09:30:06371 // Delete the file.
[email protected]c9e4738d2013-08-26 03:04:07372 base::FilePath path = GetCacheFilePath(id);
[email protected]dd3aa792013-07-16 19:10:23373 if (!base::DeleteFile(path, false /* recursive */))
[email protected]91a464e62013-07-10 09:30:06374 return FILE_ERROR_FAILED;
[email protected]3361a542013-05-22 17:38:27375
376 // Now that all file operations have completed, remove from metadata.
[email protected]cd8fd37f2014-05-20 15:45:21377 entry.mutable_file_specific_info()->clear_cache_state();
378 return storage_->PutEntry(entry);
[email protected]3361a542013-05-22 17:38:27379}
380
[email protected]823ca9712013-09-13 10:09:09381bool FileCache::ClearAll() {
382 AssertOnSequencedWorkerPool();
[email protected]f861b392012-08-03 20:41:12383
[email protected]823ca9712013-09-13 10:09:09384 // 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]f861b392012-08-03 20:41:12393}
394
[email protected]34a1bbf32013-06-17 07:24:02395bool FileCache::Initialize() {
[email protected]ca5f6da2012-06-18 12:54:59396 AssertOnSequencedWorkerPool();
397
[email protected]b1bf19a2014-01-21 04:45:19398 // Older versions do not clear MD5 when marking entries dirty.
399 // Clear MD5 of all dirty entries to deal with old data.
[email protected]cd8fd37f2014-05-20 15:45:21400 scoped_ptr<ResourceMetadataStorage::Iterator> it = storage_->GetIterator();
[email protected]b1bf19a2014-01-21 04:45:19401 for (; !it->IsAtEnd(); it->Advance()) {
[email protected]cd8fd37f2014-05-20 15:45:21402 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]b1bf19a2014-01-21 04:45:19407 return false;
408 }
409 }
[email protected]996139412014-05-10 06:19:50410 if (it->HasError())
411 return false;
[email protected]b1bf19a2014-01-21 04:45:19412
[email protected]f2731d12013-10-22 03:23:15413 if (!RenameCacheFilesToNewFormat())
414 return false;
[email protected]e8842b192013-06-11 04:05:14415 return true;
[email protected]ca5f6da2012-06-18 12:54:59416}
417
[email protected]34a1bbf32013-06-17 07:24:02418void FileCache::Destroy() {
lukasza037c10b12015-06-12 04:21:25419 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]34a1bbf32013-06-17 07:24:02420
hashimoto246e4a82015-04-17 07:44:49421 in_shutdown_.Set();
422
[email protected]34a1bbf32013-06-17 07:24:02423 // 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]eca3fc92013-05-01 03:53:40431void FileCache::DestroyOnBlockingPool() {
[email protected]73f9c742012-06-15 07:37:13432 AssertOnSequencedWorkerPool();
433 delete this;
434}
435
[email protected]b7af4f12013-10-31 06:57:45436bool FileCache::RecoverFilesFromCacheDirectory(
[email protected]760abc32013-11-01 05:13:01437 const base::FilePath& dest_directory,
[email protected]026d4a522013-11-05 14:22:18438 const ResourceMetadataStorage::RecoveredCacheInfoMap&
439 recovered_cache_info) {
[email protected]b7af4f12013-10-31 06:57:45440 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]cd8fd37f2014-05-20 15:45:21448 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]b7af4f12013-10-31 06:57:45454 // This file is managed by FileCache, no need to recover it.
455 continue;
456 }
457
[email protected]760abc32013-11-01 05:13:01458 // 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]026d4a522013-11-05 14:22:18461 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 &&
hashimoto246e4a82015-04-17 07:44:49467 it->second.md5 == util::GetMd5Digest(current, &in_shutdown_)) {
[email protected]760abc32013-11-01 05:13:01468 base::DeleteFile(current, false /* recursive */);
469 continue;
470 }
471 }
472
[email protected]b7af4f12013-10-31 06:57:45473 // Read file contents to sniff mime type.
474 std::vector<char> content(net::kMaxBytesToSniff);
475 const int read_result =
[email protected]7600d0b2013-12-08 21:43:30476 base::ReadFile(current, &content[0], content.size());
[email protected]b7af4f12013-10-31 06:57:45477 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]026d4a522013-11-05 14:22:18484 // Use recovered file name if available, otherwise decide file name with
485 // sniffed mime type.
[email protected]b7af4f12013-10-31 06:57:45486 base::FilePath dest_base_name(FILE_PATH_LITERAL("file"));
487 std::string mime_type;
[email protected]026d4a522013-11-05 14:22:18488 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]b7af4f12013-10-31 06:57:45496 // 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]426d1c92013-12-03 20:08:54521 if (!base::CreateDirectory(dest_directory) ||
[email protected]b7af4f12013-10-31 06:57:45522 !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]d105b3ad2013-11-01 05:33:13528 UMA_HISTOGRAM_COUNTS("Drive.NumberOfCacheFilesRecoveredAfterDBCorruption",
529 file_number - 1);
[email protected]b7af4f12013-10-31 06:57:45530 return true;
531}
532
[email protected]54ba37502013-05-09 08:43:40533FileError FileCache::MarkAsUnmounted(const base::FilePath& file_path) {
[email protected]9564c1502012-11-28 12:12:16534 AssertOnSequencedWorkerPool();
[email protected]eca3fc92013-05-01 03:53:40535 DCHECK(IsUnderFileCacheDirectory(file_path));
[email protected]9564c1502012-11-28 12:12:16536
[email protected]c9e4738d2013-08-26 03:04:07537 std::string id = GetIdFromPath(file_path);
[email protected]9564c1502012-11-28 12:12:16538
[email protected]cd8fd37f2014-05-20 15:45:21539 // Get the entry associated with the id.
540 ResourceEntry entry;
541 FileError error = storage_->GetEntry(id, &entry);
[email protected]996139412014-05-10 06:19:50542 if (error != FILE_ERROR_OK)
543 return error;
[email protected]9564c1502012-11-28 12:12:16544
[email protected]c9e4738d2013-08-26 03:04:07545 std::set<std::string>::iterator it = mounted_files_.find(id);
[email protected]4b60a25f2013-06-17 09:43:11546 if (it == mounted_files_.end())
[email protected]78a158b2013-04-23 06:57:49547 return FILE_ERROR_INVALID_OPERATION;
[email protected]9564c1502012-11-28 12:12:16548
[email protected]4b60a25f2013-06-17 09:43:11549 mounted_files_.erase(it);
[email protected]78a158b2013-04-23 06:57:49550 return FILE_ERROR_OK;
[email protected]9564c1502012-11-28 12:12:16551}
552
[email protected]eca3fc92013-05-01 03:53:40553bool FileCache::HasEnoughSpaceFor(int64 num_bytes,
554 const base::FilePath& path) {
[email protected]f6fd98a2012-12-14 00:04:02555 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]ab3ed7e4a2013-12-12 09:18:30562 free_space -= cryptohome::kMinFreeSpaceInBytes;
[email protected]f6fd98a2012-12-14 00:04:02563 return (free_space >= num_bytes);
564}
565
[email protected]f2731d12013-10-22 03:23:15566bool 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]91a464e62013-07-10 09:30:06583 }
[email protected]f2731d12013-10-22 03:23:15584 return true;
[email protected]91a464e62013-07-10 09:30:06585}
586
[email protected]8b03ab3a2014-01-15 17:52:45587void 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]f92367ae2014-06-02 07:35:43598
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]8b03ab3a2014-01-15 17:52:45614}
615
[email protected]59c7cdec2013-05-07 04:17:13616} // namespace internal
[email protected]d9d04df2012-10-12 07:06:35617} // namespace drive