Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7615cb18 authored by Nikita Ioffe's avatar Nikita Ioffe Committed by android-build-merger
Browse files

Merge "installd: clear existing snapshots before performing a new one" am:...

Merge "installd: clear existing snapshots before performing a new one" am: 0b643d51 am: ab0a1001
am: 168b2bfa

Change-Id: Ia8b20612de2e1adb541a0e6a099f7477adcdb4a9
parents 63ded34b 168b2bfa
Loading
Loading
Loading
Loading
+13 −5
Original line number Diff line number Diff line
@@ -808,11 +808,6 @@ static int32_t copy_directory_recursive(const char* from, const char* to) {
// TODO(narayan): For snapshotAppData as well as restoreAppDataSnapshot, we
// should validate that volumeUuid is either nullptr or TEST, we won't support
// anything else.
//
// TODO(narayan): We need to be clearer about the expected behaviour for the
// case where a snapshot already exists. We either need to clear the contents
// of the snapshot directory before we make a copy, or we need to ensure that
// the caller always clears it before requesting a snapshot.
binder::Status InstalldNativeService::snapshotAppData(
        const std::unique_ptr<std::string>& volumeUuid,
        const std::string& packageName, int32_t user, int32_t storageFlags) {
@@ -857,6 +852,12 @@ binder::Status InstalldNativeService::snapshotAppData(
        auto from = create_data_user_de_package_path(volume_uuid, user, package_name);
        auto to = create_data_misc_de_rollback_path(volume_uuid, user);

        int rd = delete_dir_contents(to, true /* ignore_if_missing */);
        if (rd != 0) {
            res = error(rd, "Failed clearing existing snapshot " + to);
            return res;
        }

        int rc = copy_directory_recursive(from.c_str(), to.c_str());
        if (rc != 0) {
            res = error(rc, "Failed copying " + from + " to " + to);
@@ -868,6 +869,13 @@ binder::Status InstalldNativeService::snapshotAppData(
    if (storageFlags & FLAG_STORAGE_CE) {
        auto from = create_data_user_ce_package_path(volume_uuid, user, package_name);
        auto to = create_data_misc_ce_rollback_path(volume_uuid, user);

        int rd = delete_dir_contents(to, true /* ignore_if_missing */);
        if (rd != 0) {
            res = error(rd, "Failed clearing existing snapshot " + to);
            return res;
        }

        int rc = copy_directory_recursive(from.c_str(), to.c_str());
        if (rc != 0) {
            res = error(rc, "Failed copying " + from + " to " + to);
+49 −0
Original line number Diff line number Diff line
@@ -363,6 +363,55 @@ TEST_F(ServiceTest, CreateAppDataSnapshot_AppDataAbsent) {
  ASSERT_EQ(-1, stat((rollback_de_dir + "/com.foo").c_str(), &sb));
}

TEST_F(ServiceTest, CreateAppDataSnapshot_ClearsExistingSnapshot) {
  auto rollback_ce_dir = create_data_misc_ce_rollback_path("TEST", 0);
  auto rollback_de_dir = create_data_misc_de_rollback_path("TEST", 0);

  ASSERT_TRUE(mkdirs(rollback_ce_dir, 700));
  ASSERT_TRUE(mkdirs(rollback_de_dir, 700));

  auto fake_package_ce_path = create_data_user_ce_package_path("TEST", 0, "com.foo");
  auto fake_package_de_path = create_data_user_de_package_path("TEST", 0, "com.foo");

  ASSERT_TRUE(mkdirs(fake_package_ce_path, 700));
  ASSERT_TRUE(mkdirs(fake_package_de_path, 700));

  auto deleter = [&rollback_ce_dir, &rollback_de_dir,
          &fake_package_ce_path, &fake_package_de_path]() {
      delete_dir_contents(rollback_ce_dir, true);
      delete_dir_contents(rollback_de_dir, true);
      delete_dir_contents(fake_package_ce_path, true);
      delete_dir_contents(fake_package_de_path, true);
      rmdir(rollback_ce_dir.c_str());
      rmdir(rollback_de_dir.c_str());
  };
  auto scope_guard = android::base::make_scope_guard(deleter);

  // Simulate presence of an existing snapshot
  ASSERT_TRUE(android::base::WriteStringToFile(
          "TEST_CONTENT_CE", rollback_ce_dir + "/file1",
          0700, 10000, 20000, false /* follow_symlinks */));
  ASSERT_TRUE(android::base::WriteStringToFile(
          "TEST_CONTENT_DE", rollback_de_dir + "/file1",
          0700, 10000, 20000, false /* follow_symlinks */));

  // Create app data.
  ASSERT_TRUE(android::base::WriteStringToFile(
          "TEST_CONTENT_2_CE", fake_package_ce_path + "/file2",
          0700, 10000, 20000, false /* follow_symlinks */));
  ASSERT_TRUE(android::base::WriteStringToFile(
          "TEST_CONTENT_2_DE", fake_package_de_path + "/file2",
          0700, 10000, 20000, false /* follow_symlinks */));

  ASSERT_TRUE(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_DE | FLAG_STORAGE_CE).isOk());

  // Previous snapshot (with data for file1) must be cleared.
  struct stat sb;
  ASSERT_EQ(-1, stat((rollback_ce_dir + "/com.foo/file1").c_str(), &sb));
  ASSERT_EQ(-1, stat((rollback_de_dir + "/com.foo/file1").c_str(), &sb));
}

TEST_F(ServiceTest, RestoreAppDataSnapshot) {
  auto rollback_ce_dir = create_data_misc_ce_rollback_path("TEST", 0);
  auto rollback_de_dir = create_data_misc_de_rollback_path("TEST", 0);