App-FileSync

 view release on metacpan or  search on metacpan

lib/App/FileSync/Scan.pm  view on Meta::CPAN

use App::FileSync::Platform;

use strict;
use warnings;
use autodie qw(:all);

use Digest::SHA ();
use File::Spec;
use Readonly;
use Sub::Private { mode => 'enforce' };
use Time::HiRes ();

use App::FileSync::Args qw(named);
use App::FileSync::Constants qw($MODE_MASK $TEMP_PREFIX $STALE_TEMP_SECONDS $TYPE_FILE $TYPE_DIR $STAT_MODE $STAT_SIZE $STAT_MTIME $SHA_BITS);
use App::FileSync::I18N qw(message os_error strip_location);
use App::FileSync::IO qw(list_dir);

use version; our $VERSION = version->declare('0.003.1');

# A cached mtime within this much of the file's is the same mtime: JSON
# round-trips the fractional part with float noise.
Readonly::Scalar my $MTIME_EPSILON => 0.001;

# Matches a temporary file left by an interrupted run.
Readonly my $TEMP_NAME => qr/\A\Q$TEMP_PREFIX\E/;

# Describe one regular file, reusing the cached hash when size and mtime
# match.  Lexical rather than a named private sub because it runs once per
# file in the tree; see App::FileSync/LIMITATIONS.
#
# Entry: $context is the scan state; $abs/$rel name the file; $stat is lstat.
# Exit:  returns the manifest entry, or undef after recording the file as
#        skipped (it could not be read).
my $file_entry = sub {
	my ($context, $abs, $rel, $stat) = @_;

	my ($size, $mtime) = @{$stat}[$STAT_SIZE, $STAT_MTIME];
	my $cached = $context->{cache}{$rel};

	my $hash;
	if ($cached
		&& ($cached->{t} || '') eq $TYPE_FILE
		&& defined $cached->{h}
		&& defined $cached->{s} && $cached->{s} == $size
		&& abs((defined $cached->{m} ? $cached->{m} : -1) - $mtime) < $MTIME_EPSILON) {
		$hash = $cached->{h};
		$context->{stats}{cached}++;
	} else {
		my $sha = Digest::SHA->new($SHA_BITS);
		unless (eval { $sha->addfile($abs, 'b'); 1 }) {
			# Digest::SHA croaks with its own location; people see the reason only.
			$context->{skipped}{$rel} = message('scan.unreadable', { error => strip_location($@) });
			$context->{unreadable}{$rel} = 1;
			$context->{stats}{skipped}++;
			return undef;	## no critic (Subroutines::ProhibitExplicitReturnUndef)
		}
		# A writer between the lstat and the read would pair the old size and
		# mtime with the new contents' hash, an entry no upload can satisfy.
		# Such a file is unknown this run, like an unreadable one: neither
		# pushed nor taken for a deletion.
		my @after = Time::HiRes::lstat($abs);
		unless (@after && $after[$STAT_SIZE] == $size && $after[$STAT_MTIME] == $mtime) {
			$context->{skipped}{$rel} = message('scan.changed');
			$context->{unreadable}{$rel} = 1;
			$context->{stats}{skipped}++;
			return undef;	## no critic (Subroutines::ProhibitExplicitReturnUndef)
		}
		$hash = $sha->hexdigest;
		$context->{stats}{hashed}++;
	}

	return {
		t    => $TYPE_FILE,
		s    => $size,
		m    => $mtime,
		h    => $hash,
		mode => $stat->[$STAT_MODE] & $MODE_MASK,
	};
};

=encoding utf8

=head1 NAME

App::FileSync::Scan - describe a directory tree as a manifest

=head1 SYNOPSIS

	my $scan = App::FileSync::Scan::scan(root => $root, ignore => $ignore, cache => $last);
	for my $path (sort keys %{ $scan->{manifest} }) { ... }
	printf "%d hashed, %d reused\n", @{ $scan->{stats} }{qw(hashed cached)};

=head1 FUNCTIONS

=head2 scan

Purpose: walk a tree and describe every regular file and empty directory.

Args: C<root> (required), C<ignore> (an L<App::FileSync::Ignore>), C<cache>
(a previous manifest whose entries are reused when size and mtime still match).

Returns: a hashref with four hashrefs:

	manifest  path => { t => 'f', s => size, m => mtime, h => sha256, mode => bits }
	          path => { t => 'd' }    (only directories empty after ignores)
	stats     { files, dirs, bytes, hashed, cached, skipped }
	skipped   path => reason, for symlinks, special files and unreadable entries
	unreadable path => 1, the skipped paths that exist but could not be read
	          ('.' when the root itself cannot be listed).  Whatever is at
	          or below them is unknown, not deleted.

Side Effects: reads every file that is not cached.  Never writes.

Usage:

	my $scan = App::FileSync::Scan::scan(root => $master, ignore => $ignore);

=head3 EXAMPLE

	my $scan = App::FileSync::Scan::scan(root => '/home/foo/Documents');
	print scalar(keys %{ $scan->{manifest} }), " entries\n";

lib/App/FileSync/Scan.pm  view on Meta::CPAN


Side Effects: deletes C<.filesync-tmp-*> files older than C<max_age> anywhere
under C<root>.  Younger ones may belong to a run in progress and are left
alone.  A file that cannot be removed is skipped.

