Skip to content

Commit

Permalink
[vm] Improve readability of delete_snapshot_helper
Browse files Browse the repository at this point in the history
With a slight reordering and a few comments.
  • Loading branch information
ricab committed Jun 23, 2023
1 parent 88dd9d5 commit d7331c3
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/platform/backends/shared/base_virtual_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,13 @@ auto BaseVirtualMachine::make_parent_update_rollback(

void BaseVirtualMachine::delete_snapshot_helper(const QDir& snapshot_dir, std::shared_ptr<Snapshot>& snapshot)
{
auto snapshot_fileinfo = find_snapshot_file(snapshot_dir, snapshot->get_name());
auto snapshot_filepath = snapshot_fileinfo.filePath();

// Remove snapshot file
QTemporaryDir tmp_dir{};
if (!tmp_dir.isValid())
throw std::runtime_error{"Could not create temporary directory"};

auto snapshot_fileinfo = find_snapshot_file(snapshot_dir, snapshot->get_name());
auto snapshot_filepath = snapshot_fileinfo.filePath();
auto deleting_filepath = tmp_dir.filePath(snapshot_fileinfo.fileName());

if (!QFile{snapshot_filepath}.rename(deleting_filepath))
Expand All @@ -301,17 +301,20 @@ void BaseVirtualMachine::delete_snapshot_helper(const QDir& snapshot_dir, std::s
QFile{deleting_filepath}.rename(snapshot_filepath); // best effort, ignore return
});

// Update head if deleted
auto wrote_head = false;
auto head_path = derive_head_path(snapshot_dir);
auto rollback_head = make_deleted_head_rollback(head_path, wrote_head);
wrote_head = updated_deleted_head(snapshot, head_path);

// Update children of deleted snapshot
std::unordered_map<Snapshot*, QString> updated_snapshot_paths;
updated_snapshot_paths.reserve(snapshots.size());

auto rollback_parent_updates = make_parent_update_rollback(snapshot, updated_snapshot_paths);
update_parents(snapshot_dir, snapshot, updated_snapshot_paths);

// Erase the snapshot with the backend and dismiss rollbacks on success
snapshot->erase();
rollback_parent_updates.dismiss();
rollback_head.dismiss();
Expand Down

0 comments on commit d7331c3

Please sign in to comment.