[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] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 5 | #include "chrome/browser/chromeos/gdata/drive_cache.h" |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 6 | |
| 7 | #include <vector> |
| 8 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 9 | #include "base/file_util.h" |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | 77472728 | 2012-08-06 09:14:44 | [diff] [blame] | 11 | #include "base/path_service.h" |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 12 | #include "base/stringprintf.h" |
| 13 | #include "base/string_util.h" |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 14 | #include "base/sys_info.h" |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 15 | #include "chrome/browser/chromeos/gdata/drive.pb.h" |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 16 | #include "chrome/browser/chromeos/gdata/drive_cache_metadata.h" |
[email protected] | 361c201 | 2012-09-05 16:31:13 | [diff] [blame] | 17 | #include "chrome/browser/chromeos/gdata/drive_file_system_util.h" |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 18 | #include "chrome/browser/profiles/profile.h" |
| 19 | #include "chrome/common/chrome_constants.h" |
| 20 | #include "chrome/common/chrome_paths_internal.h" |
[email protected] | 7986b8d | 2012-06-14 15:05:14 | [diff] [blame] | 21 | #include "content/public/browser/browser_thread.h" |
| 22 | |
| 23 | using content::BrowserThread; |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 24 | |
| 25 | namespace gdata { |
| 26 | namespace { |
| 27 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 28 | const FilePath::CharType kDriveCacheVersionDir[] = FILE_PATH_LITERAL("v1"); |
| 29 | const FilePath::CharType kDriveCacheMetaDir[] = FILE_PATH_LITERAL("meta"); |
| 30 | const FilePath::CharType kDriveCachePinnedDir[] = FILE_PATH_LITERAL("pinned"); |
| 31 | const FilePath::CharType kDriveCacheOutgoingDir[] = |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 32 | FILE_PATH_LITERAL("outgoing"); |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 33 | const FilePath::CharType kDriveCachePersistentDir[] = |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 34 | FILE_PATH_LITERAL("persistent"); |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 35 | const FilePath::CharType kDriveCacheTmpDir[] = FILE_PATH_LITERAL("tmp"); |
| 36 | const FilePath::CharType kDriveCacheTmpDownloadsDir[] = |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 37 | FILE_PATH_LITERAL("tmp/downloads"); |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 38 | const FilePath::CharType kDriveCacheTmpDocumentsDir[] = |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 39 | FILE_PATH_LITERAL("tmp/documents"); |
| 40 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 41 | // Used to tweak GetAmountOfFreeDiskSpace() behavior for testing. |
| 42 | FreeDiskSpaceGetterInterface* global_free_disk_getter_for_testing = NULL; |
| 43 | |
| 44 | // Gets the amount of free disk space. Use |
| 45 | // |global_free_disk_getter_for_testing| if set. |
| 46 | int64 GetAmountOfFreeDiskSpace() { |
| 47 | if (global_free_disk_getter_for_testing) |
| 48 | return global_free_disk_getter_for_testing->AmountOfFreeDiskSpace(); |
| 49 | |
[email protected] | 77472728 | 2012-08-06 09:14:44 | [diff] [blame] | 50 | FilePath path; |
| 51 | if (!PathService::Get(base::DIR_HOME, &path)) { |
| 52 | LOG(ERROR) << "Home directory not found"; |
| 53 | return -1; |
| 54 | } |
| 55 | return base::SysInfo::AmountOfFreeDiskSpace(path); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // Returns true if we have sufficient space to store the given number of |
| 59 | // bytes, while keeping kMinFreeSpace bytes on the disk. |
| 60 | bool HasEnoughSpaceFor(int64 num_bytes) { |
| 61 | int64 free_space = GetAmountOfFreeDiskSpace(); |
[email protected] | d7754f5 | 2012-08-01 08:45:33 | [diff] [blame] | 62 | // Subtract this as if this portion does not exist. |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 63 | free_space -= kMinFreeSpace; |
| 64 | return (free_space >= num_bytes); |
| 65 | } |
| 66 | |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 67 | // Create cache directory paths and set permissions. |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame^] | 68 | bool InitCachePaths(const std::vector<FilePath>& cache_paths) { |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 69 | if (cache_paths.size() < DriveCache::NUM_CACHE_TYPES) { |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 70 | NOTREACHED(); |
| 71 | LOG(ERROR) << "Size of cache_paths is invalid."; |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame^] | 72 | return false; |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 75 | if (!DriveCache::CreateCacheDirectories(cache_paths)) |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame^] | 76 | return false; |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 77 | |
| 78 | // Change permissions of cache persistent directory to u+rwx,og+x (711) in |
| 79 | // order to allow archive files in that directory to be mounted by cros-disks. |
| 80 | file_util::SetPosixFilePermissions( |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 81 | cache_paths[DriveCache::CACHE_TYPE_PERSISTENT], |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 82 | file_util::FILE_PERMISSION_USER_MASK | |
| 83 | file_util::FILE_PERMISSION_EXECUTE_BY_GROUP | |
| 84 | file_util::FILE_PERMISSION_EXECUTE_BY_OTHERS); |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame^] | 85 | |
| 86 | return true; |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 87 | } |
| 88 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 89 | // Remove all files under the given directory, non-recursively. |
[email protected] | d7754f5 | 2012-08-01 08:45:33 | [diff] [blame] | 90 | // 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] | 91 | // which is used for user initiated downloads like "Save As" |
| 92 | void RemoveAllFiles(const FilePath& directory) { |
| 93 | using file_util::FileEnumerator; |
| 94 | |
| 95 | FileEnumerator enumerator(directory, false /* recursive */, |
| 96 | FileEnumerator::FILES); |
| 97 | for (FilePath file_path = enumerator.Next(); !file_path.empty(); |
| 98 | file_path = enumerator.Next()) { |
| 99 | DVLOG(1) << "Removing " << file_path.value(); |
| 100 | if (!file_util::Delete(file_path, false /* recursive */)) |
| 101 | LOG(WARNING) << "Failed to delete " << file_path.value(); |
| 102 | } |
| 103 | } |
| 104 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 105 | // Modifies cache state of file on blocking pool, which involves: |
| 106 | // - moving or copying file (per |file_operation_type|) from |source_path| to |
| 107 | // |dest_path| if they're different |
| 108 | // - deleting symlink if |symlink_path| is not empty |
| 109 | // - creating symlink if |symlink_path| is not empty and |create_symlink| is |
| 110 | // true. |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 111 | DriveFileError ModifyCacheState( |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 112 | const FilePath& source_path, |
| 113 | const FilePath& dest_path, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 114 | DriveCache::FileOperationType file_operation_type, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 115 | const FilePath& symlink_path, |
| 116 | bool create_symlink) { |
| 117 | // Move or copy |source_path| to |dest_path| if they are different. |
| 118 | if (source_path != dest_path) { |
| 119 | bool success = false; |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 120 | if (file_operation_type == DriveCache::FILE_OPERATION_MOVE) |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 121 | success = file_util::Move(source_path, dest_path); |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 122 | else if (file_operation_type == DriveCache::FILE_OPERATION_COPY) |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 123 | success = file_util::CopyFile(source_path, dest_path); |
| 124 | if (!success) { |
[email protected] | 750af1d | 2012-07-13 14:32:43 | [diff] [blame] | 125 | LOG(ERROR) << "Failed to " |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 126 | << (file_operation_type == DriveCache::FILE_OPERATION_MOVE ? |
[email protected] | 750af1d | 2012-07-13 14:32:43 | [diff] [blame] | 127 | "move " : "copy ") |
| 128 | << source_path.value() |
| 129 | << " to " << dest_path.value(); |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 130 | return DRIVE_FILE_ERROR_FAILED; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 131 | } else { |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 132 | DVLOG(1) << (file_operation_type == DriveCache::FILE_OPERATION_MOVE ? |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 133 | "Moved " : "Copied ") |
| 134 | << source_path.value() |
| 135 | << " to " << dest_path.value(); |
| 136 | } |
| 137 | } else { |
| 138 | DVLOG(1) << "No need to move file: source = destination"; |
| 139 | } |
| 140 | |
| 141 | if (symlink_path.empty()) |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 142 | return DRIVE_FILE_OK; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 143 | |
| 144 | // Remove symlink regardless of |create_symlink| because creating a link will |
| 145 | // not overwrite an existing one. |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 146 | // We try to save one file operation by not checking if link exists before |
| 147 | // deleting it, so unlink may return error if link doesn't exist, but it |
| 148 | // doesn't really matter to us. |
[email protected] | 750af1d | 2012-07-13 14:32:43 | [diff] [blame] | 149 | file_util::Delete(symlink_path, false); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 150 | |
| 151 | if (!create_symlink) |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 152 | return DRIVE_FILE_OK; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 153 | |
| 154 | // Create new symlink to |dest_path|. |
| 155 | if (!file_util::CreateSymbolicLink(dest_path, symlink_path)) { |
[email protected] | 750af1d | 2012-07-13 14:32:43 | [diff] [blame] | 156 | LOG(ERROR) << "Failed to create a symlink from " << symlink_path.value() |
| 157 | << " to " << dest_path.value(); |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 158 | return DRIVE_FILE_ERROR_FAILED; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 159 | } |
| 160 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 161 | return DRIVE_FILE_OK; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Deletes all files that match |path_to_delete_pattern| except for |
| 165 | // |path_to_keep| on blocking pool. |
| 166 | // If |path_to_keep| is empty, all files in |path_to_delete_pattern| are |
| 167 | // deleted. |
| 168 | void DeleteFilesSelectively(const FilePath& path_to_delete_pattern, |
| 169 | const FilePath& path_to_keep) { |
| 170 | // Enumerate all files in directory of |path_to_delete_pattern| that match |
| 171 | // base name of |path_to_delete_pattern|. |
| 172 | // If a file is not |path_to_keep|, delete it. |
| 173 | bool success = true; |
[email protected] | 84c3f16 | 2012-08-12 01:57:23 | [diff] [blame] | 174 | file_util::FileEnumerator enumerator(path_to_delete_pattern.DirName(), |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 175 | false, // not recursive |
[email protected] | 84c3f16 | 2012-08-12 01:57:23 | [diff] [blame] | 176 | file_util::FileEnumerator::FILES | |
| 177 | file_util::FileEnumerator::SHOW_SYM_LINKS, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 178 | path_to_delete_pattern.BaseName().value()); |
| 179 | for (FilePath current = enumerator.Next(); !current.empty(); |
| 180 | current = enumerator.Next()) { |
| 181 | // If |path_to_keep| is not empty and same as current, don't delete it. |
| 182 | if (!path_to_keep.empty() && current == path_to_keep) |
| 183 | continue; |
| 184 | |
[email protected] | 0b8d4cee | 2012-07-02 20:46:26 | [diff] [blame] | 185 | success = file_util::Delete(current, false); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 186 | if (!success) |
| 187 | DVLOG(1) << "Error deleting " << current.value(); |
| 188 | else |
| 189 | DVLOG(1) << "Deleted " << current.value(); |
| 190 | } |
| 191 | } |
| 192 | |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 193 | // Appends |resource_id| ID to |to_fetch| if the file is pinned but not |
| 194 | // fetched (not present locally), or to |to_upload| if the file is dirty |
| 195 | // but not uploaded. |
| 196 | void CollectBacklog(std::vector<std::string>* to_fetch, |
| 197 | std::vector<std::string>* to_upload, |
| 198 | const std::string& resource_id, |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 199 | const DriveCacheEntry& cache_entry) { |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 200 | DCHECK(to_fetch); |
| 201 | DCHECK(to_upload); |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 202 | |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 203 | if (cache_entry.is_pinned() && !cache_entry.is_present()) |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 204 | to_fetch->push_back(resource_id); |
| 205 | |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 206 | if (cache_entry.is_dirty()) |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 207 | to_upload->push_back(resource_id); |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 208 | } |
| 209 | |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 210 | // Appends |resource_id| ID to |resource_ids| if the file is pinned and |
| 211 | // present (cached locally). |
| 212 | void CollectExistingPinnedFile(std::vector<std::string>* resource_ids, |
| 213 | const std::string& resource_id, |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 214 | const DriveCacheEntry& cache_entry) { |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 215 | DCHECK(resource_ids); |
| 216 | |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 217 | if (cache_entry.is_pinned() && cache_entry.is_present()) |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 218 | resource_ids->push_back(resource_id); |
| 219 | } |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 220 | |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 221 | // Appends |resource_id| ID to |resource_ids| unconditionally. |
| 222 | void CollectAnyFile(std::vector<std::string>* resource_ids, |
| 223 | const std::string& resource_id, |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 224 | const DriveCacheEntry& /* cache_entry */) { |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 225 | DCHECK(resource_ids); |
| 226 | |
| 227 | resource_ids->push_back(resource_id); |
| 228 | } |
| 229 | |
[email protected] | 7986b8d | 2012-06-14 15:05:14 | [diff] [blame] | 230 | // Runs callback with pointers dereferenced. |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 231 | // Used to implement SetMountedStateOnUIThread and ClearAllOnUIThread. |
| 232 | void RunChangeCacheStateCallback(const ChangeCacheStateCallback& callback, |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 233 | const DriveFileError* error, |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 234 | const FilePath* cache_file_path) { |
[email protected] | 7986b8d | 2012-06-14 15:05:14 | [diff] [blame] | 235 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 236 | DCHECK(error); |
| 237 | DCHECK(cache_file_path); |
| 238 | |
| 239 | if (!callback.is_null()) |
| 240 | callback.Run(*error, *cache_file_path); |
| 241 | } |
| 242 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 243 | // Runs callback with pointers dereferenced. |
| 244 | // Used to implement *OnUIThread methods. |
| 245 | void RunCacheOperationCallback(const CacheOperationCallback& callback, |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 246 | DriveFileError* error, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 247 | const std::string& resource_id, |
| 248 | const std::string& md5) { |
| 249 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 250 | DCHECK(error); |
| 251 | |
| 252 | if (!callback.is_null()) |
| 253 | callback.Run(*error, resource_id, md5); |
| 254 | } |
| 255 | |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 256 | // Runs callback with pointers dereferenced. |
| 257 | // Used to implement *OnUIThread methods. |
| 258 | void RunGetFileFromCacheCallback(const GetFileFromCacheCallback& callback, |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 259 | DriveFileError* error, |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 260 | FilePath* cache_file_path) { |
| 261 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 262 | DCHECK(error); |
| 263 | DCHECK(cache_file_path); |
| 264 | |
| 265 | if (!callback.is_null()) |
[email protected] | 3cacd172e2 | 2012-09-06 22:52:45 | [diff] [blame] | 266 | callback.Run(*error, *cache_file_path); |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 267 | } |
| 268 | |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 269 | // Runs callback with pointers dereferenced. |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 270 | // Used to implement GetResourceIdsOfBacklogOnUIThread(). |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 271 | void RunGetResourceIdsOfBacklogCallback( |
| 272 | const GetResourceIdsOfBacklogCallback& callback, |
| 273 | std::vector<std::string>* to_fetch, |
| 274 | std::vector<std::string>* to_upload) { |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 275 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 276 | DCHECK(to_fetch); |
| 277 | DCHECK(to_upload); |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 278 | |
| 279 | if (!callback.is_null()) |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 280 | callback.Run(*to_fetch, *to_upload); |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 281 | } |
| 282 | |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 283 | // Runs callback with pointers dereferenced. |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 284 | // Used to implement GetResourceIdsOfExistingPinnedFilesOnUIThread(). |
| 285 | void RunGetResourceIdsCallback( |
| 286 | const GetResourceIdsCallback& callback, |
| 287 | std::vector<std::string>* resource_ids) { |
| 288 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 289 | DCHECK(resource_ids); |
| 290 | |
| 291 | if (!callback.is_null()) |
| 292 | callback.Run(*resource_ids); |
| 293 | } |
| 294 | |
| 295 | // Runs callback with pointers dereferenced. |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 296 | // Used to implement GetCacheEntryOnUIThread(). |
| 297 | void RunGetCacheEntryCallback( |
[email protected] | fae353a | 2012-07-11 23:30:27 | [diff] [blame] | 298 | const GetCacheEntryCallback& callback, |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 299 | bool* success, |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 300 | DriveCacheEntry* cache_entry) { |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 301 | DCHECK(success); |
| 302 | DCHECK(cache_entry); |
| 303 | |
| 304 | if (!callback.is_null()) |
| 305 | callback.Run(*success, *cache_entry); |
| 306 | } |
| 307 | |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame^] | 308 | // Runs InitializeCacheCallback with a pointer dereferenced. |
| 309 | void RunInitializeCacheCallback(const InitializeCacheCallback& callback, |
| 310 | bool* success) { |
| 311 | DCHECK(success); |
| 312 | |
| 313 | if (!callback.is_null()) |
| 314 | callback.Run(*success); |
| 315 | } |
| 316 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 317 | } // namespace |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 318 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 319 | DriveCache::DriveCache(const FilePath& cache_root_path, |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 320 | base::SequencedTaskRunner* blocking_task_runner) |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 321 | : cache_root_path_(cache_root_path), |
[email protected] | 6b70c7b | 2012-06-14 03:10:43 | [diff] [blame] | 322 | cache_paths_(GetCachePaths(cache_root_path_)), |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 323 | blocking_task_runner_(blocking_task_runner), |
[email protected] | dc2e8f7b | 2012-08-30 20:22:44 | [diff] [blame] | 324 | ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 325 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 326 | } |
| 327 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 328 | DriveCache::~DriveCache() { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 329 | AssertOnSequencedWorkerPool(); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 330 | } |
| 331 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 332 | FilePath DriveCache::GetCacheDirectoryPath( |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 333 | CacheSubDirectoryType sub_dir_type) const { |
| 334 | DCHECK_LE(0, sub_dir_type); |
| 335 | DCHECK_GT(NUM_CACHE_TYPES, sub_dir_type); |
| 336 | return cache_paths_[sub_dir_type]; |
| 337 | } |
| 338 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 339 | FilePath DriveCache::GetCacheFilePath(const std::string& resource_id, |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 340 | const std::string& md5, |
| 341 | CacheSubDirectoryType sub_dir_type, |
| 342 | CachedFileOrigin file_origin) const { |
| 343 | DCHECK(sub_dir_type != CACHE_TYPE_META); |
| 344 | |
| 345 | // Runs on any thread. |
| 346 | // Filename is formatted as resource_id.md5, i.e. resource_id is the base |
| 347 | // name and md5 is the extension. |
| 348 | std::string base_name = util::EscapeCacheFileName(resource_id); |
| 349 | if (file_origin == CACHED_FILE_LOCALLY_MODIFIED) { |
| 350 | DCHECK(sub_dir_type == CACHE_TYPE_PERSISTENT); |
| 351 | base_name += FilePath::kExtensionSeparator; |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 352 | base_name += util::kLocallyModifiedFileExtension; |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 353 | } else if (!md5.empty()) { |
| 354 | base_name += FilePath::kExtensionSeparator; |
| 355 | base_name += util::EscapeCacheFileName(md5); |
| 356 | } |
| 357 | // For mounted archives the filename is formatted as resource_id.md5.mounted, |
| 358 | // i.e. resource_id.md5 is the base name and ".mounted" is the extension |
| 359 | if (file_origin == CACHED_FILE_MOUNTED) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 360 | DCHECK(sub_dir_type == CACHE_TYPE_PERSISTENT); |
| 361 | base_name += FilePath::kExtensionSeparator; |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 362 | base_name += util::kMountedArchiveFileExtension; |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 363 | } |
| 364 | return GetCacheDirectoryPath(sub_dir_type).Append(base_name); |
| 365 | } |
| 366 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 367 | void DriveCache::AssertOnSequencedWorkerPool() { |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 368 | DCHECK(!blocking_task_runner_ || |
| 369 | blocking_task_runner_->RunsTasksOnCurrentThread()); |
[email protected] | fcc92a5 | 2012-06-08 22:54:16 | [diff] [blame] | 370 | } |
| 371 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 372 | bool DriveCache::IsUnderDriveCacheDirectory(const FilePath& path) const { |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 373 | return cache_root_path_ == path || cache_root_path_.IsParent(path); |
| 374 | } |
| 375 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 376 | void DriveCache::AddObserver(Observer* observer) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 377 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 378 | observers_.AddObserver(observer); |
| 379 | } |
| 380 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 381 | void DriveCache::RemoveObserver(Observer* observer) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 382 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 383 | observers_.RemoveObserver(observer); |
| 384 | } |
| 385 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 386 | void DriveCache::GetCacheEntryOnUIThread( |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 387 | const std::string& resource_id, |
| 388 | const std::string& md5, |
| 389 | const GetCacheEntryCallback& callback) { |
| 390 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 391 | |
| 392 | bool* success = new bool(false); |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 393 | DriveCacheEntry* cache_entry = new DriveCacheEntry; |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 394 | blocking_task_runner_->PostTaskAndReply( |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 395 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 396 | base::Bind(&DriveCache::GetCacheEntryHelper, |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 397 | base::Unretained(this), |
| 398 | resource_id, |
| 399 | md5, |
| 400 | success, |
| 401 | cache_entry), |
| 402 | base::Bind(&RunGetCacheEntryCallback, |
| 403 | callback, |
| 404 | base::Owned(success), |
| 405 | base::Owned(cache_entry))); |
| 406 | } |
| 407 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 408 | void DriveCache::GetResourceIdsOfBacklogOnUIThread( |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 409 | const GetResourceIdsOfBacklogCallback& callback) { |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 410 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 411 | |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 412 | std::vector<std::string>* to_fetch = new std::vector<std::string>; |
| 413 | std::vector<std::string>* to_upload = new std::vector<std::string>; |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 414 | blocking_task_runner_->PostTaskAndReply( |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 415 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 416 | base::Bind(&DriveCache::GetResourceIdsOfBacklog, |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 417 | base::Unretained(this), |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 418 | to_fetch, |
| 419 | to_upload), |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 420 | base::Bind(&RunGetResourceIdsOfBacklogCallback, |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 421 | callback, |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 422 | base::Owned(to_fetch), |
| 423 | base::Owned(to_upload))); |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 424 | } |
| 425 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 426 | void DriveCache::GetResourceIdsOfExistingPinnedFilesOnUIThread( |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 427 | const GetResourceIdsCallback& callback) { |
| 428 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 429 | |
| 430 | std::vector<std::string>* resource_ids = new std::vector<std::string>; |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 431 | blocking_task_runner_->PostTaskAndReply( |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 432 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 433 | base::Bind(&DriveCache::GetResourceIdsOfExistingPinnedFiles, |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 434 | base::Unretained(this), |
| 435 | resource_ids), |
| 436 | base::Bind(&RunGetResourceIdsCallback, |
| 437 | callback, |
| 438 | base::Owned(resource_ids))); |
| 439 | } |
| 440 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 441 | void DriveCache::GetResourceIdsOfAllFilesOnUIThread( |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 442 | const GetResourceIdsCallback& callback) { |
| 443 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 444 | |
| 445 | std::vector<std::string>* resource_ids = new std::vector<std::string>; |
| 446 | blocking_task_runner_->PostTaskAndReply( |
| 447 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 448 | base::Bind(&DriveCache::GetResourceIdsOfAllFiles, |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 449 | base::Unretained(this), |
| 450 | resource_ids), |
| 451 | base::Bind(&RunGetResourceIdsCallback, |
| 452 | callback, |
| 453 | base::Owned(resource_ids))); |
| 454 | } |
| 455 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 456 | void DriveCache::FreeDiskSpaceIfNeededFor(int64 num_bytes, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 457 | bool* has_enough_space) { |
| 458 | AssertOnSequencedWorkerPool(); |
| 459 | |
| 460 | // Do nothing and return if we have enough space. |
| 461 | *has_enough_space = HasEnoughSpaceFor(num_bytes); |
| 462 | if (*has_enough_space) |
| 463 | return; |
| 464 | |
| 465 | // Otherwise, try to free up the disk space. |
| 466 | DVLOG(1) << "Freeing up disk space for " << num_bytes; |
| 467 | // First remove temporary files from the cache map. |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 468 | metadata_->RemoveTemporaryFiles(); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 469 | // Then remove all files under "tmp" directory. |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 470 | RemoveAllFiles(GetCacheDirectoryPath(DriveCache::CACHE_TYPE_TMP)); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 471 | |
| 472 | // Check the disk space again. |
| 473 | *has_enough_space = HasEnoughSpaceFor(num_bytes); |
| 474 | } |
| 475 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 476 | void DriveCache::GetFileOnUIThread(const std::string& resource_id, |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 477 | const std::string& md5, |
| 478 | const GetFileFromCacheCallback& callback) { |
| 479 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 480 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 481 | DriveFileError* error = new DriveFileError(DRIVE_FILE_OK); |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 482 | FilePath* cache_file_path = new FilePath; |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 483 | blocking_task_runner_->PostTaskAndReply( |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 484 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 485 | base::Bind(&DriveCache::GetFile, |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 486 | base::Unretained(this), |
| 487 | resource_id, |
| 488 | md5, |
| 489 | error, |
| 490 | cache_file_path), |
| 491 | base::Bind(&RunGetFileFromCacheCallback, |
| 492 | callback, |
| 493 | base::Owned(error), |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 494 | base::Owned(cache_file_path))); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 495 | } |
| 496 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 497 | void DriveCache::StoreOnUIThread(const std::string& resource_id, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 498 | const std::string& md5, |
| 499 | const FilePath& source_path, |
| 500 | FileOperationType file_operation_type, |
| 501 | const CacheOperationCallback& callback) { |
| 502 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 503 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 504 | DriveFileError* error = new DriveFileError(DRIVE_FILE_OK); |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 505 | blocking_task_runner_->PostTaskAndReply( |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 506 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 507 | base::Bind(&DriveCache::Store, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 508 | base::Unretained(this), |
| 509 | resource_id, |
| 510 | md5, |
| 511 | source_path, |
| 512 | file_operation_type, |
| 513 | error), |
| 514 | base::Bind(&RunCacheOperationCallback, |
| 515 | callback, |
| 516 | base::Owned(error), |
| 517 | resource_id, |
| 518 | md5)); |
| 519 | } |
| 520 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 521 | void DriveCache::PinOnUIThread(const std::string& resource_id, |
[email protected] | 44c0584e | 2012-06-15 23:55:41 | [diff] [blame] | 522 | const std::string& md5, |
| 523 | const CacheOperationCallback& callback) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 524 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 525 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 526 | DriveFileError* error = new DriveFileError(DRIVE_FILE_OK); |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 527 | blocking_task_runner_->PostTaskAndReply( |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 528 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 529 | base::Bind(&DriveCache::Pin, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 530 | base::Unretained(this), |
| 531 | resource_id, |
| 532 | md5, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 533 | DriveCache::FILE_OPERATION_MOVE, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 534 | error), |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 535 | base::Bind(&DriveCache::OnPinned, |
[email protected] | e53ac8f | 2012-08-02 07:05:00 | [diff] [blame] | 536 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 537 | base::Owned(error), |
| 538 | resource_id, |
| 539 | md5, |
| 540 | callback)); |
| 541 | } |
| 542 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 543 | void DriveCache::UnpinOnUIThread(const std::string& resource_id, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 544 | const std::string& md5, |
| 545 | const CacheOperationCallback& callback) { |
| 546 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 547 | DriveFileError* error = new DriveFileError(DRIVE_FILE_OK); |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 548 | blocking_task_runner_->PostTaskAndReply( |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 549 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 550 | base::Bind(&DriveCache::Unpin, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 551 | base::Unretained(this), |
| 552 | resource_id, |
| 553 | md5, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 554 | DriveCache::FILE_OPERATION_MOVE, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 555 | error), |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 556 | base::Bind(&DriveCache::OnUnpinned, |
[email protected] | e53ac8f | 2012-08-02 07:05:00 | [diff] [blame] | 557 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 558 | base::Owned(error), |
| 559 | resource_id, |
| 560 | md5, |
| 561 | callback)); |
| 562 | } |
| 563 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 564 | void DriveCache::SetMountedStateOnUIThread( |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 565 | const FilePath& file_path, |
| 566 | bool to_mount, |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 567 | const ChangeCacheStateCallback& callback) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 568 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 569 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 570 | DriveFileError* error = new DriveFileError(DRIVE_FILE_OK); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 571 | FilePath* cache_file_path = new FilePath; |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 572 | blocking_task_runner_->PostTaskAndReply( |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 573 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 574 | base::Bind(&DriveCache::SetMountedState, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 575 | base::Unretained(this), |
| 576 | file_path, |
| 577 | to_mount, |
| 578 | error, |
| 579 | cache_file_path), |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 580 | base::Bind(&RunChangeCacheStateCallback, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 581 | callback, |
| 582 | base::Owned(error), |
| 583 | base::Owned(cache_file_path))); |
| 584 | } |
| 585 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 586 | void DriveCache::MarkDirtyOnUIThread(const std::string& resource_id, |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 587 | const std::string& md5, |
| 588 | const GetFileFromCacheCallback& callback) { |
| 589 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 590 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 591 | DriveFileError* error = new DriveFileError(DRIVE_FILE_OK); |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 592 | FilePath* cache_file_path = new FilePath; |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 593 | blocking_task_runner_->PostTaskAndReply( |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 594 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 595 | base::Bind(&DriveCache::MarkDirty, |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 596 | base::Unretained(this), |
| 597 | resource_id, |
| 598 | md5, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 599 | DriveCache::FILE_OPERATION_MOVE, |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 600 | error, |
| 601 | cache_file_path), |
| 602 | base::Bind(&RunGetFileFromCacheCallback, |
| 603 | callback, |
| 604 | base::Owned(error), |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 605 | base::Owned(cache_file_path))); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 606 | } |
| 607 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 608 | void DriveCache::CommitDirtyOnUIThread(const std::string& resource_id, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 609 | const std::string& md5, |
| 610 | const CacheOperationCallback& callback) { |
| 611 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 612 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 613 | DriveFileError* error = new DriveFileError(DRIVE_FILE_OK); |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 614 | blocking_task_runner_->PostTaskAndReply( |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 615 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 616 | base::Bind(&DriveCache::CommitDirty, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 617 | base::Unretained(this), |
| 618 | resource_id, |
| 619 | md5, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 620 | DriveCache::FILE_OPERATION_MOVE, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 621 | error), |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 622 | base::Bind(&DriveCache::OnCommitDirty, |
[email protected] | e53ac8f | 2012-08-02 07:05:00 | [diff] [blame] | 623 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 624 | base::Owned(error), |
| 625 | resource_id, |
[email protected] | d7664c2 | 2012-06-18 19:35:49 | [diff] [blame] | 626 | md5, |
| 627 | callback)); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 628 | } |
| 629 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 630 | void DriveCache::ClearDirtyOnUIThread(const std::string& resource_id, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 631 | const std::string& md5, |
| 632 | const CacheOperationCallback& callback) { |
| 633 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 634 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 635 | DriveFileError* error = new DriveFileError(DRIVE_FILE_OK); |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 636 | blocking_task_runner_->PostTaskAndReply( |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 637 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 638 | base::Bind(&DriveCache::ClearDirty, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 639 | base::Unretained(this), |
| 640 | resource_id, |
| 641 | md5, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 642 | DriveCache::FILE_OPERATION_MOVE, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 643 | error), |
| 644 | base::Bind(&RunCacheOperationCallback, |
| 645 | callback, |
| 646 | base::Owned(error), |
| 647 | resource_id, |
| 648 | md5)); |
| 649 | } |
| 650 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 651 | void DriveCache::RemoveOnUIThread(const std::string& resource_id, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 652 | const CacheOperationCallback& callback) { |
| 653 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 654 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 655 | DriveFileError* error = new DriveFileError(DRIVE_FILE_OK); |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 656 | blocking_task_runner_->PostTaskAndReply( |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 657 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 658 | base::Bind(&DriveCache::Remove, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 659 | base::Unretained(this), |
| 660 | resource_id, |
| 661 | error), |
| 662 | base::Bind(&RunCacheOperationCallback, |
| 663 | callback, |
| 664 | base::Owned(error), |
| 665 | resource_id, |
| 666 | "" /* md5 */)); |
| 667 | } |
| 668 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 669 | void DriveCache::ClearAllOnUIThread(const ChangeCacheStateCallback& callback) { |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 670 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 671 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 672 | DriveFileError* error = new DriveFileError(DRIVE_FILE_OK); |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 673 | blocking_task_runner_->PostTaskAndReply( |
| 674 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 675 | base::Bind(&DriveCache::ClearAll, |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 676 | base::Unretained(this), |
| 677 | error), |
| 678 | base::Bind(&RunChangeCacheStateCallback, |
| 679 | callback, |
| 680 | base::Owned(error), |
| 681 | &cache_root_path_)); |
| 682 | } |
| 683 | |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame^] | 684 | void DriveCache::RequestInitializeOnUIThread( |
| 685 | const InitializeCacheCallback& callback) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 686 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame^] | 687 | DCHECK(!callback.is_null()); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 688 | |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame^] | 689 | bool* success = new bool(false); |
| 690 | blocking_task_runner_->PostTaskAndReply( |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 691 | FROM_HERE, |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame^] | 692 | base::Bind(&DriveCache::Initialize, |
| 693 | base::Unretained(this), |
| 694 | success), |
| 695 | base::Bind(&RunInitializeCacheCallback, |
| 696 | callback, |
| 697 | base::Owned(success))); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 698 | } |
| 699 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 700 | void DriveCache::RequestInitializeOnUIThreadForTesting() { |
[email protected] | d310bfc | 2012-08-10 09:41:28 | [diff] [blame] | 701 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 702 | |
| 703 | blocking_task_runner_->PostTask( |
| 704 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 705 | base::Bind(&DriveCache::InitializeForTesting, base::Unretained(this))); |
[email protected] | d310bfc | 2012-08-10 09:41:28 | [diff] [blame] | 706 | } |
| 707 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 708 | void DriveCache::ForceRescanOnUIThreadForTesting() { |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 709 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 710 | |
| 711 | blocking_task_runner_->PostTask( |
| 712 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 713 | base::Bind(&DriveCache::ForceRescanForTesting, base::Unretained(this))); |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 714 | } |
| 715 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 716 | bool DriveCache::GetCacheEntry(const std::string& resource_id, |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 717 | const std::string& md5, |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 718 | DriveCacheEntry* entry) { |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 719 | DCHECK(entry); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 720 | AssertOnSequencedWorkerPool(); |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 721 | return metadata_->GetCacheEntry(resource_id, md5, entry); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | // static |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 725 | DriveCache* DriveCache::CreateDriveCacheOnUIThread( |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 726 | const FilePath& cache_root_path, |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 727 | base::SequencedTaskRunner* blocking_task_runner) { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 728 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 729 | return new DriveCache(cache_root_path, blocking_task_runner); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 730 | } |
| 731 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 732 | void DriveCache::DestroyOnUIThread() { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 733 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 734 | |
| 735 | // Invalidate the weak pointer. |
[email protected] | e53ac8f | 2012-08-02 07:05:00 | [diff] [blame] | 736 | weak_ptr_factory_.InvalidateWeakPtrs(); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 737 | |
| 738 | // Destroy myself on the blocking pool. |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 739 | blocking_task_runner_->PostTask( |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 740 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 741 | base::Bind(&DriveCache::Destroy, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 742 | base::Unretained(this))); |
| 743 | } |
| 744 | |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame^] | 745 | void DriveCache::Initialize(bool* success) { |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 746 | AssertOnSequencedWorkerPool(); |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame^] | 747 | DCHECK(success); |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 748 | |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame^] | 749 | if (!InitCachePaths(cache_paths_)) { |
| 750 | *success = false; |
| 751 | return; |
| 752 | } |
| 753 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 754 | metadata_ = DriveCacheMetadata::CreateDriveCacheMetadata( |
[email protected] | f9e5cc0 | 2012-07-13 20:08:56 | [diff] [blame] | 755 | blocking_task_runner_).Pass(); |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame^] | 756 | *success = metadata_->Initialize(cache_paths_); |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 757 | } |
| 758 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 759 | void DriveCache::InitializeForTesting() { |
[email protected] | d310bfc | 2012-08-10 09:41:28 | [diff] [blame] | 760 | AssertOnSequencedWorkerPool(); |
| 761 | |
| 762 | InitCachePaths(cache_paths_); |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 763 | metadata_ = DriveCacheMetadata::CreateDriveCacheMetadataForTesting( |
[email protected] | d310bfc | 2012-08-10 09:41:28 | [diff] [blame] | 764 | blocking_task_runner_).Pass(); |
| 765 | metadata_->Initialize(cache_paths_); |
| 766 | } |
| 767 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 768 | void DriveCache::Destroy() { |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 769 | AssertOnSequencedWorkerPool(); |
| 770 | delete this; |
| 771 | } |
| 772 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 773 | void DriveCache::ForceRescanForTesting() { |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 774 | AssertOnSequencedWorkerPool(); |
| 775 | metadata_->ForceRescanForTesting(cache_paths_); |
| 776 | } |
| 777 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 778 | void DriveCache::GetResourceIdsOfBacklog( |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 779 | std::vector<std::string>* to_fetch, |
| 780 | std::vector<std::string>* to_upload) { |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 781 | AssertOnSequencedWorkerPool(); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 782 | DCHECK(to_fetch); |
| 783 | DCHECK(to_upload); |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 784 | |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 785 | metadata_->Iterate(base::Bind(&CollectBacklog, to_fetch, to_upload)); |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 786 | } |
| 787 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 788 | void DriveCache::GetResourceIdsOfExistingPinnedFiles( |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 789 | std::vector<std::string>* resource_ids) { |
| 790 | AssertOnSequencedWorkerPool(); |
| 791 | DCHECK(resource_ids); |
| 792 | |
| 793 | metadata_->Iterate(base::Bind(&CollectExistingPinnedFile, resource_ids)); |
| 794 | } |
| 795 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 796 | void DriveCache::GetResourceIdsOfAllFiles( |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 797 | std::vector<std::string>* resource_ids) { |
| 798 | AssertOnSequencedWorkerPool(); |
| 799 | DCHECK(resource_ids); |
| 800 | |
| 801 | metadata_->Iterate(base::Bind(&CollectAnyFile, resource_ids)); |
| 802 | } |
| 803 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 804 | void DriveCache::GetFile(const std::string& resource_id, |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 805 | const std::string& md5, |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 806 | DriveFileError* error, |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 807 | FilePath* cache_file_path) { |
| 808 | AssertOnSequencedWorkerPool(); |
| 809 | DCHECK(error); |
| 810 | DCHECK(cache_file_path); |
| 811 | |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 812 | DriveCacheEntry cache_entry; |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 813 | if (GetCacheEntry(resource_id, md5, &cache_entry) && |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 814 | cache_entry.is_present()) { |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 815 | CachedFileOrigin file_origin; |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 816 | if (cache_entry.is_mounted()) { |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 817 | file_origin = CACHED_FILE_MOUNTED; |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 818 | } else if (cache_entry.is_dirty()) { |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 819 | file_origin = CACHED_FILE_LOCALLY_MODIFIED; |
| 820 | } else { |
| 821 | file_origin = CACHED_FILE_FROM_SERVER; |
| 822 | } |
| 823 | *cache_file_path = GetCacheFilePath( |
| 824 | resource_id, |
| 825 | md5, |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 826 | GetSubDirectoryType(cache_entry), |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 827 | file_origin); |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 828 | *error = DRIVE_FILE_OK; |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 829 | } else { |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 830 | *error = DRIVE_FILE_ERROR_NOT_FOUND; |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 834 | void DriveCache::Store(const std::string& resource_id, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 835 | const std::string& md5, |
| 836 | const FilePath& source_path, |
| 837 | FileOperationType file_operation_type, |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 838 | DriveFileError* error) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 839 | AssertOnSequencedWorkerPool(); |
| 840 | DCHECK(error); |
| 841 | |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 842 | if (file_operation_type == FILE_OPERATION_COPY) { |
| 843 | int64 file_size; |
| 844 | if (!file_util::GetFileSize(source_path, &file_size)) { |
| 845 | LOG(WARNING) << "Couldn't get file size for: " << source_path.value(); |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 846 | *error = DRIVE_FILE_ERROR_FAILED; |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 847 | return; |
| 848 | } |
| 849 | |
| 850 | bool enough_space = false; |
| 851 | FreeDiskSpaceIfNeededFor(file_size, &enough_space); |
| 852 | if (!enough_space) { |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 853 | *error = DRIVE_FILE_ERROR_NO_SPACE; |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 854 | return; |
| 855 | } |
| 856 | } |
| 857 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 858 | FilePath dest_path; |
| 859 | FilePath symlink_path; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 860 | CacheSubDirectoryType sub_dir_type = CACHE_TYPE_TMP; |
| 861 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 862 | // If file was previously pinned, store it in persistent dir and create |
| 863 | // symlink in pinned dir. |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 864 | DriveCacheEntry cache_entry; |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 865 | if (GetCacheEntry(resource_id, md5, &cache_entry)) { // File exists in cache. |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 866 | // If file is dirty or mounted, return error. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 867 | if (cache_entry.is_dirty() || cache_entry.is_mounted()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 868 | LOG(WARNING) << "Can't store a file to replace a " |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 869 | << (cache_entry.is_dirty() ? "dirty" : "mounted") |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 870 | << " file: res_id=" << resource_id |
| 871 | << ", md5=" << md5; |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 872 | *error = DRIVE_FILE_ERROR_IN_USE; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 873 | return; |
| 874 | } |
| 875 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 876 | // If file is pinned, determines destination path. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 877 | if (cache_entry.is_pinned()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 878 | sub_dir_type = CACHE_TYPE_PERSISTENT; |
| 879 | dest_path = GetCacheFilePath(resource_id, md5, sub_dir_type, |
| 880 | CACHED_FILE_FROM_SERVER); |
| 881 | symlink_path = GetCacheFilePath( |
| 882 | resource_id, std::string(), CACHE_TYPE_PINNED, |
| 883 | CACHED_FILE_FROM_SERVER); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | // File wasn't pinned or doesn't exist in cache, store in tmp dir. |
| 888 | if (dest_path.empty()) { |
| 889 | DCHECK_EQ(CACHE_TYPE_TMP, sub_dir_type); |
| 890 | dest_path = GetCacheFilePath(resource_id, md5, sub_dir_type, |
| 891 | CACHED_FILE_FROM_SERVER); |
| 892 | } |
| 893 | |
| 894 | *error = ModifyCacheState( |
| 895 | source_path, |
| 896 | dest_path, |
| 897 | file_operation_type, |
| 898 | symlink_path, |
| 899 | !symlink_path.empty()); // create symlink |
| 900 | |
[email protected] | f60c670b | 2012-09-13 06:19:25 | [diff] [blame] | 901 | // Determine search pattern for stale filenames corresponding to resource_id, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 902 | // either "<resource_id>*" or "<resource_id>.*". |
| 903 | FilePath stale_filenames_pattern; |
| 904 | if (md5.empty()) { |
| 905 | // No md5 means no extension, append '*' after base name, i.e. |
| 906 | // "<resource_id>*". |
| 907 | // Cannot call |dest_path|.ReplaceExtension when there's no md5 extension: |
| 908 | // if base name of |dest_path| (i.e. escaped resource_id) contains the |
| 909 | // extension separator '.', ReplaceExtension will remove it and everything |
| 910 | // after it. The result will be nothing like the escaped resource_id. |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 911 | stale_filenames_pattern = FilePath(dest_path.value() + util::kWildCard); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 912 | } else { |
| 913 | // Replace md5 extension with '*' i.e. "<resource_id>.*". |
| 914 | // Note that ReplaceExtension automatically prefixes the extension with the |
| 915 | // extension separator '.'. |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 916 | stale_filenames_pattern = dest_path.ReplaceExtension(util::kWildCard); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | // Delete files that match |stale_filenames_pattern| except for |dest_path|. |
| 920 | DeleteFilesSelectively(stale_filenames_pattern, dest_path); |
| 921 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 922 | if (*error == DRIVE_FILE_OK) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 923 | // Now that file operations have completed, update cache map. |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 924 | cache_entry.set_md5(md5); |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 925 | cache_entry.set_is_present(true); |
| 926 | cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 927 | metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 931 | void DriveCache::Pin(const std::string& resource_id, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 932 | const std::string& md5, |
| 933 | FileOperationType file_operation_type, |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 934 | DriveFileError* error) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 935 | AssertOnSequencedWorkerPool(); |
| 936 | DCHECK(error); |
| 937 | |
| 938 | FilePath source_path; |
| 939 | FilePath dest_path; |
| 940 | FilePath symlink_path; |
| 941 | bool create_symlink = true; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 942 | CacheSubDirectoryType sub_dir_type = CACHE_TYPE_PERSISTENT; |
| 943 | |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 944 | DriveCacheEntry cache_entry; |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 945 | if (!GetCacheEntry(resource_id, md5, &cache_entry)) { |
| 946 | // Entry does not exist in cache. |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 947 | // Set both |dest_path| and |source_path| to /dev/null, so that: |
| 948 | // 1) ModifyCacheState won't move files when |source_path| and |dest_path| |
| 949 | // are the same. |
[email protected] | 9935913b | 2012-09-06 14:09:57 | [diff] [blame] | 950 | // 2) symlinks to /dev/null will be picked up by DriveSyncClient to download |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 951 | // pinned files that don't exist in cache. |
[email protected] | 30d9dda | 2012-06-30 05:56:28 | [diff] [blame] | 952 | dest_path = FilePath::FromUTF8Unsafe(util::kSymLinkToDevNull); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 953 | source_path = dest_path; |
| 954 | |
[email protected] | 2a6fe6e | 2012-07-10 06:01:04 | [diff] [blame] | 955 | // Set sub_dir_type to TMP. The file will be first downloaded in 'tmp', |
| 956 | // then moved to 'persistent'. |
| 957 | sub_dir_type = CACHE_TYPE_TMP; |
[email protected] | 109eb5c | 2012-07-12 03:20:05 | [diff] [blame] | 958 | } else { // File exists in cache, determines destination path. |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 959 | // Determine source and destination paths. |
| 960 | |
| 961 | // If file is dirty or mounted, don't move it, so determine |dest_path| and |
| 962 | // set |source_path| the same, because ModifyCacheState only moves files if |
| 963 | // source and destination are different. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 964 | if (cache_entry.is_dirty() || cache_entry.is_mounted()) { |
| 965 | DCHECK(cache_entry.is_persistent()); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 966 | dest_path = GetCacheFilePath(resource_id, |
| 967 | md5, |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 968 | GetSubDirectoryType(cache_entry), |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 969 | CACHED_FILE_LOCALLY_MODIFIED); |
| 970 | source_path = dest_path; |
| 971 | } else { |
| 972 | // Gets the current path of the file in cache. |
| 973 | source_path = GetCacheFilePath(resource_id, |
| 974 | md5, |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 975 | GetSubDirectoryType(cache_entry), |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 976 | CACHED_FILE_FROM_SERVER); |
| 977 | |
| 978 | // If file was pinned before but actual file blob doesn't exist in cache: |
| 979 | // - don't need to move the file, so set |dest_path| to |source_path|, |
| 980 | // because ModifyCacheState only moves files if source and destination |
| 981 | // are different |
| 982 | // - don't create symlink since it already exists. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 983 | if (!cache_entry.is_present()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 984 | dest_path = source_path; |
| 985 | create_symlink = false; |
| 986 | } else { // File exists, move it to persistent dir. |
| 987 | dest_path = GetCacheFilePath(resource_id, |
| 988 | md5, |
| 989 | CACHE_TYPE_PERSISTENT, |
| 990 | CACHED_FILE_FROM_SERVER); |
| 991 | } |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | // Create symlink in pinned dir. |
| 996 | if (create_symlink) { |
| 997 | symlink_path = GetCacheFilePath(resource_id, |
| 998 | std::string(), |
| 999 | CACHE_TYPE_PINNED, |
| 1000 | CACHED_FILE_FROM_SERVER); |
| 1001 | } |
| 1002 | |
| 1003 | *error = ModifyCacheState(source_path, |
| 1004 | dest_path, |
| 1005 | file_operation_type, |
| 1006 | symlink_path, |
| 1007 | create_symlink); |
| 1008 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1009 | if (*error == DRIVE_FILE_OK) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1010 | // Now that file operations have completed, update cache map. |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 1011 | cache_entry.set_md5(md5); |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1012 | cache_entry.set_is_pinned(true); |
| 1013 | cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 1014 | metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1018 | void DriveCache::Unpin(const std::string& resource_id, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1019 | const std::string& md5, |
| 1020 | FileOperationType file_operation_type, |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1021 | DriveFileError* error) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1022 | AssertOnSequencedWorkerPool(); |
| 1023 | DCHECK(error); |
| 1024 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1025 | // Unpinning a file means its entry must exist in cache. |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 1026 | DriveCacheEntry cache_entry; |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 1027 | if (!GetCacheEntry(resource_id, md5, &cache_entry)) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1028 | LOG(WARNING) << "Can't unpin a file that wasn't pinned or cached: res_id=" |
| 1029 | << resource_id |
| 1030 | << ", md5=" << md5; |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1031 | *error = DRIVE_FILE_ERROR_NOT_FOUND; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1032 | return; |
| 1033 | } |
| 1034 | |
| 1035 | // Entry exists in cache, determines source and destination paths. |
| 1036 | |
| 1037 | FilePath source_path; |
| 1038 | FilePath dest_path; |
| 1039 | CacheSubDirectoryType sub_dir_type = CACHE_TYPE_TMP; |
| 1040 | |
| 1041 | // If file is dirty or mounted, don't move it, so determine |dest_path| and |
| 1042 | // set |source_path| the same, because ModifyCacheState moves files if source |
| 1043 | // and destination are different. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1044 | if (cache_entry.is_dirty() || cache_entry.is_mounted()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1045 | sub_dir_type = CACHE_TYPE_PERSISTENT; |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1046 | DCHECK(cache_entry.is_persistent()); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1047 | dest_path = GetCacheFilePath(resource_id, |
| 1048 | md5, |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 1049 | GetSubDirectoryType(cache_entry), |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1050 | CACHED_FILE_LOCALLY_MODIFIED); |
| 1051 | source_path = dest_path; |
| 1052 | } else { |
| 1053 | // Gets the current path of the file in cache. |
| 1054 | source_path = GetCacheFilePath(resource_id, |
| 1055 | md5, |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 1056 | GetSubDirectoryType(cache_entry), |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1057 | CACHED_FILE_FROM_SERVER); |
| 1058 | |
| 1059 | // If file was pinned but actual file blob still doesn't exist in cache, |
| 1060 | // don't need to move the file, so set |dest_path| to |source_path|, because |
| 1061 | // ModifyCacheState only moves files if source and destination are |
| 1062 | // different. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1063 | if (!cache_entry.is_present()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1064 | dest_path = source_path; |
| 1065 | } else { // File exists, move it to tmp dir. |
| 1066 | dest_path = GetCacheFilePath(resource_id, md5, |
| 1067 | CACHE_TYPE_TMP, |
| 1068 | CACHED_FILE_FROM_SERVER); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | // If file was pinned, get absolute path of symlink in pinned dir so as to |
| 1073 | // remove it. |
| 1074 | FilePath symlink_path; |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1075 | if (cache_entry.is_pinned()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1076 | symlink_path = GetCacheFilePath(resource_id, |
| 1077 | std::string(), |
| 1078 | CACHE_TYPE_PINNED, |
| 1079 | CACHED_FILE_FROM_SERVER); |
| 1080 | } |
| 1081 | |
| 1082 | *error = ModifyCacheState( |
| 1083 | source_path, |
| 1084 | dest_path, |
| 1085 | file_operation_type, |
| 1086 | symlink_path, // This will be deleted if it exists. |
| 1087 | false /* don't create symlink*/); |
| 1088 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1089 | if (*error == DRIVE_FILE_OK) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1090 | // Now that file operations have completed, update cache map. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1091 | if (cache_entry.is_present()) { |
[email protected] | 48477fe | 2012-07-12 17:45:08 | [diff] [blame] | 1092 | cache_entry.set_md5(md5); |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1093 | cache_entry.set_is_pinned(false); |
| 1094 | cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); |
[email protected] | 48477fe | 2012-07-12 17:45:08 | [diff] [blame] | 1095 | metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); |
[email protected] | 3423871a | 2012-07-12 00:41:27 | [diff] [blame] | 1096 | } else { |
| 1097 | // Remove the existing entry if we are unpinning a non-present file. |
| 1098 | metadata_->RemoveCacheEntry(resource_id); |
| 1099 | } |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1103 | void DriveCache::SetMountedState(const FilePath& file_path, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1104 | bool to_mount, |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1105 | DriveFileError *error, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1106 | FilePath* cache_file_path) { |
| 1107 | AssertOnSequencedWorkerPool(); |
| 1108 | DCHECK(error); |
| 1109 | DCHECK(cache_file_path); |
| 1110 | |
| 1111 | // Parse file path to obtain resource_id, md5 and extra_extension. |
| 1112 | std::string resource_id; |
| 1113 | std::string md5; |
| 1114 | std::string extra_extension; |
| 1115 | util::ParseCacheFilePath(file_path, &resource_id, &md5, &extra_extension); |
| 1116 | // The extra_extension shall be ".mounted" iff we're unmounting. |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 1117 | DCHECK(!to_mount == (extra_extension == util::kMountedArchiveFileExtension)); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1118 | |
| 1119 | // Get cache entry associated with the resource_id and md5 |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 1120 | DriveCacheEntry cache_entry; |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 1121 | if (!GetCacheEntry(resource_id, md5, &cache_entry)) { |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1122 | *error = DRIVE_FILE_ERROR_NOT_FOUND; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1123 | return; |
| 1124 | } |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1125 | if (to_mount == cache_entry.is_mounted()) { |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1126 | *error = DRIVE_FILE_ERROR_INVALID_OPERATION; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1127 | return; |
| 1128 | } |
| 1129 | |
| 1130 | // Get the subdir type and path for the unmounted state. |
| 1131 | CacheSubDirectoryType unmounted_subdir = |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1132 | cache_entry.is_pinned() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1133 | FilePath unmounted_path = GetCacheFilePath( |
| 1134 | resource_id, md5, unmounted_subdir, CACHED_FILE_FROM_SERVER); |
| 1135 | |
| 1136 | // Get the subdir type and path for the mounted state. |
| 1137 | CacheSubDirectoryType mounted_subdir = CACHE_TYPE_PERSISTENT; |
| 1138 | FilePath mounted_path = GetCacheFilePath( |
| 1139 | resource_id, md5, mounted_subdir, CACHED_FILE_MOUNTED); |
| 1140 | |
| 1141 | // Determine the source and destination paths for moving the cache blob. |
| 1142 | FilePath source_path; |
| 1143 | CacheSubDirectoryType dest_subdir; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1144 | if (to_mount) { |
| 1145 | source_path = unmounted_path; |
| 1146 | *cache_file_path = mounted_path; |
| 1147 | dest_subdir = mounted_subdir; |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1148 | cache_entry.set_is_mounted(true); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1149 | } else { |
| 1150 | source_path = mounted_path; |
| 1151 | *cache_file_path = unmounted_path; |
| 1152 | dest_subdir = unmounted_subdir; |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1153 | cache_entry.set_is_mounted(false); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | // Move cache blob from source path to destination path. |
| 1157 | *error = ModifyCacheState(source_path, *cache_file_path, |
| 1158 | FILE_OPERATION_MOVE, FilePath(), false); |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1159 | if (*error == DRIVE_FILE_OK) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1160 | // Now that cache operation is complete, update cache map |
[email protected] | 48477fe | 2012-07-12 17:45:08 | [diff] [blame] | 1161 | cache_entry.set_md5(md5); |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1162 | cache_entry.set_is_persistent(dest_subdir == CACHE_TYPE_PERSISTENT); |
[email protected] | 48477fe | 2012-07-12 17:45:08 | [diff] [blame] | 1163 | metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1164 | } |
| 1165 | } |
| 1166 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1167 | void DriveCache::MarkDirty(const std::string& resource_id, |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 1168 | const std::string& md5, |
| 1169 | FileOperationType file_operation_type, |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1170 | DriveFileError* error, |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 1171 | FilePath* cache_file_path) { |
| 1172 | AssertOnSequencedWorkerPool(); |
| 1173 | DCHECK(error); |
| 1174 | DCHECK(cache_file_path); |
| 1175 | |
| 1176 | // If file has already been marked dirty in previous instance of chrome, we |
| 1177 | // would have lost the md5 info during cache initialization, because the file |
| 1178 | // would have been renamed to .local extension. |
| 1179 | // So, search for entry in cache without comparing md5. |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 1180 | |
| 1181 | // Marking a file dirty means its entry and actual file blob must exist in |
| 1182 | // cache. |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 1183 | DriveCacheEntry cache_entry; |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 1184 | if (!GetCacheEntry(resource_id, std::string(), &cache_entry) || |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1185 | !cache_entry.is_present()) { |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 1186 | LOG(WARNING) << "Can't mark dirty a file that wasn't cached: res_id=" |
| 1187 | << resource_id |
| 1188 | << ", md5=" << md5; |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1189 | *error = DRIVE_FILE_ERROR_NOT_FOUND; |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 1190 | return; |
| 1191 | } |
| 1192 | |
| 1193 | // If a file is already dirty (i.e. MarkDirtyInCache was called before), |
| 1194 | // delete outgoing symlink if it exists. |
| 1195 | // TODO(benchan): We should only delete outgoing symlink if file is currently |
| 1196 | // not being uploaded. However, for now, cache doesn't know if uploading of a |
| 1197 | // file is in progress. Per zel, the upload process should be canceled before |
| 1198 | // MarkDirtyInCache is called again. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1199 | if (cache_entry.is_dirty()) { |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 1200 | // The file must be in persistent dir. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1201 | DCHECK(cache_entry.is_persistent()); |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 1202 | |
| 1203 | // Determine symlink path in outgoing dir, so as to remove it. |
| 1204 | FilePath symlink_path = GetCacheFilePath( |
| 1205 | resource_id, |
| 1206 | std::string(), |
| 1207 | CACHE_TYPE_OUTGOING, |
| 1208 | CACHED_FILE_FROM_SERVER); |
| 1209 | |
| 1210 | // We're not moving files here, so simply use empty FilePath for both |
| 1211 | // |source_path| and |dest_path| because ModifyCacheState only move files |
| 1212 | // if source and destination are different. |
| 1213 | *error = ModifyCacheState( |
| 1214 | FilePath(), // non-applicable source path |
| 1215 | FilePath(), // non-applicable dest path |
| 1216 | file_operation_type, |
| 1217 | symlink_path, |
| 1218 | false /* don't create symlink */); |
| 1219 | |
| 1220 | // Determine current path of dirty file. |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1221 | if (*error == DRIVE_FILE_OK) { |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 1222 | *cache_file_path = GetCacheFilePath( |
| 1223 | resource_id, |
| 1224 | md5, |
| 1225 | CACHE_TYPE_PERSISTENT, |
| 1226 | CACHED_FILE_LOCALLY_MODIFIED); |
| 1227 | } |
| 1228 | return; |
| 1229 | } |
| 1230 | |
| 1231 | // Move file to persistent dir with new .local extension. |
| 1232 | |
| 1233 | // Get the current path of the file in cache. |
| 1234 | FilePath source_path = GetCacheFilePath( |
| 1235 | resource_id, |
| 1236 | md5, |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 1237 | GetSubDirectoryType(cache_entry), |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 1238 | CACHED_FILE_FROM_SERVER); |
| 1239 | |
| 1240 | // Determine destination path. |
[email protected] | 3dc88ee | 2012-07-11 21:04:11 | [diff] [blame] | 1241 | const CacheSubDirectoryType sub_dir_type = CACHE_TYPE_PERSISTENT; |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 1242 | *cache_file_path = GetCacheFilePath(resource_id, |
| 1243 | md5, |
| 1244 | sub_dir_type, |
| 1245 | CACHED_FILE_LOCALLY_MODIFIED); |
| 1246 | |
| 1247 | // If file is pinned, update symlink in pinned dir. |
| 1248 | FilePath symlink_path; |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1249 | if (cache_entry.is_pinned()) { |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 1250 | symlink_path = GetCacheFilePath(resource_id, |
| 1251 | std::string(), |
| 1252 | CACHE_TYPE_PINNED, |
| 1253 | CACHED_FILE_FROM_SERVER); |
| 1254 | } |
| 1255 | |
| 1256 | *error = ModifyCacheState( |
| 1257 | source_path, |
| 1258 | *cache_file_path, |
| 1259 | file_operation_type, |
| 1260 | symlink_path, |
| 1261 | !symlink_path.empty() /* create symlink */); |
| 1262 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1263 | if (*error == DRIVE_FILE_OK) { |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 1264 | // Now that file operations have completed, update cache map. |
[email protected] | 48477fe | 2012-07-12 17:45:08 | [diff] [blame] | 1265 | cache_entry.set_md5(md5); |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1266 | cache_entry.set_is_dirty(true); |
| 1267 | cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); |
[email protected] | 48477fe | 2012-07-12 17:45:08 | [diff] [blame] | 1268 | metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 1269 | } |
| 1270 | } |
| 1271 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1272 | void DriveCache::CommitDirty(const std::string& resource_id, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1273 | const std::string& md5, |
| 1274 | FileOperationType file_operation_type, |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1275 | DriveFileError* error) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1276 | AssertOnSequencedWorkerPool(); |
| 1277 | DCHECK(error); |
| 1278 | |
| 1279 | // If file has already been marked dirty in previous instance of chrome, we |
| 1280 | // would have lost the md5 info during cache initialization, because the file |
| 1281 | // would have been renamed to .local extension. |
| 1282 | // So, search for entry in cache without comparing md5. |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1283 | |
| 1284 | // Committing a file dirty means its entry and actual file blob must exist in |
| 1285 | // cache. |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 1286 | DriveCacheEntry cache_entry; |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 1287 | if (!GetCacheEntry(resource_id, std::string(), &cache_entry) || |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1288 | !cache_entry.is_present()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1289 | LOG(WARNING) << "Can't commit dirty a file that wasn't cached: res_id=" |
| 1290 | << resource_id |
| 1291 | << ", md5=" << md5; |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1292 | *error = DRIVE_FILE_ERROR_NOT_FOUND; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1293 | return; |
| 1294 | } |
| 1295 | |
| 1296 | // If a file is not dirty (it should have been marked dirty via |
| 1297 | // MarkDirtyInCache), committing it dirty is an invalid operation. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1298 | if (!cache_entry.is_dirty()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1299 | LOG(WARNING) << "Can't commit a non-dirty file: res_id=" |
| 1300 | << resource_id |
| 1301 | << ", md5=" << md5; |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1302 | *error = DRIVE_FILE_ERROR_INVALID_OPERATION; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1303 | return; |
| 1304 | } |
| 1305 | |
| 1306 | // Dirty files must be in persistent dir. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1307 | DCHECK(cache_entry.is_persistent()); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1308 | |
| 1309 | // Create symlink in outgoing dir. |
| 1310 | FilePath symlink_path = GetCacheFilePath(resource_id, |
| 1311 | std::string(), |
| 1312 | CACHE_TYPE_OUTGOING, |
| 1313 | CACHED_FILE_FROM_SERVER); |
| 1314 | |
| 1315 | // Get target path of symlink i.e. current path of the file in cache. |
| 1316 | FilePath target_path = GetCacheFilePath(resource_id, |
| 1317 | md5, |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 1318 | GetSubDirectoryType(cache_entry), |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1319 | CACHED_FILE_LOCALLY_MODIFIED); |
| 1320 | |
| 1321 | // Since there's no need to move files, use |target_path| for both |
| 1322 | // |source_path| and |dest_path|, because ModifyCacheState only moves files |
| 1323 | // if source and destination are different. |
| 1324 | *error = ModifyCacheState(target_path, // source |
| 1325 | target_path, // destination |
| 1326 | file_operation_type, |
| 1327 | symlink_path, |
| 1328 | true /* create symlink */); |
| 1329 | } |
| 1330 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1331 | void DriveCache::ClearDirty(const std::string& resource_id, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1332 | const std::string& md5, |
| 1333 | FileOperationType file_operation_type, |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1334 | DriveFileError* error) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1335 | AssertOnSequencedWorkerPool(); |
| 1336 | DCHECK(error); |
| 1337 | |
| 1338 | // |md5| is the new .<md5> extension to rename the file to. |
| 1339 | // So, search for entry in cache without comparing md5. |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 1340 | DriveCacheEntry cache_entry; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1341 | |
| 1342 | // Clearing a dirty file means its entry and actual file blob must exist in |
| 1343 | // cache. |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 1344 | if (!GetCacheEntry(resource_id, std::string(), &cache_entry) || |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1345 | !cache_entry.is_present()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1346 | LOG(WARNING) << "Can't clear dirty state of a file that wasn't cached: " |
| 1347 | << "res_id=" << resource_id |
| 1348 | << ", md5=" << md5; |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1349 | *error = DRIVE_FILE_ERROR_NOT_FOUND; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1350 | return; |
| 1351 | } |
| 1352 | |
| 1353 | // If a file is not dirty (it should have been marked dirty via |
| 1354 | // MarkDirtyInCache), clearing its dirty state is an invalid operation. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1355 | if (!cache_entry.is_dirty()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1356 | LOG(WARNING) << "Can't clear dirty state of a non-dirty file: res_id=" |
| 1357 | << resource_id |
| 1358 | << ", md5=" << md5; |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1359 | *error = DRIVE_FILE_ERROR_INVALID_OPERATION; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1360 | return; |
| 1361 | } |
| 1362 | |
| 1363 | // File must be dirty and hence in persistent dir. |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1364 | DCHECK(cache_entry.is_persistent()); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1365 | |
| 1366 | // Get the current path of the file in cache. |
| 1367 | FilePath source_path = GetCacheFilePath(resource_id, |
| 1368 | md5, |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 1369 | GetSubDirectoryType(cache_entry), |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1370 | CACHED_FILE_LOCALLY_MODIFIED); |
| 1371 | |
| 1372 | // Determine destination path. |
| 1373 | // If file is pinned, move it to persistent dir with .md5 extension; |
| 1374 | // otherwise, move it to tmp dir with .md5 extension. |
[email protected] | 3dc88ee | 2012-07-11 21:04:11 | [diff] [blame] | 1375 | const CacheSubDirectoryType sub_dir_type = |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1376 | cache_entry.is_pinned() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1377 | FilePath dest_path = GetCacheFilePath(resource_id, |
| 1378 | md5, |
| 1379 | sub_dir_type, |
| 1380 | CACHED_FILE_FROM_SERVER); |
| 1381 | |
| 1382 | // Delete symlink in outgoing dir. |
| 1383 | FilePath symlink_path = GetCacheFilePath(resource_id, |
| 1384 | std::string(), |
| 1385 | CACHE_TYPE_OUTGOING, |
| 1386 | CACHED_FILE_FROM_SERVER); |
| 1387 | |
| 1388 | *error = ModifyCacheState(source_path, |
| 1389 | dest_path, |
| 1390 | file_operation_type, |
| 1391 | symlink_path, |
| 1392 | false /* don't create symlink */); |
| 1393 | |
| 1394 | // If file is pinned, update symlink in pinned dir. |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1395 | if (*error == DRIVE_FILE_OK && cache_entry.is_pinned()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1396 | symlink_path = GetCacheFilePath(resource_id, |
| 1397 | std::string(), |
| 1398 | CACHE_TYPE_PINNED, |
| 1399 | CACHED_FILE_FROM_SERVER); |
| 1400 | |
| 1401 | // Since there's no moving of files here, use |dest_path| for both |
| 1402 | // |source_path| and |dest_path|, because ModifyCacheState only moves files |
| 1403 | // if source and destination are different. |
| 1404 | *error = ModifyCacheState(dest_path, // source path |
| 1405 | dest_path, // destination path |
| 1406 | file_operation_type, |
| 1407 | symlink_path, |
| 1408 | true /* create symlink */); |
| 1409 | } |
| 1410 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1411 | if (*error == DRIVE_FILE_OK) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1412 | // Now that file operations have completed, update cache map. |
[email protected] | 48477fe | 2012-07-12 17:45:08 | [diff] [blame] | 1413 | cache_entry.set_md5(md5); |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1414 | cache_entry.set_is_dirty(false); |
| 1415 | cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); |
[email protected] | 48477fe | 2012-07-12 17:45:08 | [diff] [blame] | 1416 | metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1417 | } |
| 1418 | } |
| 1419 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1420 | void DriveCache::Remove(const std::string& resource_id, |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1421 | DriveFileError* error) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1422 | AssertOnSequencedWorkerPool(); |
| 1423 | DCHECK(error); |
| 1424 | |
[email protected] | 3423871a | 2012-07-12 00:41:27 | [diff] [blame] | 1425 | // MD5 is not passed into RemoveCacheEntry because we would delete all |
| 1426 | // cache files corresponding to <resource_id> regardless of the md5. |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1427 | // So, search for entry in cache without taking md5 into account. |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 1428 | DriveCacheEntry cache_entry; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1429 | |
| 1430 | // 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] | 1431 | const bool entry_found = |
| 1432 | GetCacheEntry(resource_id, std::string(), &cache_entry); |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1433 | if (!entry_found || cache_entry.is_dirty() || cache_entry.is_mounted()) { |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1434 | DVLOG(1) << "Entry is " |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 1435 | << (entry_found ? |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1436 | (cache_entry.is_dirty() ? "dirty" : "mounted") : |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1437 | "non-existent") |
| 1438 | << " in cache, not removing"; |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1439 | *error = DRIVE_FILE_OK; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1440 | return; |
| 1441 | } |
| 1442 | |
| 1443 | // Determine paths to delete all cache versions of |resource_id| in |
| 1444 | // persistent, tmp and pinned directories. |
| 1445 | std::vector<FilePath> paths_to_delete; |
| 1446 | |
| 1447 | // For files in persistent and tmp dirs, delete files that match |
| 1448 | // "<resource_id>.*". |
| 1449 | paths_to_delete.push_back(GetCacheFilePath(resource_id, |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 1450 | util::kWildCard, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1451 | CACHE_TYPE_PERSISTENT, |
| 1452 | CACHED_FILE_FROM_SERVER)); |
| 1453 | paths_to_delete.push_back(GetCacheFilePath(resource_id, |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 1454 | util::kWildCard, |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1455 | CACHE_TYPE_TMP, |
| 1456 | CACHED_FILE_FROM_SERVER)); |
| 1457 | |
| 1458 | // For pinned files, filename is "<resource_id>" with no extension, so delete |
| 1459 | // "<resource_id>". |
| 1460 | paths_to_delete.push_back(GetCacheFilePath(resource_id, |
| 1461 | std::string(), |
| 1462 | CACHE_TYPE_PINNED, |
| 1463 | CACHED_FILE_FROM_SERVER)); |
| 1464 | |
| 1465 | // Don't delete locally modified (i.e. dirty and possibly outgoing) files. |
| 1466 | // Since we're not deleting outgoing symlinks, we don't need to append |
| 1467 | // outgoing path to |paths_to_delete|. |
| 1468 | FilePath path_to_keep = GetCacheFilePath(resource_id, |
| 1469 | std::string(), |
| 1470 | CACHE_TYPE_PERSISTENT, |
| 1471 | CACHED_FILE_LOCALLY_MODIFIED); |
| 1472 | |
| 1473 | for (size_t i = 0; i < paths_to_delete.size(); ++i) { |
| 1474 | DeleteFilesSelectively(paths_to_delete[i], path_to_keep); |
| 1475 | } |
| 1476 | |
| 1477 | // Now that all file operations have completed, remove from cache map. |
[email protected] | 3423871a | 2012-07-12 00:41:27 | [diff] [blame] | 1478 | metadata_->RemoveCacheEntry(resource_id); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1479 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1480 | *error = DRIVE_FILE_OK; |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1481 | } |
| 1482 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1483 | void DriveCache::ClearAll(DriveFileError* error) { |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 1484 | AssertOnSequencedWorkerPool(); |
| 1485 | DCHECK(error); |
| 1486 | |
| 1487 | bool success = file_util::Delete(cache_root_path_, true); |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame^] | 1488 | if (!success) { |
| 1489 | LOG(WARNING) << "Failed to delete the cache directory"; |
| 1490 | *error = DRIVE_FILE_ERROR_FAILED; |
| 1491 | return; |
| 1492 | } |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 1493 | |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame^] | 1494 | Initialize(&success); |
| 1495 | if (!success) { |
| 1496 | LOG(WARNING) << "Failed to initialize the cache"; |
| 1497 | *error = DRIVE_FILE_ERROR_FAILED; |
| 1498 | return; |
| 1499 | } |
| 1500 | |
| 1501 | *error = DRIVE_FILE_OK; |
| 1502 | return; |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 1503 | } |
| 1504 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1505 | void DriveCache::OnPinned(DriveFileError* error, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 1506 | const std::string& resource_id, |
| 1507 | const std::string& md5, |
| 1508 | const CacheOperationCallback& callback) { |
| 1509 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1510 | DCHECK(error); |
| 1511 | |
| 1512 | if (!callback.is_null()) |
| 1513 | callback.Run(*error, resource_id, md5); |
| 1514 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1515 | if (*error == DRIVE_FILE_OK) |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 1516 | FOR_EACH_OBSERVER(Observer, observers_, OnCachePinned(resource_id, md5)); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 1517 | } |
| 1518 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1519 | void DriveCache::OnUnpinned(DriveFileError* error, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 1520 | const std::string& resource_id, |
| 1521 | const std::string& md5, |
| 1522 | const CacheOperationCallback& callback) { |
| 1523 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1524 | DCHECK(error); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1525 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 1526 | if (!callback.is_null()) |
| 1527 | callback.Run(*error, resource_id, md5); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1528 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1529 | if (*error == DRIVE_FILE_OK) |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 1530 | FOR_EACH_OBSERVER(Observer, observers_, OnCacheUnpinned(resource_id, md5)); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1531 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 1532 | // Now the file is moved from "persistent" to "tmp" directory. |
| 1533 | // It's a chance to free up space if needed. |
| 1534 | bool* has_enough_space = new bool(false); |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 1535 | blocking_task_runner_->PostTask( |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 1536 | FROM_HERE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1537 | base::Bind(&DriveCache::FreeDiskSpaceIfNeededFor, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 1538 | base::Unretained(this), |
| 1539 | 0, |
| 1540 | base::Owned(has_enough_space))); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 1541 | } |
| 1542 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1543 | void DriveCache::OnCommitDirty(DriveFileError* error, |
[email protected] | d7664c2 | 2012-06-18 19:35:49 | [diff] [blame] | 1544 | const std::string& resource_id, |
| 1545 | const std::string& md5, |
| 1546 | const CacheOperationCallback& callback) { |
| 1547 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1548 | DCHECK(error); |
| 1549 | |
| 1550 | if (!callback.is_null()) |
| 1551 | callback.Run(*error, resource_id, md5); |
| 1552 | |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 1553 | if (*error == DRIVE_FILE_OK) |
[email protected] | d7664c2 | 2012-06-18 19:35:49 | [diff] [blame] | 1554 | FOR_EACH_OBSERVER(Observer, observers_, OnCacheCommitted(resource_id)); |
| 1555 | } |
| 1556 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1557 | void DriveCache::GetCacheEntryHelper(const std::string& resource_id, |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 1558 | const std::string& md5, |
| 1559 | bool* success, |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 1560 | DriveCacheEntry* cache_entry) { |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 1561 | AssertOnSequencedWorkerPool(); |
| 1562 | DCHECK(success); |
| 1563 | DCHECK(cache_entry); |
| 1564 | |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 1565 | *success = GetCacheEntry(resource_id, md5, cache_entry); |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 1566 | } |
| 1567 | |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 1568 | // static |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1569 | FilePath DriveCache::GetCacheRootPath(Profile* profile) { |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 1570 | FilePath cache_base_path; |
| 1571 | chrome::GetUserCacheDirectory(profile->GetPath(), &cache_base_path); |
| 1572 | FilePath cache_root_path = |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1573 | cache_base_path.Append(chrome::kDriveCacheDirname); |
| 1574 | return cache_root_path.Append(kDriveCacheVersionDir); |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 1575 | } |
| 1576 | |
[email protected] | 30d9dda | 2012-06-30 05:56:28 | [diff] [blame] | 1577 | // static |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1578 | std::vector<FilePath> DriveCache::GetCachePaths( |
[email protected] | 30d9dda | 2012-06-30 05:56:28 | [diff] [blame] | 1579 | const FilePath& cache_root_path) { |
| 1580 | std::vector<FilePath> cache_paths; |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1581 | // The order should match DriveCache::CacheSubDirectoryType enum. |
| 1582 | cache_paths.push_back(cache_root_path.Append(kDriveCacheMetaDir)); |
| 1583 | cache_paths.push_back(cache_root_path.Append(kDriveCachePinnedDir)); |
| 1584 | cache_paths.push_back(cache_root_path.Append(kDriveCacheOutgoingDir)); |
| 1585 | cache_paths.push_back(cache_root_path.Append(kDriveCachePersistentDir)); |
| 1586 | cache_paths.push_back(cache_root_path.Append(kDriveCacheTmpDir)); |
| 1587 | cache_paths.push_back(cache_root_path.Append(kDriveCacheTmpDownloadsDir)); |
| 1588 | cache_paths.push_back(cache_root_path.Append(kDriveCacheTmpDocumentsDir)); |
[email protected] | 30d9dda | 2012-06-30 05:56:28 | [diff] [blame] | 1589 | return cache_paths; |
| 1590 | } |
| 1591 | |
| 1592 | // static |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1593 | bool DriveCache::CreateCacheDirectories( |
[email protected] | 30d9dda | 2012-06-30 05:56:28 | [diff] [blame] | 1594 | const std::vector<FilePath>& paths_to_create) { |
| 1595 | bool success = true; |
| 1596 | |
| 1597 | for (size_t i = 0; i < paths_to_create.size(); ++i) { |
| 1598 | if (file_util::DirectoryExists(paths_to_create[i])) |
| 1599 | continue; |
| 1600 | |
| 1601 | if (!file_util::CreateDirectory(paths_to_create[i])) { |
| 1602 | // Error creating this directory, record error and proceed with next one. |
| 1603 | success = false; |
| 1604 | PLOG(ERROR) << "Error creating directory " << paths_to_create[i].value(); |
| 1605 | } else { |
| 1606 | DVLOG(1) << "Created directory " << paths_to_create[i].value(); |
| 1607 | } |
| 1608 | } |
| 1609 | return success; |
| 1610 | } |
| 1611 | |
[email protected] | fae353a | 2012-07-11 23:30:27 | [diff] [blame] | 1612 | // static |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1613 | DriveCache::CacheSubDirectoryType DriveCache::GetSubDirectoryType( |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 1614 | const DriveCacheEntry& cache_entry) { |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1615 | return cache_entry.is_persistent() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; |
[email protected] | fae353a | 2012-07-11 23:30:27 | [diff] [blame] | 1616 | } |
| 1617 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 1618 | void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) { |
| 1619 | delete global_free_disk_getter_for_testing; // Safe to delete NULL; |
| 1620 | global_free_disk_getter_for_testing = getter; |
| 1621 | } |
| 1622 | |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 1623 | } // namespace gdata |