[email protected] | 642aadb | 2012-03-27 07:18:53 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 28f051c3 | 2013-05-21 05:15:26 | [diff] [blame] | 5 | #ifndef WEBKIT_BROWSER_FILEAPI_SANDBOX_DIRECTORY_DATABASE_H_ |
| 6 | #define WEBKIT_BROWSER_FILEAPI_SANDBOX_DIRECTORY_DATABASE_H_ |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 11 | #include "base/files/file.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
[email protected] | e57a716 | 2011-06-15 04:14:23 | [diff] [blame] | 13 | #include "base/memory/scoped_ptr.h" |
[email protected] | 0a8ebe1 | 2013-06-28 15:23:23 | [diff] [blame] | 14 | #include "base/time/time.h" |
[email protected] | e5bf2fd | 2013-06-13 02:59:18 | [diff] [blame] | 15 | #include "webkit/browser/webkit_storage_browser_export.h" |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 16 | |
[email protected] | 9100e922 | 2011-09-28 09:07:33 | [diff] [blame] | 17 | namespace tracked_objects { |
| 18 | class Location; |
| 19 | } |
| 20 | |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 21 | namespace leveldb { |
[email protected] | 642aadb | 2012-03-27 07:18:53 | [diff] [blame] | 22 | class DB; |
| 23 | class Status; |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 24 | class WriteBatch; |
| 25 | } |
| 26 | |
| 27 | namespace fileapi { |
| 28 | |
| 29 | // This class WILL NOT protect you against producing directory loops, giving an |
| 30 | // empty directory a backing data file, giving two files the same backing file, |
| 31 | // or pointing to a nonexistent backing file. It does no file IO other than |
| 32 | // that involved with talking to its underlying database. It does not create or |
| 33 | // in any way touch real files; it only creates path entries in its database. |
| 34 | |
| 35 | // TODO(ericu): Safe mode, which does more checks such as the above on debug |
| 36 | // builds. |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 37 | // TODO(ericu): Add a method that will give a unique filename for a data file. |
[email protected] | e5bf2fd | 2013-06-13 02:59:18 | [diff] [blame] | 38 | class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE SandboxDirectoryDatabase { |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 39 | public: |
| 40 | typedef int64 FileId; |
| 41 | |
[email protected] | e5bf2fd | 2013-06-13 02:59:18 | [diff] [blame] | 42 | struct WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE FileInfo { |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 43 | FileInfo(); |
| 44 | ~FileInfo(); |
| 45 | |
[email protected] | f1ddaa4 | 2011-07-19 05:03:03 | [diff] [blame] | 46 | bool is_directory() const { |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 47 | return data_path.empty(); |
| 48 | } |
| 49 | |
| 50 | FileId parent_id; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 51 | base::FilePath data_path; |
| 52 | base::FilePath::StringType name; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 53 | // This modification time is valid only for directories, not files, as |
| 54 | // FileWriter will get the files out of sync. |
| 55 | // For files, look at the modification time of the underlying data_path. |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 56 | base::Time modification_time; |
| 57 | }; |
| 58 | |
[email protected] | e6bbd9c | 2013-05-16 10:46:45 | [diff] [blame] | 59 | explicit SandboxDirectoryDatabase( |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 60 | const base::FilePath& filesystem_data_directory); |
[email protected] | e6bbd9c | 2013-05-16 10:46:45 | [diff] [blame] | 61 | ~SandboxDirectoryDatabase(); |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 62 | |
| 63 | bool GetChildWithName( |
[email protected] | e6bbd9c | 2013-05-16 10:46:45 | [diff] [blame] | 64 | FileId parent_id, |
| 65 | const base::FilePath::StringType& name, |
| 66 | FileId* child_id); |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 67 | bool GetFileWithPath(const base::FilePath& path, FileId* file_id); |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 68 | // ListChildren will succeed, returning 0 children, if parent_id doesn't |
| 69 | // exist. |
| 70 | bool ListChildren(FileId parent_id, std::vector<FileId>* children); |
| 71 | bool GetFileInfo(FileId file_id, FileInfo* info); |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame^] | 72 | base::File::Error AddFileInfo(const FileInfo& info, FileId* file_id); |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 73 | bool RemoveFileInfo(FileId file_id); |
| 74 | // This does a full update of the FileInfo, and is what you'd use for moves |
| 75 | // and renames. If you just want to update the modification_time, use |
| 76 | // UpdateModificationTime. |
| 77 | bool UpdateFileInfo(FileId file_id, const FileInfo& info); |
| 78 | bool UpdateModificationTime( |
| 79 | FileId file_id, const base::Time& modification_time); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 80 | // This is used for an overwriting move of a file [not a directory] on top of |
| 81 | // another file [also not a directory]; we need to alter two files' info in a |
| 82 | // single transaction to avoid weird backing file references in the event of a |
| 83 | // partial failure. |
| 84 | bool OverwritingMoveFile(FileId src_file_id, FileId dest_file_id); |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 85 | |
[email protected] | 7b5efd93 | 2011-05-06 23:34:39 | [diff] [blame] | 86 | // This produces the series 0, 1, 2..., starting at 0 when the underlying |
| 87 | // filesystem is first created, and maintaining state across |
[email protected] | e6bbd9c | 2013-05-16 10:46:45 | [diff] [blame] | 88 | // creation/destruction of SandboxDirectoryDatabase objects. |
[email protected] | 7b5efd93 | 2011-05-06 23:34:39 | [diff] [blame] | 89 | bool GetNextInteger(int64* next); |
| 90 | |
[email protected] | 48d2c73c | 2013-11-25 11:23:02 | [diff] [blame] | 91 | bool IsDirectory(FileId file_id); |
| 92 | |
[email protected] | bb2c4daa | 2012-04-16 04:15:20 | [diff] [blame] | 93 | // Returns true if the database looks consistent with local filesystem. |
| 94 | bool IsFileSystemConsistent(); |
| 95 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 96 | static bool DestroyDatabase(const base::FilePath& path); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 97 | |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 98 | private: |
[email protected] | b969877 | 2012-03-29 16:45:48 | [diff] [blame] | 99 | enum RecoveryOption { |
| 100 | DELETE_ON_CORRUPTION, |
[email protected] | bb2c4daa | 2012-04-16 04:15:20 | [diff] [blame] | 101 | REPAIR_ON_CORRUPTION, |
[email protected] | b969877 | 2012-03-29 16:45:48 | [diff] [blame] | 102 | FAIL_ON_CORRUPTION, |
| 103 | }; |
| 104 | |
[email protected] | a623aae | 2013-06-04 06:48:46 | [diff] [blame] | 105 | friend class ObfuscatedFileUtil; |
[email protected] | e6bbd9c | 2013-05-16 10:46:45 | [diff] [blame] | 106 | friend class SandboxDirectoryDatabaseTest; |
[email protected] | bb2c4daa | 2012-04-16 04:15:20 | [diff] [blame] | 107 | |
[email protected] | b969877 | 2012-03-29 16:45:48 | [diff] [blame] | 108 | bool Init(RecoveryOption recovery_option); |
[email protected] | bb2c4daa | 2012-04-16 04:15:20 | [diff] [blame] | 109 | bool RepairDatabase(const std::string& db_path); |
[email protected] | 642aadb | 2012-03-27 07:18:53 | [diff] [blame] | 110 | void ReportInitStatus(const leveldb::Status& status); |
[email protected] | 7b5efd93 | 2011-05-06 23:34:39 | [diff] [blame] | 111 | bool StoreDefaultValues(); |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 112 | bool GetLastFileId(FileId* file_id); |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 113 | bool AddFileInfoHelper( |
[email protected] | 7b5efd93 | 2011-05-06 23:34:39 | [diff] [blame] | 114 | const FileInfo& info, FileId file_id, leveldb::WriteBatch* batch); |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 115 | bool RemoveFileInfoHelper(FileId file_id, leveldb::WriteBatch* batch); |
[email protected] | 9100e922 | 2011-09-28 09:07:33 | [diff] [blame] | 116 | void HandleError(const tracked_objects::Location& from_here, |
[email protected] | 642aadb | 2012-03-27 07:18:53 | [diff] [blame] | 117 | const leveldb::Status& status); |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 118 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 119 | const base::FilePath filesystem_data_directory_; |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 120 | scoped_ptr<leveldb::DB> db_; |
[email protected] | 642aadb | 2012-03-27 07:18:53 | [diff] [blame] | 121 | base::Time last_reported_time_; |
[email protected] | e6bbd9c | 2013-05-16 10:46:45 | [diff] [blame] | 122 | DISALLOW_COPY_AND_ASSIGN(SandboxDirectoryDatabase); |
[email protected] | ca8dfe8 | 2011-05-05 23:52:45 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | } // namespace fileapi |
| 126 | |
[email protected] | 28f051c3 | 2013-05-21 05:15:26 | [diff] [blame] | 127 | #endif // WEBKIT_BROWSER_FILEAPI_SANDBOX_DIRECTORY_DATABASE_H_ |