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 #include "storage/browser/fileapi/sandbox_directory_database.h"
12 #include "base/files/file_enumerator.h"
13 #include "base/files/file_util.h"
14 #include "base/location.h"
15 #include "base/metrics/histogram.h"
16 #include "base/pickle.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_util.h"
19 #include "storage/browser/fileapi/file_system_usage_cache.h"
20 #include "storage/common/fileapi/file_system_util.h"
21 #include "third_party/leveldatabase/env_chromium.h"
22 #include "third_party/leveldatabase/src/include/leveldb/db.h"
23 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
27 bool PickleFromFileInfo(const storage::SandboxDirectoryDatabase::FileInfo
& info
,
28 base::Pickle
* pickle
) {
30 std::string data_path
;
31 // Round off here to match the behavior of the filesystem on real files.
33 base::Time::FromDoubleT(floor(info
.modification_time
.ToDoubleT()));
36 data_path
= storage::FilePathToString(info
.data_path
);
37 name
= storage::FilePathToString(base::FilePath(info
.name
));
39 if (pickle
->WriteInt64(info
.parent_id
) &&
40 pickle
->WriteString(data_path
) &&
41 pickle
->WriteString(name
) &&
42 pickle
->WriteInt64(time
.ToInternalValue()))
49 bool FileInfoFromPickle(const base::Pickle
& pickle
,
50 storage::SandboxDirectoryDatabase::FileInfo
* info
) {
51 base::PickleIterator
iter(pickle
);
52 std::string data_path
;
56 if (iter
.ReadInt64(&info
->parent_id
) &&
57 iter
.ReadString(&data_path
) &&
58 iter
.ReadString(&name
) &&
59 iter
.ReadInt64(&internal_time
)) {
60 info
->data_path
= storage::StringToFilePath(data_path
);
61 info
->name
= storage::StringToFilePath(name
).value();
62 info
->modification_time
= base::Time::FromInternalValue(internal_time
);
65 LOG(ERROR
) << "base::Pickle could not be digested!";
69 const base::FilePath::CharType kDirectoryDatabaseName
[] =
70 FILE_PATH_LITERAL("Paths");
71 const char kChildLookupPrefix
[] = "CHILD_OF:";
72 const char kChildLookupSeparator
[] = ":";
73 const char kLastFileIdKey
[] = "LAST_FILE_ID";
74 const char kLastIntegerKey
[] = "LAST_INTEGER";
75 const int64 kMinimumReportIntervalHours
= 1;
76 const char kInitStatusHistogramLabel
[] = "FileSystem.DirectoryDatabaseInit";
77 const char kDatabaseRepairHistogramLabel
[] =
78 "FileSystem.DirectoryDatabaseRepair";
80 // These values are recorded in UMA. Changing existing values will invalidate
81 // results for older Chrome releases. Only add new values.
84 INIT_STATUS_CORRUPTION
,
86 INIT_STATUS_UNKNOWN_ERROR
,
90 // These values are recorded in UMA. Changing existing values will invalidate
91 // results for older Chrome releases. Only add new values.
93 DB_REPAIR_SUCCEEDED
= 0,
98 std::string
GetChildLookupKey(
99 storage::SandboxDirectoryDatabase::FileId parent_id
,
100 const base::FilePath::StringType
& child_name
) {
102 name
= storage::FilePathToString(base::FilePath(child_name
));
103 return std::string(kChildLookupPrefix
) + base::Int64ToString(parent_id
) +
104 std::string(kChildLookupSeparator
) + name
;
107 std::string
GetChildListingKeyPrefix(
108 storage::SandboxDirectoryDatabase::FileId parent_id
) {
109 return std::string(kChildLookupPrefix
) + base::Int64ToString(parent_id
) +
110 std::string(kChildLookupSeparator
);
113 const char* LastFileIdKey() {
114 return kLastFileIdKey
;
117 const char* LastIntegerKey() {
118 return kLastIntegerKey
;
121 std::string
GetFileLookupKey(
122 storage::SandboxDirectoryDatabase::FileId file_id
) {
123 return base::Int64ToString(file_id
);
127 // - Any database entry is one of:
128 // - ("CHILD_OF:|parent_id|:<name>", "|file_id|"),
129 // - ("LAST_FILE_ID", "|last_file_id|"),
130 // - ("LAST_INTEGER", "|last_integer|"),
131 // - ("|file_id|", "pickled FileInfo")
132 // where FileInfo has |parent_id|, |data_path|, |name| and
133 // |modification_time|,
135 // - Each file in the database has unique backing file.
136 // - Each file in |filesystem_data_directory_| has a database entry.
137 // - Directory structure is tree, i.e. connected and acyclic.
138 class DatabaseCheckHelper
{
140 typedef storage::SandboxDirectoryDatabase::FileId FileId
;
141 typedef storage::SandboxDirectoryDatabase::FileInfo FileInfo
;
143 DatabaseCheckHelper(storage::SandboxDirectoryDatabase
* dir_db
,
145 const base::FilePath
& path
);
147 bool IsFileSystemConsistent() {
148 return IsDatabaseEmpty() ||
149 (ScanDatabase() && ScanDirectory() && ScanHierarchy());
153 bool IsDatabaseEmpty();
154 // These 3 methods need to be called in the order. Each method requires its
155 // previous method finished successfully. They also require the database is
158 bool ScanDirectory();
159 bool ScanHierarchy();
161 storage::SandboxDirectoryDatabase
* dir_db_
;
163 base::FilePath path_
;
165 std::set
<base::FilePath
> files_in_db_
;
167 size_t num_directories_in_db_
;
168 size_t num_files_in_db_
;
169 size_t num_hierarchy_links_in_db_
;
171 FileId last_file_id_
;
172 FileId last_integer_
;
175 DatabaseCheckHelper::DatabaseCheckHelper(
176 storage::SandboxDirectoryDatabase
* dir_db
,
178 const base::FilePath
& path
)
182 num_directories_in_db_(0),
184 num_hierarchy_links_in_db_(0),
189 DCHECK(!path_
.empty() && base::DirectoryExists(path_
));
192 bool DatabaseCheckHelper::IsDatabaseEmpty() {
193 scoped_ptr
<leveldb::Iterator
> itr(db_
->NewIterator(leveldb::ReadOptions()));
195 return !itr
->Valid();
198 bool DatabaseCheckHelper::ScanDatabase() {
199 // Scans all database entries sequentially to verify each of them has unique
201 int64 max_file_id
= -1;
202 std::set
<FileId
> file_ids
;
204 scoped_ptr
<leveldb::Iterator
> itr(db_
->NewIterator(leveldb::ReadOptions()));
205 for (itr
->SeekToFirst(); itr
->Valid(); itr
->Next()) {
206 std::string key
= itr
->key().ToString();
207 if (base::StartsWithASCII(key
, kChildLookupPrefix
, true)) {
208 // key: "CHILD_OF:<parent_id>:<name>"
209 // value: "<child_id>"
210 ++num_hierarchy_links_in_db_
;
211 } else if (key
== kLastFileIdKey
) {
212 // key: "LAST_FILE_ID"
213 // value: "<last_file_id>"
214 if (last_file_id_
>= 0 ||
215 !base::StringToInt64(itr
->value().ToString(), &last_file_id_
))
218 if (last_file_id_
< 0)
220 } else if (key
== kLastIntegerKey
) {
221 // key: "LAST_INTEGER"
222 // value: "<last_integer>"
223 if (last_integer_
>= 0 ||
224 !base::StringToInt64(itr
->value().ToString(), &last_integer_
))
228 // value: "<pickled FileInfo>"
230 if (!FileInfoFromPickle(
231 base::Pickle(itr
->value().data(), itr
->value().size()),
236 if (!base::StringToInt64(key
, &file_id
) || file_id
< 0)
239 if (max_file_id
< file_id
)
240 max_file_id
= file_id
;
241 if (!file_ids
.insert(file_id
).second
)
244 if (file_info
.is_directory()) {
245 ++num_directories_in_db_
;
246 DCHECK(file_info
.data_path
.empty());
248 // Ensure any pair of file entry don't share their data_path.
249 if (!files_in_db_
.insert(file_info
.data_path
).second
)
252 // Ensure the backing file exists as a normal file.
253 base::File::Info platform_file_info
;
254 if (!base::GetFileInfo(
255 path_
.Append(file_info
.data_path
), &platform_file_info
) ||
256 platform_file_info
.is_directory
||
257 platform_file_info
.is_symbolic_link
) {
258 // leveldb::Iterator iterates a snapshot of the database.
259 // So even after RemoveFileInfo() call, we'll visit hierarchy link
260 // from |parent_id| to |file_id|.
261 if (!dir_db_
->RemoveFileInfo(file_id
))
263 --num_hierarchy_links_in_db_
;
264 files_in_db_
.erase(file_info
.data_path
);
272 // TODO(tzik): Add constraint for |last_integer_| to avoid possible
273 // data path confliction on ObfuscatedFileUtil.
274 return max_file_id
<= last_file_id_
;
277 bool DatabaseCheckHelper::ScanDirectory() {
278 // TODO(kinuko): Scans all local file system entries to verify each of them
279 // has a database entry.
280 const base::FilePath kExcludes
[] = {
281 base::FilePath(kDirectoryDatabaseName
),
282 base::FilePath(storage::FileSystemUsageCache::kUsageFileName
),
285 // Any path in |pending_directories| is relative to |path_|.
286 std::stack
<base::FilePath
> pending_directories
;
287 pending_directories
.push(base::FilePath());
289 while (!pending_directories
.empty()) {
290 base::FilePath dir_path
= pending_directories
.top();
291 pending_directories
.pop();
293 base::FileEnumerator
file_enum(
294 dir_path
.empty() ? path_
: path_
.Append(dir_path
),
295 false /* not recursive */,
296 base::FileEnumerator::DIRECTORIES
| base::FileEnumerator::FILES
);
298 base::FilePath absolute_file_path
;
299 while (!(absolute_file_path
= file_enum
.Next()).empty()) {
300 base::FileEnumerator::FileInfo find_info
= file_enum
.GetInfo();
302 base::FilePath relative_file_path
;
303 if (!path_
.AppendRelativePath(absolute_file_path
, &relative_file_path
))
306 if (std::find(kExcludes
, kExcludes
+ arraysize(kExcludes
),
307 relative_file_path
) != kExcludes
+ arraysize(kExcludes
))
310 if (find_info
.IsDirectory()) {
311 pending_directories
.push(relative_file_path
);
315 // Check if the file has a database entry.
316 std::set
<base::FilePath
>::iterator itr
=
317 files_in_db_
.find(relative_file_path
);
318 if (itr
== files_in_db_
.end()) {
319 if (!base::DeleteFile(absolute_file_path
, false))
322 files_in_db_
.erase(itr
);
327 return files_in_db_
.empty();
330 bool DatabaseCheckHelper::ScanHierarchy() {
331 size_t visited_directories
= 0;
332 size_t visited_files
= 0;
333 size_t visited_links
= 0;
335 std::stack
<FileId
> directories
;
338 // Check if the root directory exists as a directory.
340 if (!dir_db_
->GetFileInfo(0, &file_info
))
342 if (file_info
.parent_id
!= 0 ||
343 !file_info
.is_directory())
346 while (!directories
.empty()) {
347 ++visited_directories
;
348 FileId dir_id
= directories
.top();
351 std::vector
<FileId
> children
;
352 if (!dir_db_
->ListChildren(dir_id
, &children
))
354 for (std::vector
<FileId
>::iterator itr
= children
.begin();
355 itr
!= children
.end();
357 // Any directory must not have root directory as child.
361 // Check if the child knows the parent as its parent.
363 if (!dir_db_
->GetFileInfo(*itr
, &file_info
))
365 if (file_info
.parent_id
!= dir_id
)
368 // Check if the parent knows the name of its child correctly.
370 if (!dir_db_
->GetChildWithName(dir_id
, file_info
.name
, &file_id
) ||
374 if (file_info
.is_directory())
375 directories
.push(*itr
);
382 // Check if we've visited all database entries.
383 return num_directories_in_db_
== visited_directories
&&
384 num_files_in_db_
== visited_files
&&
385 num_hierarchy_links_in_db_
== visited_links
;
388 // Returns true if the given |data_path| contains no parent references ("..")
389 // and does not refer to special system files.
390 // This is called in GetFileInfo, AddFileInfo and UpdateFileInfo to
391 // ensure we're only dealing with valid data paths.
392 bool VerifyDataPath(const base::FilePath
& data_path
) {
393 // |data_path| should not contain any ".." and should be a relative path
394 // (to the filesystem_data_directory_).
395 if (data_path
.ReferencesParent() || data_path
.IsAbsolute())
397 // See if it's not pointing to the special system paths.
398 const base::FilePath kExcludes
[] = {
399 base::FilePath(kDirectoryDatabaseName
),
400 base::FilePath(storage::FileSystemUsageCache::kUsageFileName
),
402 for (size_t i
= 0; i
< arraysize(kExcludes
); ++i
) {
403 if (data_path
== kExcludes
[i
] || kExcludes
[i
].IsParent(data_path
))
413 SandboxDirectoryDatabase::FileInfo::FileInfo() : parent_id(0) {
416 SandboxDirectoryDatabase::FileInfo::~FileInfo() {
419 SandboxDirectoryDatabase::SandboxDirectoryDatabase(
420 const base::FilePath
& filesystem_data_directory
,
421 leveldb::Env
* env_override
)
422 : filesystem_data_directory_(filesystem_data_directory
),
423 env_override_(env_override
) {
426 SandboxDirectoryDatabase::~SandboxDirectoryDatabase() {
429 bool SandboxDirectoryDatabase::GetChildWithName(
431 const base::FilePath::StringType
& name
,
433 if (!Init(REPAIR_ON_CORRUPTION
))
436 std::string child_key
= GetChildLookupKey(parent_id
, name
);
437 std::string child_id_string
;
438 leveldb::Status status
=
439 db_
->Get(leveldb::ReadOptions(), child_key
, &child_id_string
);
440 if (status
.IsNotFound())
443 if (!base::StringToInt64(child_id_string
, child_id
)) {
444 LOG(ERROR
) << "Hit database corruption!";
449 HandleError(FROM_HERE
, status
);
453 bool SandboxDirectoryDatabase::GetFileWithPath(
454 const base::FilePath
& path
, FileId
* file_id
) {
455 std::vector
<base::FilePath::StringType
> components
;
456 VirtualPath::GetComponents(path
, &components
);
458 std::vector
<base::FilePath::StringType
>::iterator iter
;
459 for (iter
= components
.begin(); iter
!= components
.end(); ++iter
) {
460 base::FilePath::StringType name
;
462 if (name
== FILE_PATH_LITERAL("/"))
464 if (!GetChildWithName(local_id
, name
, &local_id
))
471 bool SandboxDirectoryDatabase::ListChildren(
472 FileId parent_id
, std::vector
<FileId
>* children
) {
473 // Check to add later: fail if parent is a file, at least in debug builds.
474 if (!Init(REPAIR_ON_CORRUPTION
))
477 std::string child_key_prefix
= GetChildListingKeyPrefix(parent_id
);
479 scoped_ptr
<leveldb::Iterator
> iter(db_
->NewIterator(leveldb::ReadOptions()));
480 iter
->Seek(child_key_prefix
);
482 while (iter
->Valid() && base::StartsWithASCII(iter
->key().ToString(),
483 child_key_prefix
, true)) {
484 std::string child_id_string
= iter
->value().ToString();
486 if (!base::StringToInt64(child_id_string
, &child_id
)) {
487 LOG(ERROR
) << "Hit database corruption!";
490 children
->push_back(child_id
);
496 bool SandboxDirectoryDatabase::GetFileInfo(FileId file_id
, FileInfo
* info
) {
497 if (!Init(REPAIR_ON_CORRUPTION
))
500 std::string file_key
= GetFileLookupKey(file_id
);
501 std::string file_data_string
;
502 leveldb::Status status
=
503 db_
->Get(leveldb::ReadOptions(), file_key
, &file_data_string
);
505 bool success
= FileInfoFromPickle(
506 base::Pickle(file_data_string
.data(), file_data_string
.length()), info
);
509 if (!VerifyDataPath(info
->data_path
)) {
510 LOG(ERROR
) << "Resolved data path is invalid: "
511 << info
->data_path
.value();
516 // Special-case the root, for databases that haven't been initialized yet.
517 // Without this, a query for the root's file info, made before creating the
518 // first file in the database, will fail and confuse callers.
519 if (status
.IsNotFound() && !file_id
) {
520 info
->name
= base::FilePath::StringType();
521 info
->data_path
= base::FilePath();
522 info
->modification_time
= base::Time::Now();
526 HandleError(FROM_HERE
, status
);
530 base::File::Error
SandboxDirectoryDatabase::AddFileInfo(
531 const FileInfo
& info
, FileId
* file_id
) {
532 if (!Init(REPAIR_ON_CORRUPTION
))
533 return base::File::FILE_ERROR_FAILED
;
535 std::string child_key
= GetChildLookupKey(info
.parent_id
, info
.name
);
536 std::string child_id_string
;
537 leveldb::Status status
=
538 db_
->Get(leveldb::ReadOptions(), child_key
, &child_id_string
);
540 LOG(ERROR
) << "File exists already!";
541 return base::File::FILE_ERROR_EXISTS
;
543 if (!status
.IsNotFound()) {
544 HandleError(FROM_HERE
, status
);
545 return base::File::FILE_ERROR_NOT_FOUND
;
548 if (!IsDirectory(info
.parent_id
)) {
549 LOG(ERROR
) << "New parent directory is a file!";
550 return base::File::FILE_ERROR_NOT_A_DIRECTORY
;
553 // This would be a fine place to limit the number of files in a directory, if
554 // we decide to add that restriction.
557 if (!GetLastFileId(&temp_id
))
558 return base::File::FILE_ERROR_FAILED
;
561 leveldb::WriteBatch batch
;
562 if (!AddFileInfoHelper(info
, temp_id
, &batch
))
563 return base::File::FILE_ERROR_FAILED
;
565 batch
.Put(LastFileIdKey(), base::Int64ToString(temp_id
));
566 status
= db_
->Write(leveldb::WriteOptions(), &batch
);
568 HandleError(FROM_HERE
, status
);
569 return base::File::FILE_ERROR_FAILED
;
572 return base::File::FILE_OK
;
575 bool SandboxDirectoryDatabase::RemoveFileInfo(FileId file_id
) {
576 if (!Init(REPAIR_ON_CORRUPTION
))
578 leveldb::WriteBatch batch
;
579 if (!RemoveFileInfoHelper(file_id
, &batch
))
581 leveldb::Status status
= db_
->Write(leveldb::WriteOptions(), &batch
);
583 HandleError(FROM_HERE
, status
);
589 bool SandboxDirectoryDatabase::UpdateFileInfo(
590 FileId file_id
, const FileInfo
& new_info
) {
591 // TODO(ericu): We should also check to see that this doesn't create a loop,
592 // but perhaps only in a debug build.
593 if (!Init(REPAIR_ON_CORRUPTION
))
595 DCHECK(file_id
); // You can't remove the root, ever. Just delete the DB.
597 if (!GetFileInfo(file_id
, &old_info
))
599 if (old_info
.parent_id
!= new_info
.parent_id
&&
600 !IsDirectory(new_info
.parent_id
))
602 if (old_info
.parent_id
!= new_info
.parent_id
||
603 old_info
.name
!= new_info
.name
) {
604 // Check for name clashes.
606 if (GetChildWithName(new_info
.parent_id
, new_info
.name
, &temp_id
)) {
607 LOG(ERROR
) << "Name collision on move.";
611 leveldb::WriteBatch batch
;
612 if (!RemoveFileInfoHelper(file_id
, &batch
) ||
613 !AddFileInfoHelper(new_info
, file_id
, &batch
))
615 leveldb::Status status
= db_
->Write(leveldb::WriteOptions(), &batch
);
617 HandleError(FROM_HERE
, status
);
623 bool SandboxDirectoryDatabase::UpdateModificationTime(
624 FileId file_id
, const base::Time
& modification_time
) {
626 if (!GetFileInfo(file_id
, &info
))
628 info
.modification_time
= modification_time
;
630 if (!PickleFromFileInfo(info
, &pickle
))
632 leveldb::Status status
= db_
->Put(
633 leveldb::WriteOptions(),
634 GetFileLookupKey(file_id
),
635 leveldb::Slice(reinterpret_cast<const char *>(pickle
.data()),
638 HandleError(FROM_HERE
, status
);
644 bool SandboxDirectoryDatabase::OverwritingMoveFile(
645 FileId src_file_id
, FileId dest_file_id
) {
646 FileInfo src_file_info
;
647 FileInfo dest_file_info
;
649 if (!GetFileInfo(src_file_id
, &src_file_info
))
651 if (!GetFileInfo(dest_file_id
, &dest_file_info
))
653 if (src_file_info
.is_directory() || dest_file_info
.is_directory())
655 leveldb::WriteBatch batch
;
656 // This is the only field that really gets moved over; if you add fields to
657 // FileInfo, e.g. ctime, they might need to be copied here.
658 dest_file_info
.data_path
= src_file_info
.data_path
;
659 if (!RemoveFileInfoHelper(src_file_id
, &batch
))
662 if (!PickleFromFileInfo(dest_file_info
, &pickle
))
665 GetFileLookupKey(dest_file_id
),
666 leveldb::Slice(reinterpret_cast<const char *>(pickle
.data()),
668 leveldb::Status status
= db_
->Write(leveldb::WriteOptions(), &batch
);
670 HandleError(FROM_HERE
, status
);
676 bool SandboxDirectoryDatabase::GetNextInteger(int64
* next
) {
677 if (!Init(REPAIR_ON_CORRUPTION
))
680 std::string int_string
;
681 leveldb::Status status
=
682 db_
->Get(leveldb::ReadOptions(), LastIntegerKey(), &int_string
);
685 if (!base::StringToInt64(int_string
, &temp
)) {
686 LOG(ERROR
) << "Hit database corruption!";
690 status
= db_
->Put(leveldb::WriteOptions(), LastIntegerKey(),
691 base::Int64ToString(temp
));
693 HandleError(FROM_HERE
, status
);
699 if (!status
.IsNotFound()) {
700 HandleError(FROM_HERE
, status
);
703 // The database must not yet exist; initialize it.
704 if (!StoreDefaultValues())
707 return GetNextInteger(next
);
710 bool SandboxDirectoryDatabase::DestroyDatabase() {
712 const std::string path
=
713 FilePathToString(filesystem_data_directory_
.Append(
714 kDirectoryDatabaseName
));
715 leveldb::Options options
;
717 options
.env
= env_override_
;
718 leveldb::Status status
= leveldb::DestroyDB(path
, options
);
721 LOG(WARNING
) << "Failed to destroy a database with status " <<
726 bool SandboxDirectoryDatabase::Init(RecoveryOption recovery_option
) {
731 FilePathToString(filesystem_data_directory_
.Append(
732 kDirectoryDatabaseName
));
733 leveldb::Options options
;
734 options
.max_open_files
= 0; // Use minimum.
735 options
.create_if_missing
= true;
736 options
.reuse_logs
= leveldb_env::kDefaultLogReuseOptionValue
;
738 options
.env
= env_override_
;
740 leveldb::Status status
= leveldb::DB::Open(options
, path
, &db
);
741 ReportInitStatus(status
);
746 HandleError(FROM_HERE
, status
);
748 // Corruption due to missing necessary MANIFEST-* file causes IOError instead
749 // of Corruption error.
750 // Try to repair database even when IOError case.
751 if (!status
.IsCorruption() && !status
.IsIOError())
754 switch (recovery_option
) {
755 case FAIL_ON_CORRUPTION
:
757 case REPAIR_ON_CORRUPTION
:
758 LOG(WARNING
) << "Corrupted SandboxDirectoryDatabase detected."
759 << " Attempting to repair.";
760 if (RepairDatabase(path
)) {
761 UMA_HISTOGRAM_ENUMERATION(kDatabaseRepairHistogramLabel
,
762 DB_REPAIR_SUCCEEDED
, DB_REPAIR_MAX
);
765 UMA_HISTOGRAM_ENUMERATION(kDatabaseRepairHistogramLabel
,
766 DB_REPAIR_FAILED
, DB_REPAIR_MAX
);
767 LOG(WARNING
) << "Failed to repair SandboxDirectoryDatabase.";
769 case DELETE_ON_CORRUPTION
:
770 LOG(WARNING
) << "Clearing SandboxDirectoryDatabase.";
771 if (!base::DeleteFile(filesystem_data_directory_
, true))
773 if (!base::CreateDirectory(filesystem_data_directory_
))
775 return Init(FAIL_ON_CORRUPTION
);
782 bool SandboxDirectoryDatabase::RepairDatabase(const std::string
& db_path
) {
784 leveldb::Options options
;
785 options
.max_open_files
= 0; // Use minimum.
787 options
.env
= env_override_
;
788 if (!leveldb::RepairDB(db_path
, options
).ok())
790 if (!Init(FAIL_ON_CORRUPTION
))
792 if (IsFileSystemConsistent())
798 bool SandboxDirectoryDatabase::IsDirectory(FileId file_id
) {
801 return true; // The root is a directory.
802 if (!GetFileInfo(file_id
, &info
))
804 if (!info
.is_directory())
809 bool SandboxDirectoryDatabase::IsFileSystemConsistent() {
810 if (!Init(FAIL_ON_CORRUPTION
))
812 DatabaseCheckHelper
helper(this, db_
.get(), filesystem_data_directory_
);
813 return helper
.IsFileSystemConsistent();
816 void SandboxDirectoryDatabase::ReportInitStatus(
817 const leveldb::Status
& status
) {
818 base::Time now
= base::Time::Now();
819 const base::TimeDelta minimum_interval
=
820 base::TimeDelta::FromHours(kMinimumReportIntervalHours
);
821 if (last_reported_time_
+ minimum_interval
>= now
)
823 last_reported_time_
= now
;
826 UMA_HISTOGRAM_ENUMERATION(kInitStatusHistogramLabel
,
827 INIT_STATUS_OK
, INIT_STATUS_MAX
);
828 } else if (status
.IsCorruption()) {
829 UMA_HISTOGRAM_ENUMERATION(kInitStatusHistogramLabel
,
830 INIT_STATUS_CORRUPTION
, INIT_STATUS_MAX
);
831 } else if (status
.IsIOError()) {
832 UMA_HISTOGRAM_ENUMERATION(kInitStatusHistogramLabel
,
833 INIT_STATUS_IO_ERROR
, INIT_STATUS_MAX
);
835 UMA_HISTOGRAM_ENUMERATION(kInitStatusHistogramLabel
,
836 INIT_STATUS_UNKNOWN_ERROR
, INIT_STATUS_MAX
);
840 bool SandboxDirectoryDatabase::StoreDefaultValues() {
841 // Verify that this is a totally new database, and initialize it.
842 scoped_ptr
<leveldb::Iterator
> iter(db_
->NewIterator(leveldb::ReadOptions()));
844 if (iter
->Valid()) { // DB was not empty--we shouldn't have been called.
845 LOG(ERROR
) << "File system origin database is corrupt!";
848 // This is always the first write into the database. If we ever add a
849 // version number, it should go in this transaction too.
852 root
.modification_time
= base::Time::Now();
853 leveldb::WriteBatch batch
;
854 if (!AddFileInfoHelper(root
, 0, &batch
))
856 batch
.Put(LastFileIdKey(), base::Int64ToString(0));
857 batch
.Put(LastIntegerKey(), base::Int64ToString(-1));
858 leveldb::Status status
= db_
->Write(leveldb::WriteOptions(), &batch
);
860 HandleError(FROM_HERE
, status
);
866 bool SandboxDirectoryDatabase::GetLastFileId(FileId
* file_id
) {
867 if (!Init(REPAIR_ON_CORRUPTION
))
870 std::string id_string
;
871 leveldb::Status status
=
872 db_
->Get(leveldb::ReadOptions(), LastFileIdKey(), &id_string
);
874 if (!base::StringToInt64(id_string
, file_id
)) {
875 LOG(ERROR
) << "Hit database corruption!";
880 if (!status
.IsNotFound()) {
881 HandleError(FROM_HERE
, status
);
884 // The database must not yet exist; initialize it.
885 if (!StoreDefaultValues())
891 // This does very few safety checks!
892 bool SandboxDirectoryDatabase::AddFileInfoHelper(
893 const FileInfo
& info
, FileId file_id
, leveldb::WriteBatch
* batch
) {
894 if (!VerifyDataPath(info
.data_path
)) {
895 LOG(ERROR
) << "Invalid data path is given: " << info
.data_path
.value();
898 std::string id_string
= GetFileLookupKey(file_id
);
900 // The root directory doesn't need to be looked up by path from its parent.
901 DCHECK(!info
.parent_id
);
902 DCHECK(info
.data_path
.empty());
904 std::string child_key
= GetChildLookupKey(info
.parent_id
, info
.name
);
905 batch
->Put(child_key
, id_string
);
908 if (!PickleFromFileInfo(info
, &pickle
))
912 leveldb::Slice(reinterpret_cast<const char *>(pickle
.data()),
917 // This does very few safety checks!
918 bool SandboxDirectoryDatabase::RemoveFileInfoHelper(
919 FileId file_id
, leveldb::WriteBatch
* batch
) {
920 DCHECK(file_id
); // You can't remove the root, ever. Just delete the DB.
922 if (!GetFileInfo(file_id
, &info
))
924 if (info
.data_path
.empty()) { // It's a directory
925 std::vector
<FileId
> children
;
926 // TODO(ericu): Make a faster is-the-directory-empty check.
927 if (!ListChildren(file_id
, &children
))
929 if (children
.size()) {
930 LOG(ERROR
) << "Can't remove a directory with children.";
934 batch
->Delete(GetChildLookupKey(info
.parent_id
, info
.name
));
935 batch
->Delete(GetFileLookupKey(file_id
));
939 void SandboxDirectoryDatabase::HandleError(
940 const tracked_objects::Location
& from_here
,
941 const leveldb::Status
& status
) {
942 LOG(ERROR
) << "SandboxDirectoryDatabase failed at: "
943 << from_here
.ToString() << " with error: " << status
.ToString();
947 } // namespace storage