| 1 | package Net::SSH::Any::SCP::Putter::Standard; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Net::SSH::Any::Util qw($debug _debug _debug_dump _first_defined); | ||
| 7 | |||
| 8 | require Net::SSH::Any::SCP::Putter; | ||
| 9 | our @ISA = qw(Net::SSH::Any::SCP::Putter); | ||
| 10 | |||
| 11 | use Carp; | ||
| 12 | |||
| 13 | sub _new { | ||
| 14 | my ($class, $any, $opts, @srcs) = @_; | ||
| 15 | my $target = (@srcs > 1 ? pop @srcs : '.'); | ||
| 16 | my $glob = delete $opts->{glob}; | ||
| 17 | $opts->{send_time} = _first_defined delete($opts->{copy_attr}), 1; | ||
| 18 | my $p = $class->SUPER::_new($any, $opts, $target); | ||
| 19 | if ($glob) { | ||
| 20 | $p->{glob} = 1; | ||
| 21 | require File::Glob; | ||
| 22 | @srcs = map File::Glob::bsd_glob($_), @srcs; | ||
| 23 | } | ||
| 24 | $p->{follow_links} = _first_defined delete($opts->{follow_links}), 1; | ||
| 25 | $p->{srcs} = \@srcs; | ||
| 26 | $p; | ||
| 27 | } | ||
| 28 | |||
| 29 | my @ignore_dirs = grep { defined } map { File::Spec->$_ } qw(curdir updir); | ||
| 30 | |||
| 31 | sub read_dir { | ||
| 32 | my ($p, $parent_action, $dh) = @_; | ||
| 33 | if ($parent_action) { | ||
| 34 | # _debug_dump("parent", $parent_action); | ||
| 35 | while (1) { | ||
| 36 | my $name = readdir $dh; | ||
| 37 | return unless defined $name; | ||
| 38 | redo if grep $name eq $_, @ignore_dirs; | ||
| 39 | my $local_path = File::Spec->join($parent_action->{local_path}, $name); | ||
| 40 | return { name => $name, | ||
| 41 | local_path => $local_path }; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | my $local_path = shift @{$p->{srcs}}; | ||
| 45 | if (defined $local_path) { | ||
| 46 | return { local_path => $local_path, | ||
| 47 | name => (File::Spec->splitpath($local_path))[2] }; | ||
| 48 | } | ||
| 49 | return; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub do_stat { | ||
| 53 | my ($p, $action) = @_; | ||
| 54 | if (not $p->{follow_links} and -l $action->{local_path}) { | ||
| 55 | $debug and $debug & 4096 and _debug "skipping link $action->{path}"; | ||
| 56 | $p->set_local_error($action, "not a regular file"); | ||
| 57 | } | ||
| 58 | elsif (my @s = stat $action->{local_path}) { | ||
| 59 | @{$action}{qw(perm size atime mtime)} = @s[2, 7, 8, 9]; | ||
| 60 | return 1 | ||
| 61 | } | ||
| 62 | else { | ||
| 63 | $p->set_local_error($action); | ||
| 64 | } | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub open_dir { | ||
| 69 | my ($p, $action) = @_; | ||
| 70 | if (opendir my $dh, $action->{local_path}) { | ||
| 71 | return $dh | ||
| 72 | } | ||
| 73 | $p->set_local_error($action); | ||
| 74 | return | ||
| 75 | } | ||
| 76 | |||
| 77 | sub close_dir { | ||
| 78 | my ($p, $action, $dh) = @_; | ||
| 79 | closedir $dh and return 1; | ||
| 80 | $p->set_local_error($action); | ||
| 81 | return; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub open_file { | ||
| 85 | my ($p, $action) = @_; | ||
| 86 | if (open my $fh, '<', $action->{local_path}) { | ||
| 87 | if (binmode $fh) { | ||
| 88 | return $fh | ||
| 89 | } | ||
| 90 | } | ||
| 91 | $p->set_local_error($action); | ||
| 92 | return | ||
| 93 | } | ||
| 94 | |||
| 95 | sub read_file { | ||
| 96 | my ($p, $action, $fh, $len) = @_; | ||
| 97 | my $bytes = read $fh, my($data), $len; | ||
| 98 | unless ($bytes) { | ||
| 99 | $p->set_local_error($action, (defined($bytes) ? "premature EOF reached" : $!)); | ||
| 100 | return; | ||
| 101 | } | ||
| 102 | return $data | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | sub close_file { | ||
| 107 | my ($p, $action, $fh) = @_; | ||
| 108 | close $fh and return 1; | ||
| 109 | $p->set_local_error($action); | ||
| 110 | return; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | 1; |