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

Commit 332360fe authored by David Anderson's avatar David Anderson Committed by android-build-merger
Browse files

Merge "libsnapshot: Remove all image data and metadata when cancelling an update."

am: 710d6cc4

Change-Id: If47a068143c7416f1b6aa5e479996f26403a6dd6
parents 174f8d8b 710d6cc4
Loading
Loading
Loading
Loading
+24 −1
Original line number Diff line number Diff line
@@ -129,6 +129,11 @@ bool SnapshotManager::BeginUpdate() {
    auto file = LockExclusive();
    if (!file) return false;

    // Purge the ImageManager just in case there is a corrupt lp_metadata file
    // lying around. (NB: no need to return false on an error, we can let the
    // update try to progress.)
    images_->RemoveAllImages();

    auto state = ReadUpdateState(file.get());
    if (state != UpdateState::None) {
        LOG(ERROR) << "An update is already in progress, cannot begin a new update";
@@ -1182,8 +1187,26 @@ bool SnapshotManager::RemoveAllSnapshots(LockedFile* lock) {
    }

    bool ok = true;
    bool has_mapped_cow_images = false;
    for (const auto& name : snapshots) {
        ok &= (UnmapPartitionWithSnapshot(lock, name) && DeleteSnapshot(lock, name));
        if (!UnmapPartitionWithSnapshot(lock, name) || !DeleteSnapshot(lock, name)) {
            // Remember whether or not we were able to unmap the cow image.
            auto cow_image_device = GetCowImageDeviceName(name);
            has_mapped_cow_images |= images_->IsImageMapped(cow_image_device);

            ok = false;
        }
    }

    if (ok || !has_mapped_cow_images) {
        // Delete any image artifacts as a precaution, in case an update is
        // being cancelled due to some corrupted state in an lp_metadata file.
        // Note that we do not do this if some cow images are still mapped,
        // since we must not remove backing storage if it's in use.
        if (!images_->RemoveAllImages()) {
            LOG(ERROR) << "Could not remove all snapshot artifacts";
            return false;
        }
    }
    return ok;
}