App-FileSync
view release on metacpan or search on metacpan
lib/App/FileSync/IO.pm view on Meta::CPAN
my $dh;
my $opened = do { no autodie qw(opendir); opendir($dh, $directory) };
return undef unless $opened; ## no critic (Subroutines::ProhibitExplicitReturnUndef)
my @names = sort grep { $_ ne '.' && $_ ne '..' } readdir($dh);
closedir $dh;
return \@names;
}
=head2 copy_preserving
Purpose: copy one regular file, keeping its mode and mtime.
Args: C<$source>, C<$destination>, optional C<\@stat> of the source (saves a
second C<stat> when the caller already has one).
Returns: 1.
Side Effects: creates C<$destination>'s parent directories; overwrites
C<$destination>.
Usage:
copy_preserving($from, $to, \@stat);
=head3 EXAMPLE
copy_preserving('/srv/a.txt', '/backup/a.txt');
=head3 API SPECIFICATION
=head4 INPUT
[
{ name => 'source', type => 'string', min => 1 },
{ name => 'destination', type => 'string', min => 1 },
{ name => 'stat', type => 'arrayref', optional => 1 },
]
=head4 OUTPUT
{ type => 'integer', min => 1, max => 1 }
=head3 MESSAGES
Key Meaning Resolution
io.copy the copy could not be made check space and permissions
=head3 FORMAL SPECIFICATION
ÎFS
src?, dst? : PATH
FS'(dst?) = FS(src?) â§ mtime'(dst?) = mtime(src?) â§ mode'(dst?) = mode(src?)
=cut
sub copy_preserving {
my ($source, $destination, $stat) = @_;
$stat = [ Time::HiRes::stat($source) ] unless ref($stat) eq 'ARRAY' && @{$stat};
ensure_dir(dirname($destination));
File::Copy::copy($source, $destination)
or throw('io.copy', { from => $source, to => $destination, error => "$!" });
chmod($stat->[$STAT_MODE] & $MODE_MASK, $destination) if $HAS_MODES;
utime($stat->[$STAT_MTIME], $stat->[$STAT_MTIME], $destination);
return 1;
}
=head2 append_line
Purpose: append one line to a log or journal.
Args: C<$path>, C<$line> (without a newline).
Returns: 1.
Side Effects: creates C<$path> and its directory if needed. If the file ends
in a torn line from a killed run, a newline is written first so the new entry
is not glued to the fragment.
Usage:
append_line($journal, json_encode($event));
=head3 EXAMPLE
append_line('/tmp/run.log', 'started');
=head3 API SPECIFICATION
=head4 INPUT
[
{ name => 'path', type => 'string', min => 1 },
{ name => 'line', type => 'string' },
]
=head4 OUTPUT
{ type => 'integer', min => 1, max => 1 }
=head3 MESSAGES
Key Meaning Resolution
io.append the file cannot be appended to check permissions and space
=head3 FORMAL SPECIFICATION
ÎFS
path? : PATH ; line? : seq CHAR
let old = FS(path?) if path? â dom FS, else â¨â©
let sep = "\n" if old â â¨â© â§ last old â "\n", else â¨â©
FS'(path?) = old â sep â line? â "\n"
=cut
sub append_line {
( run in 1.358 second using v1.01-cache-2.11-cpan-eb134014566 )