Usage:

	App::FileSync::Scan::clean_stale_temps(root => $root);

=head3 EXAMPLE

	my $removed = App::FileSync::Scan::clean_stale_temps(root => '/srv/filesync/master/docs', max_age => 600);

=head3 API SPECIFICATION

=head4 INPUT

	{
		root    => { type => 'string', optional => 1 },
		max_age => { type => 'integer', min => 0, optional => 1 },
	}

=head4 OUTPUT

	{ type => 'integer', min => 0 }

=head3 MESSAGES

None; failures to remove are not errors.

=head3 FORMAL SPECIFICATION

	ΔFS
	Stale == { p : dom FS | p under root ∧ prefix(TEMP, basename p) ∧ now − mtime p ≥ age }
	dom FS' = dom FS \ Removable(Stale)
	result = #(dom FS \ dom FS')

=cut

sub clean_stale_temps {
	my $args = named(\@_, {
		root    => { type => 'string', optional => 1 },
		max_age => { type => 'integer', min => 0, optional => 1 },
	}, 'Scan::clean_stale_temps');

	my $root = $args->{root};
	return 0 unless defined $root && length $root && -d $root;

	my $max_age = defined $args->{max_age} ? $args->{max_age} : $STALE_TEMP_SECONDS;
	# Sub-second, to compare like with like: lstat below is Time::HiRes's.
	my $now = Time::HiRes::time();
	my $removed = 0;

	# Breadth first with an explicit queue: no recursion depth limit, and
	# symlinked directories are never followed because lstat says what they are.
	my @queue = ($root);
	while (@queue) {
		my $dir = shift @queue;
		for my $name (@{ list_dir($dir) || [] }) {
			my $path = File::Spec->catfile($dir, $name);
			my @stat = Time::HiRes::lstat($path) or next;
			if (-d _) {
				push @queue, $path;
			} elsif ($name =~ $TEMP_NAME && $now - $stat[$STAT_MTIME] >= $max_age) {
				no autodie qw(unlink);
				$removed++ if unlink $path;
			}
		}
	}

	return $removed;
}

# _walk
# Purpose:     scan one directory, recursing into subdirectories.
# Entry:       $context is the scan state; $abs/$rel name the directory
#              ($rel is '' at the root).
# Exit:        returns how many entries inside were recorded, so the caller
#              knows whether to record this directory as empty; undef when
#              the directory could not be listed, which is not empty.
# Side effects: fills $context->{manifest}, {skipped} and {stats}.
sub _walk :Private {
	my ($context, $abs, $rel) = @_;

	my $names = list_dir($abs);
	unless ($names) {
		_skip($context, length $rel ? $rel : '.', 'scan.cannot_list', os_error());
		return undef;	## no critic (Subroutines::ProhibitExplicitReturnUndef)
	}

	my $entries = 0;
	for my $name (@{$names}) {
		my $child_rel = length $rel ? "$rel/$name" : $name;
		next if $context->{ignore} && $context->{ignore}->match($child_rel);

		my $child_abs = File::Spec->catfile($abs, $name);
		my @stat = Time::HiRes::lstat($child_abs);
		if (!@stat) {
			_skip($context, $child_rel, 'scan.cannot_stat', os_error());
		} elsif (-d _) {
			# An empty directory is worth syncing; a non-empty one is implied
			# by the files inside it.  One that cannot be listed is neither:
			# recording it as empty would describe its files as deleted.
			$entries++;
			my $inside = _walk($context, $child_abs, $child_rel);
			if (defined $inside && $inside == 0) {
				$context->{manifest}{$child_rel} = { t => $TYPE_DIR };
				$context->{stats}{dirs}++;
			}
		} elsif (-f _) {
			my $entry = $file_entry->($context, $child_abs, $child_rel, \@stat) or next;
			$context->{manifest}{$child_rel} = $entry;
			$context->{stats}{files}++;
			$context->{stats}{bytes} += $entry->{s};
			$entries++;
		} else {
			_skip($context, $child_rel, -l _ ? 'scan.symlink' : 'scan.special');
		}
	}

	return $entries;
}

# _skip
# Purpose:     record a path the manifest leaves out, and why.
# Entry:       $key names the reason in the catalogue; $error is the OS
#              reason where there is one.
# Exit:        returns 1.
# Side effects: updates $context->{skipped} and the skipped count, and
#              $context->{unreadable} for a path that exists but could not
#              be read (anything with an OS error, unlike a symlink).
sub _skip :Private {
	my ($context, $rel, $key, $error) = @_;

	$context->{skipped}{$rel} = message($key, { error => $error });
	$context->{unreadable}{$rel} = 1 if defined $error;
	$context->{stats}{skipped}++;
	return 1;
}

1;

__END__

=head1 LIMITATIONS

=over 4

=item * The cache trusts size and mtime.  A tool that rewrites a file and
restores its mtime (some backup restorers do) goes unnoticed until a
C<verify --deep>.

=item * Every run stats every file; there is no change notification.  On a
very large tree the walk, not the transfer, dominates a quiet run.

=item * Directory order is sorted, which costs an O(n log n) per directory
but makes manifests and logs reproducible.



( run in 1.843 second using v1.01-cache-2.11-cpan-eb134014566 )