File-Copy-Undoable
view release on metacpan or search on metacpan
lib/File/Copy/Undoable.pm view on Meta::CPAN
_
},
rsync_opts => {
schema => [array => {of=>'str*', default=>['-a']}],
summary => 'Rsync options',
description => <<'_',
By default, `-a` is used. You can add, for example, `--delete` or other rsync
options.
_
},
},
features => {
tx => {v=>2},
idempotent => 1,
},
deps => {
prog => 'rsync',
},
};
sub cp {
my %args = @_;
# TMP, schema
my $tx_action = $args{-tx_action} // '';
my $dry_run = $args{-dry_run};
my $source = $args{source};
defined($source) or return [400, "Please specify source"];
my $target = $args{target};
defined($target) or return [400, "Please specify target"];
my $rsync_opts = $args{rsync_opts} // ['-a'];
$rsync_opts = [$rsync_opts] unless ref($rsync_opts) eq 'ARRAY';
if ($tx_action eq 'check_state') {
return [412, "Source $source does not exist"]
unless file_exists($source);
my $te = file_exists($target);
unless ($args{-tx_recovery} || $args{-tx_rollback}) {
# in rollback/recovery, we might need to continue interrupted
# transfer, so we allow target to exist
return [304, "Target $target already exists"] if $te;
}
log_info("(DRY) ".
($te ? "Syncing" : "Copying")." $source -> $target ...")
if $dry_run;
return [200, "$source needs to be ".($te ? "synced":"copied").
" to $target", undef, {undo_actions=>[
["File::Trash::Undoable::trash" => {path=>$target}],
]}];
} elsif ($tx_action eq 'fix_state') {
my @cmd = ("rsync", @$rsync_opts, "$source/", "$target/");
log_info("Rsync-ing $source -> $target ...");
system @cmd;
return [500, "Can't rsync: ".explain_child_error($?)] if $?;
if (defined($args{target_owner}) || defined($args{target_group})) {
if ($> == 0) {
log_info("Chown-ing $target ...");
@cmd = (
"chown", "-Rh",
join("", $args{target_owner}//"", ":",
$args{target_group}//""),
$target);
system @cmd;
return [500, "Can't chown: ".explain_child_error($?)] if $?;
} else {
log_debug("Not running as root, not doing chown");
}
}
return [200, "OK"];
}
[400, "Invalid -tx_action"];
}
1;
# ABSTRACT: Copy file/directory using rsync, with undo support
__END__
=pod
=encoding UTF-8
=head1 NAME
File::Copy::Undoable - Copy file/directory using rsync, with undo support
=head1 VERSION
This document describes version 0.130 of File::Copy::Undoable (from Perl distribution File-Copy-Undoable), released on 2023-11-21.
=head1 FUNCTIONS
=head2 cp
Usage:
cp(%args) -> [$status_code, $reason, $payload, \%result_meta]
Copy fileE<sol>directory using rsync, with undo support.
On do, will copy C<source> to C<target> (which must not exist beforehand). On
undo, will trash C<target>.
Fixed state: C<source> exists and C<target> exists. Content or sizes are not
checked; only existence.
Fixable state: C<source> exists and C<target> doesn't exist.
Unfixable state: C<source> does not exist.
This function is not exported.
This function is idempotent (repeated invocations with same arguments has the same effect as single invocation). This function supports transactions.
Arguments ('*' denotes required arguments):
=over 4
=item * B<rsync_opts> => I<array[str]> (default: ["-a"])
Rsync options.
By default, C<-a> is used. You can add, for example, C<--delete> or other rsync
options.
( run in 1.245 second using v1.01-cache-2.11-cpan-71847e10f99 )