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