Commit 03585e0a authored by Yann Dago's avatar Yann Dago Committed by Commit Bot

User data snapshot: Snapshot SQLite journal files

Change-Id: I7d83599340c971cb585b32f41d862104ef639cb0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2092157
Commit-Queue: Yann Dago <ydago@chromium.org>
Reviewed-by: default avatarGreg Thompson <grt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#749212}
parent 29ab3ab4
...@@ -23,6 +23,13 @@ namespace downgrade { ...@@ -23,6 +23,13 @@ namespace downgrade {
namespace { namespace {
constexpr base::FilePath::StringPieceType kSQLiteJournalSuffix(
FILE_PATH_LITERAL("-journal"));
constexpr base::FilePath::StringPieceType kSQLiteWalSuffix(
FILE_PATH_LITERAL("-wal"));
constexpr base::FilePath::StringPieceType kSQLiteShmSuffix(
FILE_PATH_LITERAL("-shm"));
// These values are persisted to logs. Entries should not be renumbered and // These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused. // numeric values should never be reused.
enum class SnapshotOperationResult { enum class SnapshotOperationResult {
...@@ -34,25 +41,45 @@ enum class SnapshotOperationResult { ...@@ -34,25 +41,45 @@ enum class SnapshotOperationResult {
}; };
// Copies the item at |user_data_dir|/|relative_path| to // Copies the item at |user_data_dir|/|relative_path| to
// |snapshot_dir|/|relative_path| if the item exists. // |snapshot_dir|/|relative_path| if the item exists. This also copies all files
// Returns |true| if the item was found at the source and successfully copied. // related to items that are SQLite databases. Returns |true| if the item was
// Returns |false| if the item was found at the source but not successfully // found at the source and successfully copied. Returns |false| if the item was
// copied. Returns no value if the file was not at the source. // found at the source but not successfully copied. Returns no value if the file
// was not at the source.
base::Optional<bool> CopyItemToSnapshotDirectory( base::Optional<bool> CopyItemToSnapshotDirectory(
const base::FilePath& relative_path, const base::FilePath& relative_path,
const base::FilePath& user_data_dir, const base::FilePath& user_data_dir,
const base::FilePath& snapshot_dir, const base::FilePath& snapshot_dir,
bool is_directory) { bool is_directory) {
auto source = user_data_dir.Append(relative_path); const auto source = user_data_dir.Append(relative_path);
auto destination = snapshot_dir.Append(relative_path); const auto destination = snapshot_dir.Append(relative_path);
// If nothing exists to be moved, do not consider it a success or a failure. // If nothing exists to be moved, do not consider it a success or a failure.
if (!base::PathExists(source)) if (!base::PathExists(source))
return base::nullopt; return base::nullopt;
return is_directory bool copy_success = is_directory ? base::CopyDirectory(source, destination,
? base::CopyDirectory(source, destination, /*recursive=*/true) /*recursive=*/true)
: base::CopyFile(source, destination); : base::CopyFile(source, destination);
if (is_directory)
return copy_success;
// Copy SQLite journal, WAL and SHM files associated with the files that are
// snapshotted if they exist.
for (const auto& suffix :
{kSQLiteJournalSuffix, kSQLiteWalSuffix, kSQLiteShmSuffix}) {
const auto sqlite_file_path =
base::FilePath(source.value() + base::FilePath::StringType(suffix));
if (!base::PathExists(sqlite_file_path))
continue;
const auto destination_journal = base::FilePath(
destination.value() + base::FilePath::StringType(suffix));
copy_success &= base::CopyFile(sqlite_file_path, destination_journal);
}
return copy_success;
} }
// Returns true if |base_name| matches a user profile directory's format. This // Returns true if |base_name| matches a user profile directory's format. This
......
...@@ -37,6 +37,14 @@ constexpr base::FilePath::StringPieceType kUserDataFile = ...@@ -37,6 +37,14 @@ constexpr base::FilePath::StringPieceType kUserDataFile =
FILE_PATH_LITERAL("User Data File"); FILE_PATH_LITERAL("User Data File");
constexpr base::FilePath::StringPieceType kProfileDataFile = constexpr base::FilePath::StringPieceType kProfileDataFile =
FILE_PATH_LITERAL("Profile Data File"); FILE_PATH_LITERAL("Profile Data File");
constexpr base::FilePath::StringPieceType kProfileDataJournalFile =
FILE_PATH_LITERAL("Profile Data File-journal");
constexpr base::FilePath::StringPieceType kProfileDataExtFile =
FILE_PATH_LITERAL("Profile Data File.ext");
constexpr base::FilePath::StringPieceType kProfileDataExtWalFile =
FILE_PATH_LITERAL("Profile Data File.ext-wal");
constexpr base::FilePath::StringPieceType kProfileDataExtShmFile =
FILE_PATH_LITERAL("Profile Data File.ext-shm");
constexpr std::array<base::FilePath::StringPieceType, 3> constexpr std::array<base::FilePath::StringPieceType, 3>
kProfileDirectoryBaseNames = {FILE_PATH_LITERAL("Default"), kProfileDirectoryBaseNames = {FILE_PATH_LITERAL("Default"),
...@@ -120,6 +128,8 @@ class TestSnapshotManager : public SnapshotManager { ...@@ -120,6 +128,8 @@ class TestSnapshotManager : public SnapshotManager {
return std::vector<SnapshotItemDetails>{ return std::vector<SnapshotItemDetails>{
SnapshotItemDetails(base::FilePath(kProfileDataFile), SnapshotItemDetails(base::FilePath(kProfileDataFile),
SnapshotItemDetails::ItemType::kFile, 0), SnapshotItemDetails::ItemType::kFile, 0),
SnapshotItemDetails(base::FilePath(kProfileDataExtFile),
SnapshotItemDetails::ItemType::kFile, 0),
SnapshotItemDetails(base::FilePath(kProfileDataFolder), SnapshotItemDetails(base::FilePath(kProfileDataFolder),
SnapshotItemDetails::ItemType::kDirectory, 0)}; SnapshotItemDetails::ItemType::kDirectory, 0)};
} }
...@@ -175,6 +185,15 @@ TEST_F(SnapshotManagerTest, TakeSnapshot) { ...@@ -175,6 +185,15 @@ TEST_F(SnapshotManagerTest, TakeSnapshot) {
// Files and folders at Profile Data level that should be snapshotted. // Files and folders at Profile Data level that should be snapshotted.
base::File file(path.Append(kProfileDataFile), base::File file(path.Append(kProfileDataFile),
base::File::FLAG_CREATE | base::File::FLAG_WRITE); base::File::FLAG_CREATE | base::File::FLAG_WRITE);
base::File file_ext(path.Append(kProfileDataExtFile),
base::File::FLAG_CREATE | base::File::FLAG_WRITE);
base::File file_journal(path.Append(kProfileDataJournalFile),
base::File::FLAG_CREATE | base::File::FLAG_WRITE);
base::File file_ext_wal(path.Append(kProfileDataExtWalFile),
base::File::FLAG_CREATE | base::File::FLAG_WRITE);
base::File file_ext_shm(path.Append(kProfileDataExtShmFile),
base::File::FLAG_CREATE | base::File::FLAG_WRITE);
ASSERT_NO_FATAL_FAILURE(TestFolderAndFiles::CreateFilesAndFolders( ASSERT_NO_FATAL_FAILURE(TestFolderAndFiles::CreateFilesAndFolders(
path.Append(kProfileDataFolder))); path.Append(kProfileDataFolder)));
...@@ -196,6 +215,10 @@ TEST_F(SnapshotManagerTest, TakeSnapshot) { ...@@ -196,6 +215,10 @@ TEST_F(SnapshotManagerTest, TakeSnapshot) {
for (const auto& path : absolute_profile_paths) { for (const auto& path : absolute_profile_paths) {
EXPECT_TRUE(base::PathExists(path.Append(kProfileDataFile))); EXPECT_TRUE(base::PathExists(path.Append(kProfileDataFile)));
EXPECT_TRUE(base::PathExists(path.Append(kProfileDataExtFile)));
EXPECT_TRUE(base::PathExists(path.Append(kProfileDataJournalFile)));
EXPECT_TRUE(base::PathExists(path.Append(kProfileDataExtWalFile)));
EXPECT_TRUE(base::PathExists(path.Append(kProfileDataExtShmFile)));
EXPECT_TRUE(base::DirectoryExists(path.Append(kProfileDataFolder))); EXPECT_TRUE(base::DirectoryExists(path.Append(kProfileDataFolder)));
EXPECT_TRUE(TestFolderAndFiles::AllPathExists(path)); EXPECT_TRUE(TestFolderAndFiles::AllPathExists(path));
} }
...@@ -203,6 +226,14 @@ TEST_F(SnapshotManagerTest, TakeSnapshot) { ...@@ -203,6 +226,14 @@ TEST_F(SnapshotManagerTest, TakeSnapshot) {
for (const auto& path : kProfileDirectoryBaseNames) { for (const auto& path : kProfileDirectoryBaseNames) {
EXPECT_TRUE( EXPECT_TRUE(
base::PathExists(snapshot_dir.Append(path).Append(kProfileDataFile))); base::PathExists(snapshot_dir.Append(path).Append(kProfileDataFile)));
EXPECT_TRUE(base::PathExists(
snapshot_dir.Append(path).Append(kProfileDataJournalFile)));
EXPECT_TRUE(base::PathExists(
snapshot_dir.Append(path).Append(kProfileDataExtFile)));
EXPECT_TRUE(base::PathExists(
snapshot_dir.Append(path).Append(kProfileDataExtWalFile)));
EXPECT_TRUE(base::PathExists(
snapshot_dir.Append(path).Append(kProfileDataExtShmFile)));
EXPECT_TRUE(base::DirectoryExists( EXPECT_TRUE(base::DirectoryExists(
snapshot_dir.Append(path).Append(kProfileDataFolder))); snapshot_dir.Append(path).Append(kProfileDataFolder)));
EXPECT_TRUE(TestFolderAndFiles::NoPathExists(snapshot_dir.Append(path))); EXPECT_TRUE(TestFolderAndFiles::NoPathExists(snapshot_dir.Append(path)));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment