Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / webkit / browser / fileapi / sandbox_directory_database.h
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.
4
5 #ifndef WEBKIT_BROWSER_FILEAPI_SANDBOX_DIRECTORY_DATABASE_H_
6 #define WEBKIT_BROWSER_FILEAPI_SANDBOX_DIRECTORY_DATABASE_H_
7
8 #include <string>
9 #include <vector>
10
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"
16
17 namespace tracked_objects {
18 class Location;
19 }
20
21 namespace leveldb {
22 class DB;
23 class Env;
24 class Status;
25 class WriteBatch;
26 }
27
28 namespace fileapi {
29
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.
35
36 // TODO(ericu): Safe mode, which does more checks such as the above on debug
37 // builds.
38 // TODO(ericu): Add a method that will give a unique filename for a data file.
39 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE SandboxDirectoryDatabase {
40  public:
41   typedef int64 FileId;
42
43   struct WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE FileInfo {
44     FileInfo();
45     ~FileInfo();
46
47     bool is_directory() const {
48       return data_path.empty();
49     }
50
51     FileId parent_id;
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;
58   };
59
60   SandboxDirectoryDatabase(
61       const base::FilePath& filesystem_data_directory,
62       leveldb::Env* env_override);
63   ~SandboxDirectoryDatabase();
64
65   bool GetChildWithName(
66       FileId parent_id,
67       const base::FilePath::StringType& name,
68       FileId* child_id);
69   bool GetFileWithPath(const base::FilePath& path, FileId* file_id);
70   // ListChildren will succeed, returning 0 children, if parent_id doesn't
71   // exist.
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
85   // partial failure.
86   bool OverwritingMoveFile(FileId src_file_id, FileId dest_file_id);
87
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);
92
93   bool IsDirectory(FileId file_id);
94
95   // Returns true if the database looks consistent with local filesystem.
96   bool IsFileSystemConsistent();
97
98   static bool DestroyDatabase(const base::FilePath& path,
99                               leveldb::Env* env_override);
100
101  private:
102   enum RecoveryOption {
103     DELETE_ON_CORRUPTION,
104     REPAIR_ON_CORRUPTION,
105     FAIL_ON_CORRUPTION,
106   };
107
108   friend class ObfuscatedFileUtil;
109   friend class SandboxDirectoryDatabaseTest;
110
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);
121
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);
127 };
128
129 }  // namespace fileapi
130
131 #endif  // WEBKIT_BROWSER_FILEAPI_SANDBOX_DIRECTORY_DATABASE_H_