App-FileSync
view release on metacpan or search on metacpan
lib/App/FileSync/Snapshot.pm view on Meta::CPAN
=cut
sub restore {
my $args = named(\@_, {
%ROOT,
date => { type => 'string', optional => 1 },
to => { type => 'string', optional => 1 },
paths => { type => 'arrayref', optional => 1 },
dry_run => { type => 'boolean', optional => 1 },
}, 'Snapshot::restore');
my ($root, $date, $to) = @{$args}{qw(root date to)};
throw('snapshot.need_date') unless defined $date && length $date;
throw('snapshot.bad_date', { date => $date }) unless is_date($date);
my $snapshot = File::Spec->catdir(snapshot_root($root), $date);
throw('snapshot.none', { date => $date, path => snapshot_root($root) }) unless -d $snapshot;
throw('snapshot.need_to') unless defined $to && length $to;
ensure_dir($to);
throw('snapshot.to_not_dir', { path => $to }) unless -d $to;
my @paths = @{ $args->{paths} || [] };
@paths = @{ list_dir($snapshot) || [] } unless @paths;
my $counts = { files => 0, bytes => 0 };
for my $rel (@paths) {
my ($safe, $reason) = validate_relpath($rel);
throw('snapshot.unsafe', { path => $rel, reason => $reason }) unless $safe;
my $from = File::Spec->catfile($snapshot, split m{/}, $rel);
throw('snapshot.no_path', { date => $date, path => $rel }) unless -e $from || -l $from;
_copy_out($from, File::Spec->catfile($to, split m{/}, $rel), $counts, $args->{dry_run});
}
return { date => $date, from => $snapshot, to => $to, %{$counts}, paths => \@paths };
}
# _mirror
# Purpose: recreate a tree inside a snapshot, linking or copying files.
# Entry: $dst exists; $at_root is true for the replica root itself,
# where the reserved names live.
# Exit: returns 1.
# Side effects: creates directories and files under $dst; updates $counts.
# Symlinks and special files are not captured, as they are
# not synced either.
sub _mirror :Private {
my ($src, $dst, $mode, $counts, $at_root) = @_;
my $names = list_dir($src) or throw('snapshot.read', { path => $src, error => os_error() });
for my $name (@{$names}) {
# The snapshots, the root marker and temporaries are bookkeeping, not
# content; anything else, even a name starting ".filesync-", is data.
next if $name eq $SNAPSHOT_DIR || $name =~ $TEMP_FILE || ($at_root && $name eq $ROOT_MARKER);
my $from = File::Spec->catfile($src, $name);
my $to = File::Spec->catfile($dst, $name);
my @stat = Time::HiRes::lstat($from) or next;
if (-d _) {
ensure_dir($to);
_mirror($from, $to, $mode, $counts, 0);
chmod($stat[$STAT_MODE] & $MODE_MASK, $to) if $HAS_MODES;
} elsif (-f _) {
# A link shares the inode, so mode and mtime are already right;
# a copy (or a failed link across filesystems) needs them set.
my $linked = $mode eq $HARDLINK && do { no autodie qw(link); link($from, $to) };
copy_preserving($from, $to, \@stat) unless $linked;
$counts->{ $linked ? 'linked' : 'copied' }++;
$counts->{files}++;
$counts->{bytes} += $stat[$STAT_SIZE];
}
}
return 1;
}
# _clean_stale_temps
# Purpose: remove snapshot attempts that died part way.
# Entry: $dir is the snapshot directory.
# Exit: returns 1.
# Side effects: removes .tmp-PID-N directories older than an hour; younger
# ones may belong to a concurrent run.
sub _clean_stale_temps :Private {
my ($dir) = @_;
my $now = time;
for my $name (grep { $_ =~ $TEMP_DIR } @{ list_dir($dir) || [] }) {
my $path = File::Spec->catdir($dir, $name);
my @stat = Time::HiRes::lstat($path);
next unless @stat && -d _ && $now - $stat[$STAT_MTIME] >= $STALE_TEMP_SECONDS;
eval { remove_tree($path) };
}
return 1;
}
# _count
# Purpose: count the regular files and bytes in a snapshot.
# Entry: $dir exists.
# Exit: returns 1.
# Side effects: adds to $counts->{files} and {bytes}.
sub _count :Private {
my ($dir, $counts) = @_;
for my $name (@{ list_dir($dir) || [] }) {
my $path = File::Spec->catfile($dir, $name);
my @stat = Time::HiRes::lstat($path) or next;
if (-d _) {
_count($path, $counts);
} elsif (-f _) {
$counts->{files}++;
$counts->{bytes} += $stat[$STAT_SIZE];
}
}
return 1;
}
# _remove_snapshot
# Purpose: delete one snapshot directory.
# Entry: $path is a dated snapshot.
# Exit: returns 1.
# Side effects: removes the tree; raises snapshot.remove with the first
# reason File::Path gave.
sub _remove_snapshot :Private {
my ($path) = @_;
remove_tree($path, { error => \my $errors });
if (-e $path) {
my ($first) = map { my ($f, $why) = %{$_}; "$f: $why" } @{ $errors || [] };
throw('snapshot.remove', { path => $path, error => defined $first ? $first : os_error() });
}
return 1;
}
# _copy_out
# Purpose: copy a snapshot entry to its destination, replacing it.
# Entry: $src exists in the snapshot.
# Exit: returns 1.
# Side effects: replaces $dst (unless $dry_run); updates $counts.
sub _copy_out :Private {
my ($src, $dst, $counts, $dry_run) = @_;
my @stat = Time::HiRes::lstat($src);
throw('snapshot.cannot_stat', { path => $src, error => os_error() }) unless @stat;
if (-d _) {
ensure_dir($dst) unless $dry_run;
_copy_out(File::Spec->catfile($src, $_), File::Spec->catfile($dst, $_), $counts, $dry_run)
for @{ list_dir($src) || [] };
# Directory metadata last: filling it changed its mtime.
unless ($dry_run) {
chmod($stat[$STAT_MODE] & $MODE_MASK, $dst) if $HAS_MODES;
utime($stat[$STAT_MTIME], $stat[$STAT_MTIME], $dst);
}
return 1;
}
throw('snapshot.not_regular', { path => $src }) unless -f _;
$counts->{files}++;
$counts->{bytes} += $stat[$STAT_SIZE];
return 1 if $dry_run;
# A rollback replaces whatever is in the way, even a directory.
if (-d $dst && !-l $dst) {
remove_tree($dst);
throw('snapshot.replace', { path => $dst }) if -e $dst;
} elsif (-e $dst || -l $dst) {
eval { unlink $dst; 1 } or throw('snapshot.replace', { path => $dst });
}
copy_preserving($src, $dst, \@stat);
return 1;
}
1;
__END__
=head1 LIMITATIONS
=over 4
=item * A hard-linked snapshot shares the file with the live tree. The sync
replaces files by rename, which leaves the snapshot's link intact, but any
other tool that edits a replica file in place also changes every snapshot
that links it. Use C<snapshot_mode copy> if anything but filesync writes
there.
=item * C<list> walks every snapshot to count it, so it is slow with many
large snapshots.
=item * Snapshots live inside the replica root, on the same disk: they guard
against bad syncs, not against losing that disk.
=back
=head1 AUTHOR
Nigel Horne, C<< <njh at nigelhorne.com> >>
=head1 LICENCE AND COPYRIGHT
( run in 1.007 second using v1.01-cache-2.11-cpan-eb134014566 )