1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "storage/browser/quota/storage_directory.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "components/services/storage/public/cpp/constants.h"
#include "storage/browser/quota/storage_directory_util.h"
namespace storage {
namespace {
const base::FilePath::CharType kDoomedPathName[] =
FILE_PATH_LITERAL("-doomed-");
}
StorageDirectory::StorageDirectory(const base::FilePath& profile_path)
: web_storage_path_(profile_path.Append(kWebStorageDirectory)) {
DCHECK(!profile_path.empty()) << "Should not be called in incognito mode.";
}
StorageDirectory::~StorageDirectory() = default;
bool StorageDirectory::Create() {
return base::CreateDirectory(web_storage_path_);
}
bool StorageDirectory::Doom() {
return DoomPath(web_storage_path_);
}
void StorageDirectory::ClearDoomed() {
std::set<base::FilePath> paths = EnumerateDoomedDirectories();
for (const base::FilePath& path : paths)
base::DeletePathRecursively(path);
}
bool StorageDirectory::CreateBucket(const BucketLocator& bucket) {
base::FilePath bucket_path =
CreateBucketPath(web_storage_path_.DirName(), bucket);
return base::CreateDirectory(bucket_path);
}
bool StorageDirectory::DoomBucket(const BucketLocator& bucket) {
base::FilePath bucket_path =
CreateBucketPath(web_storage_path_.DirName(), bucket);
return DoomPath(bucket_path);
}
void StorageDirectory::ClearDoomedBuckets() {
std::set<base::FilePath> paths = EnumerateDoomedBuckets();
for (const base::FilePath& path : paths)
base::DeletePathRecursively(path);
}
bool StorageDirectory::DoomPath(const base::FilePath& path) {
if (!base::PathExists(path))
return true;
base::FilePath doomed_dir;
base::CreateTemporaryDirInDir(
path.DirName(), base::StrCat({path.BaseName().value(), kDoomedPathName}),
&doomed_dir);
return base::Move(path, doomed_dir);
}
std::set<base::FilePath> StorageDirectory::EnumerateDoomedDirectories() {
base::FileEnumerator enumerator(
web_storage_path_.DirName(),
/*recursive=*/false, base::FileEnumerator::DIRECTORIES,
base::StrCat(
{kWebStorageDirectory, kDoomedPathName, FILE_PATH_LITERAL("*")}));
std::set<base::FilePath> paths;
base::FilePath path;
while (path = enumerator.Next(), !path.empty())
paths.insert(path);
return paths;
}
std::set<base::FilePath> StorageDirectory::EnumerateDoomedBuckets() {
base::FileEnumerator enumerator(
web_storage_path_, /*recursive=*/false, base::FileEnumerator::DIRECTORIES,
base::StrCat(
{FILE_PATH_LITERAL("*"), kDoomedPathName, FILE_PATH_LITERAL("*")}));
std::set<base::FilePath> paths;
base::FilePath path;
while (path = enumerator.Next(), !path.empty())
paths.insert(path);
return paths;
}
} // namespace storage
|