1 // Copyright (c) 2012 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.
5 #ifndef WEBKIT_BROWSER_FILEAPI_SANDBOX_DIRECTORY_DATABASE_H_
6 #define WEBKIT_BROWSER_FILEAPI_SANDBOX_DIRECTORY_DATABASE_H_
11 #include "base/files/file.h"
12 #include "base/files/file_path.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time/time.h"
15 #include "webkit/browser/webkit_storage_browser_export.h"
17 namespace tracked_objects {
30 // This class WILL NOT protect you against producing directory loops, giving an
31 // empty directory a backing data file, giving two files the same backing file,
32 // or pointing to a nonexistent backing file. It does no file IO other than
33 // that involved with talking to its underlying database. It does not create or
34 // in any way touch real files; it only creates path entries in its database.
36 // TODO(ericu): Safe mode, which does more checks such as the above on debug
38 // TODO(ericu): Add a method that will give a unique filename for a data file.
39 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE SandboxDirectoryDatabase {
43 struct WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE FileInfo {
47 bool is_directory() const {
48 return data_path.empty();
52 base::FilePath data_path;
53 base::FilePath::StringType name;
54 // This modification time is valid only for directories, not files, as
55 // FileWriter will get the files out of sync.
56 // For files, look at the modification time of the underlying data_path.
57 base::Time modification_time;
60 SandboxDirectoryDatabase(
61 const base::FilePath& filesystem_data_directory,
62 leveldb::Env* env_override);
63 ~SandboxDirectoryDatabase();
65 bool GetChildWithName(
67 const base::FilePath::StringType& name,
69 bool GetFileWithPath(const base::FilePath& path, FileId* file_id);
70 // ListChildren will succeed, returning 0 children, if parent_id doesn't
72 bool ListChildren(FileId parent_id, std::vector<FileId>* children);
73 bool GetFileInfo(FileId file_id, FileInfo* info);
74 base::File::Error AddFileInfo(const FileInfo& info, FileId* file_id);
75 bool RemoveFileInfo(FileId file_id);
76 // This does a full update of the FileInfo, and is what you'd use for moves
77 // and renames. If you just want to update the modification_time, use
78 // UpdateModificationTime.
79 bool UpdateFileInfo(FileId file_id, const FileInfo& info);
80 bool UpdateModificationTime(
81 FileId file_id, const base::Time& modification_time);
82 // This is used for an overwriting move of a file [not a directory] on top of
83 // another file [also not a directory]; we need to alter two files' info in a
84 // single transaction to avoid weird backing file references in the event of a
86 bool OverwritingMoveFile(FileId src_file_id, FileId dest_file_id);
88 // This produces the series 0, 1, 2..., starting at 0 when the underlying
89 // filesystem is first created, and maintaining state across
90 // creation/destruction of SandboxDirectoryDatabase objects.
91 bool GetNextInteger(int64* next);
93 bool IsDirectory(FileId file_id);
95 // Returns true if the database looks consistent with local filesystem.
96 bool IsFileSystemConsistent();
98 static bool DestroyDatabase(const base::FilePath& path,
99 leveldb::Env* env_override);
102 enum RecoveryOption {
103 DELETE_ON_CORRUPTION,
104 REPAIR_ON_CORRUPTION,
108 friend class ObfuscatedFileUtil;
109 friend class SandboxDirectoryDatabaseTest;
111 bool Init(RecoveryOption recovery_option);
112 bool RepairDatabase(const std::string& db_path);
113 void ReportInitStatus(const leveldb::Status& status);
114 bool StoreDefaultValues();
115 bool GetLastFileId(FileId* file_id);
116 bool AddFileInfoHelper(
117 const FileInfo& info, FileId file_id, leveldb::WriteBatch* batch);
118 bool RemoveFileInfoHelper(FileId file_id, leveldb::WriteBatch* batch);
119 void HandleError(const tracked_objects::Location& from_here,
120 const leveldb::Status& status);
122 const base::FilePath filesystem_data_directory_;
123 leveldb::Env* env_override_;
124 scoped_ptr<leveldb::DB> db_;
125 base::Time last_reported_time_;
126 DISALLOW_COPY_AND_ASSIGN(SandboxDirectoryDatabase);
129 } // namespace fileapi
131 #endif // WEBKIT_BROWSER_FILEAPI_SANDBOX_DIRECTORY_DATABASE_H_