OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "webkit/fileapi/obfuscated_file_system_file_util.h" | |
6 | |
7 #include <queue> | |
8 #include <vector> | |
9 | |
10 #include "base/file_util.h" | |
11 #include "base/format_macros.h" | |
12 #include "base/logging.h" | |
13 #include "base/message_loop.h" | |
14 #include "base/stl_util.h" | |
15 #include "base/string_number_conversions.h" | |
16 #include "base/stringprintf.h" | |
17 #include "base/sys_string_conversions.h" | |
18 #include "googleurl/src/gurl.h" | |
19 #include "webkit/fileapi/file_system_context.h" | |
20 #include "webkit/fileapi/file_system_operation_context.h" | |
21 #include "webkit/fileapi/file_system_path_manager.h" | |
22 #include "webkit/fileapi/file_system_quota_util.h" | |
23 #include "webkit/fileapi/file_system_util.h" | |
24 #include "webkit/fileapi/sandbox_mount_point_provider.h" | |
25 #include "webkit/quota/quota_manager.h" | |
26 | |
27 namespace { | |
28 | |
29 const int64 kFlushDelaySeconds = 10 * 60; // 10 minutes | |
30 | |
31 const char kOriginDatabaseName[] = "Origins"; | |
32 const char kDirectoryDatabaseName[] = "Paths"; | |
33 | |
34 void InitFileInfo( | |
35 fileapi::FileSystemDirectoryDatabase::FileInfo* file_info, | |
36 fileapi::FileSystemDirectoryDatabase::FileId parent_id, | |
37 const FilePath::StringType& file_name) { | |
38 DCHECK(file_info); | |
39 file_info->parent_id = parent_id; | |
40 file_info->name = file_name; | |
41 } | |
42 | |
43 bool IsRootDirectory(const FilePath& virtual_path) { | |
44 return (virtual_path.empty() || | |
45 virtual_path.value() == FILE_PATH_LITERAL("/")); | |
46 } | |
47 | |
48 // Costs computed as per crbug.com/86114, based on the LevelDB implementation of | |
49 // path storage under Linux. It's not clear if that will differ on Windows, on | |
50 // which FilePath uses wide chars [since they're converted to UTF-8 for storage | |
51 // anyway], but as long as the cost is high enough that one can't cheat on quota | |
52 // by storing data in paths, it doesn't need to be all that accurate. | |
53 const int64 kPathCreationQuotaCost = 146; // Bytes per inode, basically. | |
54 const int64 kPathByteQuotaCost = 2; // Bytes per byte of path length in UTF-8. | |
55 | |
56 int64 GetPathQuotaUsage( | |
57 int growth_in_number_of_paths, | |
58 int64 growth_in_bytes_of_path_length) { | |
59 return growth_in_number_of_paths * kPathCreationQuotaCost + | |
60 growth_in_bytes_of_path_length * kPathByteQuotaCost; | |
61 } | |
62 | |
63 bool AllocateQuotaForPath( | |
64 fileapi::FileSystemOperationContext* context, | |
65 int growth_in_number_of_paths, | |
66 int64 growth_in_bytes_of_path_length) { | |
67 int64 growth = GetPathQuotaUsage(growth_in_number_of_paths, | |
68 growth_in_bytes_of_path_length); | |
69 int64 new_quota = context->allowed_bytes_growth() - growth; | |
70 | |
71 if (growth <= 0 || new_quota >= 0) { | |
72 context->set_allowed_bytes_growth(new_quota); | |
73 return true; | |
74 } | |
75 return false; | |
76 } | |
77 | |
78 void UpdatePathQuotaUsage( | |
79 fileapi::FileSystemOperationContext* context, | |
80 const GURL& origin_url, | |
81 fileapi::FileSystemType type, | |
82 int growth_in_number_of_paths, // -1, 0, or 1 | |
83 int64 growth_in_bytes_of_path_length) { | |
84 int64 growth = GetPathQuotaUsage(growth_in_number_of_paths, | |
85 growth_in_bytes_of_path_length); | |
86 fileapi::FileSystemQuotaUtil* quota_util = | |
87 context->file_system_context()->GetQuotaUtil(type); | |
88 quota::QuotaManagerProxy* quota_manager_proxy = | |
89 context->file_system_context()->quota_manager_proxy(); | |
90 quota_util->UpdateOriginUsageOnFileThread(quota_manager_proxy, origin_url, | |
91 type, growth); | |
92 } | |
93 | |
94 const FilePath::CharType kLegacyDataDirectory[] = FILE_PATH_LITERAL("Legacy"); | |
95 | |
96 const FilePath::CharType kTemporaryDirectoryName[] = FILE_PATH_LITERAL("t"); | |
97 const FilePath::CharType kPersistentDirectoryName[] = FILE_PATH_LITERAL("p"); | |
98 | |
99 } // namespace | |
100 | |
101 namespace fileapi { | |
102 | |
103 using base::PlatformFile; | |
104 using base::PlatformFileError; | |
105 | |
106 ObfuscatedFileSystemFileUtil::ObfuscatedFileSystemFileUtil( | |
107 const FilePath& file_system_directory, | |
108 FileSystemFileUtil* underlying_file_util) | |
109 : file_system_directory_(file_system_directory), | |
110 underlying_file_util_(underlying_file_util) { | |
111 } | |
112 | |
113 ObfuscatedFileSystemFileUtil::~ObfuscatedFileSystemFileUtil() { | |
114 DropDatabases(); | |
115 } | |
116 | |
117 PlatformFileError ObfuscatedFileSystemFileUtil::CreateOrOpen( | |
118 FileSystemOperationContext* context, | |
119 const FilePath& virtual_path, int file_flags, | |
120 PlatformFile* file_handle, bool* created) { | |
121 DCHECK(!(file_flags & (base::PLATFORM_FILE_DELETE_ON_CLOSE | | |
122 base::PLATFORM_FILE_HIDDEN | base::PLATFORM_FILE_EXCLUSIVE_READ | | |
123 base::PLATFORM_FILE_EXCLUSIVE_WRITE))); | |
124 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
125 context->src_origin_url(), context->src_type(), true); | |
126 if (!db) | |
127 return base::PLATFORM_FILE_ERROR_FAILED; | |
128 FileId file_id; | |
129 if (!db->GetFileWithPath(virtual_path, &file_id)) { | |
130 // The file doesn't exist. | |
131 if (!(file_flags & (base::PLATFORM_FILE_CREATE | | |
132 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_OPEN_ALWAYS))) | |
133 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
134 FileId parent_id; | |
135 if (!db->GetFileWithPath(virtual_path.DirName(), &parent_id)) | |
136 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
137 FileInfo file_info; | |
138 InitFileInfo(&file_info, parent_id, virtual_path.BaseName().value()); | |
139 if (!AllocateQuotaForPath(context, 1, file_info.name.size())) | |
140 return base::PLATFORM_FILE_ERROR_NO_SPACE; | |
141 PlatformFileError error = CreateFile( | |
142 context, context->src_origin_url(), context->src_type(), FilePath(), | |
143 &file_info, file_flags, file_handle); | |
144 if (created && base::PLATFORM_FILE_OK == error) | |
145 *created = true; | |
146 return error; | |
147 } | |
148 if (file_flags & base::PLATFORM_FILE_CREATE) | |
149 return base::PLATFORM_FILE_ERROR_EXISTS; | |
150 | |
151 FileInfo file_info; | |
152 if (!db->GetFileInfo(file_id, &file_info)) { | |
153 NOTREACHED(); | |
154 return base::PLATFORM_FILE_ERROR_FAILED; | |
155 } | |
156 if (file_info.is_directory()) | |
157 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; | |
158 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), | |
159 context->src_type(), file_info.data_path); | |
160 return underlying_file_util_->CreateOrOpen( | |
161 context, data_path, file_flags, file_handle, created); | |
162 } | |
163 | |
164 PlatformFileError ObfuscatedFileSystemFileUtil::EnsureFileExists( | |
165 FileSystemOperationContext* context, | |
166 const FilePath& virtual_path, | |
167 bool* created) { | |
168 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
169 context->src_origin_url(), context->src_type(), true); | |
170 if (!db) | |
171 return base::PLATFORM_FILE_ERROR_FAILED; | |
172 FileId file_id; | |
173 if (db->GetFileWithPath(virtual_path, &file_id)) { | |
174 FileInfo file_info; | |
175 if (!db->GetFileInfo(file_id, &file_info)) { | |
176 NOTREACHED(); | |
177 return base::PLATFORM_FILE_ERROR_FAILED; | |
178 } | |
179 if (file_info.is_directory()) | |
180 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; | |
181 if (created) | |
182 *created = false; | |
183 return base::PLATFORM_FILE_OK; | |
184 } | |
185 FileId parent_id; | |
186 if (!db->GetFileWithPath(virtual_path.DirName(), &parent_id)) | |
187 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
188 | |
189 FileInfo file_info; | |
190 InitFileInfo(&file_info, parent_id, virtual_path.BaseName().value()); | |
191 if (!AllocateQuotaForPath(context, 1, file_info.name.size())) | |
192 return base::PLATFORM_FILE_ERROR_NO_SPACE; | |
193 PlatformFileError error = CreateFile(context, context->src_origin_url(), | |
194 context->src_type(), FilePath(), &file_info, 0, NULL); | |
195 if (created && base::PLATFORM_FILE_OK == error) | |
196 *created = true; | |
197 return error; | |
198 } | |
199 | |
200 PlatformFileError ObfuscatedFileSystemFileUtil::GetLocalFilePath( | |
201 FileSystemOperationContext* context, | |
202 const FilePath& virtual_path, | |
203 FilePath* local_path) { | |
204 FilePath path = | |
205 GetLocalPath(context->src_origin_url(), context->src_type(), | |
206 virtual_path); | |
207 if (path.empty()) | |
208 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
209 | |
210 *local_path = path; | |
211 return base::PLATFORM_FILE_OK; | |
212 } | |
213 | |
214 PlatformFileError ObfuscatedFileSystemFileUtil::GetFileInfo( | |
215 FileSystemOperationContext* context, | |
216 const FilePath& virtual_path, | |
217 base::PlatformFileInfo* file_info, | |
218 FilePath* platform_file_path) { | |
219 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
220 context->src_origin_url(), context->src_type(), false); | |
221 if (!db) | |
222 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
223 FileId file_id; | |
224 if (!db->GetFileWithPath(virtual_path, &file_id)) | |
225 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
226 FileInfo local_info; | |
227 return GetFileInfoInternal(db, context, file_id, | |
228 &local_info, file_info, platform_file_path); | |
229 } | |
230 | |
231 PlatformFileError ObfuscatedFileSystemFileUtil::ReadDirectory( | |
232 FileSystemOperationContext* context, | |
233 const FilePath& virtual_path, | |
234 std::vector<base::FileUtilProxy::Entry>* entries) { | |
235 // TODO(kkanetkar): Implement directory read in multiple chunks. | |
236 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
237 context->src_origin_url(), context->src_type(), false); | |
238 if (!db) { | |
239 if (IsRootDirectory(virtual_path)) { | |
240 // It's the root directory and the database hasn't been initialized yet. | |
241 entries->clear(); | |
242 return base::PLATFORM_FILE_OK; | |
243 } | |
244 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
245 } | |
246 FileId file_id; | |
247 if (!db->GetFileWithPath(virtual_path, &file_id)) | |
248 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
249 FileInfo file_info; | |
250 if (!db->GetFileInfo(file_id, &file_info)) { | |
251 DCHECK(!file_id); | |
252 // It's the root directory and the database hasn't been initialized yet. | |
253 entries->clear(); | |
254 return base::PLATFORM_FILE_OK; | |
255 } | |
256 if (!file_info.is_directory()) | |
257 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
258 std::vector<FileId> children; | |
259 if (!db->ListChildren(file_id, &children)) { | |
260 NOTREACHED(); | |
261 return base::PLATFORM_FILE_ERROR_FAILED; | |
262 } | |
263 std::vector<FileId>::iterator iter; | |
264 for (iter = children.begin(); iter != children.end(); ++iter) { | |
265 base::PlatformFileInfo platform_file_info; | |
266 FilePath file_path; | |
267 if (GetFileInfoInternal(db, context, *iter, | |
268 &file_info, &platform_file_info, &file_path) != | |
269 base::PLATFORM_FILE_OK) { | |
270 NOTREACHED(); | |
271 return base::PLATFORM_FILE_ERROR_FAILED; | |
272 } | |
273 | |
274 base::FileUtilProxy::Entry entry; | |
275 entry.name = file_info.name; | |
276 entry.is_directory = file_info.is_directory(); | |
277 entry.size = entry.is_directory ? 0 : platform_file_info.size; | |
278 entry.last_modified_time = platform_file_info.last_modified; | |
279 entries->push_back(entry); | |
280 } | |
281 return base::PLATFORM_FILE_OK; | |
282 } | |
283 | |
284 PlatformFileError ObfuscatedFileSystemFileUtil::CreateDirectory( | |
285 FileSystemOperationContext* context, | |
286 const FilePath& virtual_path, | |
287 bool exclusive, | |
288 bool recursive) { | |
289 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
290 context->src_origin_url(), context->src_type(), true); | |
291 if (!db) | |
292 return base::PLATFORM_FILE_ERROR_FAILED; | |
293 FileId file_id; | |
294 if (db->GetFileWithPath(virtual_path, &file_id)) { | |
295 FileInfo file_info; | |
296 if (exclusive) | |
297 return base::PLATFORM_FILE_ERROR_EXISTS; | |
298 if (!db->GetFileInfo(file_id, &file_info)) { | |
299 NOTREACHED(); | |
300 return base::PLATFORM_FILE_ERROR_FAILED; | |
301 } | |
302 if (!file_info.is_directory()) | |
303 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; | |
304 return base::PLATFORM_FILE_OK; | |
305 } | |
306 | |
307 std::vector<FilePath::StringType> components; | |
308 virtual_path.GetComponents(&components); | |
309 FileId parent_id = 0; | |
310 size_t index; | |
311 for (index = 0; index < components.size(); ++index) { | |
312 FilePath::StringType name = components[index]; | |
313 if (name == FILE_PATH_LITERAL("/")) | |
314 continue; | |
315 if (!db->GetChildWithName(parent_id, name, &parent_id)) | |
316 break; | |
317 } | |
318 if (!recursive && components.size() - index > 1) | |
319 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
320 for (; index < components.size(); ++index) { | |
321 FileInfo file_info; | |
322 file_info.name = components[index]; | |
323 if (file_info.name == FILE_PATH_LITERAL("/")) | |
324 continue; | |
325 file_info.modification_time = base::Time::Now(); | |
326 file_info.parent_id = parent_id; | |
327 if (!AllocateQuotaForPath(context, 1, file_info.name.size())) | |
328 return base::PLATFORM_FILE_ERROR_NO_SPACE; | |
329 if (!db->AddFileInfo(file_info, &parent_id)) { | |
330 NOTREACHED(); | |
331 return base::PLATFORM_FILE_ERROR_FAILED; | |
332 } | |
333 UpdatePathQuotaUsage(context, context->src_origin_url(), | |
334 context->src_type(), 1, file_info.name.size()); | |
335 } | |
336 return base::PLATFORM_FILE_OK; | |
337 } | |
338 | |
339 PlatformFileError ObfuscatedFileSystemFileUtil::CopyOrMoveFile( | |
340 FileSystemOperationContext* context, | |
341 const FilePath& src_file_path, | |
342 const FilePath& dest_file_path, | |
343 bool copy) { | |
344 // Cross-filesystem copies and moves should be handled via CopyInForeignFile. | |
345 DCHECK(context->src_origin_url() == context->dest_origin_url()); | |
346 DCHECK(context->src_type() == context->dest_type()); | |
347 | |
348 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
349 context->src_origin_url(), context->src_type(), true); | |
350 if (!db) | |
351 return base::PLATFORM_FILE_ERROR_FAILED; | |
352 FileId src_file_id; | |
353 if (!db->GetFileWithPath(src_file_path, &src_file_id)) | |
354 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
355 FileId dest_file_id; | |
356 bool overwrite = db->GetFileWithPath(dest_file_path, &dest_file_id); | |
357 FileInfo src_file_info; | |
358 FileInfo dest_file_info; | |
359 if (!db->GetFileInfo(src_file_id, &src_file_info) || | |
360 src_file_info.is_directory()) { | |
361 NOTREACHED(); | |
362 return base::PLATFORM_FILE_ERROR_FAILED; | |
363 } | |
364 if (overwrite) { | |
365 if (!db->GetFileInfo(dest_file_id, &dest_file_info) || | |
366 dest_file_info.is_directory()) { | |
367 NOTREACHED(); | |
368 return base::PLATFORM_FILE_ERROR_FAILED; | |
369 } | |
370 } | |
371 /* | |
372 * Copy-with-overwrite | |
373 * Just overwrite data file | |
374 * Copy-without-overwrite | |
375 * Copy backing file | |
376 * Create new metadata pointing to new backing file. | |
377 * Move-with-overwrite | |
378 * transaction: | |
379 * Remove source entry. | |
380 * Point target entry to source entry's backing file. | |
381 * Delete target entry's old backing file | |
382 * Move-without-overwrite | |
383 * Just update metadata | |
384 */ | |
385 if (copy) { | |
386 FilePath src_data_path = DataPathToLocalPath(context->src_origin_url(), | |
387 context->src_type(), src_file_info.data_path); | |
388 if (overwrite) { | |
389 FilePath dest_data_path = DataPathToLocalPath(context->src_origin_url(), | |
390 context->src_type(), dest_file_info.data_path); | |
391 return underlying_file_util_->CopyOrMoveFile(context, | |
392 src_data_path, dest_data_path, copy); | |
393 } else { | |
394 FileId dest_parent_id; | |
395 if (!db->GetFileWithPath(dest_file_path.DirName(), &dest_parent_id)) { | |
396 NOTREACHED(); // We shouldn't be called in this case. | |
397 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
398 } | |
399 InitFileInfo(&dest_file_info, dest_parent_id, | |
400 dest_file_path.BaseName().value()); | |
401 if (!AllocateQuotaForPath(context, 1, dest_file_info.name.size())) | |
402 return base::PLATFORM_FILE_ERROR_NO_SPACE; | |
403 return CreateFile(context, context->dest_origin_url(), | |
404 context->dest_type(), src_data_path, &dest_file_info, 0, | |
405 NULL); | |
406 } | |
407 } else { // It's a move. | |
408 if (overwrite) { | |
409 AllocateQuotaForPath(context, -1, | |
410 -static_cast<int64>(src_file_info.name.size())); | |
411 if (!db->OverwritingMoveFile(src_file_id, dest_file_id)) | |
412 return base::PLATFORM_FILE_ERROR_FAILED; | |
413 FilePath dest_data_path = DataPathToLocalPath(context->src_origin_url(), | |
414 context->src_type(), dest_file_info.data_path); | |
415 if (base::PLATFORM_FILE_OK != | |
416 underlying_file_util_->DeleteFile(context, dest_data_path)) | |
417 LOG(WARNING) << "Leaked a backing file."; | |
418 UpdatePathQuotaUsage(context, context->src_origin_url(), | |
419 context->src_type(), -1, | |
420 -static_cast<int64>(src_file_info.name.size())); | |
421 return base::PLATFORM_FILE_OK; | |
422 } else { | |
423 FileId dest_parent_id; | |
424 if (!db->GetFileWithPath(dest_file_path.DirName(), &dest_parent_id)) { | |
425 NOTREACHED(); | |
426 return base::PLATFORM_FILE_ERROR_FAILED; | |
427 } | |
428 if (!AllocateQuotaForPath( | |
429 context, 0, | |
430 static_cast<int64>(dest_file_path.BaseName().value().size()) | |
431 - static_cast<int64>(src_file_info.name.size()))) | |
432 return base::PLATFORM_FILE_ERROR_NO_SPACE; | |
433 src_file_info.parent_id = dest_parent_id; | |
434 src_file_info.name = dest_file_path.BaseName().value(); | |
435 if (!db->UpdateFileInfo(src_file_id, src_file_info)) | |
436 return base::PLATFORM_FILE_ERROR_FAILED; | |
437 UpdatePathQuotaUsage( | |
438 context, context->src_origin_url(), context->src_type(), 0, | |
439 static_cast<int64>(dest_file_path.BaseName().value().size()) - | |
440 static_cast<int64>(src_file_path.BaseName().value().size())); | |
441 return base::PLATFORM_FILE_OK; | |
442 } | |
443 } | |
444 NOTREACHED(); | |
445 return base::PLATFORM_FILE_ERROR_FAILED; | |
446 } | |
447 | |
448 PlatformFileError ObfuscatedFileSystemFileUtil::CopyInForeignFile( | |
449 FileSystemOperationContext* context, | |
450 const FilePath& src_file_path, | |
451 const FilePath& dest_file_path) { | |
452 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
453 context->dest_origin_url(), context->dest_type(), true); | |
454 if (!db) | |
455 return base::PLATFORM_FILE_ERROR_FAILED; | |
456 FileId dest_file_id; | |
457 bool overwrite = db->GetFileWithPath(dest_file_path, &dest_file_id); | |
458 FileInfo dest_file_info; | |
459 if (overwrite) { | |
460 if (!db->GetFileInfo(dest_file_id, &dest_file_info) || | |
461 dest_file_info.is_directory()) { | |
462 NOTREACHED(); | |
463 return base::PLATFORM_FILE_ERROR_FAILED; | |
464 } | |
465 FilePath dest_data_path = DataPathToLocalPath(context->dest_origin_url(), | |
466 context->dest_type(), dest_file_info.data_path); | |
467 return underlying_file_util_->CopyOrMoveFile(context, | |
468 src_file_path, dest_data_path, true /* copy */); | |
469 } else { | |
470 FileId dest_parent_id; | |
471 if (!db->GetFileWithPath(dest_file_path.DirName(), &dest_parent_id)) { | |
472 NOTREACHED(); // We shouldn't be called in this case. | |
473 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
474 } | |
475 InitFileInfo(&dest_file_info, dest_parent_id, | |
476 dest_file_path.BaseName().value()); | |
477 if (!AllocateQuotaForPath(context, 1, dest_file_info.name.size())) | |
478 return base::PLATFORM_FILE_ERROR_NO_SPACE; | |
479 return CreateFile(context, context->dest_origin_url(), | |
480 context->dest_type(), src_file_path, &dest_file_info, 0, NULL); | |
481 } | |
482 return base::PLATFORM_FILE_ERROR_FAILED; | |
483 } | |
484 | |
485 PlatformFileError ObfuscatedFileSystemFileUtil::DeleteFile( | |
486 FileSystemOperationContext* context, | |
487 const FilePath& virtual_path) { | |
488 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
489 context->src_origin_url(), context->src_type(), true); | |
490 if (!db) | |
491 return base::PLATFORM_FILE_ERROR_FAILED; | |
492 FileId file_id; | |
493 if (!db->GetFileWithPath(virtual_path, &file_id)) | |
494 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
495 FileInfo file_info; | |
496 if (!db->GetFileInfo(file_id, &file_info) || file_info.is_directory()) { | |
497 NOTREACHED(); | |
498 return base::PLATFORM_FILE_ERROR_FAILED; | |
499 } | |
500 if (!db->RemoveFileInfo(file_id)) { | |
501 NOTREACHED(); | |
502 return base::PLATFORM_FILE_ERROR_FAILED; | |
503 } | |
504 AllocateQuotaForPath(context, -1, -static_cast<int64>(file_info.name.size())); | |
505 UpdatePathQuotaUsage(context, context->src_origin_url(), context->src_type(), | |
506 -1, -static_cast<int64>(file_info.name.size())); | |
507 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), | |
508 context->src_type(), file_info.data_path); | |
509 if (base::PLATFORM_FILE_OK != | |
510 underlying_file_util_->DeleteFile(context, data_path)) | |
511 LOG(WARNING) << "Leaked a backing file."; | |
512 return base::PLATFORM_FILE_OK; | |
513 } | |
514 | |
515 PlatformFileError ObfuscatedFileSystemFileUtil::DeleteSingleDirectory( | |
516 FileSystemOperationContext* context, | |
517 const FilePath& virtual_path) { | |
518 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
519 context->src_origin_url(), context->src_type(), true); | |
520 if (!db) | |
521 return base::PLATFORM_FILE_ERROR_FAILED; | |
522 FileId file_id; | |
523 if (!db->GetFileWithPath(virtual_path, &file_id)) | |
524 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
525 FileInfo file_info; | |
526 if (!db->GetFileInfo(file_id, &file_info) || !file_info.is_directory()) { | |
527 NOTREACHED(); | |
528 return base::PLATFORM_FILE_ERROR_FAILED; | |
529 } | |
530 if (!db->RemoveFileInfo(file_id)) | |
531 return base::PLATFORM_FILE_ERROR_NOT_EMPTY; | |
532 AllocateQuotaForPath(context, -1, -static_cast<int64>(file_info.name.size())); | |
533 UpdatePathQuotaUsage(context, context->src_origin_url(), context->src_type(), | |
534 -1, -static_cast<int64>(file_info.name.size())); | |
535 return base::PLATFORM_FILE_OK; | |
536 } | |
537 | |
538 PlatformFileError ObfuscatedFileSystemFileUtil::Touch( | |
539 FileSystemOperationContext* context, | |
540 const FilePath& virtual_path, | |
541 const base::Time& last_access_time, | |
542 const base::Time& last_modified_time) { | |
543 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
544 context->src_origin_url(), context->src_type(), true); | |
545 if (!db) | |
546 return base::PLATFORM_FILE_ERROR_FAILED; | |
547 FileId file_id; | |
548 if (db->GetFileWithPath(virtual_path, &file_id)) { | |
549 FileInfo file_info; | |
550 if (!db->GetFileInfo(file_id, &file_info)) { | |
551 NOTREACHED(); | |
552 return base::PLATFORM_FILE_ERROR_FAILED; | |
553 } | |
554 if (file_info.is_directory()) { | |
555 file_info.modification_time = last_modified_time; | |
556 if (!db->UpdateFileInfo(file_id, file_info)) | |
557 return base::PLATFORM_FILE_ERROR_FAILED; | |
558 return base::PLATFORM_FILE_OK; | |
559 } | |
560 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), | |
561 context->src_type(), file_info.data_path); | |
562 return underlying_file_util_->Touch( | |
563 context, data_path, last_access_time, last_modified_time); | |
564 } | |
565 FileId parent_id; | |
566 if (!db->GetFileWithPath(virtual_path.DirName(), &parent_id)) | |
567 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
568 | |
569 FileInfo file_info; | |
570 InitFileInfo(&file_info, parent_id, virtual_path.BaseName().value()); | |
571 // In the event of a sporadic underlying failure, we might create a new file, | |
572 // but fail to update its mtime + atime. | |
573 if (!AllocateQuotaForPath(context, 1, file_info.name.size())) | |
574 return base::PLATFORM_FILE_ERROR_NO_SPACE; | |
575 PlatformFileError error = CreateFile(context, context->src_origin_url(), | |
576 context->src_type(), FilePath(), &file_info, 0, NULL); | |
577 if (base::PLATFORM_FILE_OK != error) | |
578 return error; | |
579 | |
580 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), | |
581 context->src_type(), file_info.data_path); | |
582 return underlying_file_util_->Touch(context, data_path, | |
583 last_access_time, last_modified_time); | |
584 } | |
585 | |
586 PlatformFileError ObfuscatedFileSystemFileUtil::Truncate( | |
587 FileSystemOperationContext* context, | |
588 const FilePath& virtual_path, | |
589 int64 length) { | |
590 FilePath local_path = | |
591 GetLocalPath(context->src_origin_url(), context->src_type(), | |
592 virtual_path); | |
593 if (local_path.empty()) | |
594 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
595 return underlying_file_util_->Truncate( | |
596 context, local_path, length); | |
597 } | |
598 | |
599 bool ObfuscatedFileSystemFileUtil::PathExists( | |
600 FileSystemOperationContext* context, | |
601 const FilePath& virtual_path) { | |
602 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
603 context->src_origin_url(), context->src_type(), false); | |
604 if (!db) | |
605 return false; | |
606 FileId file_id; | |
607 return db->GetFileWithPath(virtual_path, &file_id); | |
608 } | |
609 | |
610 bool ObfuscatedFileSystemFileUtil::DirectoryExists( | |
611 FileSystemOperationContext* context, | |
612 const FilePath& virtual_path) { | |
613 if (IsRootDirectory(virtual_path)) { | |
614 // It's questionable whether we should return true or false for the | |
615 // root directory of nonexistent origin, but here we return true | |
616 // as the current implementation of ReadDirectory always returns an empty | |
617 // array (rather than erroring out with NOT_FOUND_ERR even) for | |
618 // nonexistent origins. | |
619 // Note: if you're going to change this behavior please also consider | |
620 // changiing the ReadDirectory's behavior! | |
621 return true; | |
622 } | |
623 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
624 context->src_origin_url(), context->src_type(), false); | |
625 if (!db) | |
626 return false; | |
627 FileId file_id; | |
628 if (!db->GetFileWithPath(virtual_path, &file_id)) | |
629 return false; | |
630 FileInfo file_info; | |
631 if (!db->GetFileInfo(file_id, &file_info)) { | |
632 NOTREACHED(); | |
633 return false; | |
634 } | |
635 return file_info.is_directory(); | |
636 } | |
637 | |
638 bool ObfuscatedFileSystemFileUtil::IsDirectoryEmpty( | |
639 FileSystemOperationContext* context, | |
640 const FilePath& virtual_path) { | |
641 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
642 context->src_origin_url(), context->src_type(), false); | |
643 if (!db) | |
644 return true; // Not a great answer, but it's what others do. | |
645 FileId file_id; | |
646 if (!db->GetFileWithPath(virtual_path, &file_id)) | |
647 return true; // Ditto. | |
648 FileInfo file_info; | |
649 if (!db->GetFileInfo(file_id, &file_info)) { | |
650 DCHECK(!file_id); | |
651 // It's the root directory and the database hasn't been initialized yet. | |
652 return true; | |
653 } | |
654 if (!file_info.is_directory()) | |
655 return true; | |
656 std::vector<FileId> children; | |
657 // TODO(ericu): This could easily be made faster with help from the database. | |
658 if (!db->ListChildren(file_id, &children)) | |
659 return true; | |
660 return children.empty(); | |
661 } | |
662 | |
663 class ObfuscatedFileSystemFileEnumerator | |
664 : public FileSystemFileUtil::AbstractFileEnumerator { | |
665 public: | |
666 ObfuscatedFileSystemFileEnumerator( | |
667 FileSystemDirectoryDatabase* db, const FilePath& virtual_root_path) | |
668 : db_(db) { | |
669 FileId file_id; | |
670 FileInfo file_info; | |
671 if (!db_->GetFileWithPath(virtual_root_path, &file_id)) | |
672 return; | |
673 if (!db_->GetFileInfo(file_id, &file_info)) | |
674 return; | |
675 if (!file_info.is_directory()) | |
676 return; | |
677 FileRecord record = { file_id, file_info, virtual_root_path }; | |
678 display_queue_.push(record); | |
679 Next(); // Enumerators don't include the directory itself. | |
680 } | |
681 | |
682 ~ObfuscatedFileSystemFileEnumerator() {} | |
683 | |
684 virtual FilePath Next() { | |
685 ProcessRecurseQueue(); | |
686 if (display_queue_.empty()) | |
687 return FilePath(); | |
688 current_ = display_queue_.front(); | |
689 display_queue_.pop(); | |
690 if (current_.file_info.is_directory()) | |
691 recurse_queue_.push(current_); | |
692 return current_.file_path; | |
693 } | |
694 | |
695 virtual bool IsDirectory() { | |
696 return current_.file_info.is_directory(); | |
697 } | |
698 | |
699 private: | |
700 typedef FileSystemDirectoryDatabase::FileId FileId; | |
701 typedef FileSystemDirectoryDatabase::FileInfo FileInfo; | |
702 | |
703 struct FileRecord { | |
704 FileId file_id; | |
705 FileInfo file_info; | |
706 FilePath file_path; | |
707 }; | |
708 | |
709 void ProcessRecurseQueue() { | |
710 while (display_queue_.empty() && !recurse_queue_.empty()) { | |
711 FileRecord directory = recurse_queue_.front(); | |
712 std::vector<FileId> children; | |
713 recurse_queue_.pop(); | |
714 if (!db_->ListChildren(directory.file_id, &children)) | |
715 return; | |
716 std::vector<FileId>::iterator iter; | |
717 for (iter = children.begin(); iter != children.end(); ++iter) { | |
718 FileRecord child; | |
719 child.file_id = *iter; | |
720 if (!db_->GetFileInfo(child.file_id, &child.file_info)) | |
721 return; | |
722 child.file_path = directory.file_path.Append(child.file_info.name); | |
723 display_queue_.push(child); | |
724 } | |
725 } | |
726 } | |
727 | |
728 std::queue<FileRecord> display_queue_; | |
729 std::queue<FileRecord> recurse_queue_; | |
730 FileRecord current_; | |
731 FileSystemDirectoryDatabase* db_; | |
732 }; | |
733 | |
734 class ObfuscatedFileSystemOriginEnumerator | |
735 : public ObfuscatedFileSystemFileUtil::AbstractOriginEnumerator { | |
736 public: | |
737 typedef FileSystemOriginDatabase::OriginRecord OriginRecord; | |
738 ObfuscatedFileSystemOriginEnumerator( | |
739 FileSystemOriginDatabase* origin_database, | |
740 const FilePath& base_path) | |
741 : base_path_(base_path) { | |
742 if (origin_database) | |
743 origin_database->ListAllOrigins(&origins_); | |
744 } | |
745 | |
746 ~ObfuscatedFileSystemOriginEnumerator() {} | |
747 | |
748 // Returns the next origin. Returns empty if there are no more origins. | |
749 virtual GURL Next() OVERRIDE { | |
750 OriginRecord record; | |
751 if (!origins_.empty()) { | |
752 record = origins_.back(); | |
753 origins_.pop_back(); | |
754 } | |
755 current_ = record; | |
756 return GetOriginURLFromIdentifier(record.origin); | |
757 } | |
758 | |
759 // Returns the current origin's information. | |
760 virtual bool HasFileSystemType(FileSystemType type) const OVERRIDE { | |
761 if (current_.path.empty()) | |
762 return false; | |
763 FilePath::StringType type_string = | |
764 ObfuscatedFileSystemFileUtil::GetDirectoryNameForType(type); | |
765 if (type_string.empty()) { | |
766 NOTREACHED(); | |
767 return false; | |
768 } | |
769 FilePath path = base_path_.Append(current_.path).Append(type_string); | |
770 return file_util::DirectoryExists(path); | |
771 } | |
772 | |
773 private: | |
774 std::vector<OriginRecord> origins_; | |
775 OriginRecord current_; | |
776 FilePath base_path_; | |
777 }; | |
778 | |
779 ObfuscatedFileSystemFileUtil::AbstractOriginEnumerator* | |
780 ObfuscatedFileSystemFileUtil::CreateOriginEnumerator() { | |
781 std::vector<FileSystemOriginDatabase::OriginRecord> origins; | |
782 | |
783 InitOriginDatabase(false); | |
784 return new ObfuscatedFileSystemOriginEnumerator( | |
785 origin_database_.get(), file_system_directory_); | |
786 } | |
787 | |
788 FileSystemFileUtil::AbstractFileEnumerator* | |
789 ObfuscatedFileSystemFileUtil::CreateFileEnumerator( | |
790 FileSystemOperationContext* context, | |
791 const FilePath& root_path) { | |
792 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
793 context->src_origin_url(), context->src_type(), false); | |
794 if (!db) | |
795 return new FileSystemFileUtil::EmptyFileEnumerator(); | |
796 return new ObfuscatedFileSystemFileEnumerator(db, root_path); | |
797 } | |
798 | |
799 PlatformFileError ObfuscatedFileSystemFileUtil::GetFileInfoInternal( | |
800 FileSystemDirectoryDatabase* db, | |
801 FileSystemOperationContext* context, | |
802 FileId file_id, | |
803 FileInfo* local_info, | |
804 base::PlatformFileInfo* file_info, | |
805 FilePath* platform_file_path) { | |
806 DCHECK(db); | |
807 DCHECK(context); | |
808 DCHECK(file_info); | |
809 DCHECK(platform_file_path); | |
810 | |
811 if (!db->GetFileInfo(file_id, local_info)) { | |
812 NOTREACHED(); | |
813 return base::PLATFORM_FILE_ERROR_FAILED; | |
814 } | |
815 | |
816 if (local_info->is_directory()) { | |
817 file_info->is_directory = true; | |
818 file_info->is_symbolic_link = false; | |
819 file_info->last_modified = local_info->modification_time; | |
820 *platform_file_path = FilePath(); | |
821 // We don't fill in ctime or atime. | |
822 return base::PLATFORM_FILE_OK; | |
823 } | |
824 if (local_info->data_path.empty()) | |
825 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
826 FilePath data_path = DataPathToLocalPath(context->src_origin_url(), | |
827 context->src_type(), local_info->data_path); | |
828 return underlying_file_util_->GetFileInfo( | |
829 context, data_path, file_info, platform_file_path); | |
830 } | |
831 | |
832 PlatformFileError ObfuscatedFileSystemFileUtil::CreateFile( | |
833 FileSystemOperationContext* context, | |
834 const GURL& origin_url, FileSystemType type, const FilePath& source_path, | |
835 FileInfo* file_info, int file_flags, PlatformFile* handle) { | |
836 if (handle) | |
837 *handle = base::kInvalidPlatformFileValue; | |
838 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
839 origin_url, type, true); | |
840 int64 number; | |
841 if (!db || !db->GetNextInteger(&number)) | |
842 return base::PLATFORM_FILE_ERROR_FAILED; | |
843 // We use the third- and fourth-to-last digits as the directory. | |
844 int64 directory_number = number % 10000 / 100; | |
845 FilePath path = | |
846 GetDirectoryForOriginAndType(origin_url, type, false); | |
847 if (path.empty()) | |
848 return base::PLATFORM_FILE_ERROR_FAILED; | |
849 | |
850 path = path.AppendASCII(StringPrintf("%02" PRIu64, directory_number)); | |
851 PlatformFileError error; | |
852 error = underlying_file_util_->CreateDirectory( | |
853 context, path, false /* exclusive */, false /* recursive */); | |
854 if (base::PLATFORM_FILE_OK != error) | |
855 return error; | |
856 path = path.AppendASCII(StringPrintf("%08" PRIu64, number)); | |
857 FilePath data_path = LocalPathToDataPath(origin_url, type, path); | |
858 if (data_path.empty()) | |
859 return base::PLATFORM_FILE_ERROR_FAILED; | |
860 bool created = false; | |
861 if (!source_path.empty()) { | |
862 DCHECK(!file_flags); | |
863 DCHECK(!handle); | |
864 error = underlying_file_util_->CopyOrMoveFile( | |
865 context, source_path, path, true /* copy */); | |
866 created = true; | |
867 } else { | |
868 if (handle) { | |
869 error = underlying_file_util_->CreateOrOpen( | |
870 context, path, file_flags, handle, &created); | |
871 // If this succeeds, we must close handle on any subsequent error. | |
872 } else { | |
873 DCHECK(!file_flags); // file_flags is only used by CreateOrOpen. | |
874 error = underlying_file_util_->EnsureFileExists( | |
875 context, path, &created); | |
876 } | |
877 } | |
878 if (error != base::PLATFORM_FILE_OK) | |
879 return error; | |
880 | |
881 if (!created) { | |
882 NOTREACHED(); | |
883 if (handle) { | |
884 DCHECK_NE(base::kInvalidPlatformFileValue, *handle); | |
885 base::ClosePlatformFile(*handle); | |
886 underlying_file_util_->DeleteFile(context, path); | |
887 } | |
888 return base::PLATFORM_FILE_ERROR_FAILED; | |
889 } | |
890 file_info->data_path = data_path; | |
891 FileId file_id; | |
892 if (!db->AddFileInfo(*file_info, &file_id)) { | |
893 if (handle) { | |
894 DCHECK_NE(base::kInvalidPlatformFileValue, *handle); | |
895 base::ClosePlatformFile(*handle); | |
896 } | |
897 underlying_file_util_->DeleteFile(context, path); | |
898 return base::PLATFORM_FILE_ERROR_FAILED; | |
899 } | |
900 UpdatePathQuotaUsage(context, origin_url, type, 1, file_info->name.size()); | |
901 | |
902 return base::PLATFORM_FILE_OK; | |
903 } | |
904 | |
905 FilePath ObfuscatedFileSystemFileUtil::GetLocalPath( | |
906 const GURL& origin_url, | |
907 FileSystemType type, | |
908 const FilePath& virtual_path) { | |
909 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
910 origin_url, type, false); | |
911 if (!db) | |
912 return FilePath(); | |
913 FileId file_id; | |
914 if (!db->GetFileWithPath(virtual_path, &file_id)) | |
915 return FilePath(); | |
916 FileInfo file_info; | |
917 if (!db->GetFileInfo(file_id, &file_info) || file_info.is_directory()) { | |
918 NOTREACHED(); | |
919 return FilePath(); // Directories have no local path. | |
920 } | |
921 return DataPathToLocalPath(origin_url, type, file_info.data_path); | |
922 } | |
923 | |
924 FilePath ObfuscatedFileSystemFileUtil::GetDirectoryForOriginAndType( | |
925 const GURL& origin, FileSystemType type, bool create) { | |
926 FilePath origin_dir = GetDirectoryForOrigin(origin, create); | |
927 if (origin_dir.empty()) | |
928 return FilePath(); | |
929 FilePath::StringType type_string = GetDirectoryNameForType(type); | |
930 if (type_string.empty()) { | |
931 LOG(WARNING) << "Unknown filesystem type requested:" << type; | |
932 return FilePath(); | |
933 } | |
934 FilePath path = origin_dir.Append(type_string); | |
935 if (!file_util::DirectoryExists(path) && | |
936 (!create || !file_util::CreateDirectory(path))) | |
937 return FilePath(); | |
938 return path; | |
939 } | |
940 | |
941 bool ObfuscatedFileSystemFileUtil::DeleteDirectoryForOriginAndType( | |
942 const GURL& origin, FileSystemType type) { | |
943 FilePath origin_type_path = GetDirectoryForOriginAndType(origin, type, false); | |
944 if (!file_util::PathExists(origin_type_path)) | |
945 return true; | |
946 | |
947 // TODO(dmikurube): Consider the return value of DestroyDirectoryDatabase. | |
948 // We ignore its error now since 1) it doesn't matter the final result, and | |
949 // 2) it always returns false in Windows because of LevelDB's implementation. | |
950 // Information about failure would be useful for debugging. | |
951 DestroyDirectoryDatabase(origin, type); | |
952 if (!file_util::Delete(origin_type_path, true /* recursive */)) | |
953 return false; | |
954 | |
955 FilePath origin_path = origin_type_path.DirName(); | |
956 DCHECK_EQ(origin_path.value(), GetDirectoryForOrigin(origin, false).value()); | |
957 | |
958 // Delete the origin directory if the deleted one was the last remaining | |
959 // type for the origin. | |
960 if (file_util::Delete(origin_path, false /* recursive */)) { | |
961 InitOriginDatabase(false); | |
962 if (origin_database_.get()) | |
963 origin_database_->RemovePathForOrigin(GetOriginIdentifierFromURL(origin)); | |
964 } | |
965 | |
966 // At this point we are sure we had successfully deleted the origin/type | |
967 // directory, so just returning true here. | |
968 return true; | |
969 } | |
970 | |
971 bool ObfuscatedFileSystemFileUtil::MigrateFromOldSandbox( | |
972 const GURL& origin_url, FileSystemType type, const FilePath& src_root) { | |
973 if (!DestroyDirectoryDatabase(origin_url, type)) | |
974 return false; | |
975 FilePath dest_root = GetDirectoryForOriginAndType(origin_url, type, true); | |
976 if (dest_root.empty()) | |
977 return false; | |
978 FileSystemDirectoryDatabase* db = GetDirectoryDatabase( | |
979 origin_url, type, true); | |
980 if (!db) | |
981 return false; | |
982 | |
983 file_util::FileEnumerator file_enum(src_root, true, | |
984 static_cast<file_util::FileEnumerator::FileType>( | |
985 file_util::FileEnumerator::FILES | | |
986 file_util::FileEnumerator::DIRECTORIES)); | |
987 FilePath src_full_path; | |
988 size_t root_path_length = src_root.value().length() + 1; // +1 for the slash | |
989 while (!(src_full_path = file_enum.Next()).empty()) { | |
990 file_util::FileEnumerator::FindInfo info; | |
991 file_enum.GetFindInfo(&info); | |
992 FilePath relative_virtual_path = | |
993 FilePath(src_full_path.value().substr(root_path_length)); | |
994 if (relative_virtual_path.empty()) { | |
995 LOG(WARNING) << "Failed to convert path to relative: " << | |
996 src_full_path.value(); | |
997 return false; | |
998 } | |
999 FileId file_id; | |
1000 if (db->GetFileWithPath(relative_virtual_path, &file_id)) { | |
1001 NOTREACHED(); // File already exists. | |
1002 return false; | |
1003 } | |
1004 if (!db->GetFileWithPath(relative_virtual_path.DirName(), &file_id)) { | |
1005 NOTREACHED(); // Parent doesn't exist. | |
1006 return false; | |
1007 } | |
1008 | |
1009 FileInfo file_info; | |
1010 file_info.name = src_full_path.BaseName().value(); | |
1011 if (file_util::FileEnumerator::IsDirectory(info)) { | |
1012 #if defined(OS_WIN) | |
1013 file_info.modification_time = | |
1014 base::Time::FromFileTime(info.ftLastWriteTime); | |
1015 #elif defined(OS_POSIX) | |
1016 file_info.modification_time = base::Time::FromTimeT(info.stat.st_mtime); | |
1017 #endif | |
1018 } else { | |
1019 file_info.data_path = | |
1020 FilePath(kLegacyDataDirectory).Append(relative_virtual_path); | |
1021 } | |
1022 file_info.parent_id = file_id; | |
1023 if (!db->AddFileInfo(file_info, &file_id)) { | |
1024 NOTREACHED(); | |
1025 return false; | |
1026 } | |
1027 } | |
1028 // TODO(ericu): Should we adjust the mtime of the root directory to match as | |
1029 // well? | |
1030 FilePath legacy_dest_dir = dest_root.Append(kLegacyDataDirectory); | |
1031 | |
1032 if (!file_util::Move(src_root, legacy_dest_dir)) { | |
1033 LOG(WARNING) << | |
1034 "The final step of a migration failed; I'll try to clean up."; | |
1035 db = NULL; | |
1036 DestroyDirectoryDatabase(origin_url, type); | |
1037 return false; | |
1038 } | |
1039 return true; | |
1040 } | |
1041 | |
1042 // static | |
1043 FilePath::StringType ObfuscatedFileSystemFileUtil::GetDirectoryNameForType( | |
1044 FileSystemType type) { | |
1045 switch (type) { | |
1046 case kFileSystemTypeTemporary: | |
1047 return kTemporaryDirectoryName; | |
1048 case kFileSystemTypePersistent: | |
1049 return kPersistentDirectoryName; | |
1050 case kFileSystemTypeUnknown: | |
1051 default: | |
1052 return FilePath::StringType(); | |
1053 } | |
1054 } | |
1055 | |
1056 FilePath ObfuscatedFileSystemFileUtil::DataPathToLocalPath( | |
1057 const GURL& origin, FileSystemType type, const FilePath& data_path) { | |
1058 FilePath root = GetDirectoryForOriginAndType(origin, type, false); | |
1059 if (root.empty()) | |
1060 return root; | |
1061 return root.Append(data_path); | |
1062 } | |
1063 | |
1064 FilePath ObfuscatedFileSystemFileUtil::LocalPathToDataPath( | |
1065 const GURL& origin, FileSystemType type, const FilePath& local_path) { | |
1066 FilePath root = GetDirectoryForOriginAndType(origin, type, false); | |
1067 if (root.empty()) | |
1068 return root; | |
1069 // This removes the root, including the trailing slash, leaving a relative | |
1070 // path. | |
1071 return FilePath(local_path.value().substr(root.value().length() + 1)); | |
1072 } | |
1073 | |
1074 // TODO: How to do the whole validation-without-creation thing? We may not have | |
1075 // quota even to create the database. Ah, in that case don't even get here? | |
1076 // Still doesn't answer the quota issue, though. | |
1077 FileSystemDirectoryDatabase* ObfuscatedFileSystemFileUtil::GetDirectoryDatabase( | |
1078 const GURL& origin, FileSystemType type, bool create) { | |
1079 std::string type_string = | |
1080 FileSystemPathManager::GetFileSystemTypeString(type); | |
1081 if (type_string.empty()) { | |
1082 LOG(WARNING) << "Unknown filesystem type requested:" << type; | |
1083 return NULL; | |
1084 } | |
1085 std::string key = GetOriginIdentifierFromURL(origin) + type_string; | |
1086 DirectoryMap::iterator iter = directories_.find(key); | |
1087 if (iter != directories_.end()) { | |
1088 MarkUsed(); | |
1089 return iter->second; | |
1090 } | |
1091 | |
1092 FilePath path = GetDirectoryForOriginAndType(origin, type, create); | |
1093 if (path.empty()) | |
1094 return NULL; | |
1095 if (!file_util::DirectoryExists(path)) { | |
1096 if (!file_util::CreateDirectory(path)) { | |
1097 LOG(WARNING) << "Failed to origin+type directory: " << path.value(); | |
1098 return NULL; | |
1099 } | |
1100 } | |
1101 MarkUsed(); | |
1102 path = path.AppendASCII(kDirectoryDatabaseName); | |
1103 FileSystemDirectoryDatabase* database = new FileSystemDirectoryDatabase(path); | |
1104 directories_[key] = database; | |
1105 return database; | |
1106 } | |
1107 | |
1108 FilePath ObfuscatedFileSystemFileUtil::GetDirectoryForOrigin( | |
1109 const GURL& origin, bool create) { | |
1110 if (!InitOriginDatabase(create)) | |
1111 return FilePath(); | |
1112 FilePath directory_name; | |
1113 std::string id = GetOriginIdentifierFromURL(origin); | |
1114 if (!create && !origin_database_->HasOriginPath(id)) | |
1115 return FilePath(); | |
1116 if (!origin_database_->GetPathForOrigin(id, &directory_name)) | |
1117 return FilePath(); | |
1118 FilePath path = file_system_directory_.Append(directory_name); | |
1119 if (!file_util::DirectoryExists(path) && | |
1120 (!create || !file_util::CreateDirectory(path))) | |
1121 return FilePath(); | |
1122 return path; | |
1123 } | |
1124 | |
1125 void ObfuscatedFileSystemFileUtil::MarkUsed() { | |
1126 if (timer_.IsRunning()) | |
1127 timer_.Reset(); | |
1128 else | |
1129 timer_.Start(base::TimeDelta::FromSeconds(kFlushDelaySeconds), this, | |
1130 &ObfuscatedFileSystemFileUtil::DropDatabases); | |
1131 } | |
1132 | |
1133 void ObfuscatedFileSystemFileUtil::DropDatabases() { | |
1134 origin_database_.reset(); | |
1135 STLDeleteContainerPairSecondPointers( | |
1136 directories_.begin(), directories_.end()); | |
1137 directories_.clear(); | |
1138 } | |
1139 | |
1140 // static | |
1141 int64 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(const FilePath& path) { | |
1142 return GetPathQuotaUsage(1, path.BaseName().value().size()); | |
1143 } | |
1144 | |
1145 bool ObfuscatedFileSystemFileUtil::DestroyDirectoryDatabase( | |
1146 const GURL& origin, FileSystemType type) { | |
1147 std::string type_string = | |
1148 FileSystemPathManager::GetFileSystemTypeString(type); | |
1149 if (type_string.empty()) { | |
1150 LOG(WARNING) << "Unknown filesystem type requested:" << type; | |
1151 return true; | |
1152 } | |
1153 std::string key = GetOriginIdentifierFromURL(origin) + type_string; | |
1154 DirectoryMap::iterator iter = directories_.find(key); | |
1155 if (iter != directories_.end()) { | |
1156 FileSystemDirectoryDatabase* database = iter->second; | |
1157 directories_.erase(iter); | |
1158 delete database; | |
1159 } | |
1160 | |
1161 FilePath path = GetDirectoryForOriginAndType(origin, type, false); | |
1162 if (path.empty()) | |
1163 return true; | |
1164 if (!file_util::DirectoryExists(path)) | |
1165 return true; | |
1166 path = path.AppendASCII(kDirectoryDatabaseName); | |
1167 return FileSystemDirectoryDatabase::DestroyDatabase(path); | |
1168 } | |
1169 | |
1170 bool ObfuscatedFileSystemFileUtil::InitOriginDatabase(bool create) { | |
1171 if (!origin_database_.get()) { | |
1172 if (!create && !file_util::DirectoryExists(file_system_directory_)) | |
1173 return false; | |
1174 if (!file_util::CreateDirectory(file_system_directory_)) { | |
1175 LOG(WARNING) << "Failed to create FileSystem directory: " << | |
1176 file_system_directory_.value(); | |
1177 return false; | |
1178 } | |
1179 origin_database_.reset( | |
1180 new FileSystemOriginDatabase( | |
1181 file_system_directory_.AppendASCII(kOriginDatabaseName))); | |
1182 } | |
1183 return true; | |
1184 } | |
1185 | |
1186 } // namespace fileapi | |
OLD | NEW |