App-rsync-new2old

 view release on metacpan or  search on metacpan

script/rsync-new2old  view on Meta::CPAN

#!perl

our $DATE = '2019-03-28'; # DATE
our $VERSION = '0.008'; # VERSION

use strict;
use warnings;

use File::Path qw(make_path);
use File::Spec;
use File::Which;
use Getopt::Long;

sub process_path {
    my $path = shift;
    my $path_ends_in_slash = $path =~ s{/+\z}{} ? 1:0;
    my $abs_path = File::Spec->rel2abs($path);
    my (undef, undef, $path_leaf) = File::Spec->splitpath($abs_path);
    return ($abs_path, $path_leaf, $path_ends_in_slash);
}

sub find_newest_mtime {
    my $path = shift;

    my @st = lstat($path) or return (0, $path);
    my $is_dir = (-d _);
    my $mtime = $st[9];
    if ($is_dir) {
        opendir my($dh), $path
            or die "rsync-new2old: Can't opendir $path: $!\n";
        my @entries = grep { $_ ne '.' && $_ ne '..' } readdir($dh);
        my @res = map { [find_newest_mtime("$path/$_")] } @entries;
        my $max_path = $path;
        my $max_mtime = $mtime;
        for my $r (@res) {
            if ($r->[0] > $max_mtime) {
                $max_mtime = $r->[0];
                $max_path = $r->[1];
            }
        }
        return ($max_mtime, $max_path);
    }
    ($mtime, $path);
}

my %Opts = (create_target_if_not_exists => 0);

Getopt::Long::Configure('bundling', 'pass_through', 'no_auto_abbrev', 'permute');
GetOptions(
    'help|h|?' => sub {
        print <<'_';
Usage: rsync-new2old [options] <source> <target>

Options:
  --help, -h, -?  Show this message and exit.
  --version       Show program version and exit.
  --create-target-if-not-exists
                  Create target if not exists.

All the other options will be passed to rsync.

See manpage for more detailed documentation.
_
        exit 0;
    },
    'version' => sub {
        no warnings 'once';
        print "rsync-new2old version ", ($main::VERSION || "dev"),
            ($main::DATE ? " ($main::DATE)" : ""), "\n";
        exit 0;
    },
    'create-target-if-not-exists' => \$Opts{create_target_if_not_exists},
);

my ($source, $target);
for (@ARGV) {
    if (/\A-/) {
        next;
    } elsif (!defined($source)) {
        $source = $_;
    } elsif (!defined($target)) {
        $target = $_;
    } else {
        last;
    }



( run in 0.594 second using v1.01-cache-2.11-cpan-39bf76dae61 )