Chromium Code Reviews| Index: chrome/browser/chromeos/gdata/gdata_file_system.cc |
| diff --git a/chrome/browser/chromeos/gdata/gdata_file_system.cc b/chrome/browser/chromeos/gdata/gdata_file_system.cc |
| index 95d8d15263c3e6253165bf3c07dfb90d1b857f8a..87660f61b552df0e6cc2a822bf6cc22811cac43c 100644 |
| --- a/chrome/browser/chromeos/gdata/gdata_file_system.cc |
| +++ b/chrome/browser/chromeos/gdata/gdata_file_system.cc |
| @@ -54,6 +54,7 @@ const FilePath::CharType kGDataRootDirectory[] = FILE_PATH_LITERAL("gdata"); |
| const char kFeedField[] = "feed"; |
| const char kWildCard[] = "*"; |
| const char kLocallyModifiedFileExtension[] = "local"; |
| +const char kMountedArchiveFileExtension[] = "mounted"; |
| const FilePath::CharType kGDataCacheVersionDir[] = FILE_PATH_LITERAL("v1"); |
| const FilePath::CharType kGDataCacheMetaDir[] = FILE_PATH_LITERAL("meta"); |
| @@ -444,6 +445,18 @@ void RunGetFromCacheCallbackHelper( |
| callback.Run(*error, resource_id, md5, gdata_file_path, *cache_file_path); |
| } |
| +// Ditto for SetMountedStateCallback |
| +void RunSetMountedStateCallbackHelper( |
| + const SetMountedStateCallback& callback, |
| + base::PlatformFileError* error, |
| + FilePath* cache_file_path) { |
| + DCHECK(error); |
| + DCHECK(cache_file_path); |
| + |
| + if (!callback.is_null()) |
| + callback.Run(*error, *cache_file_path); |
| +} |
| + |
| void RunGetCacheStateCallbackHelper( |
| const GetCacheStateCallback& callback, |
| base::PlatformFileError* error, |
| @@ -1865,6 +1878,10 @@ bool GDataFileSystem::GetFileInfoFromPath( |
| return true; |
| } |
| +bool GDataFileSystem::IsUnderGDataCacheDirectory(const FilePath& path) const { |
| + return gdata_cache_path_ == path || gdata_cache_path_.IsParent(path); |
| +} |
| + |
| FilePath GDataFileSystem::GetGDataCacheTmpDirectory() const { |
| return cache_paths_[GDataRootDirectory::CACHE_TYPE_TMP]; |
| } |
| @@ -2013,6 +2030,96 @@ void GDataFileSystem::SetPinState(const FilePath& file_path, bool to_pin, |
| Unpin(resource_id, md5, cache_callback); |
| } |
| +void GDataFileSystem::SetMountedState(const FilePath& file_path, bool to_mount, |
| + const SetMountedStateCallback& callback) { |
| + InitializeCacheIfNecessary(); |
| + |
| + base::PlatformFileError* error = |
| + new base::PlatformFileError(base::PLATFORM_FILE_OK); |
| + FilePath* cache_file_path = new FilePath; |
| + PostBlockingPoolSequencedTaskAndReply( |
| + kGDataFileSystemToken, |
| + FROM_HERE, |
| + base::Bind(&GDataFileSystem::SetMountedStateOnIOThreadPool, |
|
satorux1
2012/04/18 23:29:04
It's sad to see a new non-static member function r
|
| + base::Unretained(this), |
| + file_path, |
| + to_mount, |
| + error, |
| + cache_file_path), |
| + base::Bind(&RunSetMountedStateCallbackHelper, |
| + callback, |
| + base::Owned(error), |
| + base::Owned(cache_file_path))); |
| +} |
| + |
| +void GDataFileSystem::SetMountedStateOnIOThreadPool( |
| + const FilePath& file_path, |
| + bool to_mount, |
| + base::PlatformFileError *error, |
| + FilePath* cache_file_path) { |
| + DCHECK(error); |
| + DCHECK(cache_file_path); |
| + |
| + // Lock to access cache map. |
| + base::AutoLock lock(lock_); |
| + |
| + FilePath base_name = file_path.BaseName(); |
| + FilePath::StringType resource_id = base_name.RemoveExtension().value(); |
| + FilePath::StringType extension = base_name.Extension(); |
| + FilePath::StringType md5 = FilePath::StringType(); |
| + if (to_mount && !extension.empty()) { |
| + // FilePath::Extension returns ".", so strip it. |
| + md5 = GDataFileBase::UnescapeUtf8FileName(extension.substr(1)); |
| + } |
| + GDataRootDirectory::CacheEntry* entry = root_->GetCacheEntry(resource_id, |
| + md5); |
| + if (!entry) { |
| + *error = base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| + return; |
| + } |
| + if ((to_mount && entry->IsMounted()) || |
|
tbarzic
2012/04/18 23:43:14
if (to_mount == entry->IsMounted())
|
| + (!to_mount && !entry->IsMounted())) { |
| + *error = base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
| + return; |
| + } |
|
satorux1
2012/04/18 23:29:04
nit: maybe add a blank line here? The code here lo
|
| + md5 = entry->md5; |
| + // Get subdir types for the unmounted and mounted state. |
| + GDataRootDirectory::CacheSubDirectoryType unmounted_subdir = |
| + entry->IsPinned() ? GDataRootDirectory::CACHE_TYPE_PERSISTENT : |
| + GDataRootDirectory::CACHE_TYPE_TMP; |
| + GDataRootDirectory::CacheSubDirectoryType mounted_subdir = |
| + GDataRootDirectory::CACHE_TYPE_PERSISTENT; |
| + // Gets path of the file if it were to be unmounted. |
| + FilePath unmounted_path = GetCacheFilePath(resource_id, md5, unmounted_subdir, |
| + CACHED_FILE_FROM_SERVER); |
| + // Gets path of the file if it were to be mounted. |
| + FilePath mounted_path = GetCacheFilePath(resource_id, md5, mounted_subdir, |
| + CACHED_FILE_MOUNTED); |
|
satorux1
2012/04/18 23:29:04
add a blank line?
|
| + // Determine the source and destination paths for moving the cache blob. |
| + FilePath source_path; |
| + GDataRootDirectory::CacheSubDirectoryType dest_subdir; |
| + int cache_state = entry->cache_state; |
| + if (to_mount) { |
| + source_path = unmounted_path; |
|
tbarzic
2012/04/18 23:43:14
I would find
source_path = to_mount ? unmounted_p
|
| + *cache_file_path = mounted_path; |
| + dest_subdir = mounted_subdir; |
| + cache_state = GDataFile::SetCacheMounted(cache_state); |
| + } else { |
| + source_path = mounted_path; |
| + *cache_file_path = unmounted_path; |
| + dest_subdir = unmounted_subdir; |
| + cache_state = GDataFile::ClearCacheMounted(cache_state); |
| + } |
|
satorux1
2012/04/18 23:29:04
blank line?
|
| + // Move cache blob from source path to destination path. |
| + *error = ModifyCacheState(source_path, *cache_file_path, |
| + GDataFileSystem::FILE_OPERATION_MOVE, FilePath(), |
| + false); |
| + if (*error == base::PLATFORM_FILE_OK) { |
| + // Now that cache operation is complete, update cache map |
| + root_->UpdateCacheMap(resource_id, md5, dest_subdir, cache_state); |
| + } |
| +} |
| + |
| void GDataFileSystem::OnSetPinStateCompleted( |
| const FileOperationCallback& callback, |
| base::PlatformFileError error, |
| @@ -3184,6 +3291,10 @@ FilePath GDataFileSystem::GetCacheFilePath( |
| DCHECK(sub_dir_type == GDataRootDirectory::CACHE_TYPE_PERSISTENT); |
| base_name += FilePath::kExtensionSeparator; |
| base_name += kLocallyModifiedFileExtension; |
| + } else if (file_origin == CACHED_FILE_MOUNTED) { |
| + DCHECK(sub_dir_type == GDataRootDirectory::CACHE_TYPE_PERSISTENT); |
| + base_name += FilePath::kExtensionSeparator; |
| + base_name += kMountedArchiveFileExtension; |
| } else if (!md5.empty()) { |
| base_name += FilePath::kExtensionSeparator; |
| base_name += GDataFileBase::EscapeUtf8FileName(md5); |
| @@ -3426,12 +3537,19 @@ void GDataFileSystem::GetFromCacheOnIOThreadPool( |
| GDataRootDirectory::CacheEntry* entry = root_->GetCacheEntry(resource_id, |
| md5); |
| if (entry && entry->IsPresent()) { |
| + CachedFileOrigin file_origin; |
| + if (entry->IsMounted()) { |
| + file_origin = CACHED_FILE_MOUNTED; |
| + } else if (entry->IsDirty()) { |
| + file_origin = CACHED_FILE_LOCALLY_MODIFIED; |
| + } else { |
| + file_origin = CACHED_FILE_FROM_SERVER; |
| + } |
| *cache_file_path = GetCacheFilePath( |
| resource_id, |
| md5, |
| entry->sub_dir_type, |
| - entry->IsDirty() ? CACHED_FILE_LOCALLY_MODIFIED : |
| - CACHED_FILE_FROM_SERVER); |
| + file_origin); |
| *error = base::PLATFORM_FILE_OK; |
| } else { |
| *error = base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| @@ -3488,10 +3606,11 @@ void GDataFileSystem::StoreToCacheOnIOThreadPool( |
| // If file was previously pinned, store it in persistent dir and create |
| // symlink in pinned dir. |
| if (entry) { // File exists in cache. |
| - // If file is dirty, return error. |
| - if (entry->IsDirty()) { |
| - LOG(WARNING) << "Can't store a file to replace a dirty file: res_id=" |
| - << resource_id |
| + // If file is dirty or mounted, return error. |
| + if (entry->IsDirty() || entry->IsMounted()) { |
| + LOG(WARNING) << "Can't store a file to replace a " |
| + << (entry->IsDirty() ? "dirty" : "mounted") |
| + << " file: res_id=" << resource_id |
| << ", md5=" << md5; |
| *error = base::PLATFORM_FILE_ERROR_IN_USE; |
| return; |
| @@ -3590,10 +3709,10 @@ void GDataFileSystem::PinOnIOThreadPool(const std::string& resource_id, |
| // Determine source and destination paths. |
| - // If file is dirty, don't move it, so determine |dest_path| and set |
| - // |source_path| the same, because ModifyCacheState only moves files if |
| + // If file is dirty or mounted, don't move it, so determine |dest_path| and |
| + // set |source_path| the same, because ModifyCacheState only moves files if |
| // source and destination are different. |
| - if (entry->IsDirty()) { |
| + if (entry->IsDirty() || entry->IsMounted()) { |
| DCHECK_EQ(GDataRootDirectory::CACHE_TYPE_PERSISTENT, entry->sub_dir_type); |
| dest_path = GetCacheFilePath(resource_id, |
| md5, |
| @@ -3673,10 +3792,10 @@ void GDataFileSystem::UnpinOnIOThreadPool(const std::string& resource_id, |
| GDataRootDirectory::CacheSubDirectoryType sub_dir_type = |
| GDataRootDirectory::CACHE_TYPE_TMP; |
| - // If file is dirty, don't move it, so determine |dest_path| and set |
| - // |source_path| the same, because ModifyCacheState moves files if source |
| + // If file is dirty or mounted, don't move it, so determine |dest_path| and |
| + // set |source_path| the same, because ModifyCacheState moves files if source |
| // and destination are different. |
| - if (entry->IsDirty()) { |
| + if (entry->IsDirty() || entry->IsMounted()) { |
| sub_dir_type = GDataRootDirectory::CACHE_TYPE_PERSISTENT; |
| DCHECK_EQ(sub_dir_type, entry->sub_dir_type); |
| dest_path = GetCacheFilePath(resource_id, |
| @@ -4008,9 +4127,11 @@ void GDataFileSystem::RemoveFromCacheOnIOThreadPool( |
| GDataRootDirectory::CacheEntry* entry = root_->GetCacheEntry( |
| resource_id, std::string()); |
| - // If entry doesn't exist or is dirty in cache, nothing to do. |
| - if (!entry || entry->IsDirty()) { |
| - DVLOG(1) << "Entry " << (entry ? "is dirty" : "doesn't exist") |
| + // If entry doesn't exist or is dirty or mounted in cache, nothing to do. |
| + if (!entry || entry->IsDirty() || entry->IsMounted()) { |
| + DVLOG(1) << "Entry is " |
| + << (entry ? (entry->IsDirty() ? "dirty" : "mounted") : |
| + "non-existent") |
| << " in cache, not removing"; |
| *error = base::PLATFORM_FILE_OK; |
| return; |