blob: be1c5951b8b25bae0e6f5e20b2a003027c7ae3bc [file] [log] [blame]
[email protected]e7e46732012-01-05 11:45:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d4905e2e2011-05-13 21:56:322// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]7878ece2011-09-05 11:41:495#include "webkit/fileapi/obfuscated_file_util.h"
[email protected]d4905e2e2011-05-13 21:56:326
7#include <queue>
[email protected]172205b2011-08-30 03:36:448#include <string>
[email protected]fcc2d5f2011-05-23 22:06:269#include <vector>
[email protected]d4905e2e2011-05-13 21:56:3210
11#include "base/file_util.h"
[email protected]6b931152011-05-20 21:02:3512#include "base/format_macros.h"
[email protected]d4905e2e2011-05-13 21:56:3213#include "base/logging.h"
[email protected]0a7328532011-05-13 23:54:4314#include "base/message_loop.h"
[email protected]58b7c5a62011-08-15 13:09:2715#include "base/stl_util.h"
[email protected]6b931152011-05-20 21:02:3516#include "base/stringprintf.h"
[email protected]4ce532f2013-03-27 22:03:4017#include "base/strings/string_number_conversions.h"
[email protected]13ac53532013-03-30 00:27:0018#include "base/strings/sys_string_conversions.h"
[email protected]d4905e2e2011-05-13 21:56:3219#include "googleurl/src/gurl.h"
[email protected]caf66702012-09-07 07:02:2020#include "webkit/fileapi/file_observers.h"
[email protected]d4905e2e2011-05-13 21:56:3221#include "webkit/fileapi/file_system_context.h"
22#include "webkit/fileapi/file_system_operation_context.h"
[email protected]949f25a2012-06-27 01:53:0923#include "webkit/fileapi/file_system_url.h"
[email protected]fcc2d5f2011-05-23 22:06:2624#include "webkit/fileapi/file_system_util.h"
[email protected]95af7372012-05-28 07:51:2025#include "webkit/fileapi/native_file_util.h"
[email protected]d4905e2e2011-05-13 21:56:3226#include "webkit/fileapi/sandbox_mount_point_provider.h"
[email protected]b6c415b2013-04-16 06:41:4727#include "webkit/fileapi/syncable/syncable_file_system_util.h"
[email protected]0c5ebe32011-08-19 22:37:1628#include "webkit/quota/quota_manager.h"
[email protected]d4905e2e2011-05-13 21:56:3229
[email protected]294dd0312012-05-11 07:35:1330// Example of various paths:
[email protected]949f25a2012-06-27 01:53:0931// void ObfuscatedFileUtil::DoSomething(const FileSystemURL& url) {
[email protected]a3ef4832013-02-02 05:12:3332// base::FilePath virtual_path = url.path();
33// base::FilePath local_path = GetLocalFilePath(url);
[email protected]294dd0312012-05-11 07:35:1334//
[email protected]95af7372012-05-28 07:51:2035// NativeFileUtil::DoSomething(local_path);
36// file_util::DoAnother(local_path);
[email protected]294dd0312012-05-11 07:35:1337// }
38
[email protected]08f8feb2012-02-26 11:53:5039namespace fileapi {
40
[email protected]d4905e2e2011-05-13 21:56:3241namespace {
42
[email protected]294dd0312012-05-11 07:35:1343typedef FileSystemDirectoryDatabase::FileId FileId;
44typedef FileSystemDirectoryDatabase::FileInfo FileInfo;
45
[email protected]0a7328532011-05-13 23:54:4346const int64 kFlushDelaySeconds = 10 * 60; // 10 minutes
47
[email protected]d4905e2e2011-05-13 21:56:3248void InitFileInfo(
[email protected]08f8feb2012-02-26 11:53:5049 FileSystemDirectoryDatabase::FileInfo* file_info,
50 FileSystemDirectoryDatabase::FileId parent_id,
[email protected]a3ef4832013-02-02 05:12:3351 const base::FilePath::StringType& file_name) {
[email protected]d4905e2e2011-05-13 21:56:3252 DCHECK(file_info);
53 file_info->parent_id = parent_id;
[email protected]d4905e2e2011-05-13 21:56:3254 file_info->name = file_name;
[email protected]d4905e2e2011-05-13 21:56:3255}
56
[email protected]0c5ebe32011-08-19 22:37:1657// 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]a3ef4832013-02-02 05:12:3359// which base::FilePath uses wide chars [since they're converted to UTF-8 for storage
[email protected]0c5ebe32011-08-19 22:37:1660// 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]172205b2011-08-30 03:36:4462const int64 kPathCreationQuotaCost = 146; // Bytes per inode, basically.
63const int64 kPathByteQuotaCost = 2; // Bytes per byte of path length in UTF-8.
[email protected]0c5ebe32011-08-19 22:37:1664
[email protected]f49dfc882012-04-05 15:02:0665int64 UsageForPath(size_t length) {
66 return kPathCreationQuotaCost +
67 static_cast<int64>(length) * kPathByteQuotaCost;
[email protected]0c5ebe32011-08-19 22:37:1668}
69
[email protected]f49dfc882012-04-05 15:02:0670bool AllocateQuota(FileSystemOperationContext* context, int64 growth) {
[email protected]294dd0312012-05-11 07:35:1371 if (context->allowed_bytes_growth() == quota::QuotaManager::kNoLimit)
72 return true;
73
[email protected]0c5ebe32011-08-19 22:37:1674 int64 new_quota = context->allowed_bytes_growth() - growth;
[email protected]f49dfc882012-04-05 15:02:0675 if (growth > 0 && new_quota < 0)
76 return false;
77 context->set_allowed_bytes_growth(new_quota);
[email protected]ecdfd6c52012-04-11 13:35:4478 return true;
[email protected]0c5ebe32011-08-19 22:37:1679}
80
[email protected]f49dfc882012-04-05 15:02:0681void UpdateUsage(
[email protected]08f8feb2012-02-26 11:53:5082 FileSystemOperationContext* context,
[email protected]caf66702012-09-07 07:02:2083 const FileSystemURL& url,
[email protected]f49dfc882012-04-05 15:02:0684 int64 growth) {
[email protected]caf66702012-09-07 07:02:2085 context->update_observers()->Notify(
86 &FileUpdateObserver::OnUpdate, MakeTuple(url, growth));
[email protected]0c5ebe32011-08-19 22:37:1687}
88
[email protected]294dd0312012-05-11 07:35:1389void TouchDirectory(FileSystemDirectoryDatabase* db, FileId dir_id) {
[email protected]fad625e2f2011-12-08 05:38:0390 DCHECK(db);
91 if (!db->UpdateModificationTime(dir_id, base::Time::Now()))
92 NOTREACHED();
93}
94
[email protected]a3ef4832013-02-02 05:12:3395const base::FilePath::CharType kTemporaryDirectoryName[] = FILE_PATH_LITERAL("t");
96const base::FilePath::CharType kPersistentDirectoryName[] = FILE_PATH_LITERAL("p");
97const base::FilePath::CharType kSyncableDirectoryName[] = FILE_PATH_LITERAL("s");
[email protected]6b931152011-05-20 21:02:3598
[email protected]d4905e2e2011-05-13 21:56:3299} // namespace
100
[email protected]d4905e2e2011-05-13 21:56:32101using base::PlatformFile;
102using base::PlatformFileError;
103
[email protected]7878ece2011-09-05 11:41:49104class ObfuscatedFileEnumerator
[email protected]d4905e2e2011-05-13 21:56:32105 : public FileSystemFileUtil::AbstractFileEnumerator {
106 public:
[email protected]7878ece2011-09-05 11:41:49107 ObfuscatedFileEnumerator(
[email protected]172205b2011-08-30 03:36:44108 FileSystemDirectoryDatabase* db,
109 FileSystemOperationContext* context,
[email protected]294dd0312012-05-11 07:35:13110 ObfuscatedFileUtil* obfuscated_file_util,
[email protected]949f25a2012-06-27 01:53:09111 const FileSystemURL& root_url,
[email protected]566f223792012-03-07 03:24:28112 bool recursive)
[email protected]294dd0312012-05-11 07:35:13113 : db_(db),
[email protected]172205b2011-08-30 03:36:44114 context_(context),
[email protected]294dd0312012-05-11 07:35:13115 obfuscated_file_util_(obfuscated_file_util),
[email protected]949f25a2012-06-27 01:53:09116 origin_(root_url.origin()),
117 type_(root_url.type()),
[email protected]f1a9c522012-07-25 22:08:27118 recursive_(recursive),
119 current_file_id_(0) {
[email protected]a3ef4832013-02-02 05:12:33120 base::FilePath root_virtual_path = root_url.path();
[email protected]d4905e2e2011-05-13 21:56:32121 FileId file_id;
[email protected]294dd0312012-05-11 07:35:13122
123 if (!db_->GetFileWithPath(root_virtual_path, &file_id))
[email protected]d4905e2e2011-05-13 21:56:32124 return;
[email protected]294dd0312012-05-11 07:35:13125
126 FileRecord record = { file_id, root_virtual_path };
[email protected]566f223792012-03-07 03:24:28127 recurse_queue_.push(record);
[email protected]d4905e2e2011-05-13 21:56:32128 }
129
[email protected]294dd0312012-05-11 07:35:13130 virtual ~ObfuscatedFileEnumerator() {}
[email protected]d4905e2e2011-05-13 21:56:32131
[email protected]a3ef4832013-02-02 05:12:33132 virtual base::FilePath Next() OVERRIDE {
[email protected]d4905e2e2011-05-13 21:56:32133 ProcessRecurseQueue();
[email protected]294dd0312012-05-11 07:35:13134 if (display_stack_.empty())
[email protected]a3ef4832013-02-02 05:12:33135 return base::FilePath();
[email protected]a3938912012-03-27 14:00:55136
[email protected]294dd0312012-05-11 07:35:13137 current_file_id_ = display_stack_.back();
138 display_stack_.pop_back();
[email protected]a3938912012-03-27 14:00:55139
[email protected]294dd0312012-05-11 07:35:13140 FileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33141 base::FilePath platform_file_path;
[email protected]294dd0312012-05-11 07:35:13142 base::PlatformFileError error =
143 obfuscated_file_util_->GetFileInfoInternal(
144 db_, context_, origin_, type_, current_file_id_,
145 &file_info, &current_platform_file_info_, &platform_file_path);
146 if (error != base::PLATFORM_FILE_OK)
147 return Next();
148
[email protected]a3ef4832013-02-02 05:12:33149 base::FilePath virtual_path =
[email protected]294dd0312012-05-11 07:35:13150 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]a3938912012-03-27 14:00:55154 }
[email protected]294dd0312012-05-11 07:35:13155 return virtual_path;
[email protected]d4905e2e2011-05-13 21:56:32156 }
157
[email protected]172205b2011-08-30 03:36:44158 virtual int64 Size() OVERRIDE {
[email protected]294dd0312012-05-11 07:35:13159 return current_platform_file_info_.size;
[email protected]172205b2011-08-30 03:36:44160 }
161
[email protected]2c514f32012-03-14 05:58:13162 virtual base::Time LastModifiedTime() OVERRIDE {
[email protected]294dd0312012-05-11 07:35:13163 return current_platform_file_info_.last_modified;
[email protected]2c514f32012-03-14 05:58:13164 }
165
[email protected]172205b2011-08-30 03:36:44166 virtual bool IsDirectory() OVERRIDE {
[email protected]294dd0312012-05-11 07:35:13167 return current_platform_file_info_.is_directory;
[email protected]d4905e2e2011-05-13 21:56:32168 }
169
170 private:
171 typedef FileSystemDirectoryDatabase::FileId FileId;
172 typedef FileSystemDirectoryDatabase::FileInfo FileInfo;
173
174 struct FileRecord {
175 FileId file_id;
[email protected]a3ef4832013-02-02 05:12:33176 base::FilePath virtual_path;
[email protected]d4905e2e2011-05-13 21:56:32177 };
178
179 void ProcessRecurseQueue() {
[email protected]294dd0312012-05-11 07:35:13180 while (display_stack_.empty() && !recurse_queue_.empty()) {
181 FileRecord entry = recurse_queue_.front();
[email protected]d4905e2e2011-05-13 21:56:32182 recurse_queue_.pop();
[email protected]294dd0312012-05-11 07:35:13183 if (!db_->ListChildren(entry.file_id, &display_stack_)) {
184 display_stack_.clear();
[email protected]d4905e2e2011-05-13 21:56:32185 return;
[email protected]d4905e2e2011-05-13 21:56:32186 }
[email protected]294dd0312012-05-11 07:35:13187 current_parent_virtual_path_ = entry.virtual_path;
[email protected]d4905e2e2011-05-13 21:56:32188 }
189 }
190
[email protected]d4905e2e2011-05-13 21:56:32191 FileSystemDirectoryDatabase* db_;
[email protected]172205b2011-08-30 03:36:44192 FileSystemOperationContext* context_;
[email protected]294dd0312012-05-11 07:35:13193 ObfuscatedFileUtil* obfuscated_file_util_;
194 GURL origin_;
195 FileSystemType type_;
[email protected]566f223792012-03-07 03:24:28196 bool recursive_;
[email protected]294dd0312012-05-11 07:35:13197
198 std::queue<FileRecord> recurse_queue_;
199 std::vector<FileId> display_stack_;
[email protected]a3ef4832013-02-02 05:12:33200 base::FilePath current_parent_virtual_path_;
[email protected]294dd0312012-05-11 07:35:13201
202 FileId current_file_id_;
203 base::PlatformFileInfo current_platform_file_info_;
[email protected]d4905e2e2011-05-13 21:56:32204};
205
[email protected]7878ece2011-09-05 11:41:49206class ObfuscatedOriginEnumerator
207 : public ObfuscatedFileUtil::AbstractOriginEnumerator {
[email protected]fcc2d5f2011-05-23 22:06:26208 public:
209 typedef FileSystemOriginDatabase::OriginRecord OriginRecord;
[email protected]7878ece2011-09-05 11:41:49210 ObfuscatedOriginEnumerator(
[email protected]fcc2d5f2011-05-23 22:06:26211 FileSystemOriginDatabase* origin_database,
[email protected]a3ef4832013-02-02 05:12:33212 const base::FilePath& base_file_path)
[email protected]294dd0312012-05-11 07:35:13213 : base_file_path_(base_file_path) {
[email protected]fcc2d5f2011-05-23 22:06:26214 if (origin_database)
215 origin_database->ListAllOrigins(&origins_);
216 }
217
[email protected]7fd8fa4f2013-02-07 05:43:50218 virtual ~ObfuscatedOriginEnumerator() {}
[email protected]fcc2d5f2011-05-23 22:06:26219
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]a3ef4832013-02-02 05:12:33235 base::FilePath::StringType type_string =
[email protected]7878ece2011-09-05 11:41:49236 ObfuscatedFileUtil::GetDirectoryNameForType(type);
[email protected]fcc2d5f2011-05-23 22:06:26237 if (type_string.empty()) {
238 NOTREACHED();
239 return false;
240 }
[email protected]a3ef4832013-02-02 05:12:33241 base::FilePath path = base_file_path_.Append(current_.path).Append(type_string);
[email protected]fcc2d5f2011-05-23 22:06:26242 return file_util::DirectoryExists(path);
243 }
244
245 private:
246 std::vector<OriginRecord> origins_;
247 OriginRecord current_;
[email protected]a3ef4832013-02-02 05:12:33248 base::FilePath base_file_path_;
[email protected]fcc2d5f2011-05-23 22:06:26249};
250
[email protected]7878ece2011-09-05 11:41:49251ObfuscatedFileUtil::ObfuscatedFileUtil(
[email protected]a3ef4832013-02-02 05:12:33252 const base::FilePath& file_system_directory)
[email protected]95af7372012-05-28 07:51:20253 : file_system_directory_(file_system_directory) {
[email protected]7878ece2011-09-05 11:41:49254}
[email protected]fcc2d5f2011-05-23 22:06:26255
[email protected]3cfc10f2012-05-24 01:20:41256ObfuscatedFileUtil::~ObfuscatedFileUtil() {
257 DropDatabases();
258}
259
[email protected]7878ece2011-09-05 11:41:49260PlatformFileError ObfuscatedFileUtil::CreateOrOpen(
261 FileSystemOperationContext* context,
[email protected]949f25a2012-06-27 01:53:09262 const FileSystemURL& url, int file_flags,
[email protected]7878ece2011-09-05 11:41:49263 PlatformFile* file_handle, bool* created) {
[email protected]f7ac94e2013-04-30 08:34:50264 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]c4e6f9c2012-09-09 17:42:10273 }
[email protected]7878ece2011-09-05 11:41:49274 return error;
275}
276
[email protected]95af7372012-05-28 07:51:20277PlatformFileError ObfuscatedFileUtil::Close(
278 FileSystemOperationContext* context,
279 base::PlatformFile file) {
280 return NativeFileUtil::Close(file);
281}
282
[email protected]7878ece2011-09-05 11:41:49283PlatformFileError ObfuscatedFileUtil::EnsureFileExists(
284 FileSystemOperationContext* context,
[email protected]949f25a2012-06-27 01:53:09285 const FileSystemURL& url,
[email protected]7878ece2011-09-05 11:41:49286 bool* created) {
287 FileSystemDirectoryDatabase* db = GetDirectoryDatabase(
[email protected]949f25a2012-06-27 01:53:09288 url.origin(), url.type(), true);
[email protected]7878ece2011-09-05 11:41:49289 if (!db)
290 return base::PLATFORM_FILE_ERROR_FAILED;
[email protected]294dd0312012-05-11 07:35:13291
[email protected]7878ece2011-09-05 11:41:49292 FileId file_id;
[email protected]949f25a2012-06-27 01:53:09293 if (db->GetFileWithPath(url.path(), &file_id)) {
[email protected]7878ece2011-09-05 11:41:49294 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]8a020f62013-02-18 08:05:44306 if (!db->GetFileWithPath(VirtualPath::DirName(url.path()), &parent_id))
[email protected]7878ece2011-09-05 11:41:49307 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
308
309 FileInfo file_info;
[email protected]08f8feb2012-02-26 11:53:50310 InitFileInfo(&file_info, parent_id,
[email protected]949f25a2012-06-27 01:53:09311 VirtualPath::BaseName(url.path()).value());
[email protected]f49dfc882012-04-05 15:02:06312
313 int64 growth = UsageForPath(file_info.name.size());
314 if (!AllocateQuota(context, growth))
[email protected]7878ece2011-09-05 11:41:49315 return base::PLATFORM_FILE_ERROR_NO_SPACE;
[email protected]294dd0312012-05-11 07:35:13316 PlatformFileError error = CreateFile(
[email protected]a3ef4832013-02-02 05:12:33317 context, base::FilePath(), url.origin(), url.type(), &file_info, 0, NULL);
[email protected]f49dfc882012-04-05 15:02:06318 if (created && base::PLATFORM_FILE_OK == error) {
[email protected]7878ece2011-09-05 11:41:49319 *created = true;
[email protected]caf66702012-09-07 07:02:20320 UpdateUsage(context, url, growth);
[email protected]c4e6f9c2012-09-09 17:42:10321 context->change_observers()->Notify(
322 &FileChangeObserver::OnCreateFile, MakeTuple(url));
[email protected]f49dfc882012-04-05 15:02:06323 }
[email protected]7878ece2011-09-05 11:41:49324 return error;
325}
326
327PlatformFileError ObfuscatedFileUtil::CreateDirectory(
328 FileSystemOperationContext* context,
[email protected]949f25a2012-06-27 01:53:09329 const FileSystemURL& url,
[email protected]7878ece2011-09-05 11:41:49330 bool exclusive,
331 bool recursive) {
332 FileSystemDirectoryDatabase* db = GetDirectoryDatabase(
[email protected]949f25a2012-06-27 01:53:09333 url.origin(), url.type(), true);
[email protected]7878ece2011-09-05 11:41:49334 if (!db)
335 return base::PLATFORM_FILE_ERROR_FAILED;
[email protected]294dd0312012-05-11 07:35:13336
[email protected]8c5b3692012-11-20 09:56:49337 // 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]b6c415b2013-04-16 06:41:47341 !sync_file_system::IsSyncDirectoryOperationEnabled()) {
[email protected]8c5b3692012-11-20 09:56:49342 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION;
343 }
344
[email protected]7878ece2011-09-05 11:41:49345 FileId file_id;
[email protected]949f25a2012-06-27 01:53:09346 if (db->GetFileWithPath(url.path(), &file_id)) {
[email protected]7878ece2011-09-05 11:41:49347 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]a3ef4832013-02-02 05:12:33359 std::vector<base::FilePath::StringType> components;
[email protected]949f25a2012-06-27 01:53:09360 VirtualPath::GetComponents(url.path(), &components);
[email protected]7878ece2011-09-05 11:41:49361 FileId parent_id = 0;
362 size_t index;
363 for (index = 0; index < components.size(); ++index) {
[email protected]a3ef4832013-02-02 05:12:33364 base::FilePath::StringType name = components[index];
[email protected]7878ece2011-09-05 11:41:49365 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]fad625e2f2011-12-08 05:38:03372 bool first = true;
[email protected]7878ece2011-09-05 11:41:49373 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]f49dfc882012-04-05 15:02:06380 int64 growth = UsageForPath(file_info.name.size());
381 if (!AllocateQuota(context, growth))
[email protected]7878ece2011-09-05 11:41:49382 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]caf66702012-09-07 07:02:20387 UpdateUsage(context, url, growth);
[email protected]c4e6f9c2012-09-09 17:42:10388 context->change_observers()->Notify(
389 &FileChangeObserver::OnCreateDirectory, MakeTuple(url));
[email protected]fad625e2f2011-12-08 05:38:03390 if (first) {
391 first = false;
392 TouchDirectory(db, file_info.parent_id);
393 }
[email protected]7878ece2011-09-05 11:41:49394 }
395 return base::PLATFORM_FILE_OK;
396}
397
398PlatformFileError ObfuscatedFileUtil::GetFileInfo(
399 FileSystemOperationContext* context,
[email protected]949f25a2012-06-27 01:53:09400 const FileSystemURL& url,
[email protected]7878ece2011-09-05 11:41:49401 base::PlatformFileInfo* file_info,
[email protected]a3ef4832013-02-02 05:12:33402 base::FilePath* platform_file_path) {
[email protected]7878ece2011-09-05 11:41:49403 FileSystemDirectoryDatabase* db = GetDirectoryDatabase(
[email protected]949f25a2012-06-27 01:53:09404 url.origin(), url.type(), false);
[email protected]7878ece2011-09-05 11:41:49405 if (!db)
406 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
407 FileId file_id;
[email protected]949f25a2012-06-27 01:53:09408 if (!db->GetFileWithPath(url.path(), &file_id))
[email protected]7878ece2011-09-05 11:41:49409 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
410 FileInfo local_info;
[email protected]08f8feb2012-02-26 11:53:50411 return GetFileInfoInternal(db, context,
[email protected]949f25a2012-06-27 01:53:09412 url.origin(), url.type(),
[email protected]08f8feb2012-02-26 11:53:50413 file_id, &local_info,
414 file_info, platform_file_path);
[email protected]7878ece2011-09-05 11:41:49415}
416
[email protected]d109fcb2012-11-07 19:44:33417scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator>
418 ObfuscatedFileUtil::CreateFileEnumerator(
[email protected]d4905e2e2011-05-13 21:56:32419 FileSystemOperationContext* context,
[email protected]5453ca052013-04-11 21:15:39420 const FileSystemURL& root_url) {
421 return CreateFileEnumerator(context, root_url, false /* recursive */);
[email protected]d4905e2e2011-05-13 21:56:32422}
423
[email protected]7878ece2011-09-05 11:41:49424PlatformFileError ObfuscatedFileUtil::GetLocalFilePath(
[email protected]f1ddaa42011-07-19 05:03:03425 FileSystemOperationContext* context,
[email protected]949f25a2012-06-27 01:53:09426 const FileSystemURL& url,
[email protected]a3ef4832013-02-02 05:12:33427 base::FilePath* local_path) {
[email protected]95af7372012-05-28 07:51:20428 FileSystemDirectoryDatabase* db = GetDirectoryDatabase(
[email protected]949f25a2012-06-27 01:53:09429 url.origin(), url.type(), false);
[email protected]95af7372012-05-28 07:51:20430 if (!db)
[email protected]7878ece2011-09-05 11:41:49431 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
[email protected]95af7372012-05-28 07:51:20432 FileId file_id;
[email protected]949f25a2012-06-27 01:53:09433 if (!db->GetFileWithPath(url.path(), &file_id))
[email protected]95af7372012-05-28 07:51:20434 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]949f25a2012-06-27 01:53:09442 url.origin(), url.type(), file_info.data_path);
[email protected]f1ddaa42011-07-19 05:03:03443
[email protected]95af7372012-05-28 07:51:20444 if (local_path->empty())
445 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
[email protected]d4905e2e2011-05-13 21:56:32446 return base::PLATFORM_FILE_OK;
447}
448
[email protected]7878ece2011-09-05 11:41:49449PlatformFileError ObfuscatedFileUtil::Touch(
450 FileSystemOperationContext* context,
[email protected]949f25a2012-06-27 01:53:09451 const FileSystemURL& url,
[email protected]7878ece2011-09-05 11:41:49452 const base::Time& last_access_time,
453 const base::Time& last_modified_time) {
[email protected]4b4d53bd2011-07-08 07:26:05454 FileSystemDirectoryDatabase* db = GetDirectoryDatabase(
[email protected]949f25a2012-06-27 01:53:09455 url.origin(), url.type(), false);
[email protected]d4905e2e2011-05-13 21:56:32456 if (!db)
[email protected]7878ece2011-09-05 11:41:49457 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
[email protected]d4905e2e2011-05-13 21:56:32458 FileId file_id;
[email protected]949f25a2012-06-27 01:53:09459 if (!db->GetFileWithPath(url.path(), &file_id))
[email protected]7878ece2011-09-05 11:41:49460 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]fad625e2f2011-12-08 05:38:03468 if (!db->UpdateModificationTime(file_id, last_modified_time))
[email protected]7878ece2011-09-05 11:41:49469 return base::PLATFORM_FILE_ERROR_FAILED;
470 return base::PLATFORM_FILE_OK;
471 }
[email protected]a3ef4832013-02-02 05:12:33472 base::FilePath local_path = DataPathToLocalPath(
[email protected]949f25a2012-06-27 01:53:09473 url.origin(), url.type(), file_info.data_path);
[email protected]95af7372012-05-28 07:51:20474 return NativeFileUtil::Touch(
475 local_path, last_access_time, last_modified_time);
[email protected]7878ece2011-09-05 11:41:49476}
477
478PlatformFileError ObfuscatedFileUtil::Truncate(
479 FileSystemOperationContext* context,
[email protected]949f25a2012-06-27 01:53:09480 const FileSystemURL& url,
[email protected]7878ece2011-09-05 11:41:49481 int64 length) {
[email protected]ecdfd6c52012-04-11 13:35:44482 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33483 base::FilePath local_path;
[email protected]ecdfd6c52012-04-11 13:35:44484 base::PlatformFileError error =
[email protected]949f25a2012-06-27 01:53:09485 GetFileInfo(context, url, &file_info, &local_path);
[email protected]ecdfd6c52012-04-11 13:35:44486 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]95af7372012-05-28 07:51:20492 error = NativeFileUtil::Truncate(local_path, length);
[email protected]c4e6f9c2012-09-09 17:42:10493 if (error == base::PLATFORM_FILE_OK) {
[email protected]caf66702012-09-07 07:02:20494 UpdateUsage(context, url, growth);
[email protected]c4e6f9c2012-09-09 17:42:10495 context->change_observers()->Notify(
496 &FileChangeObserver::OnModifyFile, MakeTuple(url));
497 }
[email protected]ecdfd6c52012-04-11 13:35:44498 return error;
[email protected]7878ece2011-09-05 11:41:49499}
500
[email protected]7878ece2011-09-05 11:41:49501PlatformFileError ObfuscatedFileUtil::CopyOrMoveFile(
502 FileSystemOperationContext* context,
[email protected]949f25a2012-06-27 01:53:09503 const FileSystemURL& src_url,
504 const FileSystemURL& dest_url,
[email protected]7878ece2011-09-05 11:41:49505 bool copy) {
506 // Cross-filesystem copies and moves should be handled via CopyInForeignFile.
[email protected]949f25a2012-06-27 01:53:09507 DCHECK(src_url.origin() == dest_url.origin());
508 DCHECK(src_url.type() == dest_url.type());
[email protected]7878ece2011-09-05 11:41:49509
510 FileSystemDirectoryDatabase* db = GetDirectoryDatabase(
[email protected]949f25a2012-06-27 01:53:09511 src_url.origin(), src_url.type(), true);
[email protected]7878ece2011-09-05 11:41:49512 if (!db)
513 return base::PLATFORM_FILE_ERROR_FAILED;
[email protected]294dd0312012-05-11 07:35:13514
[email protected]7878ece2011-09-05 11:41:49515 FileId src_file_id;
[email protected]949f25a2012-06-27 01:53:09516 if (!db->GetFileWithPath(src_url.path(), &src_file_id))
[email protected]7878ece2011-09-05 11:41:49517 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
[email protected]294dd0312012-05-11 07:35:13518
[email protected]7878ece2011-09-05 11:41:49519 FileId dest_file_id;
[email protected]949f25a2012-06-27 01:53:09520 bool overwrite = db->GetFileWithPath(dest_url.path(),
[email protected]08f8feb2012-02-26 11:53:50521 &dest_file_id);
[email protected]294dd0312012-05-11 07:35:13522
[email protected]7878ece2011-09-05 11:41:49523 FileInfo src_file_info;
[email protected]294dd0312012-05-11 07:35:13524 base::PlatformFileInfo src_platform_file_info;
[email protected]a3ef4832013-02-02 05:12:33525 base::FilePath src_local_path;
[email protected]294dd0312012-05-11 07:35:13526 base::PlatformFileError error = GetFileInfoInternal(
[email protected]949f25a2012-06-27 01:53:09527 db, context, src_url.origin(), src_url.type(), src_file_id,
[email protected]95af7372012-05-28 07:51:20528 &src_file_info, &src_platform_file_info, &src_local_path);
[email protected]294dd0312012-05-11 07:35:13529 if (error != base::PLATFORM_FILE_OK)
530 return error;
531 if (src_file_info.is_directory())
[email protected]bab213be2013-01-23 15:13:08532 return base::PLATFORM_FILE_ERROR_NOT_A_FILE;
[email protected]294dd0312012-05-11 07:35:13533
534 FileInfo dest_file_info;
535 base::PlatformFileInfo dest_platform_file_info; // overwrite case only
[email protected]a3ef4832013-02-02 05:12:33536 base::FilePath dest_local_path; // overwrite case only
[email protected]7878ece2011-09-05 11:41:49537 if (overwrite) {
[email protected]294dd0312012-05-11 07:35:13538 base::PlatformFileError error = GetFileInfoInternal(
[email protected]949f25a2012-06-27 01:53:09539 db, context, dest_url.origin(), dest_url.type(), dest_file_id,
[email protected]95af7372012-05-28 07:51:20540 &dest_file_info, &dest_platform_file_info, &dest_local_path);
[email protected]294dd0312012-05-11 07:35:13541 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]bab213be2013-01-23 15:13:08546 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION;
[email protected]7878ece2011-09-05 11:41:49547 }
[email protected]294dd0312012-05-11 07:35:13548 if (!overwrite) {
549 FileId dest_parent_id;
[email protected]8a020f62013-02-18 08:05:44550 if (!db->GetFileWithPath(VirtualPath::DirName(dest_url.path()),
[email protected]294dd0312012-05-11 07:35:13551 &dest_parent_id)) {
[email protected]294dd0312012-05-11 07:35:13552 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]949f25a2012-06-27 01:53:09558 VirtualPath::BaseName(dest_url.path()).value();
[email protected]294dd0312012-05-11 07:35:13559 }
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]7878ece2011-09-05 11:41:49573 /*
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]294dd0312012-05-11 07:35:13587 error = base::PLATFORM_FILE_ERROR_FAILED;
[email protected]7878ece2011-09-05 11:41:49588 if (copy) {
[email protected]7878ece2011-09-05 11:41:49589 if (overwrite) {
[email protected]95af7372012-05-28 07:51:20590 error = NativeFileUtil::CopyOrMoveFile(
591 src_local_path,
592 dest_local_path,
[email protected]294dd0312012-05-11 07:35:13593 true /* copy */);
594 } else { // non-overwrite
[email protected]95af7372012-05-28 07:51:20595 error = CreateFile(context, src_local_path,
[email protected]949f25a2012-06-27 01:53:09596 dest_url.origin(), dest_url.type(),
[email protected]294dd0312012-05-11 07:35:13597 &dest_file_info, 0, NULL);
[email protected]7878ece2011-09-05 11:41:49598 }
[email protected]294dd0312012-05-11 07:35:13599 } else {
[email protected]7878ece2011-09-05 11:41:49600 if (overwrite) {
[email protected]294dd0312012-05-11 07:35:13601 if (db->OverwritingMoveFile(src_file_id, dest_file_id)) {
602 if (base::PLATFORM_FILE_OK !=
[email protected]95af7372012-05-28 07:51:20603 NativeFileUtil::DeleteFile(dest_local_path))
[email protected]294dd0312012-05-11 07:35:13604 LOG(WARNING) << "Leaked a backing file.";
605 error = base::PLATFORM_FILE_OK;
606 } else {
607 error = base::PLATFORM_FILE_ERROR_FAILED;
[email protected]7878ece2011-09-05 11:41:49608 }
[email protected]294dd0312012-05-11 07:35:13609 } 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]7878ece2011-09-05 11:41:49614 }
615 }
[email protected]294dd0312012-05-11 07:35:13616
617 if (error != base::PLATFORM_FILE_OK)
618 return error;
619
[email protected]c4e6f9c2012-09-09 17:42:10620 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]294dd0312012-05-11 07:35:13633 TouchDirectory(db, src_file_info.parent_id);
[email protected]c4e6f9c2012-09-09 17:42:10634 }
635
[email protected]294dd0312012-05-11 07:35:13636 TouchDirectory(db, dest_file_info.parent_id);
637
[email protected]caf66702012-09-07 07:02:20638 UpdateUsage(context, dest_url, growth);
[email protected]294dd0312012-05-11 07:35:13639 return error;
[email protected]7878ece2011-09-05 11:41:49640}
641
642PlatformFileError ObfuscatedFileUtil::CopyInForeignFile(
643 FileSystemOperationContext* context,
[email protected]a3ef4832013-02-02 05:12:33644 const base::FilePath& src_file_path,
[email protected]949f25a2012-06-27 01:53:09645 const FileSystemURL& dest_url) {
[email protected]7878ece2011-09-05 11:41:49646 FileSystemDirectoryDatabase* db = GetDirectoryDatabase(
[email protected]949f25a2012-06-27 01:53:09647 dest_url.origin(), dest_url.type(), true);
[email protected]7878ece2011-09-05 11:41:49648 if (!db)
649 return base::PLATFORM_FILE_ERROR_FAILED;
[email protected]294dd0312012-05-11 07:35:13650
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]7878ece2011-09-05 11:41:49655 FileId dest_file_id;
[email protected]949f25a2012-06-27 01:53:09656 bool overwrite = db->GetFileWithPath(dest_url.path(),
[email protected]08f8feb2012-02-26 11:53:50657 &dest_file_id);
[email protected]294dd0312012-05-11 07:35:13658
[email protected]7878ece2011-09-05 11:41:49659 FileInfo dest_file_info;
[email protected]294dd0312012-05-11 07:35:13660 base::PlatformFileInfo dest_platform_file_info; // overwrite case only
[email protected]7878ece2011-09-05 11:41:49661 if (overwrite) {
[email protected]a3ef4832013-02-02 05:12:33662 base::FilePath dest_local_path;
[email protected]294dd0312012-05-11 07:35:13663 base::PlatformFileError error = GetFileInfoInternal(
[email protected]949f25a2012-06-27 01:53:09664 db, context, dest_url.origin(), dest_url.type(), dest_file_id,
[email protected]95af7372012-05-28 07:51:20665 &dest_file_info, &dest_platform_file_info, &dest_local_path);
[email protected]294dd0312012-05-11 07:35:13666 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]bab213be2013-01-23 15:13:08671 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION;
[email protected]294dd0312012-05-11 07:35:13672 }
673 if (!overwrite) {
[email protected]7878ece2011-09-05 11:41:49674 FileId dest_parent_id;
[email protected]8a020f62013-02-18 08:05:44675 if (!db->GetFileWithPath(VirtualPath::DirName(dest_url.path()),
[email protected]bab213be2013-01-23 15:13:08676 &dest_parent_id)) {
[email protected]7878ece2011-09-05 11:41:49677 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
678 }
[email protected]bab213be2013-01-23 15:13:08679 if (!dest_file_info.is_directory())
680 return base::PLATFORM_FILE_ERROR_FAILED;
[email protected]7878ece2011-09-05 11:41:49681 InitFileInfo(&dest_file_info, dest_parent_id,
[email protected]949f25a2012-06-27 01:53:09682 VirtualPath::BaseName(dest_url.path()).value());
[email protected]7878ece2011-09-05 11:41:49683 }
[email protected]294dd0312012-05-11 07:35:13684
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]a3ef4832013-02-02 05:12:33695 base::FilePath dest_local_path = DataPathToLocalPath(
[email protected]949f25a2012-06-27 01:53:09696 dest_url.origin(), dest_url.type(), dest_file_info.data_path);
[email protected]95af7372012-05-28 07:51:20697 error = NativeFileUtil::CopyOrMoveFile(
698 src_file_path, dest_local_path, true);
[email protected]294dd0312012-05-11 07:35:13699 } else {
700 error = CreateFile(context, src_file_path,
[email protected]949f25a2012-06-27 01:53:09701 dest_url.origin(), dest_url.type(),
[email protected]294dd0312012-05-11 07:35:13702 &dest_file_info, 0, NULL);
703 }
704
705 if (error != base::PLATFORM_FILE_OK)
706 return error;
707
[email protected]c4e6f9c2012-09-09 17:42:10708 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]caf66702012-09-07 07:02:20716 UpdateUsage(context, dest_url, growth);
[email protected]294dd0312012-05-11 07:35:13717 TouchDirectory(db, dest_file_info.parent_id);
718 return base::PLATFORM_FILE_OK;
[email protected]7878ece2011-09-05 11:41:49719}
720
721PlatformFileError ObfuscatedFileUtil::DeleteFile(
722 FileSystemOperationContext* context,
[email protected]949f25a2012-06-27 01:53:09723 const FileSystemURL& url) {
[email protected]7878ece2011-09-05 11:41:49724 FileSystemDirectoryDatabase* db = GetDirectoryDatabase(
[email protected]949f25a2012-06-27 01:53:09725 url.origin(), url.type(), true);
[email protected]7878ece2011-09-05 11:41:49726 if (!db)
727 return base::PLATFORM_FILE_ERROR_FAILED;
728 FileId file_id;
[email protected]949f25a2012-06-27 01:53:09729 if (!db->GetFileWithPath(url.path(), &file_id))
[email protected]7878ece2011-09-05 11:41:49730 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
[email protected]294dd0312012-05-11 07:35:13731
[email protected]d4905e2e2011-05-13 21:56:32732 FileInfo file_info;
[email protected]294dd0312012-05-11 07:35:13733 base::PlatformFileInfo platform_file_info;
[email protected]a3ef4832013-02-02 05:12:33734 base::FilePath local_path;
[email protected]294dd0312012-05-11 07:35:13735 base::PlatformFileError error = GetFileInfoInternal(
[email protected]949f25a2012-06-27 01:53:09736 db, context, url.origin(), url.type(), file_id,
[email protected]95af7372012-05-28 07:51:20737 &file_info, &platform_file_info, &local_path);
[email protected]294dd0312012-05-11 07:35:13738 if (error != base::PLATFORM_FILE_ERROR_NOT_FOUND &&
739 error != base::PLATFORM_FILE_OK)
740 return error;
741
[email protected]92b808802013-01-28 05:10:51742 if (file_info.is_directory())
743 return base::PLATFORM_FILE_ERROR_NOT_A_FILE;
[email protected]294dd0312012-05-11 07:35:13744
745 int64 growth = -UsageForPath(file_info.name.size()) - platform_file_info.size;
746 AllocateQuota(context, growth);
[email protected]7878ece2011-09-05 11:41:49747 if (!db->RemoveFileInfo(file_id)) {
748 NOTREACHED();
749 return base::PLATFORM_FILE_ERROR_FAILED;
750 }
[email protected]caf66702012-09-07 07:02:20751 UpdateUsage(context, url, growth);
[email protected]fad625e2f2011-12-08 05:38:03752 TouchDirectory(db, file_info.parent_id);
[email protected]294dd0312012-05-11 07:35:13753
[email protected]c4e6f9c2012-09-09 17:42:10754 context->change_observers()->Notify(
755 &FileChangeObserver::OnRemoveFile, MakeTuple(url));
756
[email protected]294dd0312012-05-11 07:35:13757 if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND)
758 return base::PLATFORM_FILE_OK;
759
[email protected]95af7372012-05-28 07:51:20760 error = NativeFileUtil::DeleteFile(local_path);
[email protected]294dd0312012-05-11 07:35:13761 if (base::PLATFORM_FILE_OK != error)
762 LOG(WARNING) << "Leaked a backing file.";
[email protected]7878ece2011-09-05 11:41:49763 return base::PLATFORM_FILE_OK;
[email protected]d4905e2e2011-05-13 21:56:32764}
765
[email protected]bab213be2013-01-23 15:13:08766PlatformFileError ObfuscatedFileUtil::DeleteDirectory(
[email protected]7878ece2011-09-05 11:41:49767 FileSystemOperationContext* context,
[email protected]949f25a2012-06-27 01:53:09768 const FileSystemURL& url) {
[email protected]7878ece2011-09-05 11:41:49769 FileSystemDirectoryDatabase* db = GetDirectoryDatabase(
[email protected]949f25a2012-06-27 01:53:09770 url.origin(), url.type(), true);
[email protected]7878ece2011-09-05 11:41:49771 if (!db)
772 return base::PLATFORM_FILE_ERROR_FAILED;
[email protected]294dd0312012-05-11 07:35:13773
[email protected]7878ece2011-09-05 11:41:49774 FileId file_id;
[email protected]949f25a2012-06-27 01:53:09775 if (!db->GetFileWithPath(url.path(), &file_id))
[email protected]7878ece2011-09-05 11:41:49776 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
777 FileInfo file_info;
[email protected]bab213be2013-01-23 15:13:08778 if (!db->GetFileInfo(file_id, &file_info)) {
[email protected]7878ece2011-09-05 11:41:49779 NOTREACHED();
780 return base::PLATFORM_FILE_ERROR_FAILED;
781 }
[email protected]bab213be2013-01-23 15:13:08782 if (!file_info.is_directory())
783 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY;
[email protected]7878ece2011-09-05 11:41:49784 if (!db->RemoveFileInfo(file_id))
785 return base::PLATFORM_FILE_ERROR_NOT_EMPTY;
[email protected]f49dfc882012-04-05 15:02:06786 int64 growth = -UsageForPath(file_info.name.size());
787 AllocateQuota(context, growth);
[email protected]caf66702012-09-07 07:02:20788 UpdateUsage(context, url, growth);
[email protected]fad625e2f2011-12-08 05:38:03789 TouchDirectory(db, file_info.parent_id);
[email protected]c4e6f9c2012-09-09 17:42:10790 context->change_observers()->Notify(
791 &FileChangeObserver::OnRemoveDirectory, MakeTuple(url));
[email protected]7878ece2011-09-05 11:41:49792 return base::PLATFORM_FILE_OK;
793}
794
[email protected]7ab45cfa82013-04-26 07:13:20795webkit_blob::ScopedFile ObfuscatedFileUtil::CreateSnapshotFile(
[email protected]d0a1f0372012-07-19 11:17:37796 FileSystemOperationContext* context,
797 const FileSystemURL& url,
[email protected]7ab45cfa82013-04-26 07:13:20798 base::PlatformFileError* error,
[email protected]d0a1f0372012-07-19 11:17:37799 base::PlatformFileInfo* file_info,
[email protected]7ab45cfa82013-04-26 07:13:20800 base::FilePath* platform_path) {
[email protected]826df822012-08-04 01:23:56801 // We're just returning the local file information.
[email protected]7ab45cfa82013-04-26 07:13:20802 *error = GetFileInfo(context, url, file_info, platform_path);
803 if (*error == base::PLATFORM_FILE_OK && file_info->is_directory) {
[email protected]aeba9c12013-01-28 10:44:42804 *file_info = base::PlatformFileInfo();
[email protected]7ab45cfa82013-04-26 07:13:20805 *error = base::PLATFORM_FILE_ERROR_NOT_A_FILE;
[email protected]aeba9c12013-01-28 10:44:42806 }
[email protected]7ab45cfa82013-04-26 07:13:20807 return webkit_blob::ScopedFile();
[email protected]bab213be2013-01-23 15:13:08808}
809
[email protected]5453ca052013-04-11 21:15:39810scoped_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]bab213be2013-01-23 15:13:08824bool 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]d0a1f0372012-07-19 11:17:37847}
848
[email protected]a3ef4832013-02-02 05:12:33849base::FilePath ObfuscatedFileUtil::GetDirectoryForOriginAndType(
[email protected]d247b842012-05-08 06:43:36850 const GURL& origin,
851 FileSystemType type,
852 bool create,
853 base::PlatformFileError* error_code) {
[email protected]a3ef4832013-02-02 05:12:33854 base::FilePath origin_dir = GetDirectoryForOrigin(origin, create, error_code);
[email protected]c7a4a10f2011-05-19 04:56:06855 if (origin_dir.empty())
[email protected]a3ef4832013-02-02 05:12:33856 return base::FilePath();
857 base::FilePath::StringType type_string = GetDirectoryNameForType(type);
[email protected]c7a4a10f2011-05-19 04:56:06858 if (type_string.empty()) {
859 LOG(WARNING) << "Unknown filesystem type requested:" << type;
[email protected]d247b842012-05-08 06:43:36860
861 if (error_code)
862 *error_code = base::PLATFORM_FILE_ERROR_INVALID_URL;
[email protected]a3ef4832013-02-02 05:12:33863 return base::FilePath();
[email protected]c7a4a10f2011-05-19 04:56:06864 }
[email protected]a3ef4832013-02-02 05:12:33865 base::FilePath path = origin_dir.Append(type_string);
[email protected]bf48a1f2012-07-11 09:35:03866 base::PlatformFileError error = base::PLATFORM_FILE_OK;
[email protected]fcc2d5f2011-05-23 22:06:26867 if (!file_util::DirectoryExists(path) &&
[email protected]d247b842012-05-08 06:43:36868 (!create || !file_util::CreateDirectory(path))) {
[email protected]bf48a1f2012-07-11 09:35:03869 error = create ?
[email protected]d247b842012-05-08 06:43:36870 base::PLATFORM_FILE_ERROR_FAILED :
871 base::PLATFORM_FILE_ERROR_NOT_FOUND;
[email protected]d247b842012-05-08 06:43:36872 }
873
874 if (error_code)
[email protected]bf48a1f2012-07-11 09:35:03875 *error_code = error;
[email protected]fcc2d5f2011-05-23 22:06:26876 return path;
[email protected]c7a4a10f2011-05-19 04:56:06877}
878
[email protected]7878ece2011-09-05 11:41:49879bool ObfuscatedFileUtil::DeleteDirectoryForOriginAndType(
[email protected]6ff17052011-07-12 06:06:07880 const GURL& origin, FileSystemType type) {
[email protected]bf48a1f2012-07-11 09:35:03881 base::PlatformFileError error = base::PLATFORM_FILE_OK;
[email protected]a3ef4832013-02-02 05:12:33882 base::FilePath origin_type_path = GetDirectoryForOriginAndType(origin, type, false,
[email protected]bf48a1f2012-07-11 09:35:03883 &error);
884 if (origin_type_path.empty())
[email protected]6ff17052011-07-12 06:06:07885 return true;
886
[email protected]bf48a1f2012-07-11 09:35:03887 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]c0229d02011-07-14 06:54:18897
[email protected]8a020f62013-02-18 08:05:44898 base::FilePath origin_path = VirtualPath::DirName(origin_type_path);
[email protected]d247b842012-05-08 06:43:36899 DCHECK_EQ(origin_path.value(),
900 GetDirectoryForOrigin(origin, false, NULL).value());
[email protected]c0229d02011-07-14 06:54:18901
[email protected]69fe5a72012-09-13 05:05:16902 // 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]69fe5a72012-09-13 05:05:16904 // 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]c0229d02011-07-14 06:54:18919 }
920
[email protected]69fe5a72012-09-13 05:05:16921 // 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]c0229d02011-07-14 06:54:18928 return true;
[email protected]6ff17052011-07-12 06:06:07929}
930
[email protected]fcc2d5f2011-05-23 22:06:26931// static
[email protected]a3ef4832013-02-02 05:12:33932base::FilePath::StringType ObfuscatedFileUtil::GetDirectoryNameForType(
[email protected]fcc2d5f2011-05-23 22:06:26933 FileSystemType type) {
[email protected]6b931152011-05-20 21:02:35934 switch (type) {
935 case kFileSystemTypeTemporary:
936 return kTemporaryDirectoryName;
937 case kFileSystemTypePersistent:
938 return kPersistentDirectoryName;
[email protected]69fe5a72012-09-13 05:05:16939 case kFileSystemTypeSyncable:
940 return kSyncableDirectoryName;
[email protected]6b931152011-05-20 21:02:35941 case kFileSystemTypeUnknown:
942 default:
[email protected]a3ef4832013-02-02 05:12:33943 return base::FilePath::StringType();
[email protected]6b931152011-05-20 21:02:35944 }
945}
946
[email protected]7878ece2011-09-05 11:41:49947ObfuscatedFileUtil::AbstractOriginEnumerator*
948ObfuscatedFileUtil::CreateOriginEnumerator() {
949 std::vector<FileSystemOriginDatabase::OriginRecord> origins;
950
951 InitOriginDatabase(false);
952 return new ObfuscatedOriginEnumerator(
953 origin_database_.get(), file_system_directory_);
954}
955
956bool ObfuscatedFileUtil::DestroyDirectoryDatabase(
957 const GURL& origin, FileSystemType type) {
[email protected]e7e46732012-01-05 11:45:55958 std::string type_string = GetFileSystemTypeString(type);
[email protected]7878ece2011-09-05 11:41:49959 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]bf48a1f2012-07-11 09:35:03971 PlatformFileError error = base::PLATFORM_FILE_OK;
[email protected]a3ef4832013-02-02 05:12:33972 base::FilePath path = GetDirectoryForOriginAndType(origin, type, false, &error);
[email protected]bf48a1f2012-07-11 09:35:03973 if (path.empty() || error == base::PLATFORM_FILE_ERROR_NOT_FOUND)
[email protected]7878ece2011-09-05 11:41:49974 return true;
[email protected]7878ece2011-09-05 11:41:49975 return FileSystemDirectoryDatabase::DestroyDatabase(path);
976}
977
978// static
[email protected]a3ef4832013-02-02 05:12:33979int64 ObfuscatedFileUtil::ComputeFilePathCost(const base::FilePath& path) {
[email protected]f49dfc882012-04-05 15:02:06980 return UsageForPath(VirtualPath::BaseName(path).value().size());
[email protected]7878ece2011-09-05 11:41:49981}
982
983PlatformFileError ObfuscatedFileUtil::GetFileInfoInternal(
984 FileSystemDirectoryDatabase* db,
985 FileSystemOperationContext* context,
[email protected]08f8feb2012-02-26 11:53:50986 const GURL& origin,
987 FileSystemType type,
[email protected]7878ece2011-09-05 11:41:49988 FileId file_id,
989 FileInfo* local_info,
990 base::PlatformFileInfo* file_info,
[email protected]a3ef4832013-02-02 05:12:33991 base::FilePath* platform_file_path) {
[email protected]7878ece2011-09-05 11:41:49992 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]294dd0312012-05-11 07:35:131003 file_info->size = 0;
[email protected]7878ece2011-09-05 11:41:491004 file_info->is_directory = true;
1005 file_info->is_symbolic_link = false;
1006 file_info->last_modified = local_info->modification_time;
[email protected]a3ef4832013-02-02 05:12:331007 *platform_file_path = base::FilePath();
[email protected]7878ece2011-09-05 11:41:491008 // 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]a3ef4832013-02-02 05:12:331013 base::FilePath local_path = DataPathToLocalPath(
[email protected]08f8feb2012-02-26 11:53:501014 origin, type, local_info->data_path);
[email protected]95af7372012-05-28 07:51:201015 base::PlatformFileError error = NativeFileUtil::GetFileInfo(
1016 local_path, file_info);
[email protected]272a9d12012-06-19 02:17:551017 // 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]95af7372012-05-28 07:51:201022 if (error == base::PLATFORM_FILE_OK) {
1023 *platform_file_path = local_path;
1024 } else if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) {
[email protected]294dd0312012-05-11 07:35:131025 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]7878ece2011-09-05 11:41:491031}
1032
1033PlatformFileError ObfuscatedFileUtil::CreateFile(
1034 FileSystemOperationContext* context,
[email protected]a3ef4832013-02-02 05:12:331035 const base::FilePath& src_file_path,
[email protected]08f8feb2012-02-26 11:53:501036 const GURL& dest_origin,
1037 FileSystemType dest_type,
1038 FileInfo* dest_file_info, int file_flags, PlatformFile* handle) {
[email protected]7878ece2011-09-05 11:41:491039 if (handle)
1040 *handle = base::kInvalidPlatformFileValue;
1041 FileSystemDirectoryDatabase* db = GetDirectoryDatabase(
[email protected]08f8feb2012-02-26 11:53:501042 dest_origin, dest_type, true);
[email protected]7878ece2011-09-05 11:41:491043
[email protected]bf48a1f2012-07-11 09:35:031044 PlatformFileError error = base::PLATFORM_FILE_OK;
[email protected]a3ef4832013-02-02 05:12:331045 base::FilePath root = GetDirectoryForOriginAndType(dest_origin, dest_type, false,
[email protected]bf48a1f2012-07-11 09:35:031046 &error);
1047 if (error != base::PLATFORM_FILE_OK)
1048 return error;
[email protected]95af7372012-05-28 07:51:201049
[email protected]a3ef4832013-02-02 05:12:331050 base::FilePath dest_local_path;
[email protected]bf48a1f2012-07-11 09:35:031051 error = GenerateNewLocalPath(db, context, dest_origin, dest_type,
1052 &dest_local_path);
[email protected]294dd0312012-05-11 07:35:131053 if (error != base::PLATFORM_FILE_OK)
[email protected]7878ece2011-09-05 11:41:491054 return error;
[email protected]08f8feb2012-02-26 11:53:501055
[email protected]7878ece2011-09-05 11:41:491056 bool created = false;
[email protected]294dd0312012-05-11 07:35:131057 if (!src_file_path.empty()) {
[email protected]7878ece2011-09-05 11:41:491058 DCHECK(!file_flags);
1059 DCHECK(!handle);
[email protected]95af7372012-05-28 07:51:201060 error = NativeFileUtil::CopyOrMoveFile(
1061 src_file_path, dest_local_path, true /* copy */);
[email protected]7878ece2011-09-05 11:41:491062 created = true;
1063 } else {
[email protected]95af7372012-05-28 07:51:201064 if (file_util::PathExists(dest_local_path)) {
1065 if (!file_util::Delete(dest_local_path, true /* recursive */)) {
[email protected]7878ece2011-09-05 11:41:491066 NOTREACHED();
1067 return base::PLATFORM_FILE_ERROR_FAILED;
1068 }
1069 LOG(WARNING) << "A stray file detected";
[email protected]294dd0312012-05-11 07:35:131070 InvalidateUsageCache(context, dest_origin, dest_type);
[email protected]7878ece2011-09-05 11:41:491071 }
1072
1073 if (handle) {
[email protected]95af7372012-05-28 07:51:201074 error = NativeFileUtil::CreateOrOpen(
1075 dest_local_path, file_flags, handle, &created);
[email protected]7878ece2011-09-05 11:41:491076 // 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]95af7372012-05-28 07:51:201079 error = NativeFileUtil::EnsureFileExists(dest_local_path, &created);
[email protected]7878ece2011-09-05 11:41:491080 }
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]95af7372012-05-28 07:51:201090 file_util::Delete(dest_local_path, false /* recursive */);
[email protected]7878ece2011-09-05 11:41:491091 }
1092 return base::PLATFORM_FILE_ERROR_FAILED;
1093 }
[email protected]95af7372012-05-28 07:51:201094
1095 // This removes the root, including the trailing slash, leaving a relative
1096 // path.
[email protected]a3ef4832013-02-02 05:12:331097 dest_file_info->data_path = base::FilePath(
[email protected]95af7372012-05-28 07:51:201098 dest_local_path.value().substr(root.value().length() + 1));
1099
[email protected]7878ece2011-09-05 11:41:491100 FileId file_id;
[email protected]08f8feb2012-02-26 11:53:501101 if (!db->AddFileInfo(*dest_file_info, &file_id)) {
[email protected]7878ece2011-09-05 11:41:491102 if (handle) {
1103 DCHECK_NE(base::kInvalidPlatformFileValue, *handle);
1104 base::ClosePlatformFile(*handle);
1105 }
[email protected]95af7372012-05-28 07:51:201106 file_util::Delete(dest_local_path, false /* recursive */);
[email protected]7878ece2011-09-05 11:41:491107 return base::PLATFORM_FILE_ERROR_FAILED;
1108 }
[email protected]08f8feb2012-02-26 11:53:501109 TouchDirectory(db, dest_file_info->parent_id);
[email protected]7878ece2011-09-05 11:41:491110
1111 return base::PLATFORM_FILE_OK;
1112}
1113
[email protected]a3ef4832013-02-02 05:12:331114base::FilePath ObfuscatedFileUtil::DataPathToLocalPath(
1115 const GURL& origin, FileSystemType type, const base::FilePath& data_path) {
[email protected]bf48a1f2012-07-11 09:35:031116 PlatformFileError error = base::PLATFORM_FILE_OK;
[email protected]a3ef4832013-02-02 05:12:331117 base::FilePath root = GetDirectoryForOriginAndType(origin, type, false, &error);
[email protected]bf48a1f2012-07-11 09:35:031118 if (error != base::PLATFORM_FILE_OK)
[email protected]a3ef4832013-02-02 05:12:331119 return base::FilePath();
[email protected]95af7372012-05-28 07:51:201120 return root.Append(data_path);
[email protected]6b931152011-05-20 21:02:351121}
1122
[email protected]b9566e7c2012-10-29 10:57:461123// 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]c7a4a10f2011-05-19 04:56:061126// Still doesn't answer the quota issue, though.
[email protected]7878ece2011-09-05 11:41:491127FileSystemDirectoryDatabase* ObfuscatedFileUtil::GetDirectoryDatabase(
[email protected]4b4d53bd2011-07-08 07:26:051128 const GURL& origin, FileSystemType type, bool create) {
[email protected]e7e46732012-01-05 11:45:551129 std::string type_string = GetFileSystemTypeString(type);
[email protected]d4905e2e2011-05-13 21:56:321130 if (type_string.empty()) {
1131 LOG(WARNING) << "Unknown filesystem type requested:" << type;
1132 return NULL;
1133 }
[email protected]fcc2d5f2011-05-23 22:06:261134 std::string key = GetOriginIdentifierFromURL(origin) + type_string;
[email protected]d4905e2e2011-05-13 21:56:321135 DirectoryMap::iterator iter = directories_.find(key);
[email protected]4b4d53bd2011-07-08 07:26:051136 if (iter != directories_.end()) {
1137 MarkUsed();
[email protected]d4905e2e2011-05-13 21:56:321138 return iter->second;
[email protected]4b4d53bd2011-07-08 07:26:051139 }
[email protected]d4905e2e2011-05-13 21:56:321140
[email protected]bf48a1f2012-07-11 09:35:031141 PlatformFileError error = base::PLATFORM_FILE_OK;
[email protected]a3ef4832013-02-02 05:12:331142 base::FilePath path = GetDirectoryForOriginAndType(origin, type, create, &error);
[email protected]bf48a1f2012-07-11 09:35:031143 if (error != base::PLATFORM_FILE_OK) {
1144 LOG(WARNING) << "Failed to get origin+type directory: " << path.value();
[email protected]c7a4a10f2011-05-19 04:56:061145 return NULL;
[email protected]d4905e2e2011-05-13 21:56:321146 }
[email protected]4b4d53bd2011-07-08 07:26:051147 MarkUsed();
[email protected]d4905e2e2011-05-13 21:56:321148 FileSystemDirectoryDatabase* database = new FileSystemDirectoryDatabase(path);
1149 directories_[key] = database;
1150 return database;
1151}
1152
[email protected]a3ef4832013-02-02 05:12:331153base::FilePath ObfuscatedFileUtil::GetDirectoryForOrigin(
[email protected]d247b842012-05-08 06:43:361154 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]a3ef4832013-02-02 05:12:331161 return base::FilePath();
[email protected]d247b842012-05-08 06:43:361162 }
[email protected]a3ef4832013-02-02 05:12:331163 base::FilePath directory_name;
[email protected]83c0d3c2011-07-15 03:36:301164 std::string id = GetOriginIdentifierFromURL(origin);
[email protected]34583332011-08-31 08:59:471165
1166 bool exists_in_db = origin_database_->HasOriginPath(id);
[email protected]d247b842012-05-08 06:43:361167 if (!exists_in_db && !create) {
1168 if (error_code)
1169 *error_code = base::PLATFORM_FILE_ERROR_NOT_FOUND;
[email protected]a3ef4832013-02-02 05:12:331170 return base::FilePath();
[email protected]d247b842012-05-08 06:43:361171 }
1172 if (!origin_database_->GetPathForOrigin(id, &directory_name)) {
1173 if (error_code)
1174 *error_code = base::PLATFORM_FILE_ERROR_FAILED;
[email protected]a3ef4832013-02-02 05:12:331175 return base::FilePath();
[email protected]d247b842012-05-08 06:43:361176 }
[email protected]34583332011-08-31 08:59:471177
[email protected]a3ef4832013-02-02 05:12:331178 base::FilePath path = file_system_directory_.Append(directory_name);
[email protected]34583332011-08-31 08:59:471179 bool exists_in_fs = file_util::DirectoryExists(path);
1180 if (!exists_in_db && exists_in_fs) {
[email protected]d247b842012-05-08 06:43:361181 if (!file_util::Delete(path, true)) {
1182 if (error_code)
1183 *error_code = base::PLATFORM_FILE_ERROR_FAILED;
[email protected]a3ef4832013-02-02 05:12:331184 return base::FilePath();
[email protected]d247b842012-05-08 06:43:361185 }
[email protected]34583332011-08-31 08:59:471186 exists_in_fs = false;
1187 }
1188
1189 if (!exists_in_fs) {
[email protected]d247b842012-05-08 06:43:361190 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]a3ef4832013-02-02 05:12:331195 return base::FilePath();
[email protected]d247b842012-05-08 06:43:361196 }
[email protected]34583332011-08-31 08:59:471197 }
1198
[email protected]d247b842012-05-08 06:43:361199 if (error_code)
1200 *error_code = base::PLATFORM_FILE_OK;
1201
[email protected]83c0d3c2011-07-15 03:36:301202 return path;
1203}
1204
[email protected]294dd0312012-05-11 07:35:131205void 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]7878ece2011-09-05 11:41:491213void ObfuscatedFileUtil::MarkUsed() {
[email protected]0a7328532011-05-13 23:54:431214 if (timer_.IsRunning())
1215 timer_.Reset();
1216 else
[email protected]d323a172011-09-02 18:23:021217 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(kFlushDelaySeconds),
[email protected]7878ece2011-09-05 11:41:491218 this, &ObfuscatedFileUtil::DropDatabases);
[email protected]0a7328532011-05-13 23:54:431219}
1220
[email protected]7878ece2011-09-05 11:41:491221void ObfuscatedFileUtil::DropDatabases() {
[email protected]d4905e2e2011-05-13 21:56:321222 origin_database_.reset();
1223 STLDeleteContainerPairSecondPointers(
1224 directories_.begin(), directories_.end());
1225 directories_.clear();
1226}
1227
[email protected]7878ece2011-09-05 11:41:491228bool ObfuscatedFileUtil::InitOriginDatabase(bool create) {
[email protected]fcc2d5f2011-05-23 22:06:261229 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]b9698772012-03-29 16:45:481238 new FileSystemOriginDatabase(file_system_directory_));
[email protected]fcc2d5f2011-05-23 22:06:261239 }
1240 return true;
1241}
1242
[email protected]95af7372012-05-28 07:51:201243PlatformFileError ObfuscatedFileUtil::GenerateNewLocalPath(
[email protected]294dd0312012-05-11 07:35:131244 FileSystemDirectoryDatabase* db,
1245 FileSystemOperationContext* context,
1246 const GURL& origin,
1247 FileSystemType type,
[email protected]a3ef4832013-02-02 05:12:331248 base::FilePath* local_path) {
[email protected]95af7372012-05-28 07:51:201249 DCHECK(local_path);
[email protected]294dd0312012-05-11 07:35:131250 int64 number;
1251 if (!db || !db->GetNextInteger(&number))
1252 return base::PLATFORM_FILE_ERROR_FAILED;
1253
[email protected]bf48a1f2012-07-11 09:35:031254 PlatformFileError error = base::PLATFORM_FILE_OK;
[email protected]a3ef4832013-02-02 05:12:331255 base::FilePath new_local_path = GetDirectoryForOriginAndType(origin, type,
1256 false, &error);
[email protected]bf48a1f2012-07-11 09:35:031257 if (error != base::PLATFORM_FILE_OK)
[email protected]294dd0312012-05-11 07:35:131258 return base::PLATFORM_FILE_ERROR_FAILED;
1259
[email protected]bf48a1f2012-07-11 09:35:031260 // We use the third- and fourth-to-last digits as the directory.
1261 int64 directory_number = number % 10000 / 100;
[email protected]95af7372012-05-28 07:51:201262 new_local_path = new_local_path.AppendASCII(
[email protected]7d3cbc92013-03-18 22:33:041263 base::StringPrintf("%02" PRId64, directory_number));
[email protected]294dd0312012-05-11 07:35:131264
[email protected]bf48a1f2012-07-11 09:35:031265 error = NativeFileUtil::CreateDirectory(
[email protected]95af7372012-05-28 07:51:201266 new_local_path, false /* exclusive */, false /* recursive */);
[email protected]294dd0312012-05-11 07:35:131267 if (error != base::PLATFORM_FILE_OK)
1268 return error;
1269
[email protected]7d3cbc92013-03-18 22:33:041270 *local_path =
1271 new_local_path.AppendASCII(base::StringPrintf("%08" PRId64, number));
[email protected]294dd0312012-05-11 07:35:131272 return base::PLATFORM_FILE_OK;
1273}
1274
[email protected]f7ac94e2013-04-30 08:34:501275PlatformFileError 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]d4905e2e2011-05-13 21:56:321357} // namespace fileapi