[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 15de814 | 2012-10-11 06:00:54 | [diff] [blame] | 5 | #include "chrome/browser/chromeos/drive/drive_cache.h" |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 6 | |
| 7 | #include <vector> |
| 8 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 9 | #include "base/file_util.h" |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 11 | #include "base/string_util.h" |
[email protected] | c2b55773 | 2013-04-24 01:46:34 | [diff] [blame] | 12 | #include "base/stringprintf.h" |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 13 | #include "base/sys_info.h" |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 14 | #include "base/task_runner_util.h" |
[email protected] | c2b55773 | 2013-04-24 01:46:34 | [diff] [blame] | 15 | #include "chrome/browser/chromeos/drive/cache_metadata.h" |
[email protected] | bd547ad8 | 2013-04-26 04:52:29 | [diff] [blame] | 16 | #include "chrome/browser/chromeos/drive/cache_observer.h" |
[email protected] | 15de814 | 2012-10-11 06:00:54 | [diff] [blame] | 17 | #include "chrome/browser/chromeos/drive/drive.pb.h" |
[email protected] | 4fa2fd5d | 2013-04-26 03:42:52 | [diff] [blame] | 18 | #include "chrome/browser/chromeos/drive/file_system_util.h" |
[email protected] | d68ede0 | 2012-11-07 14:30:53 | [diff] [blame] | 19 | #include "chrome/browser/google_apis/task_util.h" |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 20 | #include "chrome/browser/profiles/profile.h" |
| 21 | #include "chrome/common/chrome_constants.h" |
| 22 | #include "chrome/common/chrome_paths_internal.h" |
[email protected] | 7986b8d | 2012-06-14 15:05:14 | [diff] [blame] | 23 | #include "content/public/browser/browser_thread.h" |
| 24 | |
| 25 | using content::BrowserThread; |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 26 | |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 27 | namespace drive { |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 28 | namespace { |
| 29 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 30 | const base::FilePath::CharType kDriveCacheVersionDir[] = |
| 31 | FILE_PATH_LITERAL("v1"); |
| 32 | const base::FilePath::CharType kDriveCacheMetaDir[] = FILE_PATH_LITERAL("meta"); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 33 | const base::FilePath::CharType kDriveCacheOutgoingDir[] = |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 34 | FILE_PATH_LITERAL("outgoing"); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 35 | const base::FilePath::CharType kDriveCachePersistentDir[] = |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 36 | FILE_PATH_LITERAL("persistent"); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 37 | const base::FilePath::CharType kDriveCacheTmpDir[] = FILE_PATH_LITERAL("tmp"); |
| 38 | const base::FilePath::CharType kDriveCacheTmpDownloadsDir[] = |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 39 | FILE_PATH_LITERAL("tmp/downloads"); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 40 | const base::FilePath::CharType kDriveCacheTmpDocumentsDir[] = |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 41 | FILE_PATH_LITERAL("tmp/documents"); |
| 42 | |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 43 | // Create cache directory paths and set permissions. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 44 | bool InitCachePaths(const std::vector<base::FilePath>& cache_paths) { |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 45 | if (cache_paths.size() < DriveCache::NUM_CACHE_TYPES) { |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 46 | NOTREACHED(); |
| 47 | LOG(ERROR) << "Size of cache_paths is invalid."; |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame] | 48 | return false; |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 49 | } |
| 50 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 51 | if (!DriveCache::CreateCacheDirectories(cache_paths)) |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame] | 52 | return false; |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 53 | |
| 54 | // Change permissions of cache persistent directory to u+rwx,og+x (711) in |
| 55 | // order to allow archive files in that directory to be mounted by cros-disks. |
| 56 | file_util::SetPosixFilePermissions( |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 57 | cache_paths[DriveCache::CACHE_TYPE_PERSISTENT], |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 58 | file_util::FILE_PERMISSION_USER_MASK | |
| 59 | file_util::FILE_PERMISSION_EXECUTE_BY_GROUP | |
| 60 | file_util::FILE_PERMISSION_EXECUTE_BY_OTHERS); |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame] | 61 | |
| 62 | return true; |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 63 | } |
| 64 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 65 | // Remove all files under the given directory, non-recursively. |
[email protected] | d7754f5 | 2012-08-01 08:45:33 | [diff] [blame] | 66 | // Do not remove recursively as we don't want to touch <gcache>/tmp/downloads, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 67 | // which is used for user initiated downloads like "Save As" |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 68 | void RemoveAllFiles(const base::FilePath& directory) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 69 | using file_util::FileEnumerator; |
| 70 | |
| 71 | FileEnumerator enumerator(directory, false /* recursive */, |
| 72 | FileEnumerator::FILES); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 73 | for (base::FilePath file_path = enumerator.Next(); !file_path.empty(); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 74 | file_path = enumerator.Next()) { |
| 75 | DVLOG(1) << "Removing " << file_path.value(); |
| 76 | if (!file_util::Delete(file_path, false /* recursive */)) |
| 77 | LOG(WARNING) << "Failed to delete " << file_path.value(); |
| 78 | } |
| 79 | } |
| 80 | |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 81 | // Deletes the symlink. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 82 | void DeleteSymlink(const base::FilePath& symlink_path) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 83 | // We try to save one file operation by not checking if link exists before |
| 84 | // deleting it, so unlink may return error if link doesn't exist, but it |
| 85 | // doesn't really matter to us. |
[email protected] | 750af1d | 2012-07-13 14:32:43 | [diff] [blame] | 86 | file_util::Delete(symlink_path, false); |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 87 | } |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 88 | |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 89 | // Creates a symlink. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 90 | bool CreateSymlink(const base::FilePath& cache_file_path, |
| 91 | const base::FilePath& symlink_path) { |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 92 | // Remove symlink because creating a link will not overwrite an existing one. |
| 93 | DeleteSymlink(symlink_path); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 94 | |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 95 | // Create new symlink to |cache_file_path|. |
| 96 | if (!file_util::CreateSymbolicLink(cache_file_path, symlink_path)) { |
[email protected] | 750af1d | 2012-07-13 14:32:43 | [diff] [blame] | 97 | LOG(ERROR) << "Failed to create a symlink from " << symlink_path.value() |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 98 | << " to " << cache_file_path.value(); |
| 99 | return false; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 100 | } |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 101 | return true; |
| 102 | } |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 103 | |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 104 | // Moves the file. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 105 | bool MoveFile(const base::FilePath& source_path, |
| 106 | const base::FilePath& dest_path) { |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 107 | if (!file_util::Move(source_path, dest_path)) { |
| 108 | LOG(ERROR) << "Failed to move " << source_path.value() |
| 109 | << " to " << dest_path.value(); |
| 110 | return false; |
| 111 | } |
| 112 | DVLOG(1) << "Moved " << source_path.value() << " to " << dest_path.value(); |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | // Copies the file. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 117 | bool CopyFile(const base::FilePath& source_path, |
| 118 | const base::FilePath& dest_path) { |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 119 | if (!file_util::CopyFile(source_path, dest_path)) { |
| 120 | LOG(ERROR) << "Failed to copy " << source_path.value() |
| 121 | << " to " << dest_path.value(); |
| 122 | return false; |
| 123 | } |
| 124 | DVLOG(1) << "Copied " << source_path.value() << " to " << dest_path.value(); |
| 125 | return true; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // Deletes all files that match |path_to_delete_pattern| except for |
| 129 | // |path_to_keep| on blocking pool. |
| 130 | // If |path_to_keep| is empty, all files in |path_to_delete_pattern| are |
| 131 | // deleted. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 132 | void DeleteFilesSelectively(const base::FilePath& path_to_delete_pattern, |
| 133 | const base::FilePath& path_to_keep) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 134 | // Enumerate all files in directory of |path_to_delete_pattern| that match |
| 135 | // base name of |path_to_delete_pattern|. |
| 136 | // If a file is not |path_to_keep|, delete it. |
| 137 | bool success = true; |
[email protected] | 84c3f16 | 2012-08-12 01:57:23 | [diff] [blame] | 138 | file_util::FileEnumerator enumerator(path_to_delete_pattern.DirName(), |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 139 | false, // not recursive |
[email protected] | 84c3f16 | 2012-08-12 01:57:23 | [diff] [blame] | 140 | file_util::FileEnumerator::FILES | |
| 141 | file_util::FileEnumerator::SHOW_SYM_LINKS, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 142 | path_to_delete_pattern.BaseName().value()); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 143 | for (base::FilePath current = enumerator.Next(); !current.empty(); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 144 | current = enumerator.Next()) { |
| 145 | // If |path_to_keep| is not empty and same as current, don't delete it. |
| 146 | if (!path_to_keep.empty() && current == path_to_keep) |
| 147 | continue; |
| 148 | |
[email protected] | 0b8d4cee | 2012-07-02 20:46:26 | [diff] [blame] | 149 | success = file_util::Delete(current, false); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 150 | if (!success) |
| 151 | DVLOG(1) << "Error deleting " << current.value(); |
| 152 | else |
| 153 | DVLOG(1) << "Deleted " << current.value(); |
| 154 | } |
| 155 | } |
| 156 | |
[email protected] | 7986b8d | 2012-06-14 15:05:14 | [diff] [blame] | 157 | // Runs callback with pointers dereferenced. |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 158 | // Used to implement GetFile, MarkAsMounted. |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 159 | void RunGetFileFromCacheCallback( |
| 160 | const GetFileFromCacheCallback& callback, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 161 | scoped_ptr<std::pair<FileError, base::FilePath> > result) { |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 162 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 163 | DCHECK(!callback.is_null()); |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 164 | DCHECK(result.get()); |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 165 | |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 166 | callback.Run(result->first, result->second); |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 169 | // Runs callback with pointers dereferenced. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 170 | // Used to implement GetCacheEntry(). |
| 171 | void RunGetCacheEntryCallback(const GetCacheEntryCallback& callback, |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 172 | DriveCacheEntry* cache_entry, |
| 173 | bool success) { |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 174 | DCHECK(cache_entry); |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 175 | DCHECK(!callback.is_null()); |
| 176 | callback.Run(success, *cache_entry); |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 179 | } // namespace |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 180 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 181 | DriveCache::DriveCache(const base::FilePath& cache_root_path, |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 182 | base::SequencedTaskRunner* blocking_task_runner, |
| 183 | FreeDiskSpaceGetterInterface* free_disk_space_getter) |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 184 | : cache_root_path_(cache_root_path), |
[email protected] | 6b70c7b | 2012-06-14 03:10:43 | [diff] [blame] | 185 | cache_paths_(GetCachePaths(cache_root_path_)), |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 186 | blocking_task_runner_(blocking_task_runner), |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 187 | free_disk_space_getter_(free_disk_space_getter), |
[email protected] | dc2e8f7b | 2012-08-30 20:22:44 | [diff] [blame] | 188 | ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
[email protected] | 17196ee | 2012-12-13 06:23:51 | [diff] [blame] | 189 | DCHECK(blocking_task_runner_); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 190 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 191 | } |
| 192 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 193 | DriveCache::~DriveCache() { |
[email protected] | 17196ee | 2012-12-13 06:23:51 | [diff] [blame] | 194 | // Must be on the sequenced worker pool, as |metadata_| must be deleted on |
| 195 | // the sequenced worker pool. |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 196 | AssertOnSequencedWorkerPool(); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 197 | } |
| 198 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 199 | base::FilePath DriveCache::GetCacheDirectoryPath( |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 200 | CacheSubDirectoryType sub_dir_type) const { |
| 201 | DCHECK_LE(0, sub_dir_type); |
| 202 | DCHECK_GT(NUM_CACHE_TYPES, sub_dir_type); |
| 203 | return cache_paths_[sub_dir_type]; |
| 204 | } |
| 205 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 206 | base::FilePath DriveCache::GetCacheFilePath( |
| 207 | const std::string& resource_id, |
| 208 | const std::string& md5, |
| 209 | CacheSubDirectoryType sub_dir_type, |
| 210 | CachedFileOrigin file_origin) const { |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 211 | DCHECK(sub_dir_type != CACHE_TYPE_META); |
| 212 | |
| 213 | // Runs on any thread. |
| 214 | // Filename is formatted as resource_id.md5, i.e. resource_id is the base |
| 215 | // name and md5 is the extension. |
| 216 | std::string base_name = util::EscapeCacheFileName(resource_id); |
| 217 | if (file_origin == CACHED_FILE_LOCALLY_MODIFIED) { |
| 218 | DCHECK(sub_dir_type == CACHE_TYPE_PERSISTENT); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 219 | base_name += base::FilePath::kExtensionSeparator; |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 220 | base_name += util::kLocallyModifiedFileExtension; |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 221 | } else if (!md5.empty()) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 222 | base_name += base::FilePath::kExtensionSeparator; |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 223 | base_name += util::EscapeCacheFileName(md5); |
| 224 | } |
| 225 | // For mounted archives the filename is formatted as resource_id.md5.mounted, |
| 226 | // i.e. resource_id.md5 is the base name and ".mounted" is the extension |
| 227 | if (file_origin == CACHED_FILE_MOUNTED) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 228 | DCHECK(sub_dir_type == CACHE_TYPE_PERSISTENT); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 229 | base_name += base::FilePath::kExtensionSeparator; |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 230 | base_name += util::kMountedArchiveFileExtension; |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 231 | } |
[email protected] | 2333b2a1 | 2013-04-26 07:22:22 | [diff] [blame^] | 232 | return GetCacheDirectoryPath(sub_dir_type).Append( |
| 233 | base::FilePath::FromUTF8Unsafe(base_name)); |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 234 | } |
| 235 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 236 | void DriveCache::AssertOnSequencedWorkerPool() { |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 237 | DCHECK(!blocking_task_runner_ || |
| 238 | blocking_task_runner_->RunsTasksOnCurrentThread()); |
[email protected] | fcc92a5 | 2012-06-08 22:54:16 | [diff] [blame] | 239 | } |
| 240 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 241 | bool DriveCache::IsUnderDriveCacheDirectory(const base::FilePath& path) const { |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 242 | return cache_root_path_ == path || cache_root_path_.IsParent(path); |
| 243 | } |
| 244 | |
[email protected] | bd547ad8 | 2013-04-26 04:52:29 | [diff] [blame] | 245 | void DriveCache::AddObserver(CacheObserver* observer) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 246 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 247 | observers_.AddObserver(observer); |
| 248 | } |
| 249 | |
[email protected] | bd547ad8 | 2013-04-26 04:52:29 | [diff] [blame] | 250 | void DriveCache::RemoveObserver(CacheObserver* observer) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 251 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 252 | observers_.RemoveObserver(observer); |
| 253 | } |
| 254 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 255 | void DriveCache::GetCacheEntry(const std::string& resource_id, |
| 256 | const std::string& md5, |
| 257 | const GetCacheEntryCallback& callback) { |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 258 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 259 | DCHECK(!callback.is_null()); |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 260 | |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 261 | DriveCacheEntry* cache_entry = new DriveCacheEntry; |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 262 | base::PostTaskAndReplyWithResult( |
| 263 | blocking_task_runner_, |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 264 | FROM_HERE, |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 265 | base::Bind(&DriveCache::GetCacheEntryOnBlockingPool, |
[email protected] | 3f5e290 | 2012-11-06 08:21:15 | [diff] [blame] | 266 | base::Unretained(this), resource_id, md5, cache_entry), |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 267 | base::Bind(&RunGetCacheEntryCallback, |
[email protected] | 3f5e290 | 2012-11-06 08:21:15 | [diff] [blame] | 268 | callback, base::Owned(cache_entry))); |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 269 | } |
| 270 | |
[email protected] | d68ede0 | 2012-11-07 14:30:53 | [diff] [blame] | 271 | void DriveCache::Iterate(const CacheIterateCallback& iteration_callback, |
| 272 | const base::Closure& completion_callback) { |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 273 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | d68ede0 | 2012-11-07 14:30:53 | [diff] [blame] | 274 | DCHECK(!iteration_callback.is_null()); |
| 275 | DCHECK(!completion_callback.is_null()); |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 276 | |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 277 | blocking_task_runner_->PostTaskAndReply( |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 278 | FROM_HERE, |
[email protected] | d68ede0 | 2012-11-07 14:30:53 | [diff] [blame] | 279 | base::Bind(&DriveCache::IterateOnBlockingPool, |
| 280 | base::Unretained(this), |
| 281 | google_apis::CreateRelayCallback(iteration_callback)), |
| 282 | completion_callback); |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 283 | } |
| 284 | |
[email protected] | f423c0b | 2012-11-22 09:51:10 | [diff] [blame] | 285 | void DriveCache::FreeDiskSpaceIfNeededFor( |
| 286 | int64 num_bytes, |
| 287 | const InitializeCacheCallback& callback) { |
| 288 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 289 | DCHECK(!callback.is_null()); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 290 | |
[email protected] | f423c0b | 2012-11-22 09:51:10 | [diff] [blame] | 291 | base::PostTaskAndReplyWithResult( |
| 292 | blocking_task_runner_, |
| 293 | FROM_HERE, |
| 294 | base::Bind(&DriveCache::FreeDiskSpaceOnBlockingPoolIfNeededFor, |
| 295 | base::Unretained(this), |
| 296 | num_bytes), |
| 297 | callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 298 | } |
| 299 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 300 | void DriveCache::GetFile(const std::string& resource_id, |
| 301 | const std::string& md5, |
| 302 | const GetFileFromCacheCallback& callback) { |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 303 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 304 | DCHECK(!callback.is_null()); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 305 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 306 | base::PostTaskAndReplyWithResult( |
| 307 | blocking_task_runner_, |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 308 | FROM_HERE, |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 309 | base::Bind(&DriveCache::GetFileOnBlockingPool, |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 310 | base::Unretained(this), resource_id, md5), |
| 311 | base::Bind(&RunGetFileFromCacheCallback, callback)); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 312 | } |
| 313 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 314 | void DriveCache::Store(const std::string& resource_id, |
| 315 | const std::string& md5, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 316 | const base::FilePath& source_path, |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 317 | FileOperationType file_operation_type, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 318 | const FileOperationCallback& callback) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 319 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 320 | DCHECK(!callback.is_null()); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 321 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 322 | base::PostTaskAndReplyWithResult( |
| 323 | blocking_task_runner_, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 324 | FROM_HERE, |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 325 | base::Bind(&DriveCache::StoreOnBlockingPool, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 326 | base::Unretained(this), |
[email protected] | 3f5e290 | 2012-11-06 08:21:15 | [diff] [blame] | 327 | resource_id, md5, source_path, file_operation_type), |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 328 | callback); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 329 | } |
| 330 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 331 | void DriveCache::Pin(const std::string& resource_id, |
| 332 | const std::string& md5, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 333 | const FileOperationCallback& callback) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 334 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 335 | DCHECK(!callback.is_null()); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 336 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 337 | base::PostTaskAndReplyWithResult( |
| 338 | blocking_task_runner_, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 339 | FROM_HERE, |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 340 | base::Bind(&DriveCache::PinOnBlockingPool, |
[email protected] | 3f5e290 | 2012-11-06 08:21:15 | [diff] [blame] | 341 | base::Unretained(this), resource_id, md5), |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 342 | base::Bind(&DriveCache::OnPinned, |
[email protected] | 3f5e290 | 2012-11-06 08:21:15 | [diff] [blame] | 343 | weak_ptr_factory_.GetWeakPtr(), resource_id, md5, callback)); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 344 | } |
| 345 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 346 | void DriveCache::Unpin(const std::string& resource_id, |
| 347 | const std::string& md5, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 348 | const FileOperationCallback& callback) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 349 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 350 | DCHECK(!callback.is_null()); |
| 351 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 352 | base::PostTaskAndReplyWithResult( |
| 353 | blocking_task_runner_, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 354 | FROM_HERE, |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 355 | base::Bind(&DriveCache::UnpinOnBlockingPool, |
[email protected] | 3f5e290 | 2012-11-06 08:21:15 | [diff] [blame] | 356 | base::Unretained(this), resource_id, md5), |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 357 | base::Bind(&DriveCache::OnUnpinned, |
[email protected] | 3f5e290 | 2012-11-06 08:21:15 | [diff] [blame] | 358 | weak_ptr_factory_.GetWeakPtr(), resource_id, md5, callback)); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 359 | } |
| 360 | |
[email protected] | 35c1f9b | 2013-02-07 07:39:42 | [diff] [blame] | 361 | void DriveCache::MarkAsMounted(const std::string& resource_id, |
| 362 | const std::string& md5, |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 363 | const GetFileFromCacheCallback& callback) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 364 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 365 | DCHECK(!callback.is_null()); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 366 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 367 | base::PostTaskAndReplyWithResult( |
| 368 | blocking_task_runner_, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 369 | FROM_HERE, |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 370 | base::Bind(&DriveCache::MarkAsMountedOnBlockingPool, |
[email protected] | 35c1f9b | 2013-02-07 07:39:42 | [diff] [blame] | 371 | base::Unretained(this), resource_id, md5), |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 372 | base::Bind(RunGetFileFromCacheCallback, callback)); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 373 | } |
| 374 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 375 | void DriveCache::MarkAsUnmounted(const base::FilePath& file_path, |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 376 | const FileOperationCallback& callback) { |
| 377 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 378 | DCHECK(!callback.is_null()); |
| 379 | |
| 380 | base::PostTaskAndReplyWithResult( |
| 381 | blocking_task_runner_, |
| 382 | FROM_HERE, |
| 383 | base::Bind(&DriveCache::MarkAsUnmountedOnBlockingPool, |
| 384 | base::Unretained(this), file_path), |
| 385 | callback); |
| 386 | } |
| 387 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 388 | void DriveCache::MarkDirty(const std::string& resource_id, |
| 389 | const std::string& md5, |
[email protected] | bc809e4 | 2012-11-28 04:46:29 | [diff] [blame] | 390 | const FileOperationCallback& callback) { |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 391 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 392 | DCHECK(!callback.is_null()); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 393 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 394 | base::PostTaskAndReplyWithResult( |
| 395 | blocking_task_runner_, |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 396 | FROM_HERE, |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 397 | base::Bind(&DriveCache::MarkDirtyOnBlockingPool, |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 398 | base::Unretained(this), resource_id, md5), |
[email protected] | bc809e4 | 2012-11-28 04:46:29 | [diff] [blame] | 399 | callback); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 400 | } |
| 401 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 402 | void DriveCache::CommitDirty(const std::string& resource_id, |
| 403 | const std::string& md5, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 404 | const FileOperationCallback& callback) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 405 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 406 | DCHECK(!callback.is_null()); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 407 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 408 | base::PostTaskAndReplyWithResult( |
| 409 | blocking_task_runner_, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 410 | FROM_HERE, |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 411 | base::Bind(&DriveCache::CommitDirtyOnBlockingPool, |
[email protected] | 3f5e290 | 2012-11-06 08:21:15 | [diff] [blame] | 412 | base::Unretained(this), resource_id, md5), |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 413 | base::Bind(&DriveCache::OnCommitDirty, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 414 | weak_ptr_factory_.GetWeakPtr(), resource_id, callback)); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 415 | } |
| 416 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 417 | void DriveCache::ClearDirty(const std::string& resource_id, |
| 418 | const std::string& md5, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 419 | const FileOperationCallback& callback) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 420 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 421 | DCHECK(!callback.is_null()); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 422 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 423 | base::PostTaskAndReplyWithResult( |
| 424 | blocking_task_runner_, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 425 | FROM_HERE, |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 426 | base::Bind(&DriveCache::ClearDirtyOnBlockingPool, |
[email protected] | 3f5e290 | 2012-11-06 08:21:15 | [diff] [blame] | 427 | base::Unretained(this), resource_id, md5), |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 428 | callback); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 429 | } |
| 430 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 431 | void DriveCache::Remove(const std::string& resource_id, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 432 | const FileOperationCallback& callback) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 433 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 434 | DCHECK(!callback.is_null()); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 435 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 436 | base::PostTaskAndReplyWithResult( |
| 437 | blocking_task_runner_, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 438 | FROM_HERE, |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 439 | base::Bind(&DriveCache::RemoveOnBlockingPool, |
[email protected] | 3f5e290 | 2012-11-06 08:21:15 | [diff] [blame] | 440 | base::Unretained(this), resource_id), |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 441 | callback); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 442 | } |
| 443 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 444 | void DriveCache::ClearAll(const InitializeCacheCallback& callback) { |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 445 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 446 | DCHECK(!callback.is_null()); |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 447 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 448 | base::PostTaskAndReplyWithResult( |
| 449 | blocking_task_runner_, |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 450 | FROM_HERE, |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 451 | base::Bind(&DriveCache::ClearAllOnBlockingPool, base::Unretained(this)), |
| 452 | callback); |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 453 | } |
| 454 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 455 | void DriveCache::RequestInitialize(const InitializeCacheCallback& callback) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 456 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame] | 457 | DCHECK(!callback.is_null()); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 458 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 459 | base::PostTaskAndReplyWithResult( |
| 460 | blocking_task_runner_, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 461 | FROM_HERE, |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 462 | base::Bind(&DriveCache::InitializeOnBlockingPool, base::Unretained(this)), |
| 463 | callback); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 464 | } |
| 465 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 466 | void DriveCache::RequestInitializeForTesting() { |
[email protected] | d310bfc | 2012-08-10 09:41:28 | [diff] [blame] | 467 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 468 | |
| 469 | blocking_task_runner_->PostTask( |
| 470 | FROM_HERE, |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 471 | base::Bind(&DriveCache::InitializeOnBlockingPoolForTesting, |
| 472 | base::Unretained(this))); |
[email protected] | d310bfc | 2012-08-10 09:41:28 | [diff] [blame] | 473 | } |
| 474 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 475 | void DriveCache::Destroy() { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 476 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 477 | |
| 478 | // Invalidate the weak pointer. |
[email protected] | e53ac8f | 2012-08-02 07:05:00 | [diff] [blame] | 479 | weak_ptr_factory_.InvalidateWeakPtrs(); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 480 | |
| 481 | // Destroy myself on the blocking pool. |
[email protected] | 17196ee | 2012-12-13 06:23:51 | [diff] [blame] | 482 | // Note that base::DeletePointer<> cannot be used as the destructor of this |
| 483 | // class is private. |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 484 | blocking_task_runner_->PostTask( |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 485 | FROM_HERE, |
[email protected] | 3f5e290 | 2012-11-06 08:21:15 | [diff] [blame] | 486 | base::Bind(&DriveCache::DestroyOnBlockingPool, base::Unretained(this))); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 487 | } |
| 488 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 489 | bool DriveCache::InitializeOnBlockingPool() { |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 490 | AssertOnSequencedWorkerPool(); |
| 491 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 492 | if (!InitCachePaths(cache_paths_)) |
| 493 | return false; |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame] | 494 | |
[email protected] | c2b55773 | 2013-04-24 01:46:34 | [diff] [blame] | 495 | metadata_ = CacheMetadata::CreateCacheMetadata( |
[email protected] | f5532c4 | 2012-11-21 10:39:56 | [diff] [blame] | 496 | blocking_task_runner_); |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 497 | return metadata_->Initialize(cache_paths_); |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 498 | } |
| 499 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 500 | void DriveCache::InitializeOnBlockingPoolForTesting() { |
[email protected] | d310bfc | 2012-08-10 09:41:28 | [diff] [blame] | 501 | AssertOnSequencedWorkerPool(); |
| 502 | |
| 503 | InitCachePaths(cache_paths_); |
[email protected] | c2b55773 | 2013-04-24 01:46:34 | [diff] [blame] | 504 | metadata_ = CacheMetadata::CreateCacheMetadataForTesting( |
[email protected] | f5532c4 | 2012-11-21 10:39:56 | [diff] [blame] | 505 | blocking_task_runner_); |
[email protected] | d310bfc | 2012-08-10 09:41:28 | [diff] [blame] | 506 | metadata_->Initialize(cache_paths_); |
| 507 | } |
| 508 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 509 | void DriveCache::DestroyOnBlockingPool() { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 510 | AssertOnSequencedWorkerPool(); |
| 511 | delete this; |
| 512 | } |
| 513 | |
[email protected] | 717e43c | 2012-11-22 07:47:39 | [diff] [blame] | 514 | bool DriveCache::GetCacheEntryOnBlockingPool(const std::string& resource_id, |
| 515 | const std::string& md5, |
| 516 | DriveCacheEntry* entry) { |
| 517 | DCHECK(entry); |
| 518 | AssertOnSequencedWorkerPool(); |
| 519 | return metadata_->GetCacheEntry(resource_id, md5, entry); |
| 520 | } |
| 521 | |
[email protected] | d68ede0 | 2012-11-07 14:30:53 | [diff] [blame] | 522 | void DriveCache::IterateOnBlockingPool( |
| 523 | const CacheIterateCallback& iteration_callback) { |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 524 | AssertOnSequencedWorkerPool(); |
[email protected] | d68ede0 | 2012-11-07 14:30:53 | [diff] [blame] | 525 | DCHECK(!iteration_callback.is_null()); |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 526 | |
[email protected] | d68ede0 | 2012-11-07 14:30:53 | [diff] [blame] | 527 | metadata_->Iterate(iteration_callback); |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 528 | } |
| 529 | |
[email protected] | f423c0b | 2012-11-22 09:51:10 | [diff] [blame] | 530 | bool DriveCache::FreeDiskSpaceOnBlockingPoolIfNeededFor(int64 num_bytes) { |
| 531 | AssertOnSequencedWorkerPool(); |
| 532 | |
| 533 | // Do nothing and return if we have enough space. |
| 534 | if (HasEnoughSpaceFor(num_bytes, cache_root_path_)) |
| 535 | return true; |
| 536 | |
| 537 | // Otherwise, try to free up the disk space. |
| 538 | DVLOG(1) << "Freeing up disk space for " << num_bytes; |
| 539 | // First remove temporary files from the metadata. |
| 540 | metadata_->RemoveTemporaryFiles(); |
| 541 | // Then remove all files under "tmp" directory. |
| 542 | RemoveAllFiles(GetCacheDirectoryPath(CACHE_TYPE_TMP)); |
| 543 | |
| 544 | // Check the disk space again. |
| 545 | return HasEnoughSpaceFor(num_bytes, cache_root_path_); |
| 546 | } |
| 547 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 548 | scoped_ptr<DriveCache::GetFileResult> DriveCache::GetFileOnBlockingPool( |
| 549 | const std::string& resource_id, |
| 550 | const std::string& md5) { |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 551 | AssertOnSequencedWorkerPool(); |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 552 | |
| 553 | scoped_ptr<GetFileResult> result(new GetFileResult); |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 554 | |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 555 | DriveCacheEntry cache_entry; |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 556 | if (!GetCacheEntryOnBlockingPool(resource_id, md5, &cache_entry) || |
| 557 | !cache_entry.is_present()) { |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 558 | result->first = FILE_ERROR_NOT_FOUND; |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 559 | return result.Pass(); |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 560 | } |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 561 | |
| 562 | CachedFileOrigin file_origin; |
| 563 | if (cache_entry.is_mounted()) { |
| 564 | file_origin = CACHED_FILE_MOUNTED; |
| 565 | } else if (cache_entry.is_dirty()) { |
| 566 | file_origin = CACHED_FILE_LOCALLY_MODIFIED; |
| 567 | } else { |
| 568 | file_origin = CACHED_FILE_FROM_SERVER; |
| 569 | } |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 570 | result->first = FILE_ERROR_OK; |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 571 | result->second = GetCacheFilePath(resource_id, |
| 572 | md5, |
| 573 | GetSubDirectoryType(cache_entry), |
| 574 | file_origin); |
| 575 | return result.Pass(); |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 576 | } |
| 577 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 578 | FileError DriveCache::StoreOnBlockingPool( |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 579 | const std::string& resource_id, |
| 580 | const std::string& md5, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 581 | const base::FilePath& source_path, |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 582 | FileOperationType file_operation_type) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 583 | AssertOnSequencedWorkerPool(); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 584 | |
[email protected] | fb1b37b | 2013-04-09 04:37:49 | [diff] [blame] | 585 | int64 file_size = 0; |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 586 | if (file_operation_type == FILE_OPERATION_COPY) { |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 587 | if (!file_util::GetFileSize(source_path, &file_size)) { |
| 588 | LOG(WARNING) << "Couldn't get file size for: " << source_path.value(); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 589 | return FILE_ERROR_FAILED; |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 590 | } |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 591 | } |
[email protected] | fb1b37b | 2013-04-09 04:37:49 | [diff] [blame] | 592 | if (!FreeDiskSpaceOnBlockingPoolIfNeededFor(file_size)) |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 593 | return FILE_ERROR_NO_SPACE; |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 594 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 595 | base::FilePath symlink_path; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 596 | CacheSubDirectoryType sub_dir_type = CACHE_TYPE_TMP; |
| 597 | |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 598 | // If file was previously pinned, store it in persistent dir. |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 599 | DriveCacheEntry cache_entry; |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 600 | if (GetCacheEntryOnBlockingPool(resource_id, md5, &cache_entry)) { |
| 601 | // File exists in cache. |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 602 | // If file is dirty or mounted, return error. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 603 | if (cache_entry.is_dirty() || cache_entry.is_mounted()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 604 | LOG(WARNING) << "Can't store a file to replace a " |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 605 | << (cache_entry.is_dirty() ? "dirty" : "mounted") |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 606 | << " file: res_id=" << resource_id |
| 607 | << ", md5=" << md5; |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 608 | return FILE_ERROR_IN_USE; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 609 | } |
| 610 | |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 611 | if (cache_entry.is_pinned()) |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 612 | sub_dir_type = CACHE_TYPE_PERSISTENT; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 613 | } |
| 614 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 615 | base::FilePath dest_path = GetCacheFilePath(resource_id, md5, sub_dir_type, |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 616 | CACHED_FILE_FROM_SERVER); |
| 617 | bool success = false; |
| 618 | switch (file_operation_type) { |
| 619 | case FILE_OPERATION_MOVE: |
| 620 | success = MoveFile(source_path, dest_path); |
| 621 | break; |
| 622 | case FILE_OPERATION_COPY: |
| 623 | success = CopyFile(source_path, dest_path); |
| 624 | break; |
| 625 | default: |
| 626 | NOTREACHED(); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 627 | } |
| 628 | |
[email protected] | f60c670b | 2012-09-13 06:19:25 | [diff] [blame] | 629 | // Determine search pattern for stale filenames corresponding to resource_id, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 630 | // either "<resource_id>*" or "<resource_id>.*". |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 631 | base::FilePath stale_filenames_pattern; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 632 | if (md5.empty()) { |
| 633 | // No md5 means no extension, append '*' after base name, i.e. |
| 634 | // "<resource_id>*". |
| 635 | // Cannot call |dest_path|.ReplaceExtension when there's no md5 extension: |
| 636 | // if base name of |dest_path| (i.e. escaped resource_id) contains the |
| 637 | // extension separator '.', ReplaceExtension will remove it and everything |
| 638 | // after it. The result will be nothing like the escaped resource_id. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 639 | stale_filenames_pattern = |
| 640 | base::FilePath(dest_path.value() + util::kWildCard); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 641 | } else { |
| 642 | // Replace md5 extension with '*' i.e. "<resource_id>.*". |
| 643 | // Note that ReplaceExtension automatically prefixes the extension with the |
| 644 | // extension separator '.'. |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 645 | stale_filenames_pattern = dest_path.ReplaceExtension(util::kWildCard); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | // Delete files that match |stale_filenames_pattern| except for |dest_path|. |
| 649 | DeleteFilesSelectively(stale_filenames_pattern, dest_path); |
| 650 | |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 651 | if (success) { |
| 652 | // Now that file operations have completed, update metadata. |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 653 | cache_entry.set_md5(md5); |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 654 | cache_entry.set_is_present(true); |
| 655 | cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 656 | metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 657 | } |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 658 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 659 | return success ? FILE_ERROR_OK : FILE_ERROR_FAILED; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 660 | } |
| 661 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 662 | FileError DriveCache::PinOnBlockingPool(const std::string& resource_id, |
| 663 | const std::string& md5) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 664 | AssertOnSequencedWorkerPool(); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 665 | |
[email protected] | ae248f0 | 2013-03-06 12:55:41 | [diff] [blame] | 666 | bool is_persistent = true; |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 667 | DriveCacheEntry cache_entry; |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 668 | if (!GetCacheEntryOnBlockingPool(resource_id, md5, &cache_entry)) { |
[email protected] | ae248f0 | 2013-03-06 12:55:41 | [diff] [blame] | 669 | // The file will be first downloaded in 'tmp', then moved to 'persistent'. |
| 670 | is_persistent = false; |
[email protected] | 109eb5c | 2012-07-12 03:20:05 | [diff] [blame] | 671 | } else { // File exists in cache, determines destination path. |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 672 | // Determine source and destination paths. |
| 673 | |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 674 | // If file is dirty or mounted, don't move it. |
[email protected] | ae248f0 | 2013-03-06 12:55:41 | [diff] [blame] | 675 | if (!cache_entry.is_dirty() && !cache_entry.is_mounted()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 676 | // If file was pinned before but actual file blob doesn't exist in cache: |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 677 | // - don't need to move the file. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 678 | if (!cache_entry.is_present()) { |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 679 | DCHECK(cache_entry.is_pinned()); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 680 | return FILE_ERROR_OK; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 681 | } |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 682 | // File exists, move it to persistent dir. |
| 683 | // Gets the current path of the file in cache. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 684 | base::FilePath source_path = GetCacheFilePath(resource_id, |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 685 | md5, |
| 686 | GetSubDirectoryType(cache_entry), |
| 687 | CACHED_FILE_FROM_SERVER); |
[email protected] | ae248f0 | 2013-03-06 12:55:41 | [diff] [blame] | 688 | base::FilePath dest_path = GetCacheFilePath(resource_id, |
| 689 | md5, |
| 690 | CACHE_TYPE_PERSISTENT, |
| 691 | CACHED_FILE_FROM_SERVER); |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 692 | if (!MoveFile(source_path, dest_path)) |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 693 | return FILE_ERROR_FAILED; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 697 | // Now that file operations have completed, update metadata. |
| 698 | cache_entry.set_md5(md5); |
| 699 | cache_entry.set_is_pinned(true); |
[email protected] | ae248f0 | 2013-03-06 12:55:41 | [diff] [blame] | 700 | cache_entry.set_is_persistent(is_persistent); |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 701 | metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 702 | return FILE_ERROR_OK; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 703 | } |
| 704 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 705 | FileError DriveCache::UnpinOnBlockingPool(const std::string& resource_id, |
| 706 | const std::string& md5) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 707 | AssertOnSequencedWorkerPool(); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 708 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 709 | // Unpinning a file means its entry must exist in cache. |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 710 | DriveCacheEntry cache_entry; |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 711 | if (!GetCacheEntryOnBlockingPool(resource_id, md5, &cache_entry)) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 712 | LOG(WARNING) << "Can't unpin a file that wasn't pinned or cached: res_id=" |
| 713 | << resource_id |
| 714 | << ", md5=" << md5; |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 715 | return FILE_ERROR_NOT_FOUND; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 716 | } |
| 717 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 718 | CacheSubDirectoryType sub_dir_type = CACHE_TYPE_TMP; |
| 719 | |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 720 | // If file is dirty or mounted, don't move it. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 721 | if (cache_entry.is_dirty() || cache_entry.is_mounted()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 722 | sub_dir_type = CACHE_TYPE_PERSISTENT; |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 723 | DCHECK(cache_entry.is_persistent()); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 724 | } else { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 725 | // If file was pinned but actual file blob still doesn't exist in cache, |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 726 | // don't need to move the file. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 727 | if (cache_entry.is_present()) { |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 728 | // Gets the current path of the file in cache. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 729 | base::FilePath source_path = GetCacheFilePath(resource_id, |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 730 | md5, |
| 731 | GetSubDirectoryType(cache_entry), |
| 732 | CACHED_FILE_FROM_SERVER); |
| 733 | // File exists, move it to tmp dir. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 734 | base::FilePath dest_path = GetCacheFilePath(resource_id, |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 735 | md5, |
| 736 | CACHE_TYPE_TMP, |
| 737 | CACHED_FILE_FROM_SERVER); |
| 738 | if (!MoveFile(source_path, dest_path)) |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 739 | return FILE_ERROR_FAILED; |
[email protected] | 3423871a | 2012-07-12 00:41:27 | [diff] [blame] | 740 | } |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 741 | } |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 742 | |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 743 | // Now that file operations have completed, update metadata. |
| 744 | if (cache_entry.is_present()) { |
| 745 | cache_entry.set_md5(md5); |
| 746 | cache_entry.set_is_pinned(false); |
| 747 | cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); |
| 748 | metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); |
| 749 | } else { |
| 750 | // Remove the existing entry if we are unpinning a non-present file. |
| 751 | metadata_->RemoveCacheEntry(resource_id); |
| 752 | } |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 753 | return FILE_ERROR_OK; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 754 | } |
| 755 | |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 756 | scoped_ptr<DriveCache::GetFileResult> DriveCache::MarkAsMountedOnBlockingPool( |
[email protected] | 35c1f9b | 2013-02-07 07:39:42 | [diff] [blame] | 757 | const std::string& resource_id, |
| 758 | const std::string& md5) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 759 | AssertOnSequencedWorkerPool(); |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 760 | |
| 761 | scoped_ptr<GetFileResult> result(new GetFileResult); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 762 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 763 | // Get cache entry associated with the resource_id and md5 |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 764 | DriveCacheEntry cache_entry; |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 765 | if (!GetCacheEntryOnBlockingPool(resource_id, md5, &cache_entry)) { |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 766 | result->first = FILE_ERROR_NOT_FOUND; |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 767 | return result.Pass(); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 768 | } |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 769 | |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 770 | if (cache_entry.is_mounted()) { |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 771 | result->first = FILE_ERROR_INVALID_OPERATION; |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 772 | return result.Pass(); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | // Get the subdir type and path for the unmounted state. |
| 776 | CacheSubDirectoryType unmounted_subdir = |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 777 | cache_entry.is_pinned() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 778 | base::FilePath unmounted_path = GetCacheFilePath( |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 779 | resource_id, md5, unmounted_subdir, CACHED_FILE_FROM_SERVER); |
| 780 | |
| 781 | // Get the subdir type and path for the mounted state. |
| 782 | CacheSubDirectoryType mounted_subdir = CACHE_TYPE_PERSISTENT; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 783 | base::FilePath mounted_path = GetCacheFilePath( |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 784 | resource_id, md5, mounted_subdir, CACHED_FILE_MOUNTED); |
| 785 | |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 786 | // Move cache file. |
| 787 | bool success = MoveFile(unmounted_path, mounted_path); |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 788 | |
| 789 | if (success) { |
| 790 | // Now that cache operation is complete, update metadata. |
[email protected] | 48477fe | 2012-07-12 17:45:08 | [diff] [blame] | 791 | cache_entry.set_md5(md5); |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 792 | cache_entry.set_is_mounted(true); |
| 793 | cache_entry.set_is_persistent(true); |
[email protected] | 48477fe | 2012-07-12 17:45:08 | [diff] [blame] | 794 | metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 795 | } |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 796 | result->first = success ? FILE_ERROR_OK : FILE_ERROR_FAILED; |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 797 | result->second = mounted_path; |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 798 | return result.Pass(); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 799 | } |
| 800 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 801 | FileError DriveCache::MarkAsUnmountedOnBlockingPool( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 802 | const base::FilePath& file_path) { |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 803 | AssertOnSequencedWorkerPool(); |
[email protected] | 3a680f3 | 2013-03-01 04:07:27 | [diff] [blame] | 804 | DCHECK(IsUnderDriveCacheDirectory(file_path)); |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 805 | |
| 806 | // Parse file path to obtain resource_id, md5 and extra_extension. |
| 807 | std::string resource_id; |
| 808 | std::string md5; |
| 809 | std::string extra_extension; |
| 810 | util::ParseCacheFilePath(file_path, &resource_id, &md5, &extra_extension); |
| 811 | // The extra_extension shall be ".mounted" iff we're unmounting. |
[email protected] | 3a680f3 | 2013-03-01 04:07:27 | [diff] [blame] | 812 | DCHECK_EQ(util::kMountedArchiveFileExtension, extra_extension); |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 813 | |
| 814 | // Get cache entry associated with the resource_id and md5 |
| 815 | DriveCacheEntry cache_entry; |
| 816 | if (!GetCacheEntryOnBlockingPool(resource_id, md5, &cache_entry)) |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 817 | return FILE_ERROR_NOT_FOUND; |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 818 | |
| 819 | if (!cache_entry.is_mounted()) |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 820 | return FILE_ERROR_INVALID_OPERATION; |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 821 | |
| 822 | // Get the subdir type and path for the unmounted state. |
| 823 | CacheSubDirectoryType unmounted_subdir = |
| 824 | cache_entry.is_pinned() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 825 | base::FilePath unmounted_path = GetCacheFilePath( |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 826 | resource_id, md5, unmounted_subdir, CACHED_FILE_FROM_SERVER); |
| 827 | |
| 828 | // Get the subdir type and path for the mounted state. |
| 829 | CacheSubDirectoryType mounted_subdir = CACHE_TYPE_PERSISTENT; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 830 | base::FilePath mounted_path = GetCacheFilePath( |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 831 | resource_id, md5, mounted_subdir, CACHED_FILE_MOUNTED); |
| 832 | |
| 833 | // Move cache file. |
| 834 | if (!MoveFile(mounted_path, unmounted_path)) |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 835 | return FILE_ERROR_FAILED; |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 836 | |
| 837 | // Now that cache operation is complete, update metadata. |
| 838 | cache_entry.set_md5(md5); |
| 839 | cache_entry.set_is_mounted(false); |
| 840 | cache_entry.set_is_persistent(unmounted_subdir == CACHE_TYPE_PERSISTENT); |
| 841 | metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 842 | return FILE_ERROR_OK; |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 843 | } |
| 844 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 845 | FileError DriveCache::MarkDirtyOnBlockingPool( |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 846 | const std::string& resource_id, |
| 847 | const std::string& md5) { |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 848 | AssertOnSequencedWorkerPool(); |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 849 | |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 850 | // If file has already been marked dirty in previous instance of chrome, we |
| 851 | // would have lost the md5 info during cache initialization, because the file |
| 852 | // would have been renamed to .local extension. |
| 853 | // So, search for entry in cache without comparing md5. |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 854 | |
| 855 | // Marking a file dirty means its entry and actual file blob must exist in |
| 856 | // cache. |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 857 | DriveCacheEntry cache_entry; |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 858 | if (!GetCacheEntryOnBlockingPool(resource_id, std::string(), &cache_entry) || |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 859 | !cache_entry.is_present()) { |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 860 | LOG(WARNING) << "Can't mark dirty a file that wasn't cached: res_id=" |
| 861 | << resource_id |
| 862 | << ", md5=" << md5; |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 863 | return FILE_ERROR_NOT_FOUND; |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | // If a file is already dirty (i.e. MarkDirtyInCache was called before), |
| 867 | // delete outgoing symlink if it exists. |
| 868 | // TODO(benchan): We should only delete outgoing symlink if file is currently |
| 869 | // not being uploaded. However, for now, cache doesn't know if uploading of a |
| 870 | // file is in progress. Per zel, the upload process should be canceled before |
| 871 | // MarkDirtyInCache is called again. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 872 | if (cache_entry.is_dirty()) { |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 873 | // The file must be in persistent dir. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 874 | DCHECK(cache_entry.is_persistent()); |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 875 | |
| 876 | // Determine symlink path in outgoing dir, so as to remove it. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 877 | base::FilePath symlink_path = GetCacheFilePath(resource_id, |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 878 | std::string(), |
| 879 | CACHE_TYPE_OUTGOING, |
| 880 | CACHED_FILE_FROM_SERVER); |
| 881 | DeleteSymlink(symlink_path); |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 882 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 883 | return FILE_ERROR_OK; |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | // Move file to persistent dir with new .local extension. |
| 887 | |
| 888 | // Get the current path of the file in cache. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 889 | base::FilePath source_path = GetCacheFilePath(resource_id, |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 890 | md5, |
| 891 | GetSubDirectoryType(cache_entry), |
| 892 | CACHED_FILE_FROM_SERVER); |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 893 | // Determine destination path. |
[email protected] | 3dc88ee | 2012-07-11 21:04:11 | [diff] [blame] | 894 | const CacheSubDirectoryType sub_dir_type = CACHE_TYPE_PERSISTENT; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 895 | base::FilePath cache_file_path = GetCacheFilePath(resource_id, |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 896 | md5, |
| 897 | sub_dir_type, |
| 898 | CACHED_FILE_LOCALLY_MODIFIED); |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 899 | |
[email protected] | ae248f0 | 2013-03-06 12:55:41 | [diff] [blame] | 900 | if (!MoveFile(source_path, cache_file_path)) |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 901 | return FILE_ERROR_FAILED; |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 902 | |
[email protected] | ae248f0 | 2013-03-06 12:55:41 | [diff] [blame] | 903 | // Now that file operations have completed, update metadata. |
| 904 | cache_entry.set_md5(md5); |
| 905 | cache_entry.set_is_dirty(true); |
| 906 | cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); |
| 907 | metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 908 | return FILE_ERROR_OK; |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 909 | } |
| 910 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 911 | FileError DriveCache::CommitDirtyOnBlockingPool( |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 912 | const std::string& resource_id, |
[email protected] | 3f5e290 | 2012-11-06 08:21:15 | [diff] [blame] | 913 | const std::string& md5) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 914 | AssertOnSequencedWorkerPool(); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 915 | |
| 916 | // If file has already been marked dirty in previous instance of chrome, we |
| 917 | // would have lost the md5 info during cache initialization, because the file |
| 918 | // would have been renamed to .local extension. |
| 919 | // So, search for entry in cache without comparing md5. |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 920 | |
| 921 | // Committing a file dirty means its entry and actual file blob must exist in |
| 922 | // cache. |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 923 | DriveCacheEntry cache_entry; |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 924 | if (!GetCacheEntryOnBlockingPool(resource_id, std::string(), &cache_entry) || |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 925 | !cache_entry.is_present()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 926 | LOG(WARNING) << "Can't commit dirty a file that wasn't cached: res_id=" |
| 927 | << resource_id |
| 928 | << ", md5=" << md5; |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 929 | return FILE_ERROR_NOT_FOUND; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | // If a file is not dirty (it should have been marked dirty via |
| 933 | // MarkDirtyInCache), committing it dirty is an invalid operation. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 934 | if (!cache_entry.is_dirty()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 935 | LOG(WARNING) << "Can't commit a non-dirty file: res_id=" |
| 936 | << resource_id |
| 937 | << ", md5=" << md5; |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 938 | return FILE_ERROR_INVALID_OPERATION; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | // Dirty files must be in persistent dir. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 942 | DCHECK(cache_entry.is_persistent()); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 943 | |
| 944 | // Create symlink in outgoing dir. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 945 | base::FilePath symlink_path = GetCacheFilePath(resource_id, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 946 | std::string(), |
| 947 | CACHE_TYPE_OUTGOING, |
| 948 | CACHED_FILE_FROM_SERVER); |
| 949 | |
| 950 | // Get target path of symlink i.e. current path of the file in cache. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 951 | base::FilePath target_path = GetCacheFilePath(resource_id, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 952 | md5, |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 953 | GetSubDirectoryType(cache_entry), |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 954 | CACHED_FILE_LOCALLY_MODIFIED); |
| 955 | |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 956 | return CreateSymlink(target_path, symlink_path) ? |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 957 | FILE_ERROR_OK : FILE_ERROR_FAILED; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 958 | } |
| 959 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 960 | FileError DriveCache::ClearDirtyOnBlockingPool( |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 961 | const std::string& resource_id, |
[email protected] | 3f5e290 | 2012-11-06 08:21:15 | [diff] [blame] | 962 | const std::string& md5) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 963 | AssertOnSequencedWorkerPool(); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 964 | |
| 965 | // |md5| is the new .<md5> extension to rename the file to. |
| 966 | // So, search for entry in cache without comparing md5. |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 967 | DriveCacheEntry cache_entry; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 968 | |
| 969 | // Clearing a dirty file means its entry and actual file blob must exist in |
| 970 | // cache. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 971 | if (!GetCacheEntryOnBlockingPool(resource_id, std::string(), &cache_entry) || |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 972 | !cache_entry.is_present()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 973 | LOG(WARNING) << "Can't clear dirty state of a file that wasn't cached: " |
| 974 | << "res_id=" << resource_id |
| 975 | << ", md5=" << md5; |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 976 | return FILE_ERROR_NOT_FOUND; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | // If a file is not dirty (it should have been marked dirty via |
| 980 | // MarkDirtyInCache), clearing its dirty state is an invalid operation. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 981 | if (!cache_entry.is_dirty()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 982 | LOG(WARNING) << "Can't clear dirty state of a non-dirty file: res_id=" |
| 983 | << resource_id |
| 984 | << ", md5=" << md5; |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 985 | return FILE_ERROR_INVALID_OPERATION; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | // File must be dirty and hence in persistent dir. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 989 | DCHECK(cache_entry.is_persistent()); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 990 | |
| 991 | // Get the current path of the file in cache. |
[email protected] | ae248f0 | 2013-03-06 12:55:41 | [diff] [blame] | 992 | base::FilePath source_path = |
| 993 | GetCacheFilePath(resource_id, |
| 994 | md5, |
| 995 | GetSubDirectoryType(cache_entry), |
| 996 | CACHED_FILE_LOCALLY_MODIFIED); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 997 | |
| 998 | // Determine destination path. |
| 999 | // If file is pinned, move it to persistent dir with .md5 extension; |
| 1000 | // otherwise, move it to tmp dir with .md5 extension. |
[email protected] | 3dc88ee | 2012-07-11 21:04:11 | [diff] [blame] | 1001 | const CacheSubDirectoryType sub_dir_type = |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1002 | cache_entry.is_pinned() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 1003 | base::FilePath dest_path = GetCacheFilePath(resource_id, |
[email protected] | ae248f0 | 2013-03-06 12:55:41 | [diff] [blame] | 1004 | md5, |
| 1005 | sub_dir_type, |
| 1006 | CACHED_FILE_FROM_SERVER); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1007 | |
[email protected] | ae248f0 | 2013-03-06 12:55:41 | [diff] [blame] | 1008 | if (!MoveFile(source_path, dest_path)) |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1009 | return FILE_ERROR_FAILED; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1010 | |
[email protected] | ae248f0 | 2013-03-06 12:55:41 | [diff] [blame] | 1011 | // Delete symlink in outgoing dir. |
| 1012 | base::FilePath symlink_path = GetCacheFilePath(resource_id, |
| 1013 | std::string(), |
| 1014 | CACHE_TYPE_OUTGOING, |
| 1015 | CACHED_FILE_FROM_SERVER); |
| 1016 | DeleteSymlink(symlink_path); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1017 | |
[email protected] | ae248f0 | 2013-03-06 12:55:41 | [diff] [blame] | 1018 | // Now that file operations have completed, update metadata. |
| 1019 | cache_entry.set_md5(md5); |
| 1020 | cache_entry.set_is_dirty(false); |
| 1021 | cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); |
| 1022 | metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1023 | return FILE_ERROR_OK; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1024 | } |
| 1025 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1026 | FileError DriveCache::RemoveOnBlockingPool( |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 1027 | const std::string& resource_id) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1028 | AssertOnSequencedWorkerPool(); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1029 | |
[email protected] | 3423871a | 2012-07-12 00:41:27 | [diff] [blame] | 1030 | // MD5 is not passed into RemoveCacheEntry because we would delete all |
| 1031 | // cache files corresponding to <resource_id> regardless of the md5. |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1032 | // So, search for entry in cache without taking md5 into account. |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 1033 | DriveCacheEntry cache_entry; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1034 | |
| 1035 | // If entry doesn't exist or is dirty or mounted in cache, nothing to do. |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 1036 | const bool entry_found = |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 1037 | GetCacheEntryOnBlockingPool(resource_id, std::string(), &cache_entry); |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1038 | if (!entry_found || cache_entry.is_dirty() || cache_entry.is_mounted()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1039 | DVLOG(1) << "Entry is " |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 1040 | << (entry_found ? |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1041 | (cache_entry.is_dirty() ? "dirty" : "mounted") : |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1042 | "non-existent") |
| 1043 | << " in cache, not removing"; |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1044 | return FILE_ERROR_OK; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | // Determine paths to delete all cache versions of |resource_id| in |
| 1048 | // persistent, tmp and pinned directories. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 1049 | std::vector<base::FilePath> paths_to_delete; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1050 | |
| 1051 | // For files in persistent and tmp dirs, delete files that match |
| 1052 | // "<resource_id>.*". |
| 1053 | paths_to_delete.push_back(GetCacheFilePath(resource_id, |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 1054 | util::kWildCard, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1055 | CACHE_TYPE_PERSISTENT, |
| 1056 | CACHED_FILE_FROM_SERVER)); |
| 1057 | paths_to_delete.push_back(GetCacheFilePath(resource_id, |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 1058 | util::kWildCard, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1059 | CACHE_TYPE_TMP, |
| 1060 | CACHED_FILE_FROM_SERVER)); |
| 1061 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1062 | // Don't delete locally modified (i.e. dirty and possibly outgoing) files. |
| 1063 | // Since we're not deleting outgoing symlinks, we don't need to append |
| 1064 | // outgoing path to |paths_to_delete|. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 1065 | base::FilePath path_to_keep = GetCacheFilePath(resource_id, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1066 | std::string(), |
| 1067 | CACHE_TYPE_PERSISTENT, |
| 1068 | CACHED_FILE_LOCALLY_MODIFIED); |
| 1069 | |
| 1070 | for (size_t i = 0; i < paths_to_delete.size(); ++i) { |
| 1071 | DeleteFilesSelectively(paths_to_delete[i], path_to_keep); |
| 1072 | } |
| 1073 | |
[email protected] | c80ca12f | 2012-11-09 10:56:03 | [diff] [blame] | 1074 | // Now that all file operations have completed, remove from metadata. |
[email protected] | 3423871a | 2012-07-12 00:41:27 | [diff] [blame] | 1075 | metadata_->RemoveCacheEntry(resource_id); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1076 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1077 | return FILE_ERROR_OK; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1078 | } |
| 1079 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 1080 | bool DriveCache::ClearAllOnBlockingPool() { |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 1081 | AssertOnSequencedWorkerPool(); |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 1082 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 1083 | if (!file_util::Delete(cache_root_path_, true)) { |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame] | 1084 | LOG(WARNING) << "Failed to delete the cache directory"; |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 1085 | return false; |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame] | 1086 | } |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 1087 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 1088 | if (!InitializeOnBlockingPool()) { |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame] | 1089 | LOG(WARNING) << "Failed to initialize the cache"; |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 1090 | return false; |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame] | 1091 | } |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 1092 | return true; |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 1093 | } |
| 1094 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 1095 | void DriveCache::OnPinned(const std::string& resource_id, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 1096 | const std::string& md5, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 1097 | const FileOperationCallback& callback, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1098 | FileError error) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 1099 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 1100 | DCHECK(!callback.is_null()); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 1101 | |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 1102 | callback.Run(error); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 1103 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1104 | if (error == FILE_ERROR_OK) |
[email protected] | bd547ad8 | 2013-04-26 04:52:29 | [diff] [blame] | 1105 | FOR_EACH_OBSERVER(CacheObserver, |
[email protected] | a0927550 | 2012-10-10 04:48:01 | [diff] [blame] | 1106 | observers_, |
| 1107 | OnCachePinned(resource_id, md5)); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 1108 | } |
| 1109 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 1110 | void DriveCache::OnUnpinned(const std::string& resource_id, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 1111 | const std::string& md5, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 1112 | const FileOperationCallback& callback, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1113 | FileError error) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 1114 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 1115 | DCHECK(!callback.is_null()); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1116 | |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 1117 | callback.Run(error); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1118 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1119 | if (error == FILE_ERROR_OK) |
[email protected] | bd547ad8 | 2013-04-26 04:52:29 | [diff] [blame] | 1120 | FOR_EACH_OBSERVER(CacheObserver, |
[email protected] | a0927550 | 2012-10-10 04:48:01 | [diff] [blame] | 1121 | observers_, |
| 1122 | OnCacheUnpinned(resource_id, md5)); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1123 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 1124 | // Now the file is moved from "persistent" to "tmp" directory. |
| 1125 | // It's a chance to free up space if needed. |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 1126 | blocking_task_runner_->PostTask( |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 1127 | FROM_HERE, |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 1128 | base::Bind( |
| 1129 | base::IgnoreResult( |
| 1130 | &DriveCache::FreeDiskSpaceOnBlockingPoolIfNeededFor), |
| 1131 | base::Unretained(this), 0)); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 1132 | } |
| 1133 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 1134 | void DriveCache::OnCommitDirty(const std::string& resource_id, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 1135 | const FileOperationCallback& callback, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1136 | FileError error) { |
[email protected] | d7664c2 | 2012-06-18 19:35:49 | [diff] [blame] | 1137 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 1138 | DCHECK(!callback.is_null()); |
[email protected] | d7664c2 | 2012-06-18 19:35:49 | [diff] [blame] | 1139 | |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 1140 | callback.Run(error); |
[email protected] | d7664c2 | 2012-06-18 19:35:49 | [diff] [blame] | 1141 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1142 | if (error == FILE_ERROR_OK) |
[email protected] | bd547ad8 | 2013-04-26 04:52:29 | [diff] [blame] | 1143 | FOR_EACH_OBSERVER(CacheObserver, |
[email protected] | a0927550 | 2012-10-10 04:48:01 | [diff] [blame] | 1144 | observers_, |
| 1145 | OnCacheCommitted(resource_id)); |
[email protected] | d7664c2 | 2012-06-18 19:35:49 | [diff] [blame] | 1146 | } |
| 1147 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 1148 | bool DriveCache::HasEnoughSpaceFor(int64 num_bytes, |
| 1149 | const base::FilePath& path) { |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 1150 | int64 free_space = 0; |
| 1151 | if (free_disk_space_getter_) |
| 1152 | free_space = free_disk_space_getter_->AmountOfFreeDiskSpace(); |
| 1153 | else |
| 1154 | free_space = base::SysInfo::AmountOfFreeDiskSpace(path); |
| 1155 | |
| 1156 | // Subtract this as if this portion does not exist. |
| 1157 | free_space -= kMinFreeSpace; |
| 1158 | return (free_space >= num_bytes); |
| 1159 | } |
| 1160 | |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 1161 | // static |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 1162 | base::FilePath DriveCache::GetCacheRootPath(Profile* profile) { |
| 1163 | base::FilePath cache_base_path; |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 1164 | chrome::GetUserCacheDirectory(profile->GetPath(), &cache_base_path); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 1165 | base::FilePath cache_root_path = |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1166 | cache_base_path.Append(chrome::kDriveCacheDirname); |
| 1167 | return cache_root_path.Append(kDriveCacheVersionDir); |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 1168 | } |
| 1169 | |
[email protected] | 30d9dda | 2012-06-30 05:56:28 | [diff] [blame] | 1170 | // static |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 1171 | std::vector<base::FilePath> DriveCache::GetCachePaths( |
| 1172 | const base::FilePath& cache_root_path) { |
| 1173 | std::vector<base::FilePath> cache_paths; |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1174 | // The order should match DriveCache::CacheSubDirectoryType enum. |
| 1175 | cache_paths.push_back(cache_root_path.Append(kDriveCacheMetaDir)); |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1176 | cache_paths.push_back(cache_root_path.Append(kDriveCacheOutgoingDir)); |
| 1177 | cache_paths.push_back(cache_root_path.Append(kDriveCachePersistentDir)); |
| 1178 | cache_paths.push_back(cache_root_path.Append(kDriveCacheTmpDir)); |
| 1179 | cache_paths.push_back(cache_root_path.Append(kDriveCacheTmpDownloadsDir)); |
| 1180 | cache_paths.push_back(cache_root_path.Append(kDriveCacheTmpDocumentsDir)); |
[email protected] | 30d9dda | 2012-06-30 05:56:28 | [diff] [blame] | 1181 | return cache_paths; |
| 1182 | } |
| 1183 | |
| 1184 | // static |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1185 | bool DriveCache::CreateCacheDirectories( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 1186 | const std::vector<base::FilePath>& paths_to_create) { |
[email protected] | 30d9dda | 2012-06-30 05:56:28 | [diff] [blame] | 1187 | bool success = true; |
| 1188 | |
| 1189 | for (size_t i = 0; i < paths_to_create.size(); ++i) { |
| 1190 | if (file_util::DirectoryExists(paths_to_create[i])) |
| 1191 | continue; |
| 1192 | |
| 1193 | if (!file_util::CreateDirectory(paths_to_create[i])) { |
| 1194 | // Error creating this directory, record error and proceed with next one. |
| 1195 | success = false; |
| 1196 | PLOG(ERROR) << "Error creating directory " << paths_to_create[i].value(); |
| 1197 | } else { |
| 1198 | DVLOG(1) << "Created directory " << paths_to_create[i].value(); |
| 1199 | } |
| 1200 | } |
| 1201 | return success; |
| 1202 | } |
| 1203 | |
[email protected] | fae353a | 2012-07-11 23:30:27 | [diff] [blame] | 1204 | // static |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1205 | DriveCache::CacheSubDirectoryType DriveCache::GetSubDirectoryType( |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 1206 | const DriveCacheEntry& cache_entry) { |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1207 | return cache_entry.is_persistent() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; |
[email protected] | fae353a | 2012-07-11 23:30:27 | [diff] [blame] | 1208 | } |
| 1209 | |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 1210 | } // namespace drive |