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_path.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/platform_file.h"
14 #include "base/time/time.h"
15 #include "webkit/browser/webkit_storage_browser_export.h"
17 namespace tracked_objects {
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.
35 // TODO(ericu): Safe mode, which does more checks such as the above on debug
37 // TODO(ericu): Add a method that will give a unique filename for a data file.
38 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE SandboxDirectoryDatabase {
42 struct WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE FileInfo {
46 bool is_directory() const {
47 return data_path.empty();
51 base::FilePath data_path;
52 base::FilePath::StringType name;
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.
56 base::Time modification_time;
59 explicit SandboxDirectoryDatabase(
60 const base::FilePath& filesystem_data_directory);
61 ~SandboxDirectoryDatabase();
63 bool GetChildWithName(
65 const base::FilePath::StringType& name,
67 bool GetFileWithPath(const base::FilePath& path, FileId* file_id);
68 // ListChildren will succeed, returning 0 children, if parent_id doesn't
70 bool ListChildren(FileId parent_id, std::vector<FileId>* children);
71 bool GetFileInfo(FileId file_id, FileInfo* info);
72 base::PlatformFileError AddFileInfo(const FileInfo& info, FileId* file_id);
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);
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
84 bool OverwritingMoveFile(FileId src_file_id, FileId dest_file_id);
86 // This produces the series 0, 1, 2..., starting at 0 when the underlying
87 // filesystem is first created, and maintaining state across
88 // creation/destruction of SandboxDirectoryDatabase objects.
89 bool GetNextInteger(int64* next);
91 // Returns true if the database looks consistent with local filesystem.
92 bool IsFileSystemConsistent();
94 static bool DestroyDatabase(const base::FilePath& path);
103 friend class ObfuscatedFileUtil;
104 friend class SandboxDirectoryDatabaseTest;
106 bool Init(RecoveryOption recovery_option);
107 bool RepairDatabase(const std::string& db_path);
108 void ReportInitStatus(const leveldb::Status& status);
109 bool StoreDefaultValues();
110 bool GetLastFileId(FileId* file_id);
111 bool VerifyIsDirectory(FileId file_id);
112 bool AddFileInfoHelper(
113 const FileInfo& info, FileId file_id, leveldb::WriteBatch* batch);
114 bool RemoveFileInfoHelper(FileId file_id, leveldb::WriteBatch* batch);
115 void HandleError(const tracked_objects::Location& from_here,
116 const leveldb::Status& status);
118 const base::FilePath filesystem_data_directory_;
119 scoped_ptr<leveldb::DB> db_;
120 base::Time last_reported_time_;
121 DISALLOW_COPY_AND_ASSIGN(SandboxDirectoryDatabase);
124 } // namespace fileapi
126 #endif // WEBKIT_BROWSER_FILEAPI_SANDBOX_DIRECTORY_DATABASE_H_