From 465a3eb92ea615d41ac26ee1e7f0becf20a5cce8 Mon Sep 17 00:00:00 2001 From: Axel Burri Date: Sat, 9 May 2015 16:00:41 +0200 Subject: [PATCH] btrbk: send/receive: delete possibly left-behind garbled subvolume on failure (upstream btrfs-progs does not delete unfinished "btrfs receive") --- btrbk | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/btrbk b/btrbk index 7b35d36..c9d3e61 100755 --- a/btrbk +++ b/btrbk @@ -972,7 +972,8 @@ sub macro_send_receive($@) INFO "Receiving from snapshot: $snapshot->{PRINT}"; # add info to $config->{SUBVOL_RECEIVED} - $info{received_name} = "$target->{PRINT}/$snapshot->{NAME}"; + my $vol_received = vinfo_child($target, $snapshot->{NAME}); + $info{received_subvolume} = $vol_received; $config_target->{SUBVOL_RECEIVED} //= []; push(@{$config_target->{SUBVOL_RECEIVED}}, \%info); @@ -1002,6 +1003,19 @@ sub macro_send_receive($@) } else { $info{ERROR} = 1; $config_target->{ABORTED} = "Failed to send/receive subvolume"; + + # NOTE: btrfs-progs v3.19.1 does not delete garbled received subvolume, + # we need to do this by hand. + # TODO: remove this as soon as btrfs-progs handle receive errors correctly. + DEBUG "send/received failed, deleting (possibly present and garbled) received subvolume: $vol_received->{PRINT}"; + my $ret = btrfs_subvolume_delete($vol_received, commit => "after"); + if(defined($ret)) { + WARN "Deleted partially received (garbled) subvolume: $vol_received->{PRINT}"; + } + else { + WARN "Deletion of partially received (garbled) subvolume failed, assuming clean environment: $vol_received->{PRINT}"; + } + return undef; } } @@ -2002,7 +2016,7 @@ MAIN: $create_mode = ">>>" if($_->{parent}); # substr($create_mode, 0, 1, '%') if($_->{resume}); $create_mode = "!!!" if($_->{ERROR}); - push @out, "$create_mode $_->{received_name}"; + push @out, "$create_mode $_->{received_subvolume}->{PRINT}"; } if($config_target->{SUBVOL_DELETED}) {