blob: a984cc4d59746e2068fda4f1a10db0b8dd4ab2f6 [file] [log] [blame]
[email protected]642aadb2012-03-27 07:18:531// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ca8dfe82011-05-05 23:52:452// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]28f051c32013-05-21 05:15:265#ifndef WEBKIT_BROWSER_FILEAPI_SANDBOX_DIRECTORY_DATABASE_H_
6#define WEBKIT_BROWSER_FILEAPI_SANDBOX_DIRECTORY_DATABASE_H_
[email protected]ca8dfe82011-05-05 23:52:457
8#include <string>
9#include <vector>
10
[email protected]141bcc52014-01-27 21:36:0011#include "base/files/file.h"
[email protected]57999812013-02-24 05:40:5212#include "base/files/file_path.h"
[email protected]e57a7162011-06-15 04:14:2313#include "base/memory/scoped_ptr.h"
[email protected]0a8ebe12013-06-28 15:23:2314#include "base/time/time.h"
[email protected]e5bf2fd2013-06-13 02:59:1815#include "webkit/browser/webkit_storage_browser_export.h"
[email protected]ca8dfe82011-05-05 23:52:4516
[email protected]9100e9222011-09-28 09:07:3317namespace tracked_objects {
18class Location;
19}
20
[email protected]ca8dfe82011-05-05 23:52:4521namespace leveldb {
[email protected]642aadb2012-03-27 07:18:5322class DB;
23class Status;
[email protected]ca8dfe82011-05-05 23:52:4524class WriteBatch;
25}
26
27namespace 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]ca8dfe82011-05-05 23:52:4537// TODO(ericu): Add a method that will give a unique filename for a data file.
[email protected]e5bf2fd2013-06-13 02:59:1838class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE SandboxDirectoryDatabase {
[email protected]ca8dfe82011-05-05 23:52:4539 public:
40 typedef int64 FileId;
41
[email protected]e5bf2fd2013-06-13 02:59:1842 struct WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE FileInfo {
[email protected]ca8dfe82011-05-05 23:52:4543 FileInfo();
44 ~FileInfo();
45
[email protected]f1ddaa42011-07-19 05:03:0346 bool is_directory() const {
[email protected]ca8dfe82011-05-05 23:52:4547 return data_path.empty();
48 }
49
50 FileId parent_id;
[email protected]a3ef4832013-02-02 05:12:3351 base::FilePath data_path;
52 base::FilePath::StringType name;
[email protected]d4905e2e2011-05-13 21:56:3253 // 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]ca8dfe82011-05-05 23:52:4556 base::Time modification_time;
57 };
58
[email protected]e6bbd9c2013-05-16 10:46:4559 explicit SandboxDirectoryDatabase(
[email protected]a3ef4832013-02-02 05:12:3360 const base::FilePath& filesystem_data_directory);
[email protected]e6bbd9c2013-05-16 10:46:4561 ~SandboxDirectoryDatabase();
[email protected]ca8dfe82011-05-05 23:52:4562
63 bool GetChildWithName(
[email protected]e6bbd9c2013-05-16 10:46:4564 FileId parent_id,
65 const base::FilePath::StringType& name,
66 FileId* child_id);
[email protected]a3ef4832013-02-02 05:12:3367 bool GetFileWithPath(const base::FilePath& path, FileId* file_id);
[email protected]ca8dfe82011-05-05 23:52:4568 // 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]141bcc52014-01-27 21:36:0072 base::File::Error AddFileInfo(const FileInfo& info, FileId* file_id);
[email protected]ca8dfe82011-05-05 23:52:4573 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]d4905e2e2011-05-13 21:56:3280 // 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]ca8dfe82011-05-05 23:52:4585
[email protected]7b5efd932011-05-06 23:34:3986 // This produces the series 0, 1, 2..., starting at 0 when the underlying
87 // filesystem is first created, and maintaining state across
[email protected]e6bbd9c2013-05-16 10:46:4588 // creation/destruction of SandboxDirectoryDatabase objects.
[email protected]7b5efd932011-05-06 23:34:3989 bool GetNextInteger(int64* next);
90
[email protected]48d2c73c2013-11-25 11:23:0291 bool IsDirectory(FileId file_id);
92
[email protected]bb2c4daa2012-04-16 04:15:2093 // Returns true if the database looks consistent with local filesystem.
94 bool IsFileSystemConsistent();
95
[email protected]a3ef4832013-02-02 05:12:3396 static bool DestroyDatabase(const base::FilePath& path);
[email protected]6b931152011-05-20 21:02:3597
[email protected]ca8dfe82011-05-05 23:52:4598 private:
[email protected]b9698772012-03-29 16:45:4899 enum RecoveryOption {
100 DELETE_ON_CORRUPTION,
[email protected]bb2c4daa2012-04-16 04:15:20101 REPAIR_ON_CORRUPTION,
[email protected]b9698772012-03-29 16:45:48102 FAIL_ON_CORRUPTION,
103 };
104
[email protected]a623aae2013-06-04 06:48:46105 friend class ObfuscatedFileUtil;
[email protected]e6bbd9c2013-05-16 10:46:45106 friend class SandboxDirectoryDatabaseTest;
[email protected]bb2c4daa2012-04-16 04:15:20107
[email protected]b9698772012-03-29 16:45:48108 bool Init(RecoveryOption recovery_option);
[email protected]bb2c4daa2012-04-16 04:15:20109 bool RepairDatabase(const std::string& db_path);
[email protected]642aadb2012-03-27 07:18:53110 void ReportInitStatus(const leveldb::Status& status);
[email protected]7b5efd932011-05-06 23:34:39111 bool StoreDefaultValues();
[email protected]ca8dfe82011-05-05 23:52:45112 bool GetLastFileId(FileId* file_id);
[email protected]ca8dfe82011-05-05 23:52:45113 bool AddFileInfoHelper(
[email protected]7b5efd932011-05-06 23:34:39114 const FileInfo& info, FileId file_id, leveldb::WriteBatch* batch);
[email protected]ca8dfe82011-05-05 23:52:45115 bool RemoveFileInfoHelper(FileId file_id, leveldb::WriteBatch* batch);
[email protected]9100e9222011-09-28 09:07:33116 void HandleError(const tracked_objects::Location& from_here,
[email protected]642aadb2012-03-27 07:18:53117 const leveldb::Status& status);
[email protected]ca8dfe82011-05-05 23:52:45118
[email protected]a3ef4832013-02-02 05:12:33119 const base::FilePath filesystem_data_directory_;
[email protected]ca8dfe82011-05-05 23:52:45120 scoped_ptr<leveldb::DB> db_;
[email protected]642aadb2012-03-27 07:18:53121 base::Time last_reported_time_;
[email protected]e6bbd9c2013-05-16 10:46:45122 DISALLOW_COPY_AND_ASSIGN(SandboxDirectoryDatabase);
[email protected]ca8dfe82011-05-05 23:52:45123};
124
125} // namespace fileapi
126
[email protected]28f051c32013-05-21 05:15:26127#endif // WEBKIT_BROWSER_FILEAPI_SANDBOX_DIRECTORY_DATABASE_H_