Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / webkit / fileapi / file_system_directory_database.h
blobe157b65518049674f0aba9c27e841a1506582eea
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_FILEAPI_FILE_SYSTEM_DIRECTORY_DATABASE_H_
6 #define WEBKIT_FILEAPI_FILE_SYSTEM_DIRECTORY_DATABASE_H_
8 #include <string>
9 #include <vector>
11 #include "base/files/file_path.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/time.h"
14 #include "webkit/storage/webkit_storage_export.h"
16 namespace tracked_objects {
17 class Location;
20 namespace leveldb {
21 class DB;
22 class Status;
23 class WriteBatch;
26 namespace fileapi {
28 // This class WILL NOT protect you against producing directory loops, giving an
29 // empty directory a backing data file, giving two files the same backing file,
30 // or pointing to a nonexistent backing file. It does no file IO other than
31 // that involved with talking to its underlying database. It does not create or
32 // in any way touch real files; it only creates path entries in its database.
34 // TODO(ericu): Safe mode, which does more checks such as the above on debug
35 // builds.
36 // TODO(ericu): Add a method that will give a unique filename for a data file.
37 class WEBKIT_STORAGE_EXPORT_PRIVATE FileSystemDirectoryDatabase {
38 public:
39 typedef int64 FileId;
41 struct WEBKIT_STORAGE_EXPORT_PRIVATE FileInfo {
42 FileInfo();
43 ~FileInfo();
45 bool is_directory() const {
46 return data_path.empty();
49 FileId parent_id;
50 base::FilePath data_path;
51 base::FilePath::StringType name;
52 // This modification time is valid only for directories, not files, as
53 // FileWriter will get the files out of sync.
54 // For files, look at the modification time of the underlying data_path.
55 base::Time modification_time;
58 explicit FileSystemDirectoryDatabase(
59 const base::FilePath& filesystem_data_directory);
60 ~FileSystemDirectoryDatabase();
62 bool GetChildWithName(
63 FileId parent_id, const base::FilePath::StringType& name, FileId* child_id);
64 bool GetFileWithPath(const base::FilePath& path, FileId* file_id);
65 // ListChildren will succeed, returning 0 children, if parent_id doesn't
66 // exist.
67 bool ListChildren(FileId parent_id, std::vector<FileId>* children);
68 bool GetFileInfo(FileId file_id, FileInfo* info);
69 bool AddFileInfo(const FileInfo& info, FileId* file_id);
70 bool RemoveFileInfo(FileId file_id);
71 // This does a full update of the FileInfo, and is what you'd use for moves
72 // and renames. If you just want to update the modification_time, use
73 // UpdateModificationTime.
74 bool UpdateFileInfo(FileId file_id, const FileInfo& info);
75 bool UpdateModificationTime(
76 FileId file_id, const base::Time& modification_time);
77 // This is used for an overwriting move of a file [not a directory] on top of
78 // another file [also not a directory]; we need to alter two files' info in a
79 // single transaction to avoid weird backing file references in the event of a
80 // partial failure.
81 bool OverwritingMoveFile(FileId src_file_id, FileId dest_file_id);
83 // This produces the series 0, 1, 2..., starting at 0 when the underlying
84 // filesystem is first created, and maintaining state across
85 // creation/destruction of FileSystemDirectoryDatabase objects.
86 bool GetNextInteger(int64* next);
88 // Returns true if the database looks consistent with local filesystem.
89 bool IsFileSystemConsistent();
91 static bool DestroyDatabase(const base::FilePath& path);
93 private:
94 enum RecoveryOption {
95 DELETE_ON_CORRUPTION,
96 REPAIR_ON_CORRUPTION,
97 FAIL_ON_CORRUPTION,
100 friend class FileSystemDirectoryDatabaseTest;
102 bool Init(RecoveryOption recovery_option);
103 bool RepairDatabase(const std::string& db_path);
104 void ReportInitStatus(const leveldb::Status& status);
105 bool StoreDefaultValues();
106 bool GetLastFileId(FileId* file_id);
107 bool VerifyIsDirectory(FileId file_id);
108 bool AddFileInfoHelper(
109 const FileInfo& info, FileId file_id, leveldb::WriteBatch* batch);
110 bool RemoveFileInfoHelper(FileId file_id, leveldb::WriteBatch* batch);
111 void HandleError(const tracked_objects::Location& from_here,
112 const leveldb::Status& status);
114 const base::FilePath filesystem_data_directory_;
115 scoped_ptr<leveldb::DB> db_;
116 base::Time last_reported_time_;
117 DISALLOW_COPY_AND_ASSIGN(FileSystemDirectoryDatabase);
120 } // namespace fileapi
122 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_DIRECTORY_DATABASE_H_