| 1 | package Net::SSH::Any::SCP::Getter::Standard; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use File::Spec; | ||
| 7 | use Carp; | ||
| 8 | use Fcntl (); | ||
| 9 | use Net::SSH::Any::Util qw($debug _debug _debugf _debug_hexdump _first_defined); | ||
| 10 | use Net::SSH::Any::Constants (); | ||
| 11 | |||
| 12 | require Net::SSH::Any::SCP::Getter; | ||
| 13 | our @ISA = qw(Net::SSH::Any::SCP::Getter); | ||
| 14 | |||
| 15 | sub _new { | ||
| 16 | my ($class, $any, $opts, @srcs) = @_; | ||
| 17 | my $target = (@srcs > 1 ? pop @srcs : File::Spec->curdir); | ||
| 18 | my $copy_attr = _first_defined delete($opts->{copy_attr}), 1; | ||
| 19 | my $copy_perm = _first_defined delete($opts->{copy_perm}), $copy_attr; | ||
| 20 | my $copy_time = _first_defined delete($opts->{copy_time}), $copy_attr; | ||
| 21 | my $update = delete $opts->{update}; | ||
| 22 | my $numbered = delete $opts->{numbered}; | ||
| 23 | my $overwrite = _first_defined delete($opts->{overwrite}), !$numbered; | ||
| 24 | $opts->{request_time} = 1 if ($copy_time or $update); | ||
| 25 | my $g = $class->SUPER::_new($any, $opts, @srcs); | ||
| 26 | $g->{lcwd} = []; | ||
| 27 | $g->{copy_perm} = $copy_perm; | ||
| 28 | $g->{copy_time} = $copy_time; | ||
| 29 | $g->{target} = $target; | ||
| 30 | $g->{keep_after_error} = delete $opts->{keep_after_error}; | ||
| 31 | $g->{target_is_dir} = delete $opts->{target_is_dir}; | ||
| 32 | $g->{update} = $update; | ||
| 33 | $g->{numbered} = $numbered; | ||
| 34 | $g->{overwrite} = $overwrite; | ||
| 35 | croak "bad combination of options, numbered can not be used together with overwrite or update" | ||
| 36 | if $numbered and ($update or $overwrite); | ||
| 37 | croak "bad combination of options, update requires overwrite" | ||
| 38 | if $update and not $overwrite; | ||
| 39 | |||
| 40 | $g; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub run { | ||
| 44 | my $g = shift; | ||
| 45 | my $target = $g->{target}; | ||
| 46 | if ($g->{target_is_dir}) { | ||
| 47 | unless (-d $target or mkdir $target) { | ||
| 48 | $g->_or_set_error(Net::SSH::Any::Constants::SSHA_SCP_ERROR(), | ||
| 49 | "unable to create directory", $!); | ||
| 50 | return | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | push @{$g->{lcwd}}, $target if -d $target; | ||
| 55 | |||
| 56 | $g->SUPER::run(@_); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub _resolve_local_path { | ||
| 60 | my ($g, $name) = @_; | ||
| 61 | return ( @{$g->{lcwd}} | ||
| 62 | ? File::Spec->join($g->{lcwd}[-1], $name) | ||
| 63 | : $g->{target} ) | ||
| 64 | } | ||
| 65 | |||
| 66 | sub on_open_before_wanted { | ||
| 67 | my ($g, $action) = @_; | ||
| 68 | $action->{local_path} = $g->_resolve_local_path($action->{name}); | ||
| 69 | 1; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub _perm { | ||
| 73 | my ($g, $action) = @_; | ||
| 74 | $g->{copy_perm} ? $action->{perm} & 0777 : 0777 | ||
| 75 | } | ||
| 76 | |||
| 77 | sub on_open_dir { | ||
| 78 | my ($g, $action) = @_; | ||
| 79 | my $dn = $action->{local_path}; | ||
| 80 | |||
| 81 | my $perm = $g->_perm($action); | ||
| 82 | unless (-d $dn or mkdir($dn, 0700 | $perm)) { | ||
| 83 | $g->set_local_error($action); | ||
| 84 | return; | ||
| 85 | } | ||
| 86 | |||
| 87 | push @{$g->{lcwd}}, $dn; | ||
| 88 | |||
| 89 | 1; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub on_close_dir { | ||
| 93 | my ($g, $action) = @_; | ||
| 94 | pop @{$g->{lcwd}}; | ||
| 95 | |||
| 96 | my $perm = $g->_perm($action); | ||
| 97 | if (($perm | 0700) != $perm) { | ||
| 98 | chmod $perm, $action->{local_path} | ||
| 99 | } | ||
| 100 | 1; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub on_open_file { | ||
| 104 | my ($g, $action) = @_; | ||
| 105 | my $fn = $action->{local_path}; | ||
| 106 | my $perm = $g->_perm($action); | ||
| 107 | |||
| 108 | if ($g->{update}) { | ||
| 109 | if (my @s = stat $fn) { | ||
| 110 | if ($s[7] == $g->{size} and $s[9] == $g->{mtime}) { | ||
| 111 | $action->{already_up_to_date} = 1; | ||
| 112 | return; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | unlink $fn if $g->{overwrite}; | ||
| 118 | my $flags = Fcntl::O_CREAT|Fcntl::O_WRONLY; | ||
| 119 | $flags |= Fcntl::O_EXCL if $g->{numbered} or not $g->{overwrite}; | ||
| 120 | |||
| 121 | my $cfh; | ||
| 122 | while (1) { | ||
| 123 | sysopen $cfh, $fn, $flags, $perm and last; | ||
| 124 | my $error = $!; | ||
| 125 | unless ($g->{numbered} and -e $fn) { | ||
| 126 | $g->set_local_error($action, $error); | ||
| 127 | return; | ||
| 128 | } | ||
| 129 | _inc_numbered($fn); | ||
| 130 | $action->{local_path} = $fn; | ||
| 131 | } | ||
| 132 | |||
| 133 | binmode $cfh; | ||
| 134 | $g->{cfh} = $cfh; | ||
| 135 | |||
| 136 | 1; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub on_write { | ||
| 140 | my ($g, $action) = @_; | ||
| 141 | # $debug and $debug & 4096 and _debug_hexdump('data received', $_[2]); | ||
| 142 | print {$g->{cfh}} $_[2]; | ||
| 143 | 1; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub on_close_file { | ||
| 147 | my ($g, $action, $failed) = @_; | ||
| 148 | $debug and $debug & 4096 and _debugf("%s->on_close_file(%s, %s)", $g, $action, $failed); | ||
| 149 | |||
| 150 | my $cfh = delete $g->{cfh} | ||
| 151 | or croak "internal error: on_close_file called but there isn't file handle"; | ||
| 152 | |||
| 153 | unless (close $cfh) { | ||
| 154 | $g->set_local_error($action); | ||
| 155 | $failed = 1; | ||
| 156 | } | ||
| 157 | |||
| 158 | unlink $action->{local_path} if ($failed and not $g->{keep_after_error}); | ||
| 159 | not $failed; | ||
| 160 | } | ||
| 161 | |||
| 162 | 1; |