[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 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] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 5 | #include "webkit/fileapi/obfuscated_file_util.h" |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 6 | |
| 7 | #include <queue> |
[email protected] | 172205b | 2011-08-30 03:36:44 | [diff] [blame] | 8 | #include <string> |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 9 | #include <vector> |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 10 | |
| 11 | #include "base/file_util.h" |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 12 | #include "base/format_macros.h" |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 13 | #include "base/logging.h" |
[email protected] | 0a732853 | 2011-05-13 23:54:43 | [diff] [blame] | 14 | #include "base/message_loop.h" |
[email protected] | 58b7c5a6 | 2011-08-15 13:09:27 | [diff] [blame] | 15 | #include "base/stl_util.h" |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 16 | #include "base/stringprintf.h" |
[email protected] | 4ce532f | 2013-03-27 22:03:40 | [diff] [blame] | 17 | #include "base/strings/string_number_conversions.h" |
[email protected] | 13ac5353 | 2013-03-30 00:27:00 | [diff] [blame] | 18 | #include "base/strings/sys_string_conversions.h" |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 19 | #include "googleurl/src/gurl.h" |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 20 | #include "webkit/fileapi/file_observers.h" |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 21 | #include "webkit/fileapi/file_system_context.h" |
| 22 | #include "webkit/fileapi/file_system_operation_context.h" |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 23 | #include "webkit/fileapi/file_system_url.h" |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 24 | #include "webkit/fileapi/file_system_util.h" |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 25 | #include "webkit/fileapi/native_file_util.h" |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 26 | #include "webkit/fileapi/sandbox_mount_point_provider.h" |
[email protected] | b6c415b | 2013-04-16 06:41:47 | [diff] [blame] | 27 | #include "webkit/fileapi/syncable/syncable_file_system_util.h" |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 28 | #include "webkit/quota/quota_manager.h" |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 29 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 30 | // Example of various paths: |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 31 | // void ObfuscatedFileUtil::DoSomething(const FileSystemURL& url) { |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 32 | // base::FilePath virtual_path = url.path(); |
| 33 | // base::FilePath local_path = GetLocalFilePath(url); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 34 | // |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 35 | // NativeFileUtil::DoSomething(local_path); |
| 36 | // file_util::DoAnother(local_path); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 37 | // } |
| 38 | |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 39 | namespace fileapi { |
| 40 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 41 | namespace { |
| 42 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 43 | typedef FileSystemDirectoryDatabase::FileId FileId; |
| 44 | typedef FileSystemDirectoryDatabase::FileInfo FileInfo; |
| 45 | |
[email protected] | 0a732853 | 2011-05-13 23:54:43 | [diff] [blame] | 46 | const int64 kFlushDelaySeconds = 10 * 60; // 10 minutes |
| 47 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 48 | void InitFileInfo( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 49 | FileSystemDirectoryDatabase::FileInfo* file_info, |
| 50 | FileSystemDirectoryDatabase::FileId parent_id, |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 51 | const base::FilePath::StringType& file_name) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 52 | DCHECK(file_info); |
| 53 | file_info->parent_id = parent_id; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 54 | file_info->name = file_name; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 57 | // Costs computed as per crbug.com/86114, based on the LevelDB implementation of |
| 58 | // path storage under Linux. It's not clear if that will differ on Windows, on |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 59 | // which base::FilePath uses wide chars [since they're converted to UTF-8 for storage |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 60 | // anyway], but as long as the cost is high enough that one can't cheat on quota |
| 61 | // by storing data in paths, it doesn't need to be all that accurate. |
[email protected] | 172205b | 2011-08-30 03:36:44 | [diff] [blame] | 62 | const int64 kPathCreationQuotaCost = 146; // Bytes per inode, basically. |
| 63 | const int64 kPathByteQuotaCost = 2; // Bytes per byte of path length in UTF-8. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 64 | |
[email protected] | f49dfc88 | 2012-04-05 15:02:06 | [diff] [blame] | 65 | int64 UsageForPath(size_t length) { |
| 66 | return kPathCreationQuotaCost + |
| 67 | static_cast<int64>(length) * kPathByteQuotaCost; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 68 | } |
| 69 | |
[email protected] | f49dfc88 | 2012-04-05 15:02:06 | [diff] [blame] | 70 | bool AllocateQuota(FileSystemOperationContext* context, int64 growth) { |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 71 | if (context->allowed_bytes_growth() == quota::QuotaManager::kNoLimit) |
| 72 | return true; |
| 73 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 74 | int64 new_quota = context->allowed_bytes_growth() - growth; |
[email protected] | f49dfc88 | 2012-04-05 15:02:06 | [diff] [blame] | 75 | if (growth > 0 && new_quota < 0) |
| 76 | return false; |
| 77 | context->set_allowed_bytes_growth(new_quota); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 78 | return true; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | f49dfc88 | 2012-04-05 15:02:06 | [diff] [blame] | 81 | void UpdateUsage( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 82 | FileSystemOperationContext* context, |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 83 | const FileSystemURL& url, |
[email protected] | f49dfc88 | 2012-04-05 15:02:06 | [diff] [blame] | 84 | int64 growth) { |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 85 | context->update_observers()->Notify( |
| 86 | &FileUpdateObserver::OnUpdate, MakeTuple(url, growth)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 87 | } |
| 88 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 89 | void TouchDirectory(FileSystemDirectoryDatabase* db, FileId dir_id) { |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 90 | DCHECK(db); |
| 91 | if (!db->UpdateModificationTime(dir_id, base::Time::Now())) |
| 92 | NOTREACHED(); |
| 93 | } |
| 94 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 95 | const base::FilePath::CharType kTemporaryDirectoryName[] = FILE_PATH_LITERAL("t"); |
| 96 | const base::FilePath::CharType kPersistentDirectoryName[] = FILE_PATH_LITERAL("p"); |
| 97 | const base::FilePath::CharType kSyncableDirectoryName[] = FILE_PATH_LITERAL("s"); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 98 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 99 | } // namespace |
| 100 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 101 | using base::PlatformFile; |
| 102 | using base::PlatformFileError; |
| 103 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 104 | class ObfuscatedFileEnumerator |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 105 | : public FileSystemFileUtil::AbstractFileEnumerator { |
| 106 | public: |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 107 | ObfuscatedFileEnumerator( |
[email protected] | 172205b | 2011-08-30 03:36:44 | [diff] [blame] | 108 | FileSystemDirectoryDatabase* db, |
| 109 | FileSystemOperationContext* context, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 110 | ObfuscatedFileUtil* obfuscated_file_util, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 111 | const FileSystemURL& root_url, |
[email protected] | 566f22379 | 2012-03-07 03:24:28 | [diff] [blame] | 112 | bool recursive) |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 113 | : db_(db), |
[email protected] | 172205b | 2011-08-30 03:36:44 | [diff] [blame] | 114 | context_(context), |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 115 | obfuscated_file_util_(obfuscated_file_util), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 116 | origin_(root_url.origin()), |
| 117 | type_(root_url.type()), |
[email protected] | f1a9c52 | 2012-07-25 22:08:27 | [diff] [blame] | 118 | recursive_(recursive), |
| 119 | current_file_id_(0) { |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 120 | base::FilePath root_virtual_path = root_url.path(); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 121 | FileId file_id; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 122 | |
| 123 | if (!db_->GetFileWithPath(root_virtual_path, &file_id)) |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 124 | return; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 125 | |
| 126 | FileRecord record = { file_id, root_virtual_path }; |
[email protected] | 566f22379 | 2012-03-07 03:24:28 | [diff] [blame] | 127 | recurse_queue_.push(record); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 130 | virtual ~ObfuscatedFileEnumerator() {} |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 131 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 132 | virtual base::FilePath Next() OVERRIDE { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 133 | ProcessRecurseQueue(); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 134 | if (display_stack_.empty()) |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 135 | return base::FilePath(); |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 136 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 137 | current_file_id_ = display_stack_.back(); |
| 138 | display_stack_.pop_back(); |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 139 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 140 | FileInfo file_info; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 141 | base::FilePath platform_file_path; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 142 | base::PlatformFileError error = |
| 143 | obfuscated_file_util_->GetFileInfoInternal( |
| 144 | db_, context_, origin_, type_, current_file_id_, |
| 145 | &file_info, ¤t_platform_file_info_, &platform_file_path); |
| 146 | if (error != base::PLATFORM_FILE_OK) |
| 147 | return Next(); |
| 148 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 149 | base::FilePath virtual_path = |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 150 | current_parent_virtual_path_.Append(file_info.name); |
| 151 | if (recursive_ && file_info.is_directory()) { |
| 152 | FileRecord record = { current_file_id_, virtual_path }; |
| 153 | recurse_queue_.push(record); |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 154 | } |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 155 | return virtual_path; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 172205b | 2011-08-30 03:36:44 | [diff] [blame] | 158 | virtual int64 Size() OVERRIDE { |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 159 | return current_platform_file_info_.size; |
[email protected] | 172205b | 2011-08-30 03:36:44 | [diff] [blame] | 160 | } |
| 161 | |
[email protected] | 2c514f3 | 2012-03-14 05:58:13 | [diff] [blame] | 162 | virtual base::Time LastModifiedTime() OVERRIDE { |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 163 | return current_platform_file_info_.last_modified; |
[email protected] | 2c514f3 | 2012-03-14 05:58:13 | [diff] [blame] | 164 | } |
| 165 | |
[email protected] | 172205b | 2011-08-30 03:36:44 | [diff] [blame] | 166 | virtual bool IsDirectory() OVERRIDE { |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 167 | return current_platform_file_info_.is_directory; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | private: |
| 171 | typedef FileSystemDirectoryDatabase::FileId FileId; |
| 172 | typedef FileSystemDirectoryDatabase::FileInfo FileInfo; |
| 173 | |
| 174 | struct FileRecord { |
| 175 | FileId file_id; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 176 | base::FilePath virtual_path; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | void ProcessRecurseQueue() { |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 180 | while (display_stack_.empty() && !recurse_queue_.empty()) { |
| 181 | FileRecord entry = recurse_queue_.front(); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 182 | recurse_queue_.pop(); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 183 | if (!db_->ListChildren(entry.file_id, &display_stack_)) { |
| 184 | display_stack_.clear(); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 185 | return; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 186 | } |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 187 | current_parent_virtual_path_ = entry.virtual_path; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 191 | FileSystemDirectoryDatabase* db_; |
[email protected] | 172205b | 2011-08-30 03:36:44 | [diff] [blame] | 192 | FileSystemOperationContext* context_; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 193 | ObfuscatedFileUtil* obfuscated_file_util_; |
| 194 | GURL origin_; |
| 195 | FileSystemType type_; |
[email protected] | 566f22379 | 2012-03-07 03:24:28 | [diff] [blame] | 196 | bool recursive_; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 197 | |
| 198 | std::queue<FileRecord> recurse_queue_; |
| 199 | std::vector<FileId> display_stack_; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 200 | base::FilePath current_parent_virtual_path_; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 201 | |
| 202 | FileId current_file_id_; |
| 203 | base::PlatformFileInfo current_platform_file_info_; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 204 | }; |
| 205 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 206 | class ObfuscatedOriginEnumerator |
| 207 | : public ObfuscatedFileUtil::AbstractOriginEnumerator { |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 208 | public: |
| 209 | typedef FileSystemOriginDatabase::OriginRecord OriginRecord; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 210 | ObfuscatedOriginEnumerator( |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 211 | FileSystemOriginDatabase* origin_database, |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 212 | const base::FilePath& base_file_path) |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 213 | : base_file_path_(base_file_path) { |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 214 | if (origin_database) |
| 215 | origin_database->ListAllOrigins(&origins_); |
| 216 | } |
| 217 | |
[email protected] | 7fd8fa4f | 2013-02-07 05:43:50 | [diff] [blame] | 218 | virtual ~ObfuscatedOriginEnumerator() {} |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 219 | |
| 220 | // Returns the next origin. Returns empty if there are no more origins. |
| 221 | virtual GURL Next() OVERRIDE { |
| 222 | OriginRecord record; |
| 223 | if (!origins_.empty()) { |
| 224 | record = origins_.back(); |
| 225 | origins_.pop_back(); |
| 226 | } |
| 227 | current_ = record; |
| 228 | return GetOriginURLFromIdentifier(record.origin); |
| 229 | } |
| 230 | |
| 231 | // Returns the current origin's information. |
| 232 | virtual bool HasFileSystemType(FileSystemType type) const OVERRIDE { |
| 233 | if (current_.path.empty()) |
| 234 | return false; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 235 | base::FilePath::StringType type_string = |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 236 | ObfuscatedFileUtil::GetDirectoryNameForType(type); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 237 | if (type_string.empty()) { |
| 238 | NOTREACHED(); |
| 239 | return false; |
| 240 | } |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 241 | base::FilePath path = base_file_path_.Append(current_.path).Append(type_string); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 242 | return file_util::DirectoryExists(path); |
| 243 | } |
| 244 | |
| 245 | private: |
| 246 | std::vector<OriginRecord> origins_; |
| 247 | OriginRecord current_; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 248 | base::FilePath base_file_path_; |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 249 | }; |
| 250 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 251 | ObfuscatedFileUtil::ObfuscatedFileUtil( |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 252 | const base::FilePath& file_system_directory) |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 253 | : file_system_directory_(file_system_directory) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 254 | } |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 255 | |
[email protected] | 3cfc10f | 2012-05-24 01:20:41 | [diff] [blame] | 256 | ObfuscatedFileUtil::~ObfuscatedFileUtil() { |
| 257 | DropDatabases(); |
| 258 | } |
| 259 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 260 | PlatformFileError ObfuscatedFileUtil::CreateOrOpen( |
| 261 | FileSystemOperationContext* context, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 262 | const FileSystemURL& url, int file_flags, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 263 | PlatformFile* file_handle, bool* created) { |
[email protected] | f7ac94e | 2013-04-30 08:34:50 | [diff] [blame^] | 264 | PlatformFileError error = CreateOrOpenInternal(context, url, file_flags, |
| 265 | file_handle, created); |
| 266 | if (*file_handle != base::kInvalidPlatformFileValue && |
| 267 | file_flags & base::PLATFORM_FILE_WRITE && |
| 268 | context->quota_limit_type() == quota::kQuotaLimitTypeUnlimited) { |
| 269 | DCHECK_EQ(base::PLATFORM_FILE_OK, error); |
| 270 | DCHECK_EQ(kFileSystemTypePersistent, url.type()); |
| 271 | context->file_system_context()->GetQuotaUtil(url.type())-> |
| 272 | StickyInvalidateUsageCache(url.origin(), url.type()); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame] | 273 | } |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 274 | return error; |
| 275 | } |
| 276 | |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 277 | PlatformFileError ObfuscatedFileUtil::Close( |
| 278 | FileSystemOperationContext* context, |
| 279 | base::PlatformFile file) { |
| 280 | return NativeFileUtil::Close(file); |
| 281 | } |
| 282 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 283 | PlatformFileError ObfuscatedFileUtil::EnsureFileExists( |
| 284 | FileSystemOperationContext* context, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 285 | const FileSystemURL& url, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 286 | bool* created) { |
| 287 | FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 288 | url.origin(), url.type(), true); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 289 | if (!db) |
| 290 | return base::PLATFORM_FILE_ERROR_FAILED; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 291 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 292 | FileId file_id; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 293 | if (db->GetFileWithPath(url.path(), &file_id)) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 294 | FileInfo file_info; |
| 295 | if (!db->GetFileInfo(file_id, &file_info)) { |
| 296 | NOTREACHED(); |
| 297 | return base::PLATFORM_FILE_ERROR_FAILED; |
| 298 | } |
| 299 | if (file_info.is_directory()) |
| 300 | return base::PLATFORM_FILE_ERROR_NOT_A_FILE; |
| 301 | if (created) |
| 302 | *created = false; |
| 303 | return base::PLATFORM_FILE_OK; |
| 304 | } |
| 305 | FileId parent_id; |
[email protected] | 8a020f6 | 2013-02-18 08:05:44 | [diff] [blame] | 306 | if (!db->GetFileWithPath(VirtualPath::DirName(url.path()), &parent_id)) |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 307 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 308 | |
| 309 | FileInfo file_info; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 310 | InitFileInfo(&file_info, parent_id, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 311 | VirtualPath::BaseName(url.path()).value()); |
[email protected] | f49dfc88 | 2012-04-05 15:02:06 | [diff] [blame] | 312 | |
| 313 | int64 growth = UsageForPath(file_info.name.size()); |
| 314 | if (!AllocateQuota(context, growth)) |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 315 | return base::PLATFORM_FILE_ERROR_NO_SPACE; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 316 | PlatformFileError error = CreateFile( |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 317 | context, base::FilePath(), url.origin(), url.type(), &file_info, 0, NULL); |
[email protected] | f49dfc88 | 2012-04-05 15:02:06 | [diff] [blame] | 318 | if (created && base::PLATFORM_FILE_OK == error) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 319 | *created = true; |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 320 | UpdateUsage(context, url, growth); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame] | 321 | context->change_observers()->Notify( |
| 322 | &FileChangeObserver::OnCreateFile, MakeTuple(url)); |
[email protected] | f49dfc88 | 2012-04-05 15:02:06 | [diff] [blame] | 323 | } |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 324 | return error; |
| 325 | } |
| 326 | |
| 327 | PlatformFileError ObfuscatedFileUtil::CreateDirectory( |
| 328 | FileSystemOperationContext* context, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 329 | const FileSystemURL& url, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 330 | bool exclusive, |
| 331 | bool recursive) { |
| 332 | FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 333 | url.origin(), url.type(), true); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 334 | if (!db) |
| 335 | return base::PLATFORM_FILE_ERROR_FAILED; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 336 | |
[email protected] | 8c5b369 | 2012-11-20 09:56:49 | [diff] [blame] | 337 | // TODO(kinuko): Remove this dirty hack when we fully support directory |
| 338 | // operations or clean up the code if we decided not to support directory |
| 339 | // operations. (http://crbug.com/161442) |
| 340 | if (url.type() == kFileSystemTypeSyncable && |
[email protected] | b6c415b | 2013-04-16 06:41:47 | [diff] [blame] | 341 | !sync_file_system::IsSyncDirectoryOperationEnabled()) { |
[email protected] | 8c5b369 | 2012-11-20 09:56:49 | [diff] [blame] | 342 | return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
| 343 | } |
| 344 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 345 | FileId file_id; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 346 | if (db->GetFileWithPath(url.path(), &file_id)) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 347 | FileInfo file_info; |
| 348 | if (exclusive) |
| 349 | return base::PLATFORM_FILE_ERROR_EXISTS; |
| 350 | if (!db->GetFileInfo(file_id, &file_info)) { |
| 351 | NOTREACHED(); |
| 352 | return base::PLATFORM_FILE_ERROR_FAILED; |
| 353 | } |
| 354 | if (!file_info.is_directory()) |
| 355 | return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; |
| 356 | return base::PLATFORM_FILE_OK; |
| 357 | } |
| 358 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 359 | std::vector<base::FilePath::StringType> components; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 360 | VirtualPath::GetComponents(url.path(), &components); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 361 | FileId parent_id = 0; |
| 362 | size_t index; |
| 363 | for (index = 0; index < components.size(); ++index) { |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 364 | base::FilePath::StringType name = components[index]; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 365 | if (name == FILE_PATH_LITERAL("/")) |
| 366 | continue; |
| 367 | if (!db->GetChildWithName(parent_id, name, &parent_id)) |
| 368 | break; |
| 369 | } |
| 370 | if (!recursive && components.size() - index > 1) |
| 371 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 372 | bool first = true; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 373 | for (; index < components.size(); ++index) { |
| 374 | FileInfo file_info; |
| 375 | file_info.name = components[index]; |
| 376 | if (file_info.name == FILE_PATH_LITERAL("/")) |
| 377 | continue; |
| 378 | file_info.modification_time = base::Time::Now(); |
| 379 | file_info.parent_id = parent_id; |
[email protected] | f49dfc88 | 2012-04-05 15:02:06 | [diff] [blame] | 380 | int64 growth = UsageForPath(file_info.name.size()); |
| 381 | if (!AllocateQuota(context, growth)) |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 382 | return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 383 | if (!db->AddFileInfo(file_info, &parent_id)) { |
| 384 | NOTREACHED(); |
| 385 | return base::PLATFORM_FILE_ERROR_FAILED; |
| 386 | } |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 387 | UpdateUsage(context, url, growth); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame] | 388 | context->change_observers()->Notify( |
| 389 | &FileChangeObserver::OnCreateDirectory, MakeTuple(url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 390 | if (first) { |
| 391 | first = false; |
| 392 | TouchDirectory(db, file_info.parent_id); |
| 393 | } |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 394 | } |
| 395 | return base::PLATFORM_FILE_OK; |
| 396 | } |
| 397 | |
| 398 | PlatformFileError ObfuscatedFileUtil::GetFileInfo( |
| 399 | FileSystemOperationContext* context, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 400 | const FileSystemURL& url, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 401 | base::PlatformFileInfo* file_info, |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 402 | base::FilePath* platform_file_path) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 403 | FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 404 | url.origin(), url.type(), false); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 405 | if (!db) |
| 406 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 407 | FileId file_id; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 408 | if (!db->GetFileWithPath(url.path(), &file_id)) |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 409 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 410 | FileInfo local_info; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 411 | return GetFileInfoInternal(db, context, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 412 | url.origin(), url.type(), |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 413 | file_id, &local_info, |
| 414 | file_info, platform_file_path); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 415 | } |
| 416 | |
[email protected] | d109fcb | 2012-11-07 19:44:33 | [diff] [blame] | 417 | scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> |
| 418 | ObfuscatedFileUtil::CreateFileEnumerator( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 419 | FileSystemOperationContext* context, |
[email protected] | 5453ca05 | 2013-04-11 21:15:39 | [diff] [blame] | 420 | const FileSystemURL& root_url) { |
| 421 | return CreateFileEnumerator(context, root_url, false /* recursive */); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 422 | } |
| 423 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 424 | PlatformFileError ObfuscatedFileUtil::GetLocalFilePath( |
[email protected] | f1ddaa4 | 2011-07-19 05:03:03 | [diff] [blame] | 425 | FileSystemOperationContext* context, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 426 | const FileSystemURL& url, |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 427 | base::FilePath* local_path) { |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 428 | FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 429 | url.origin(), url.type(), false); |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 430 | if (!db) |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 431 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 432 | FileId file_id; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 433 | if (!db->GetFileWithPath(url.path(), &file_id)) |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 434 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 435 | FileInfo file_info; |
| 436 | if (!db->GetFileInfo(file_id, &file_info) || file_info.is_directory()) { |
| 437 | NOTREACHED(); |
| 438 | // Directories have no local file path. |
| 439 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 440 | } |
| 441 | *local_path = DataPathToLocalPath( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 442 | url.origin(), url.type(), file_info.data_path); |
[email protected] | f1ddaa4 | 2011-07-19 05:03:03 | [diff] [blame] | 443 | |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 444 | if (local_path->empty()) |
| 445 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 446 | return base::PLATFORM_FILE_OK; |
| 447 | } |
| 448 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 449 | PlatformFileError ObfuscatedFileUtil::Touch( |
| 450 | FileSystemOperationContext* context, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 451 | const FileSystemURL& url, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 452 | const base::Time& last_access_time, |
| 453 | const base::Time& last_modified_time) { |
[email protected] | 4b4d53bd | 2011-07-08 07:26:05 | [diff] [blame] | 454 | FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 455 | url.origin(), url.type(), false); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 456 | if (!db) |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 457 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 458 | FileId file_id; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 459 | if (!db->GetFileWithPath(url.path(), &file_id)) |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 460 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 461 | |
| 462 | FileInfo file_info; |
| 463 | if (!db->GetFileInfo(file_id, &file_info)) { |
| 464 | NOTREACHED(); |
| 465 | return base::PLATFORM_FILE_ERROR_FAILED; |
| 466 | } |
| 467 | if (file_info.is_directory()) { |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 468 | if (!db->UpdateModificationTime(file_id, last_modified_time)) |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 469 | return base::PLATFORM_FILE_ERROR_FAILED; |
| 470 | return base::PLATFORM_FILE_OK; |
| 471 | } |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 472 | base::FilePath local_path = DataPathToLocalPath( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 473 | url.origin(), url.type(), file_info.data_path); |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 474 | return NativeFileUtil::Touch( |
| 475 | local_path, last_access_time, last_modified_time); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | PlatformFileError ObfuscatedFileUtil::Truncate( |
| 479 | FileSystemOperationContext* context, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 480 | const FileSystemURL& url, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 481 | int64 length) { |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 482 | base::PlatformFileInfo file_info; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 483 | base::FilePath local_path; |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 484 | base::PlatformFileError error = |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 485 | GetFileInfo(context, url, &file_info, &local_path); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 486 | if (error != base::PLATFORM_FILE_OK) |
| 487 | return error; |
| 488 | |
| 489 | int64 growth = length - file_info.size; |
| 490 | if (!AllocateQuota(context, growth)) |
| 491 | return base::PLATFORM_FILE_ERROR_NO_SPACE; |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 492 | error = NativeFileUtil::Truncate(local_path, length); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame] | 493 | if (error == base::PLATFORM_FILE_OK) { |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 494 | UpdateUsage(context, url, growth); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame] | 495 | context->change_observers()->Notify( |
| 496 | &FileChangeObserver::OnModifyFile, MakeTuple(url)); |
| 497 | } |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 498 | return error; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 499 | } |
| 500 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 501 | PlatformFileError ObfuscatedFileUtil::CopyOrMoveFile( |
| 502 | FileSystemOperationContext* context, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 503 | const FileSystemURL& src_url, |
| 504 | const FileSystemURL& dest_url, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 505 | bool copy) { |
| 506 | // Cross-filesystem copies and moves should be handled via CopyInForeignFile. |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 507 | DCHECK(src_url.origin() == dest_url.origin()); |
| 508 | DCHECK(src_url.type() == dest_url.type()); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 509 | |
| 510 | FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 511 | src_url.origin(), src_url.type(), true); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 512 | if (!db) |
| 513 | return base::PLATFORM_FILE_ERROR_FAILED; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 514 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 515 | FileId src_file_id; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 516 | if (!db->GetFileWithPath(src_url.path(), &src_file_id)) |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 517 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 518 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 519 | FileId dest_file_id; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 520 | bool overwrite = db->GetFileWithPath(dest_url.path(), |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 521 | &dest_file_id); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 522 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 523 | FileInfo src_file_info; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 524 | base::PlatformFileInfo src_platform_file_info; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 525 | base::FilePath src_local_path; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 526 | base::PlatformFileError error = GetFileInfoInternal( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 527 | db, context, src_url.origin(), src_url.type(), src_file_id, |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 528 | &src_file_info, &src_platform_file_info, &src_local_path); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 529 | if (error != base::PLATFORM_FILE_OK) |
| 530 | return error; |
| 531 | if (src_file_info.is_directory()) |
[email protected] | bab213be | 2013-01-23 15:13:08 | [diff] [blame] | 532 | return base::PLATFORM_FILE_ERROR_NOT_A_FILE; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 533 | |
| 534 | FileInfo dest_file_info; |
| 535 | base::PlatformFileInfo dest_platform_file_info; // overwrite case only |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 536 | base::FilePath dest_local_path; // overwrite case only |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 537 | if (overwrite) { |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 538 | base::PlatformFileError error = GetFileInfoInternal( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 539 | db, context, dest_url.origin(), dest_url.type(), dest_file_id, |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 540 | &dest_file_info, &dest_platform_file_info, &dest_local_path); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 541 | if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) |
| 542 | overwrite = false; // fallback to non-overwrite case |
| 543 | else if (error != base::PLATFORM_FILE_OK) |
| 544 | return error; |
| 545 | else if (dest_file_info.is_directory()) |
[email protected] | bab213be | 2013-01-23 15:13:08 | [diff] [blame] | 546 | return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 547 | } |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 548 | if (!overwrite) { |
| 549 | FileId dest_parent_id; |
[email protected] | 8a020f6 | 2013-02-18 08:05:44 | [diff] [blame] | 550 | if (!db->GetFileWithPath(VirtualPath::DirName(dest_url.path()), |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 551 | &dest_parent_id)) { |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 552 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 553 | } |
| 554 | |
| 555 | dest_file_info = src_file_info; |
| 556 | dest_file_info.parent_id = dest_parent_id; |
| 557 | dest_file_info.name = |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 558 | VirtualPath::BaseName(dest_url.path()).value(); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | int64 growth = 0; |
| 562 | if (copy) |
| 563 | growth += src_platform_file_info.size; |
| 564 | else |
| 565 | growth -= UsageForPath(src_file_info.name.size()); |
| 566 | if (overwrite) |
| 567 | growth -= dest_platform_file_info.size; |
| 568 | else |
| 569 | growth += UsageForPath(dest_file_info.name.size()); |
| 570 | if (!AllocateQuota(context, growth)) |
| 571 | return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 572 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 573 | /* |
| 574 | * Copy-with-overwrite |
| 575 | * Just overwrite data file |
| 576 | * Copy-without-overwrite |
| 577 | * Copy backing file |
| 578 | * Create new metadata pointing to new backing file. |
| 579 | * Move-with-overwrite |
| 580 | * transaction: |
| 581 | * Remove source entry. |
| 582 | * Point target entry to source entry's backing file. |
| 583 | * Delete target entry's old backing file |
| 584 | * Move-without-overwrite |
| 585 | * Just update metadata |
| 586 | */ |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 587 | error = base::PLATFORM_FILE_ERROR_FAILED; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 588 | if (copy) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 589 | if (overwrite) { |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 590 | error = NativeFileUtil::CopyOrMoveFile( |
| 591 | src_local_path, |
| 592 | dest_local_path, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 593 | true /* copy */); |
| 594 | } else { // non-overwrite |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 595 | error = CreateFile(context, src_local_path, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 596 | dest_url.origin(), dest_url.type(), |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 597 | &dest_file_info, 0, NULL); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 598 | } |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 599 | } else { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 600 | if (overwrite) { |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 601 | if (db->OverwritingMoveFile(src_file_id, dest_file_id)) { |
| 602 | if (base::PLATFORM_FILE_OK != |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 603 | NativeFileUtil::DeleteFile(dest_local_path)) |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 604 | LOG(WARNING) << "Leaked a backing file."; |
| 605 | error = base::PLATFORM_FILE_OK; |
| 606 | } else { |
| 607 | error = base::PLATFORM_FILE_ERROR_FAILED; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 608 | } |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 609 | } else { // non-overwrite |
| 610 | if (db->UpdateFileInfo(src_file_id, dest_file_info)) |
| 611 | error = base::PLATFORM_FILE_OK; |
| 612 | else |
| 613 | error = base::PLATFORM_FILE_ERROR_FAILED; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 614 | } |
| 615 | } |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 616 | |
| 617 | if (error != base::PLATFORM_FILE_OK) |
| 618 | return error; |
| 619 | |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame] | 620 | if (overwrite) { |
| 621 | context->change_observers()->Notify( |
| 622 | &FileChangeObserver::OnModifyFile, |
| 623 | MakeTuple(dest_url)); |
| 624 | } else { |
| 625 | context->change_observers()->Notify( |
| 626 | &FileChangeObserver::OnCreateFileFrom, |
| 627 | MakeTuple(dest_url, src_url)); |
| 628 | } |
| 629 | |
| 630 | if (!copy) { |
| 631 | context->change_observers()->Notify( |
| 632 | &FileChangeObserver::OnRemoveFile, MakeTuple(src_url)); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 633 | TouchDirectory(db, src_file_info.parent_id); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame] | 634 | } |
| 635 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 636 | TouchDirectory(db, dest_file_info.parent_id); |
| 637 | |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 638 | UpdateUsage(context, dest_url, growth); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 639 | return error; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | PlatformFileError ObfuscatedFileUtil::CopyInForeignFile( |
| 643 | FileSystemOperationContext* context, |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 644 | const base::FilePath& src_file_path, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 645 | const FileSystemURL& dest_url) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 646 | FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 647 | dest_url.origin(), dest_url.type(), true); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 648 | if (!db) |
| 649 | return base::PLATFORM_FILE_ERROR_FAILED; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 650 | |
| 651 | base::PlatformFileInfo src_platform_file_info; |
| 652 | if (!file_util::GetFileInfo(src_file_path, &src_platform_file_info)) |
| 653 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 654 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 655 | FileId dest_file_id; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 656 | bool overwrite = db->GetFileWithPath(dest_url.path(), |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 657 | &dest_file_id); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 658 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 659 | FileInfo dest_file_info; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 660 | base::PlatformFileInfo dest_platform_file_info; // overwrite case only |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 661 | if (overwrite) { |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 662 | base::FilePath dest_local_path; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 663 | base::PlatformFileError error = GetFileInfoInternal( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 664 | db, context, dest_url.origin(), dest_url.type(), dest_file_id, |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 665 | &dest_file_info, &dest_platform_file_info, &dest_local_path); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 666 | if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) |
| 667 | overwrite = false; // fallback to non-overwrite case |
| 668 | else if (error != base::PLATFORM_FILE_OK) |
| 669 | return error; |
| 670 | else if (dest_file_info.is_directory()) |
[email protected] | bab213be | 2013-01-23 15:13:08 | [diff] [blame] | 671 | return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 672 | } |
| 673 | if (!overwrite) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 674 | FileId dest_parent_id; |
[email protected] | 8a020f6 | 2013-02-18 08:05:44 | [diff] [blame] | 675 | if (!db->GetFileWithPath(VirtualPath::DirName(dest_url.path()), |
[email protected] | bab213be | 2013-01-23 15:13:08 | [diff] [blame] | 676 | &dest_parent_id)) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 677 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 678 | } |
[email protected] | bab213be | 2013-01-23 15:13:08 | [diff] [blame] | 679 | if (!dest_file_info.is_directory()) |
| 680 | return base::PLATFORM_FILE_ERROR_FAILED; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 681 | InitFileInfo(&dest_file_info, dest_parent_id, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 682 | VirtualPath::BaseName(dest_url.path()).value()); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 683 | } |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 684 | |
| 685 | int64 growth = src_platform_file_info.size; |
| 686 | if (overwrite) |
| 687 | growth -= dest_platform_file_info.size; |
| 688 | else |
| 689 | growth += UsageForPath(dest_file_info.name.size()); |
| 690 | if (!AllocateQuota(context, growth)) |
| 691 | return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 692 | |
| 693 | base::PlatformFileError error; |
| 694 | if (overwrite) { |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 695 | base::FilePath dest_local_path = DataPathToLocalPath( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 696 | dest_url.origin(), dest_url.type(), dest_file_info.data_path); |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 697 | error = NativeFileUtil::CopyOrMoveFile( |
| 698 | src_file_path, dest_local_path, true); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 699 | } else { |
| 700 | error = CreateFile(context, src_file_path, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 701 | dest_url.origin(), dest_url.type(), |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 702 | &dest_file_info, 0, NULL); |
| 703 | } |
| 704 | |
| 705 | if (error != base::PLATFORM_FILE_OK) |
| 706 | return error; |
| 707 | |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame] | 708 | if (overwrite) { |
| 709 | context->change_observers()->Notify( |
| 710 | &FileChangeObserver::OnModifyFile, MakeTuple(dest_url)); |
| 711 | } else { |
| 712 | context->change_observers()->Notify( |
| 713 | &FileChangeObserver::OnCreateFile, MakeTuple(dest_url)); |
| 714 | } |
| 715 | |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 716 | UpdateUsage(context, dest_url, growth); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 717 | TouchDirectory(db, dest_file_info.parent_id); |
| 718 | return base::PLATFORM_FILE_OK; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | PlatformFileError ObfuscatedFileUtil::DeleteFile( |
| 722 | FileSystemOperationContext* context, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 723 | const FileSystemURL& url) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 724 | FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 725 | url.origin(), url.type(), true); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 726 | if (!db) |
| 727 | return base::PLATFORM_FILE_ERROR_FAILED; |
| 728 | FileId file_id; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 729 | if (!db->GetFileWithPath(url.path(), &file_id)) |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 730 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 731 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 732 | FileInfo file_info; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 733 | base::PlatformFileInfo platform_file_info; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 734 | base::FilePath local_path; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 735 | base::PlatformFileError error = GetFileInfoInternal( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 736 | db, context, url.origin(), url.type(), file_id, |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 737 | &file_info, &platform_file_info, &local_path); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 738 | if (error != base::PLATFORM_FILE_ERROR_NOT_FOUND && |
| 739 | error != base::PLATFORM_FILE_OK) |
| 740 | return error; |
| 741 | |
[email protected] | 92b80880 | 2013-01-28 05:10:51 | [diff] [blame] | 742 | if (file_info.is_directory()) |
| 743 | return base::PLATFORM_FILE_ERROR_NOT_A_FILE; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 744 | |
| 745 | int64 growth = -UsageForPath(file_info.name.size()) - platform_file_info.size; |
| 746 | AllocateQuota(context, growth); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 747 | if (!db->RemoveFileInfo(file_id)) { |
| 748 | NOTREACHED(); |
| 749 | return base::PLATFORM_FILE_ERROR_FAILED; |
| 750 | } |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 751 | UpdateUsage(context, url, growth); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 752 | TouchDirectory(db, file_info.parent_id); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 753 | |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame] | 754 | context->change_observers()->Notify( |
| 755 | &FileChangeObserver::OnRemoveFile, MakeTuple(url)); |
| 756 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 757 | if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) |
| 758 | return base::PLATFORM_FILE_OK; |
| 759 | |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 760 | error = NativeFileUtil::DeleteFile(local_path); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 761 | if (base::PLATFORM_FILE_OK != error) |
| 762 | LOG(WARNING) << "Leaked a backing file."; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 763 | return base::PLATFORM_FILE_OK; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 764 | } |
| 765 | |
[email protected] | bab213be | 2013-01-23 15:13:08 | [diff] [blame] | 766 | PlatformFileError ObfuscatedFileUtil::DeleteDirectory( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 767 | FileSystemOperationContext* context, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 768 | const FileSystemURL& url) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 769 | FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 770 | url.origin(), url.type(), true); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 771 | if (!db) |
| 772 | return base::PLATFORM_FILE_ERROR_FAILED; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 773 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 774 | FileId file_id; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 775 | if (!db->GetFileWithPath(url.path(), &file_id)) |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 776 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 777 | FileInfo file_info; |
[email protected] | bab213be | 2013-01-23 15:13:08 | [diff] [blame] | 778 | if (!db->GetFileInfo(file_id, &file_info)) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 779 | NOTREACHED(); |
| 780 | return base::PLATFORM_FILE_ERROR_FAILED; |
| 781 | } |
[email protected] | bab213be | 2013-01-23 15:13:08 | [diff] [blame] | 782 | if (!file_info.is_directory()) |
| 783 | return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 784 | if (!db->RemoveFileInfo(file_id)) |
| 785 | return base::PLATFORM_FILE_ERROR_NOT_EMPTY; |
[email protected] | f49dfc88 | 2012-04-05 15:02:06 | [diff] [blame] | 786 | int64 growth = -UsageForPath(file_info.name.size()); |
| 787 | AllocateQuota(context, growth); |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 788 | UpdateUsage(context, url, growth); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 789 | TouchDirectory(db, file_info.parent_id); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame] | 790 | context->change_observers()->Notify( |
| 791 | &FileChangeObserver::OnRemoveDirectory, MakeTuple(url)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 792 | return base::PLATFORM_FILE_OK; |
| 793 | } |
| 794 | |
[email protected] | 7ab45cfa8 | 2013-04-26 07:13:20 | [diff] [blame] | 795 | webkit_blob::ScopedFile ObfuscatedFileUtil::CreateSnapshotFile( |
[email protected] | d0a1f037 | 2012-07-19 11:17:37 | [diff] [blame] | 796 | FileSystemOperationContext* context, |
| 797 | const FileSystemURL& url, |
[email protected] | 7ab45cfa8 | 2013-04-26 07:13:20 | [diff] [blame] | 798 | base::PlatformFileError* error, |
[email protected] | d0a1f037 | 2012-07-19 11:17:37 | [diff] [blame] | 799 | base::PlatformFileInfo* file_info, |
[email protected] | 7ab45cfa8 | 2013-04-26 07:13:20 | [diff] [blame] | 800 | base::FilePath* platform_path) { |
[email protected] | 826df82 | 2012-08-04 01:23:56 | [diff] [blame] | 801 | // We're just returning the local file information. |
[email protected] | 7ab45cfa8 | 2013-04-26 07:13:20 | [diff] [blame] | 802 | *error = GetFileInfo(context, url, file_info, platform_path); |
| 803 | if (*error == base::PLATFORM_FILE_OK && file_info->is_directory) { |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 804 | *file_info = base::PlatformFileInfo(); |
[email protected] | 7ab45cfa8 | 2013-04-26 07:13:20 | [diff] [blame] | 805 | *error = base::PLATFORM_FILE_ERROR_NOT_A_FILE; |
[email protected] | aeba9c1 | 2013-01-28 10:44:42 | [diff] [blame] | 806 | } |
[email protected] | 7ab45cfa8 | 2013-04-26 07:13:20 | [diff] [blame] | 807 | return webkit_blob::ScopedFile(); |
[email protected] | bab213be | 2013-01-23 15:13:08 | [diff] [blame] | 808 | } |
| 809 | |
[email protected] | 5453ca05 | 2013-04-11 21:15:39 | [diff] [blame] | 810 | scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> |
| 811 | ObfuscatedFileUtil::CreateFileEnumerator( |
| 812 | FileSystemOperationContext* context, |
| 813 | const FileSystemURL& root_url, |
| 814 | bool recursive) { |
| 815 | FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 816 | root_url.origin(), root_url.type(), false); |
| 817 | if (!db) { |
| 818 | return scoped_ptr<AbstractFileEnumerator>(new EmptyFileEnumerator()); |
| 819 | } |
| 820 | return scoped_ptr<AbstractFileEnumerator>( |
| 821 | new ObfuscatedFileEnumerator(db, context, this, root_url, recursive)); |
| 822 | } |
| 823 | |
[email protected] | bab213be | 2013-01-23 15:13:08 | [diff] [blame] | 824 | bool ObfuscatedFileUtil::IsDirectoryEmpty( |
| 825 | FileSystemOperationContext* context, |
| 826 | const FileSystemURL& url) { |
| 827 | FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 828 | url.origin(), url.type(), false); |
| 829 | if (!db) |
| 830 | return true; // Not a great answer, but it's what others do. |
| 831 | FileId file_id; |
| 832 | if (!db->GetFileWithPath(url.path(), &file_id)) |
| 833 | return true; // Ditto. |
| 834 | FileInfo file_info; |
| 835 | if (!db->GetFileInfo(file_id, &file_info)) { |
| 836 | DCHECK(!file_id); |
| 837 | // It's the root directory and the database hasn't been initialized yet. |
| 838 | return true; |
| 839 | } |
| 840 | if (!file_info.is_directory()) |
| 841 | return true; |
| 842 | std::vector<FileId> children; |
| 843 | // TODO(ericu): This could easily be made faster with help from the database. |
| 844 | if (!db->ListChildren(file_id, &children)) |
| 845 | return true; |
| 846 | return children.empty(); |
[email protected] | d0a1f037 | 2012-07-19 11:17:37 | [diff] [blame] | 847 | } |
| 848 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 849 | base::FilePath ObfuscatedFileUtil::GetDirectoryForOriginAndType( |
[email protected] | d247b84 | 2012-05-08 06:43:36 | [diff] [blame] | 850 | const GURL& origin, |
| 851 | FileSystemType type, |
| 852 | bool create, |
| 853 | base::PlatformFileError* error_code) { |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 854 | base::FilePath origin_dir = GetDirectoryForOrigin(origin, create, error_code); |
[email protected] | c7a4a10f | 2011-05-19 04:56:06 | [diff] [blame] | 855 | if (origin_dir.empty()) |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 856 | return base::FilePath(); |
| 857 | base::FilePath::StringType type_string = GetDirectoryNameForType(type); |
[email protected] | c7a4a10f | 2011-05-19 04:56:06 | [diff] [blame] | 858 | if (type_string.empty()) { |
| 859 | LOG(WARNING) << "Unknown filesystem type requested:" << type; |
[email protected] | d247b84 | 2012-05-08 06:43:36 | [diff] [blame] | 860 | |
| 861 | if (error_code) |
| 862 | *error_code = base::PLATFORM_FILE_ERROR_INVALID_URL; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 863 | return base::FilePath(); |
[email protected] | c7a4a10f | 2011-05-19 04:56:06 | [diff] [blame] | 864 | } |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 865 | base::FilePath path = origin_dir.Append(type_string); |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 866 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 867 | if (!file_util::DirectoryExists(path) && |
[email protected] | d247b84 | 2012-05-08 06:43:36 | [diff] [blame] | 868 | (!create || !file_util::CreateDirectory(path))) { |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 869 | error = create ? |
[email protected] | d247b84 | 2012-05-08 06:43:36 | [diff] [blame] | 870 | base::PLATFORM_FILE_ERROR_FAILED : |
| 871 | base::PLATFORM_FILE_ERROR_NOT_FOUND; |
[email protected] | d247b84 | 2012-05-08 06:43:36 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | if (error_code) |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 875 | *error_code = error; |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 876 | return path; |
[email protected] | c7a4a10f | 2011-05-19 04:56:06 | [diff] [blame] | 877 | } |
| 878 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 879 | bool ObfuscatedFileUtil::DeleteDirectoryForOriginAndType( |
[email protected] | 6ff1705 | 2011-07-12 06:06:07 | [diff] [blame] | 880 | const GURL& origin, FileSystemType type) { |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 881 | base::PlatformFileError error = base::PLATFORM_FILE_OK; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 882 | base::FilePath origin_type_path = GetDirectoryForOriginAndType(origin, type, false, |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 883 | &error); |
| 884 | if (origin_type_path.empty()) |
[email protected] | 6ff1705 | 2011-07-12 06:06:07 | [diff] [blame] | 885 | return true; |
| 886 | |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 887 | if (error != base::PLATFORM_FILE_ERROR_NOT_FOUND) { |
| 888 | // TODO(dmikurube): Consider the return value of DestroyDirectoryDatabase. |
| 889 | // We ignore its error now since 1) it doesn't matter the final result, and |
| 890 | // 2) it always returns false in Windows because of LevelDB's |
| 891 | // implementation. |
| 892 | // Information about failure would be useful for debugging. |
| 893 | DestroyDirectoryDatabase(origin, type); |
| 894 | if (!file_util::Delete(origin_type_path, true /* recursive */)) |
| 895 | return false; |
| 896 | } |
[email protected] | c0229d0 | 2011-07-14 06:54:18 | [diff] [blame] | 897 | |
[email protected] | 8a020f6 | 2013-02-18 08:05:44 | [diff] [blame] | 898 | base::FilePath origin_path = VirtualPath::DirName(origin_type_path); |
[email protected] | d247b84 | 2012-05-08 06:43:36 | [diff] [blame] | 899 | DCHECK_EQ(origin_path.value(), |
| 900 | GetDirectoryForOrigin(origin, false, NULL).value()); |
[email protected] | c0229d0 | 2011-07-14 06:54:18 | [diff] [blame] | 901 | |
[email protected] | 69fe5a7 | 2012-09-13 05:05:16 | [diff] [blame] | 902 | // At this point we are sure we had successfully deleted the origin/type |
| 903 | // directory (i.e. we're ready to just return true). |
[email protected] | 69fe5a7 | 2012-09-13 05:05:16 | [diff] [blame] | 904 | // See if we have other directories in this origin directory. |
| 905 | std::vector<FileSystemType> other_types; |
| 906 | if (type != kFileSystemTypeTemporary) |
| 907 | other_types.push_back(kFileSystemTypeTemporary); |
| 908 | if (type != kFileSystemTypePersistent) |
| 909 | other_types.push_back(kFileSystemTypePersistent); |
| 910 | if (type != kFileSystemTypeSyncable) |
| 911 | other_types.push_back(kFileSystemTypeSyncable); |
| 912 | |
| 913 | for (size_t i = 0; i < other_types.size(); ++i) { |
| 914 | if (file_util::DirectoryExists( |
| 915 | origin_path.Append(GetDirectoryNameForType(other_types[i])))) { |
| 916 | // Other type's directory exists; just return true here. |
| 917 | return true; |
| 918 | } |
[email protected] | c0229d0 | 2011-07-14 06:54:18 | [diff] [blame] | 919 | } |
| 920 | |
[email protected] | 69fe5a7 | 2012-09-13 05:05:16 | [diff] [blame] | 921 | // No other directories seem exist. Try deleting the entire origin directory. |
| 922 | InitOriginDatabase(false); |
| 923 | if (origin_database_.get()) |
| 924 | origin_database_->RemovePathForOrigin(GetOriginIdentifierFromURL(origin)); |
| 925 | if (!file_util::Delete(origin_path, true /* recursive */)) |
| 926 | return false; |
| 927 | |
[email protected] | c0229d0 | 2011-07-14 06:54:18 | [diff] [blame] | 928 | return true; |
[email protected] | 6ff1705 | 2011-07-12 06:06:07 | [diff] [blame] | 929 | } |
| 930 | |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 931 | // static |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 932 | base::FilePath::StringType ObfuscatedFileUtil::GetDirectoryNameForType( |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 933 | FileSystemType type) { |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 934 | switch (type) { |
| 935 | case kFileSystemTypeTemporary: |
| 936 | return kTemporaryDirectoryName; |
| 937 | case kFileSystemTypePersistent: |
| 938 | return kPersistentDirectoryName; |
[email protected] | 69fe5a7 | 2012-09-13 05:05:16 | [diff] [blame] | 939 | case kFileSystemTypeSyncable: |
| 940 | return kSyncableDirectoryName; |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 941 | case kFileSystemTypeUnknown: |
| 942 | default: |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 943 | return base::FilePath::StringType(); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 947 | ObfuscatedFileUtil::AbstractOriginEnumerator* |
| 948 | ObfuscatedFileUtil::CreateOriginEnumerator() { |
| 949 | std::vector<FileSystemOriginDatabase::OriginRecord> origins; |
| 950 | |
| 951 | InitOriginDatabase(false); |
| 952 | return new ObfuscatedOriginEnumerator( |
| 953 | origin_database_.get(), file_system_directory_); |
| 954 | } |
| 955 | |
| 956 | bool ObfuscatedFileUtil::DestroyDirectoryDatabase( |
| 957 | const GURL& origin, FileSystemType type) { |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 958 | std::string type_string = GetFileSystemTypeString(type); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 959 | if (type_string.empty()) { |
| 960 | LOG(WARNING) << "Unknown filesystem type requested:" << type; |
| 961 | return true; |
| 962 | } |
| 963 | std::string key = GetOriginIdentifierFromURL(origin) + type_string; |
| 964 | DirectoryMap::iterator iter = directories_.find(key); |
| 965 | if (iter != directories_.end()) { |
| 966 | FileSystemDirectoryDatabase* database = iter->second; |
| 967 | directories_.erase(iter); |
| 968 | delete database; |
| 969 | } |
| 970 | |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 971 | PlatformFileError error = base::PLATFORM_FILE_OK; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 972 | base::FilePath path = GetDirectoryForOriginAndType(origin, type, false, &error); |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 973 | if (path.empty() || error == base::PLATFORM_FILE_ERROR_NOT_FOUND) |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 974 | return true; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 975 | return FileSystemDirectoryDatabase::DestroyDatabase(path); |
| 976 | } |
| 977 | |
| 978 | // static |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 979 | int64 ObfuscatedFileUtil::ComputeFilePathCost(const base::FilePath& path) { |
[email protected] | f49dfc88 | 2012-04-05 15:02:06 | [diff] [blame] | 980 | return UsageForPath(VirtualPath::BaseName(path).value().size()); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | PlatformFileError ObfuscatedFileUtil::GetFileInfoInternal( |
| 984 | FileSystemDirectoryDatabase* db, |
| 985 | FileSystemOperationContext* context, |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 986 | const GURL& origin, |
| 987 | FileSystemType type, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 988 | FileId file_id, |
| 989 | FileInfo* local_info, |
| 990 | base::PlatformFileInfo* file_info, |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 991 | base::FilePath* platform_file_path) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 992 | DCHECK(db); |
| 993 | DCHECK(context); |
| 994 | DCHECK(file_info); |
| 995 | DCHECK(platform_file_path); |
| 996 | |
| 997 | if (!db->GetFileInfo(file_id, local_info)) { |
| 998 | NOTREACHED(); |
| 999 | return base::PLATFORM_FILE_ERROR_FAILED; |
| 1000 | } |
| 1001 | |
| 1002 | if (local_info->is_directory()) { |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 1003 | file_info->size = 0; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1004 | file_info->is_directory = true; |
| 1005 | file_info->is_symbolic_link = false; |
| 1006 | file_info->last_modified = local_info->modification_time; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1007 | *platform_file_path = base::FilePath(); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1008 | // We don't fill in ctime or atime. |
| 1009 | return base::PLATFORM_FILE_OK; |
| 1010 | } |
| 1011 | if (local_info->data_path.empty()) |
| 1012 | return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1013 | base::FilePath local_path = DataPathToLocalPath( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1014 | origin, type, local_info->data_path); |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 1015 | base::PlatformFileError error = NativeFileUtil::GetFileInfo( |
| 1016 | local_path, file_info); |
[email protected] | 272a9d1 | 2012-06-19 02:17:55 | [diff] [blame] | 1017 | // We should not follow symbolic links in sandboxed file system. |
| 1018 | if (file_util::IsLink(local_path)) { |
| 1019 | LOG(WARNING) << "Found a symbolic file."; |
| 1020 | error = base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 1021 | } |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 1022 | if (error == base::PLATFORM_FILE_OK) { |
| 1023 | *platform_file_path = local_path; |
| 1024 | } else if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) { |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 1025 | LOG(WARNING) << "Lost a backing file."; |
| 1026 | InvalidateUsageCache(context, origin, type); |
| 1027 | if (!db->RemoveFileInfo(file_id)) |
| 1028 | return base::PLATFORM_FILE_ERROR_FAILED; |
| 1029 | } |
| 1030 | return error; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | PlatformFileError ObfuscatedFileUtil::CreateFile( |
| 1034 | FileSystemOperationContext* context, |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1035 | const base::FilePath& src_file_path, |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1036 | const GURL& dest_origin, |
| 1037 | FileSystemType dest_type, |
| 1038 | FileInfo* dest_file_info, int file_flags, PlatformFile* handle) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1039 | if (handle) |
| 1040 | *handle = base::kInvalidPlatformFileValue; |
| 1041 | FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1042 | dest_origin, dest_type, true); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1043 | |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 1044 | PlatformFileError error = base::PLATFORM_FILE_OK; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1045 | base::FilePath root = GetDirectoryForOriginAndType(dest_origin, dest_type, false, |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 1046 | &error); |
| 1047 | if (error != base::PLATFORM_FILE_OK) |
| 1048 | return error; |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 1049 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1050 | base::FilePath dest_local_path; |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 1051 | error = GenerateNewLocalPath(db, context, dest_origin, dest_type, |
| 1052 | &dest_local_path); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 1053 | if (error != base::PLATFORM_FILE_OK) |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1054 | return error; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1055 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1056 | bool created = false; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 1057 | if (!src_file_path.empty()) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1058 | DCHECK(!file_flags); |
| 1059 | DCHECK(!handle); |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 1060 | error = NativeFileUtil::CopyOrMoveFile( |
| 1061 | src_file_path, dest_local_path, true /* copy */); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1062 | created = true; |
| 1063 | } else { |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 1064 | if (file_util::PathExists(dest_local_path)) { |
| 1065 | if (!file_util::Delete(dest_local_path, true /* recursive */)) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1066 | NOTREACHED(); |
| 1067 | return base::PLATFORM_FILE_ERROR_FAILED; |
| 1068 | } |
| 1069 | LOG(WARNING) << "A stray file detected"; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 1070 | InvalidateUsageCache(context, dest_origin, dest_type); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | if (handle) { |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 1074 | error = NativeFileUtil::CreateOrOpen( |
| 1075 | dest_local_path, file_flags, handle, &created); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1076 | // If this succeeds, we must close handle on any subsequent error. |
| 1077 | } else { |
| 1078 | DCHECK(!file_flags); // file_flags is only used by CreateOrOpen. |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 1079 | error = NativeFileUtil::EnsureFileExists(dest_local_path, &created); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | if (error != base::PLATFORM_FILE_OK) |
| 1083 | return error; |
| 1084 | |
| 1085 | if (!created) { |
| 1086 | NOTREACHED(); |
| 1087 | if (handle) { |
| 1088 | DCHECK_NE(base::kInvalidPlatformFileValue, *handle); |
| 1089 | base::ClosePlatformFile(*handle); |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 1090 | file_util::Delete(dest_local_path, false /* recursive */); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1091 | } |
| 1092 | return base::PLATFORM_FILE_ERROR_FAILED; |
| 1093 | } |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 1094 | |
| 1095 | // This removes the root, including the trailing slash, leaving a relative |
| 1096 | // path. |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1097 | dest_file_info->data_path = base::FilePath( |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 1098 | dest_local_path.value().substr(root.value().length() + 1)); |
| 1099 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1100 | FileId file_id; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1101 | if (!db->AddFileInfo(*dest_file_info, &file_id)) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1102 | if (handle) { |
| 1103 | DCHECK_NE(base::kInvalidPlatformFileValue, *handle); |
| 1104 | base::ClosePlatformFile(*handle); |
| 1105 | } |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 1106 | file_util::Delete(dest_local_path, false /* recursive */); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1107 | return base::PLATFORM_FILE_ERROR_FAILED; |
| 1108 | } |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1109 | TouchDirectory(db, dest_file_info->parent_id); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1110 | |
| 1111 | return base::PLATFORM_FILE_OK; |
| 1112 | } |
| 1113 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1114 | base::FilePath ObfuscatedFileUtil::DataPathToLocalPath( |
| 1115 | const GURL& origin, FileSystemType type, const base::FilePath& data_path) { |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 1116 | PlatformFileError error = base::PLATFORM_FILE_OK; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1117 | base::FilePath root = GetDirectoryForOriginAndType(origin, type, false, &error); |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 1118 | if (error != base::PLATFORM_FILE_OK) |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1119 | return base::FilePath(); |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 1120 | return root.Append(data_path); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1121 | } |
| 1122 | |
[email protected] | b9566e7c | 2012-10-29 10:57:46 | [diff] [blame] | 1123 | // TODO(ericu): How to do the whole validation-without-creation thing? |
| 1124 | // We may not have quota even to create the database. |
| 1125 | // Ah, in that case don't even get here? |
[email protected] | c7a4a10f | 2011-05-19 04:56:06 | [diff] [blame] | 1126 | // Still doesn't answer the quota issue, though. |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1127 | FileSystemDirectoryDatabase* ObfuscatedFileUtil::GetDirectoryDatabase( |
[email protected] | 4b4d53bd | 2011-07-08 07:26:05 | [diff] [blame] | 1128 | const GURL& origin, FileSystemType type, bool create) { |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 1129 | std::string type_string = GetFileSystemTypeString(type); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1130 | if (type_string.empty()) { |
| 1131 | LOG(WARNING) << "Unknown filesystem type requested:" << type; |
| 1132 | return NULL; |
| 1133 | } |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1134 | std::string key = GetOriginIdentifierFromURL(origin) + type_string; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1135 | DirectoryMap::iterator iter = directories_.find(key); |
[email protected] | 4b4d53bd | 2011-07-08 07:26:05 | [diff] [blame] | 1136 | if (iter != directories_.end()) { |
| 1137 | MarkUsed(); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1138 | return iter->second; |
[email protected] | 4b4d53bd | 2011-07-08 07:26:05 | [diff] [blame] | 1139 | } |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1140 | |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 1141 | PlatformFileError error = base::PLATFORM_FILE_OK; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1142 | base::FilePath path = GetDirectoryForOriginAndType(origin, type, create, &error); |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 1143 | if (error != base::PLATFORM_FILE_OK) { |
| 1144 | LOG(WARNING) << "Failed to get origin+type directory: " << path.value(); |
[email protected] | c7a4a10f | 2011-05-19 04:56:06 | [diff] [blame] | 1145 | return NULL; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1146 | } |
[email protected] | 4b4d53bd | 2011-07-08 07:26:05 | [diff] [blame] | 1147 | MarkUsed(); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1148 | FileSystemDirectoryDatabase* database = new FileSystemDirectoryDatabase(path); |
| 1149 | directories_[key] = database; |
| 1150 | return database; |
| 1151 | } |
| 1152 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1153 | base::FilePath ObfuscatedFileUtil::GetDirectoryForOrigin( |
[email protected] | d247b84 | 2012-05-08 06:43:36 | [diff] [blame] | 1154 | const GURL& origin, bool create, base::PlatformFileError* error_code) { |
| 1155 | if (!InitOriginDatabase(create)) { |
| 1156 | if (error_code) { |
| 1157 | *error_code = create ? |
| 1158 | base::PLATFORM_FILE_ERROR_FAILED : |
| 1159 | base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 1160 | } |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1161 | return base::FilePath(); |
[email protected] | d247b84 | 2012-05-08 06:43:36 | [diff] [blame] | 1162 | } |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1163 | base::FilePath directory_name; |
[email protected] | 83c0d3c | 2011-07-15 03:36:30 | [diff] [blame] | 1164 | std::string id = GetOriginIdentifierFromURL(origin); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1165 | |
| 1166 | bool exists_in_db = origin_database_->HasOriginPath(id); |
[email protected] | d247b84 | 2012-05-08 06:43:36 | [diff] [blame] | 1167 | if (!exists_in_db && !create) { |
| 1168 | if (error_code) |
| 1169 | *error_code = base::PLATFORM_FILE_ERROR_NOT_FOUND; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1170 | return base::FilePath(); |
[email protected] | d247b84 | 2012-05-08 06:43:36 | [diff] [blame] | 1171 | } |
| 1172 | if (!origin_database_->GetPathForOrigin(id, &directory_name)) { |
| 1173 | if (error_code) |
| 1174 | *error_code = base::PLATFORM_FILE_ERROR_FAILED; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1175 | return base::FilePath(); |
[email protected] | d247b84 | 2012-05-08 06:43:36 | [diff] [blame] | 1176 | } |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1177 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1178 | base::FilePath path = file_system_directory_.Append(directory_name); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1179 | bool exists_in_fs = file_util::DirectoryExists(path); |
| 1180 | if (!exists_in_db && exists_in_fs) { |
[email protected] | d247b84 | 2012-05-08 06:43:36 | [diff] [blame] | 1181 | if (!file_util::Delete(path, true)) { |
| 1182 | if (error_code) |
| 1183 | *error_code = base::PLATFORM_FILE_ERROR_FAILED; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1184 | return base::FilePath(); |
[email protected] | d247b84 | 2012-05-08 06:43:36 | [diff] [blame] | 1185 | } |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1186 | exists_in_fs = false; |
| 1187 | } |
| 1188 | |
| 1189 | if (!exists_in_fs) { |
[email protected] | d247b84 | 2012-05-08 06:43:36 | [diff] [blame] | 1190 | if (!create || !file_util::CreateDirectory(path)) { |
| 1191 | if (error_code) |
| 1192 | *error_code = create ? |
| 1193 | base::PLATFORM_FILE_ERROR_FAILED : |
| 1194 | base::PLATFORM_FILE_ERROR_NOT_FOUND; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1195 | return base::FilePath(); |
[email protected] | d247b84 | 2012-05-08 06:43:36 | [diff] [blame] | 1196 | } |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1197 | } |
| 1198 | |
[email protected] | d247b84 | 2012-05-08 06:43:36 | [diff] [blame] | 1199 | if (error_code) |
| 1200 | *error_code = base::PLATFORM_FILE_OK; |
| 1201 | |
[email protected] | 83c0d3c | 2011-07-15 03:36:30 | [diff] [blame] | 1202 | return path; |
| 1203 | } |
| 1204 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 1205 | void ObfuscatedFileUtil::InvalidateUsageCache( |
| 1206 | FileSystemOperationContext* context, |
| 1207 | const GURL& origin, |
| 1208 | FileSystemType type) { |
| 1209 | context->file_system_context()->GetQuotaUtil(type)-> |
| 1210 | InvalidateUsageCache(origin, type); |
| 1211 | } |
| 1212 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1213 | void ObfuscatedFileUtil::MarkUsed() { |
[email protected] | 0a732853 | 2011-05-13 23:54:43 | [diff] [blame] | 1214 | if (timer_.IsRunning()) |
| 1215 | timer_.Reset(); |
| 1216 | else |
[email protected] | d323a17 | 2011-09-02 18:23:02 | [diff] [blame] | 1217 | timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(kFlushDelaySeconds), |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1218 | this, &ObfuscatedFileUtil::DropDatabases); |
[email protected] | 0a732853 | 2011-05-13 23:54:43 | [diff] [blame] | 1219 | } |
| 1220 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1221 | void ObfuscatedFileUtil::DropDatabases() { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1222 | origin_database_.reset(); |
| 1223 | STLDeleteContainerPairSecondPointers( |
| 1224 | directories_.begin(), directories_.end()); |
| 1225 | directories_.clear(); |
| 1226 | } |
| 1227 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1228 | bool ObfuscatedFileUtil::InitOriginDatabase(bool create) { |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1229 | if (!origin_database_.get()) { |
| 1230 | if (!create && !file_util::DirectoryExists(file_system_directory_)) |
| 1231 | return false; |
| 1232 | if (!file_util::CreateDirectory(file_system_directory_)) { |
| 1233 | LOG(WARNING) << "Failed to create FileSystem directory: " << |
| 1234 | file_system_directory_.value(); |
| 1235 | return false; |
| 1236 | } |
| 1237 | origin_database_.reset( |
[email protected] | b969877 | 2012-03-29 16:45:48 | [diff] [blame] | 1238 | new FileSystemOriginDatabase(file_system_directory_)); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1239 | } |
| 1240 | return true; |
| 1241 | } |
| 1242 | |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 1243 | PlatformFileError ObfuscatedFileUtil::GenerateNewLocalPath( |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 1244 | FileSystemDirectoryDatabase* db, |
| 1245 | FileSystemOperationContext* context, |
| 1246 | const GURL& origin, |
| 1247 | FileSystemType type, |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1248 | base::FilePath* local_path) { |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 1249 | DCHECK(local_path); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 1250 | int64 number; |
| 1251 | if (!db || !db->GetNextInteger(&number)) |
| 1252 | return base::PLATFORM_FILE_ERROR_FAILED; |
| 1253 | |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 1254 | PlatformFileError error = base::PLATFORM_FILE_OK; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 1255 | base::FilePath new_local_path = GetDirectoryForOriginAndType(origin, type, |
| 1256 | false, &error); |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 1257 | if (error != base::PLATFORM_FILE_OK) |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 1258 | return base::PLATFORM_FILE_ERROR_FAILED; |
| 1259 | |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 1260 | // We use the third- and fourth-to-last digits as the directory. |
| 1261 | int64 directory_number = number % 10000 / 100; |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 1262 | new_local_path = new_local_path.AppendASCII( |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 1263 | base::StringPrintf("%02" PRId64, directory_number)); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 1264 | |
[email protected] | bf48a1f | 2012-07-11 09:35:03 | [diff] [blame] | 1265 | error = NativeFileUtil::CreateDirectory( |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 1266 | new_local_path, false /* exclusive */, false /* recursive */); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 1267 | if (error != base::PLATFORM_FILE_OK) |
| 1268 | return error; |
| 1269 | |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 1270 | *local_path = |
| 1271 | new_local_path.AppendASCII(base::StringPrintf("%08" PRId64, number)); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 1272 | return base::PLATFORM_FILE_OK; |
| 1273 | } |
| 1274 | |
[email protected] | f7ac94e | 2013-04-30 08:34:50 | [diff] [blame^] | 1275 | PlatformFileError ObfuscatedFileUtil::CreateOrOpenInternal( |
| 1276 | FileSystemOperationContext* context, |
| 1277 | const FileSystemURL& url, int file_flags, |
| 1278 | PlatformFile* file_handle, bool* created) { |
| 1279 | DCHECK(!(file_flags & (base::PLATFORM_FILE_DELETE_ON_CLOSE | |
| 1280 | base::PLATFORM_FILE_HIDDEN | base::PLATFORM_FILE_EXCLUSIVE_READ | |
| 1281 | base::PLATFORM_FILE_EXCLUSIVE_WRITE))); |
| 1282 | FileSystemDirectoryDatabase* db = GetDirectoryDatabase( |
| 1283 | url.origin(), url.type(), true); |
| 1284 | if (!db) |
| 1285 | return base::PLATFORM_FILE_ERROR_FAILED; |
| 1286 | FileId file_id; |
| 1287 | if (!db->GetFileWithPath(url.path(), &file_id)) { |
| 1288 | // The file doesn't exist. |
| 1289 | if (!(file_flags & (base::PLATFORM_FILE_CREATE | |
| 1290 | base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_OPEN_ALWAYS))) |
| 1291 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 1292 | FileId parent_id; |
| 1293 | if (!db->GetFileWithPath(VirtualPath::DirName(url.path()), |
| 1294 | &parent_id)) |
| 1295 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 1296 | FileInfo file_info; |
| 1297 | InitFileInfo(&file_info, parent_id, |
| 1298 | VirtualPath::BaseName(url.path()).value()); |
| 1299 | |
| 1300 | int64 growth = UsageForPath(file_info.name.size()); |
| 1301 | if (!AllocateQuota(context, growth)) |
| 1302 | return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 1303 | PlatformFileError error = CreateFile( |
| 1304 | context, base::FilePath(), |
| 1305 | url.origin(), url.type(), &file_info, |
| 1306 | file_flags, file_handle); |
| 1307 | if (created && base::PLATFORM_FILE_OK == error) { |
| 1308 | *created = true; |
| 1309 | UpdateUsage(context, url, growth); |
| 1310 | context->change_observers()->Notify( |
| 1311 | &FileChangeObserver::OnCreateFile, MakeTuple(url)); |
| 1312 | } |
| 1313 | return error; |
| 1314 | } |
| 1315 | |
| 1316 | if (file_flags & base::PLATFORM_FILE_CREATE) |
| 1317 | return base::PLATFORM_FILE_ERROR_EXISTS; |
| 1318 | |
| 1319 | base::PlatformFileInfo platform_file_info; |
| 1320 | base::FilePath local_path; |
| 1321 | FileInfo file_info; |
| 1322 | base::PlatformFileError error = GetFileInfoInternal( |
| 1323 | db, context, url.origin(), url.type(), file_id, |
| 1324 | &file_info, &platform_file_info, &local_path); |
| 1325 | if (error != base::PLATFORM_FILE_OK) |
| 1326 | return error; |
| 1327 | if (file_info.is_directory()) |
| 1328 | return base::PLATFORM_FILE_ERROR_NOT_A_FILE; |
| 1329 | |
| 1330 | int64 delta = 0; |
| 1331 | if (file_flags & (base::PLATFORM_FILE_CREATE_ALWAYS | |
| 1332 | base::PLATFORM_FILE_OPEN_TRUNCATED)) { |
| 1333 | // The file exists and we're truncating. |
| 1334 | delta = -platform_file_info.size; |
| 1335 | AllocateQuota(context, delta); |
| 1336 | } |
| 1337 | |
| 1338 | error = NativeFileUtil::CreateOrOpen( |
| 1339 | local_path, file_flags, file_handle, created); |
| 1340 | if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) { |
| 1341 | // TODO(tzik): Also invalidate on-memory usage cache in UsageTracker. |
| 1342 | // TODO(tzik): Delete database entry after ensuring the file lost. |
| 1343 | InvalidateUsageCache(context, url.origin(), url.type()); |
| 1344 | LOG(WARNING) << "Lost a backing file."; |
| 1345 | error = base::PLATFORM_FILE_ERROR_FAILED; |
| 1346 | } |
| 1347 | |
| 1348 | // If truncating we need to update the usage. |
| 1349 | if (error == base::PLATFORM_FILE_OK && delta) { |
| 1350 | UpdateUsage(context, url, delta); |
| 1351 | context->change_observers()->Notify( |
| 1352 | &FileChangeObserver::OnModifyFile, MakeTuple(url)); |
| 1353 | } |
| 1354 | return error; |
| 1355 | } |
| 1356 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1357 | } // namespace fileapi |