| 1 | # $Id: SFTP.pm,v 1.35 2005/10/05 06:19:36 dbrobins Exp $ | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Net::SFTP; | ||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Net::SFTP::Constants qw( :fxp :flags :status :att SSH2_FILEXFER_VERSION ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Net::SFTP::Util qw( fx2txt ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Net::SFTP::Attributes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Net::SFTP::Buffer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Net::SSH::Perl::Constants qw( :msg2 ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Net::SSH::Perl 2.12; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Carp qw( carp croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | $VERSION = '0.11'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use constant COPY_SIZE => 65536; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | sub new { | ||||||||||||||||||||||||||||
| 21 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 0 | my $sftp = bless { }, $class; | |||||||||||||||||||||||||||
| 23 | 0 | $sftp->{host} = shift; | |||||||||||||||||||||||||||
| 24 | 0 | $sftp->init(@_); | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # call the warning handler with the object and message | ||||||||||||||||||||||||||||
| 28 | sub warn { | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | my ($sftp,$msg,$status) = @_; | ||||||||||||||||||||||||||
|
Subroutine
warn: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | $msg .= ': '.fx2txt($status) if defined $status; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | $sftp->{status} = $status || SSH2_FX_OK; | ||||||||||||||||||||||||||
|
Condition0%0/3
$status || &SSH2_FX_OK()
MC/DC0%0/2
$status || &SSH2_FX_OK()
$status
&SSH2_FX_OK()
Truth table0%0/3
A || B
A= $statusB= &SSH2_FX_OK()
| |||||||||||||||||||||||||||||
| 32 | 0 | $sftp->{warn_h}->($sftp,$msg); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # returns last SSH error, or SSH2_FX_OK (only useful after failure) | ||||||||||||||||||||||||||||
| 36 | sub status { | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
status: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 0 | my $status = $sftp->{status}; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | wantarray ? ($status,fx2txt($status)) : $status | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # returns the new object | ||||||||||||||||||||||||||||
| 43 | sub init { | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 0 | my %param = @_; | |||||||||||||||||||||||||||
| 46 | 0 | $sftp->{debug} = delete $param{debug}; | |||||||||||||||||||||||||||
| 47 | 0 | $sftp->{status} = SSH2_FX_OK; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $param{ssh_args} ||= []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$param{"ssh_args"} ||= []
MC/DC0%0/1
$param{"ssh_args"} ||= []
$param{"ssh_args"}
Truth table0%0/2
A
A= $param{"ssh_args"}
| |||||||||||||||||||||||||||||
| 50 | 0 | $param{ssh_args} = [%{$param{ssh_args}}] | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | if UNIVERSAL::isa($param{ssh_args},'HASH'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $param{warn} = 1 if not defined $param{warn}; # default | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $sftp->{warn_h} = delete $param{warn} || sub {}; # false => ignore | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
delete $param{'warn'} || sub {
}
MC/DC0%0/2
delete $param{'warn'} || sub {
}
delete $param{'warn'}
sub {
}
Truth table0%0/3
A || B
A= delete $param{'warn'}B= sub {
}
| |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | $sftp->{warn_h} = sub { carp $_[1] } # true => emit warning | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if $sftp->{warn_h} and not ref $sftp->{warn_h}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$sftp->{"warn_h"} and not ref $sftp->{"warn_h"}
MC/DC0%0/2
$sftp->{"warn_h"} and not ref $sftp->{"warn_h"}
$sftp->{"warn_h"}
not ref $sftp->{"warn_h"}
Truth table0%0/3
A and not(B)
A= $sftp->{"warn_h"}B= not ref $sftp->{"warn_h"}
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | $sftp->{_msg_id} = 0; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | my $ssh = Net::SSH::Perl->new($sftp->{host}, protocol => 2, | ||||||||||||||||||||||||||||
| 61 | 0 | debug => $sftp->{debug}, @{ $param{ssh_args} }); | |||||||||||||||||||||||||||
| 62 | 0 | $ssh->login($param{user}, $param{password}, 'supress_shell'); | |||||||||||||||||||||||||||
| 63 | 0 | $sftp->{ssh} = $ssh; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | my $channel = $sftp->_open_channel; | |||||||||||||||||||||||||||
| 66 | 0 | $sftp->{channel} = $channel; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | $sftp->do_init; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | $sftp; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # returns the new channel object | ||||||||||||||||||||||||||||
| 74 | sub _open_channel { | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
_open_channel: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 76 | 0 | my $ssh = $sftp->{ssh}; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | my $channel = $ssh->_session_channel; | |||||||||||||||||||||||||||
| 79 | 0 | $channel->open; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | $channel->register_handler(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, sub { | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my($c, $packet) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 83 | 0 | $c->{ssh}->debug("Sending subsystem: sftp"); | |||||||||||||||||||||||||||
| 84 | 0 | my $r_packet = $c->request_start("subsystem", 1); | |||||||||||||||||||||||||||
| 85 | 0 | $r_packet->put_str("sftp"); | |||||||||||||||||||||||||||
| 86 | 0 | $r_packet->send; | |||||||||||||||||||||||||||
| 87 | 0 | }); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | my $subsystem_reply = sub { | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | my($c, $packet) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 91 | 0 | my $id = $packet->get_int32; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ($packet->type == SSH2_MSG_CHANNEL_FAILURE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | $c->{ssh}->fatal_disconnect("Request for " . | |||||||||||||||||||||||||||
| 94 | "subsystem 'sftp' failed on channel '$id'"); | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 0 | $c->{ssh}->break_client_loop; | |||||||||||||||||||||||||||
| 97 | 0 | }; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | my $cmgr = $ssh->channel_mgr; | |||||||||||||||||||||||||||
| 100 | 0 | $cmgr->register_handler(SSH2_MSG_CHANNEL_FAILURE, $subsystem_reply); | |||||||||||||||||||||||||||
| 101 | 0 | $cmgr->register_handler(SSH2_MSG_CHANNEL_SUCCESS, $subsystem_reply); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | $sftp->{incoming} = Net::SFTP::Buffer->new; | |||||||||||||||||||||||||||
| 104 | 0 | my $incoming = $sftp->{incoming}; | |||||||||||||||||||||||||||
| 105 | $channel->register_handler("_output_buffer", sub { | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | my($c, $buffer) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 107 | 0 | $incoming->append($buffer->bytes); | |||||||||||||||||||||||||||
| 108 | 0 | $c->{ssh}->break_client_loop; | |||||||||||||||||||||||||||
| 109 | 0 | }); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | ## Get channel confirmation, etc. Break once we get a response | ||||||||||||||||||||||||||||
| 112 | ## to subsystem execution. | ||||||||||||||||||||||||||||
| 113 | 0 | $ssh->client_loop; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | $channel; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | sub do_init { | ||||||||||||||||||||||||||||
| 119 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
do_init: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 120 | 0 | my $ssh = $sftp->{ssh}; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | $sftp->debug("Sending SSH2_FXP_INIT"); | |||||||||||||||||||||||||||
| 123 | 0 | my $msg = $sftp->new_msg(SSH2_FXP_INIT); | |||||||||||||||||||||||||||
| 124 | 0 | $msg->put_int32(SSH2_FILEXFER_VERSION); | |||||||||||||||||||||||||||
| 125 | 0 | $sftp->send_msg($msg); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | $msg = $sftp->get_msg; | |||||||||||||||||||||||||||
| 128 | 0 | my $type = $msg->get_int8; | |||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ($type != SSH2_FXP_VERSION) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | croak "Invalid packet back from SSH2_FXP_INIT (type $type)"; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 0 | my $version = $msg->get_int32; | |||||||||||||||||||||||||||
| 133 | 0 | $sftp->debug("Remote version: $version"); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | ## XXX Check for extensions. | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | sub debug { | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
debug: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if ($sftp->{debug}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | $sftp->{ssh}->debug("sftp: @_"); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | ## Server -> client methods. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # reads SSH2_FXP_STATUS packet and returns Net::SFTP::Attributes object or undef | ||||||||||||||||||||||||||||
| 148 | sub get_attrs { | ||||||||||||||||||||||||||||
| 149 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_attrs: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 150 | 0 | my($expected_id) = @_; | |||||||||||||||||||||||||||
| 151 | 0 | my $msg = $sftp->get_msg; | |||||||||||||||||||||||||||
| 152 | 0 | my $type = $msg->get_int8; | |||||||||||||||||||||||||||
| 153 | 0 | my $id = $msg->get_int32; | |||||||||||||||||||||||||||
| 154 | 0 | $sftp->debug("Received stat reply T:$type I:$id"); | |||||||||||||||||||||||||||
| 155 | 0 | ▶ | croak "ID mismatch ($id != $expected_id)" unless $id == $expected_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | if ($type == SSH2_FXP_STATUS) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 157 | 0 | my $status = $msg->get_int32; | |||||||||||||||||||||||||||
| 158 | 0 | $sftp->warn("Couldn't stat remote file",$status); | |||||||||||||||||||||||||||
| 159 | 0 | return; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | elsif ($type != SSH2_FXP_ATTRS) { | ||||||||||||||||||||||||||||
| 162 | 0 | croak "Expected SSH2_FXP_ATTRS packet, got $type"; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | 0 | $msg->get_attributes; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | # reads SSH2_FXP_STATUS packet and returns SFTP status value | ||||||||||||||||||||||||||||
| 168 | sub get_status { | ||||||||||||||||||||||||||||
| 169 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_status: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 170 | 0 | my($expected_id) = @_; | |||||||||||||||||||||||||||
| 171 | 0 | my $msg = $sftp->get_msg; | |||||||||||||||||||||||||||
| 172 | 0 | my $type = $msg->get_int8; | |||||||||||||||||||||||||||
| 173 | 0 | my $id = $msg->get_int32; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | ▶ | croak "ID mismatch ($id != $expected_id)" unless $id == $expected_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | if ($type != SSH2_FXP_STATUS) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 0 | croak "Expected SSH2_FXP_STATUS packet, got $type"; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | $msg->get_int32; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # reads SSH2_FXP_HANDLE packet and returns handle, or undef on failure | ||||||||||||||||||||||||||||
| 184 | sub get_handle { | ||||||||||||||||||||||||||||
| 185 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_handle: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 186 | 0 | my($expected_id) = @_; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 0 | my $msg = $sftp->get_msg; | |||||||||||||||||||||||||||
| 189 | 0 | my $type = $msg->get_int8; | |||||||||||||||||||||||||||
| 190 | 0 | my $id = $msg->get_int32; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | ▶ | croak "ID mismatch ($id != $expected_id)" unless $id == $expected_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 0 | ▶ | if ($type == SSH2_FXP_STATUS) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 194 | 0 | my $status = $msg->get_int32; | |||||||||||||||||||||||||||
| 195 | 0 | $sftp->warn("Couldn't get handle",$status); | |||||||||||||||||||||||||||
| 196 | 0 | return; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | elsif ($type != SSH2_FXP_HANDLE) { | ||||||||||||||||||||||||||||
| 199 | 0 | croak "Expected SSH2_FXP_HANDLE packet, got $type"; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | $msg->get_str; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | ## Client -> server methods. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | sub _send_str_request { | ||||||||||||||||||||||||||||
| 208 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
_send_str_request: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 209 | 0 | my($code, $str) = @_; | |||||||||||||||||||||||||||
| 210 | 0 | my($msg, $id) = $sftp->new_msg_w_id($code); | |||||||||||||||||||||||||||
| 211 | 0 | $msg->put_str($str); | |||||||||||||||||||||||||||
| 212 | 0 | $sftp->send_msg($msg); | |||||||||||||||||||||||||||
| 213 | 0 | $sftp->debug("Sent message T:$code I:$id"); | |||||||||||||||||||||||||||
| 214 | 0 | $id; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | sub _send_str_attrs_request { | ||||||||||||||||||||||||||||
| 218 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
_send_str_attrs_request: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 219 | 0 | my($code, $str, $a) = @_; | |||||||||||||||||||||||||||
| 220 | 0 | my($msg, $id) = $sftp->new_msg_w_id($code); | |||||||||||||||||||||||||||
| 221 | 0 | $msg->put_str($str); | |||||||||||||||||||||||||||
| 222 | 0 | $msg->put_attributes($a); | |||||||||||||||||||||||||||
| 223 | 0 | $sftp->send_msg($msg); | |||||||||||||||||||||||||||
| 224 | 0 | $sftp->debug("Sent message T:$code I:$id"); | |||||||||||||||||||||||||||
| 225 | 0 | $id; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | sub _check_ok_status { | ||||||||||||||||||||||||||||
| 229 | 0 | ▶ | my $status = $_[0]->get_status($_[1]); | ||||||||||||||||||||||||||
|
Subroutine
_check_ok_status: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 230 | 0 | ▶ | $_[0]->warn("Couldn't $_[2]",$status) unless $status == SSH2_FX_OK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 0 | $status; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | ## SSH2_FXP_OPEN (3) | ||||||||||||||||||||||||||||
| 235 | # returns handle on success, undef on failure | ||||||||||||||||||||||||||||
| 236 | sub do_open { | ||||||||||||||||||||||||||||
| 237 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
do_open: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 238 | 0 | my($path, $flags, $a) = @_; | |||||||||||||||||||||||||||
| 239 | 0 | ▶ | $a ||= Net::SFTP::Attributes->new; | ||||||||||||||||||||||||||
|
Condition0%0/3
$a ||= "Net::SFTP::Attributes"->new
MC/DC0%0/2
$a ||= "Net::SFTP::Attributes"->new
$a
"Net::SFTP::Attributes"->new
Truth table0%0/3
A ||= B
A= $aB= "Net::SFTP::Attributes"->new
| |||||||||||||||||||||||||||||
| 240 | 0 | my($msg, $id) = $sftp->new_msg_w_id(SSH2_FXP_OPEN); | |||||||||||||||||||||||||||
| 241 | 0 | $msg->put_str($path); | |||||||||||||||||||||||||||
| 242 | 0 | $msg->put_int32($flags); | |||||||||||||||||||||||||||
| 243 | 0 | $msg->put_attributes($a); | |||||||||||||||||||||||||||
| 244 | 0 | $sftp->send_msg($msg); | |||||||||||||||||||||||||||
| 245 | 0 | $sftp->debug("Sent SSH2_FXP_OPEN I:$id P:$path"); | |||||||||||||||||||||||||||
| 246 | 0 | $sftp->get_handle($id); | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | ## SSH2_FXP_READ (4) | ||||||||||||||||||||||||||||
| 250 | # returns data on success, (undef,$status) on failure | ||||||||||||||||||||||||||||
| 251 | sub do_read { | ||||||||||||||||||||||||||||
| 252 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
do_read: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 253 | 0 | my($handle, $offset, $size) = @_; | |||||||||||||||||||||||||||
| 254 | 0 | ▶ | $size ||= COPY_SIZE; | ||||||||||||||||||||||||||
|
Condition0%0/2
$size ||= 65536
MC/DC0%0/1
$size ||= 65536
$size
Truth table0%0/2
A
A= $size
| |||||||||||||||||||||||||||||
| 255 | 0 | my($msg, $expected_id) = $sftp->new_msg_w_id(SSH2_FXP_READ); | |||||||||||||||||||||||||||
| 256 | 0 | $msg->put_str($handle); | |||||||||||||||||||||||||||
| 257 | 0 | $msg->put_int64(int $offset); | |||||||||||||||||||||||||||
| 258 | 0 | $msg->put_int32($size); | |||||||||||||||||||||||||||
| 259 | 0 | $sftp->send_msg($msg); | |||||||||||||||||||||||||||
| 260 | 0 | $sftp->debug("Sent message SSH2_FXP_READ I:$expected_id O:$offset"); | |||||||||||||||||||||||||||
| 261 | 0 | $msg = $sftp->get_msg; | |||||||||||||||||||||||||||
| 262 | 0 | my $type = $msg->get_int8; | |||||||||||||||||||||||||||
| 263 | 0 | my $id = $msg->get_int32; | |||||||||||||||||||||||||||
| 264 | 0 | $sftp->debug("Received reply T:$type I:$id"); | |||||||||||||||||||||||||||
| 265 | 0 | ▶ | croak "ID mismatch ($id != $expected_id)" unless $id == $expected_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | 0 | ▶ | if ($type == SSH2_FXP_STATUS) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 267 | 0 | my $status = $msg->get_int32; | |||||||||||||||||||||||||||
| 268 | 0 | ▶ | if ($status != SSH2_FX_EOF) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 0 | $sftp->warn("Couldn't read from remote file",$status); | |||||||||||||||||||||||||||
| 270 | 0 | $sftp->do_close($handle); | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | 0 | return(undef, $status); | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | elsif ($type != SSH2_FXP_DATA) { | ||||||||||||||||||||||||||||
| 275 | 0 | croak "Expected SSH2_FXP_DATA packet, got $type"; | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | 0 | $msg->get_str; | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | ## SSH2_FXP_WRITE (6) | ||||||||||||||||||||||||||||
| 281 | # returns status (SSH2_FX_OK on success) | ||||||||||||||||||||||||||||
| 282 | sub do_write { | ||||||||||||||||||||||||||||
| 283 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
do_write: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 284 | 0 | my($handle, $offset, $data) = @_; | |||||||||||||||||||||||||||
| 285 | 0 | my($msg, $id) = $sftp->new_msg_w_id(SSH2_FXP_WRITE); | |||||||||||||||||||||||||||
| 286 | 0 | $msg->put_str($handle); | |||||||||||||||||||||||||||
| 287 | 0 | $msg->put_int64(int $offset); | |||||||||||||||||||||||||||
| 288 | 0 | $msg->put_str($data); | |||||||||||||||||||||||||||
| 289 | 0 | $sftp->send_msg($msg); | |||||||||||||||||||||||||||
| 290 | 0 | $sftp->debug("Sent message SSH2_FXP_WRITE I:$id O:$offset"); | |||||||||||||||||||||||||||
| 291 | 0 | my $status = $sftp->_check_ok_status($id,'write to remote file'); | |||||||||||||||||||||||||||
| 292 | 0 | ▶ | $sftp->do_close($handle) unless $status == SSH2_FX_OK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | 0 | return $status; | |||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | ## SSH2_FXP_LSTAT (7), SSH2_FXP_FSTAT (8), SSH2_FXP_STAT (17) | ||||||||||||||||||||||||||||
| 297 | # these all return a Net::SFTP::Attributes object on success, undef on failure | ||||||||||||||||||||||||||||
| 298 | 0 | ▶ | sub do_lstat { $_[0]->_do_stat(SSH2_FXP_LSTAT, $_[1]) } | ||||||||||||||||||||||||||
|
Subroutine
do_lstat: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 299 | 0 | ▶ | sub do_fstat { $_[0]->_do_stat(SSH2_FXP_FSTAT, $_[1]) } | ||||||||||||||||||||||||||
|
Subroutine
do_fstat: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 300 | 0 | ▶ | sub do_stat { $_[0]->_do_stat(SSH2_FXP_STAT , $_[1]) } | ||||||||||||||||||||||||||
|
Subroutine
do_stat: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 301 | sub _do_stat { | ||||||||||||||||||||||||||||
| 302 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
_do_stat: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 303 | 0 | my $id = $sftp->_send_str_request(@_); | |||||||||||||||||||||||||||
| 304 | 0 | $sftp->get_attrs($id); | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | ## SSH2_FXP_OPENDIR (11) | ||||||||||||||||||||||||||||
| 308 | sub do_opendir { | ||||||||||||||||||||||||||||
| 309 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
do_opendir: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 310 | 0 | my $id = $sftp->_send_str_request(SSH2_FXP_OPENDIR, @_); | |||||||||||||||||||||||||||
| 311 | 0 | $sftp->get_handle($id); | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | ## SSH2_FXP_CLOSE (4), SSH2_FXP_REMOVE (13), | ||||||||||||||||||||||||||||
| 315 | ## SSH2_FXP_MKDIR (14), SSH2_FXP_RMDIR (15), | ||||||||||||||||||||||||||||
| 316 | ## SSH2_FXP_SETSTAT (9), SSH2_FXP_FSETSTAT (10) | ||||||||||||||||||||||||||||
| 317 | # all of these return a status (SSH2_FX_OK on success) | ||||||||||||||||||||||||||||
| 318 | { | ||||||||||||||||||||||||||||
| 319 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 320 | 2 | *do_close = _gen_simple_method(SSH2_FXP_CLOSE, 'close file'); | |||||||||||||||||||||||||||
| 321 | 2 | *do_remove = _gen_simple_method(SSH2_FXP_REMOVE, 'delete file'); | |||||||||||||||||||||||||||
| 322 | 2 | *do_mkdir = _gen_simple_method(SSH2_FXP_MKDIR, 'create directory'); | |||||||||||||||||||||||||||
| 323 | 2 | *do_rmdir = _gen_simple_method(SSH2_FXP_RMDIR, 'remove directory'); | |||||||||||||||||||||||||||
| 324 | 2 | *do_setstat = _gen_simple_method(SSH2_FXP_SETSTAT , 'setstat'); | |||||||||||||||||||||||||||
| 325 | 2 | *do_fsetstat = _gen_simple_method(SSH2_FXP_FSETSTAT , 'fsetstat'); | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | sub _gen_simple_method { | ||||||||||||||||||||||||||||
| 329 | 12 | ▶ | my($code, $msg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_gen_simple_method: called CC=1 SCAR=3.1
| |||||||||||||||||||||||||||||
| 330 | sub { | ||||||||||||||||||||||||||||
| 331 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 332 | 0 | ▶ | my $id = @_ > 1 ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 333 | $sftp->_send_str_attrs_request($code, @_) : | ||||||||||||||||||||||||||||
| 334 | $sftp->_send_str_request($code, @_); | ||||||||||||||||||||||||||||
| 335 | 0 | $sftp->_check_ok_status($id, $msg); | |||||||||||||||||||||||||||
| 336 | 12 | }; | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | ## SSH2_FXP_REALPATH (16) | ||||||||||||||||||||||||||||
| 340 | sub do_realpath { | ||||||||||||||||||||||||||||
| 341 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
do_realpath: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 342 | 0 | my($path) = @_; | |||||||||||||||||||||||||||
| 343 | 0 | my $expected_id = $sftp->_send_str_request(SSH2_FXP_REALPATH, $path); | |||||||||||||||||||||||||||
| 344 | 0 | my $msg = $sftp->get_msg; | |||||||||||||||||||||||||||
| 345 | 0 | my $type = $msg->get_int8; | |||||||||||||||||||||||||||
| 346 | 0 | my $id = $msg->get_int32; | |||||||||||||||||||||||||||
| 347 | 0 | ▶ | croak "ID mismatch ($id != $expected_id)" unless $id == $expected_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 0 | ▶ | if ($type == SSH2_FXP_STATUS) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 349 | 0 | my $status = $msg->get_int32; | |||||||||||||||||||||||||||
| 350 | 0 | $sftp->warn("Couldn't canonicalise $path",$status); | |||||||||||||||||||||||||||
| 351 | 0 | return; | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | elsif ($type != SSH2_FXP_NAME) { | ||||||||||||||||||||||||||||
| 354 | 0 | croak "Expected SSH2_FXP_NAME packet, got $type"; | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | 0 | my $count = $msg->get_int32; | |||||||||||||||||||||||||||
| 357 | 0 | ▶ | croak "Got multiple names ($count) from SSH2_FXP_REALPATH" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | unless $count == 1; | ||||||||||||||||||||||||||||
| 359 | 0 | $msg->get_str; ## Filename. | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | ## SSH2_FXP_RENAME (18) | ||||||||||||||||||||||||||||
| 363 | sub do_rename { | ||||||||||||||||||||||||||||
| 364 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
do_rename: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 365 | 0 | my($old, $new) = @_; | |||||||||||||||||||||||||||
| 366 | 0 | my($msg, $id) = $sftp->new_msg_w_id(SSH2_FXP_RENAME); | |||||||||||||||||||||||||||
| 367 | 0 | $msg->put_str($old); | |||||||||||||||||||||||||||
| 368 | 0 | $msg->put_str($new); | |||||||||||||||||||||||||||
| 369 | 0 | $sftp->send_msg($msg); | |||||||||||||||||||||||||||
| 370 | 0 | $sftp->debug("Sent message SSH2_FXP_RENAME '$old' => '$new'"); | |||||||||||||||||||||||||||
| 371 | 0 | $sftp->_check_ok_status($id, "rename '$old' to '$new'"); | |||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | ## High-level client -> server methods. | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | # always returns undef on failure | ||||||||||||||||||||||||||||
| 377 | # if local filename is provided, returns '' on success, else file contents | ||||||||||||||||||||||||||||
| 378 | sub get { | ||||||||||||||||||||||||||||
| 379 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=19 SCAR=59.4
Pod
documented
| |||||||||||||||||||||||||||||
| 380 | 0 | my($remote, $local, $cb) = @_; | |||||||||||||||||||||||||||
| 381 | 0 | my $ssh = $sftp->{ssh}; | |||||||||||||||||||||||||||
| 382 | 0 | ▶ | my $want = defined wantarray ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 0 | ▶ | my $a = $sftp->do_stat($remote) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 385 | 0 | my $handle = $sftp->do_open($remote, SSH2_FXF_READ); | |||||||||||||||||||||||||||
| 386 | 0 | ▶ | return unless defined $handle; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | 0 | local *FH; | |||||||||||||||||||||||||||
| 389 | 0 | ▶ | if ($local) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 390 | 0 | ▶ | open FH, ">$local" or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 391 | $sftp->do_close($handle), croak "Can't open $local: $!"; | ||||||||||||||||||||||||||||
| 392 | 0 | ▶ | binmode FH or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 393 | $sftp->do_close($handle), croak "Can't binmode FH: $!"; | ||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 0 | my $offset = 0; | |||||||||||||||||||||||||||
| 397 | 0 | my $ret = ''; | |||||||||||||||||||||||||||
| 398 | 0 | while (1) { | |||||||||||||||||||||||||||
| 399 | 0 | my($data, $status) = $sftp->do_read($handle, $offset, COPY_SIZE); | |||||||||||||||||||||||||||
| 400 | 0 | ▶ | last if defined $status && $status == SSH2_FX_EOF; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $status and $status == &SSH2_FX_EOF()
MC/DC0%0/2
defined $status and $status == &SSH2_FX_EOF()
defined $status
$status == &SSH2_FX_EOF()
Truth table0%0/3
A and B
A= defined $statusB= $status == &SSH2_FX_EOF()
| |||||||||||||||||||||||||||||
| 401 | 0 | ▶ | return unless $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 402 | 0 | my $len = length($data); | |||||||||||||||||||||||||||
| 403 | 0 | ▶ | croak "Received more data than asked for $len > " . COPY_SIZE | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 404 | if $len > COPY_SIZE; | ||||||||||||||||||||||||||||
| 405 | 0 | $sftp->debug("In read loop, got $len offset $offset"); | |||||||||||||||||||||||||||
| 406 | 0 | ▶ | $cb->($sftp, $data, $offset, $a->size) if defined $cb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 407 | 0 | ▶ | if ($local) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 408 | 0 | print FH $data; | |||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | elsif ($want) { | ||||||||||||||||||||||||||||
| 411 | 0 | $ret .= $data; | |||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||
| 413 | 0 | $offset += $len; | |||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | 0 | $sftp->do_close($handle); | |||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | 0 | ▶ | if ($local) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 418 | 0 | close FH; | |||||||||||||||||||||||||||
| 419 | 0 | my $flags = $a->flags; | |||||||||||||||||||||||||||
| 420 | 0 | ▶ | my $mode = $flags & SSH2_FILEXFER_ATTR_PERMISSIONS ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 421 | $a->perm & 0777 : 0666; | ||||||||||||||||||||||||||||
| 422 | 0 | ▶ | chmod $mode, $local or croak "Can't chmod $local: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | 0 | ▶ | if ($flags & SSH2_FILEXFER_ATTR_ACMODTIME) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 425 | 0 | ▶ | utime $a->atime, $a->mtime, $local or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 426 | croak "Can't utime $local: $!"; | ||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||
| 429 | 0 | $ret; | |||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | sub put { | ||||||||||||||||||||||||||||
| 433 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
put: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 434 | 0 | my($local, $remote, $cb) = @_; | |||||||||||||||||||||||||||
| 435 | 0 | my $ssh = $sftp->{ssh}; | |||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | 0 | ▶ | my @stat = stat $local or croak "Can't stat local $local: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 438 | 0 | my $size = $stat[7]; | |||||||||||||||||||||||||||
| 439 | 0 | my $a = Net::SFTP::Attributes->new(Stat => \@stat); | |||||||||||||||||||||||||||
| 440 | 0 | my $flags = $a->flags; | |||||||||||||||||||||||||||
| 441 | 0 | $flags &= ~SSH2_FILEXFER_ATTR_SIZE; | |||||||||||||||||||||||||||
| 442 | 0 | $flags &= ~SSH2_FILEXFER_ATTR_UIDGID; | |||||||||||||||||||||||||||
| 443 | 0 | $a->flags($flags); | |||||||||||||||||||||||||||
| 444 | 0 | $a->perm( $a->perm & 0777 ); | |||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | 0 | local *FH; | |||||||||||||||||||||||||||
| 447 | 0 | ▶ | open FH, $local or croak "Can't open local file $local: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 448 | 0 | ▶ | binmode FH or croak "Can't binmode FH: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | 0 | my $handle = $sftp->do_open($remote, SSH2_FXF_WRITE | SSH2_FXF_CREAT | | |||||||||||||||||||||||||||
| 451 | SSH2_FXF_TRUNC, $a); # check status for info | ||||||||||||||||||||||||||||
| 452 | 0 | ▶ | return unless defined $handle; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | 0 | my $offset = 0; | |||||||||||||||||||||||||||
| 455 | 0 | while (1) { | |||||||||||||||||||||||||||
| 456 | 0 | my($len, $data, $msg, $id); | |||||||||||||||||||||||||||
| 457 | 0 | $len = read FH, $data, COPY_SIZE; | |||||||||||||||||||||||||||
| 458 | 0 | ▶ | last unless $len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 459 | 0 | ▶ | $cb->($sftp, $data, $offset, $size) if defined $cb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 460 | 0 | my $status = $sftp->do_write($handle, $offset, $data); | |||||||||||||||||||||||||||
| 461 | 0 | ▶ | if ($status != SSH2_FX_OK) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 462 | 0 | close FH; | |||||||||||||||||||||||||||
| 463 | 0 | return; | |||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||
| 465 | 0 | $sftp->debug("In write loop, got $len offset $offset"); | |||||||||||||||||||||||||||
| 466 | 0 | $offset += $len; | |||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | 0 | ▶ | close FH or $sftp->warn("Can't close local file $local: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | # ignore failures here, the transmission is the important part | ||||||||||||||||||||||||||||
| 472 | 0 | $sftp->do_fsetstat($handle, $a); | |||||||||||||||||||||||||||
| 473 | 0 | $sftp->do_close($handle); | |||||||||||||||||||||||||||
| 474 | 0 | return 1; | |||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | # returns ()/undef on error, directory list/reference to same otherwise | ||||||||||||||||||||||||||||
| 478 | sub ls { | ||||||||||||||||||||||||||||
| 479 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
ls: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 480 | 0 | my($remote, $code) = @_; | |||||||||||||||||||||||||||
| 481 | 0 | my @dir; | |||||||||||||||||||||||||||
| 482 | 0 | my $handle = $sftp->do_opendir($remote); | |||||||||||||||||||||||||||
| 483 | 0 | ▶ | return unless defined $handle; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | 0 | while (1) { | |||||||||||||||||||||||||||
| 486 | 0 | my $expected_id = $sftp->_send_str_request(SSH2_FXP_READDIR, $handle); | |||||||||||||||||||||||||||
| 487 | 0 | my $msg = $sftp->get_msg; | |||||||||||||||||||||||||||
| 488 | 0 | my $type = $msg->get_int8; | |||||||||||||||||||||||||||
| 489 | 0 | my $id = $msg->get_int32; | |||||||||||||||||||||||||||
| 490 | 0 | $sftp->debug("Received reply T:$type I:$id"); | |||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | 0 | ▶ | croak "ID mismatch ($id != $expected_id)" unless $id == $expected_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 493 | 0 | ▶ | if ($type == SSH2_FXP_STATUS) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 494 | 0 | my $status = $msg->get_int32; | |||||||||||||||||||||||||||
| 495 | 0 | $sftp->debug("Received SSH2_FXP_STATUS $status"); | |||||||||||||||||||||||||||
| 496 | 0 | ▶ | if ($status == SSH2_FX_EOF) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 497 | 0 | last; | |||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||
| 499 | else { | ||||||||||||||||||||||||||||
| 500 | 0 | $sftp->warn("Couldn't read directory",$status); | |||||||||||||||||||||||||||
| 501 | 0 | $sftp->do_close($handle); | |||||||||||||||||||||||||||
| 502 | 0 | return; | |||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||
| 505 | elsif ($type != SSH2_FXP_NAME) { | ||||||||||||||||||||||||||||
| 506 | 0 | croak "Expected SSH2_FXP_NAME packet, got $type"; | |||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | 0 | my $count = $msg->get_int32; | |||||||||||||||||||||||||||
| 510 | 0 | ▶ | last unless $count; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 511 | 0 | $sftp->debug("Received $count SSH2_FXP_NAME responses"); | |||||||||||||||||||||||||||
| 512 | 0 | for my $i (0..$count-1) { | |||||||||||||||||||||||||||
| 513 | 0 | my $fname = $msg->get_str; | |||||||||||||||||||||||||||
| 514 | 0 | my $lname = $msg->get_str; | |||||||||||||||||||||||||||
| 515 | 0 | my $a = $msg->get_attributes; | |||||||||||||||||||||||||||
| 516 | 0 | my $rec = { | |||||||||||||||||||||||||||
| 517 | filename => $fname, | ||||||||||||||||||||||||||||
| 518 | longname => $lname, | ||||||||||||||||||||||||||||
| 519 | a => $a, | ||||||||||||||||||||||||||||
| 520 | }; | ||||||||||||||||||||||||||||
| 521 | 0 | ▶ | if ($code && ref($code) eq "CODE") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$code and ref $code eq "CODE"
MC/DC0%0/2
$code and ref $code eq "CODE"
$code
ref $code eq "CODE"
Truth table0%0/3
A and B
A= $codeB= ref $code eq "CODE"
| |||||||||||||||||||||||||||||
| 522 | 0 | $code->($rec); | |||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||
| 524 | else { | ||||||||||||||||||||||||||||
| 525 | 0 | push @dir, $rec; | |||||||||||||||||||||||||||
| 526 | } | ||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||
| 529 | 0 | $sftp->do_close($handle); | |||||||||||||||||||||||||||
| 530 | 0 | ▶ | wantarray ? @dir : \@dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | ## Messaging methods--messages are essentially sub-packets. | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | 0 | ▶ | sub msg_id { $_[0]->{_msg_id}++ } | ||||||||||||||||||||||||||
|
Subroutine
msg_id: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | sub new_msg { | ||||||||||||||||||||||||||||
| 538 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
new_msg: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 539 | 0 | my($code) = @_; | |||||||||||||||||||||||||||
| 540 | 0 | my $msg = Net::SFTP::Buffer->new; | |||||||||||||||||||||||||||
| 541 | 0 | $msg->put_int8($code); | |||||||||||||||||||||||||||
| 542 | 0 | $msg; | |||||||||||||||||||||||||||
| 543 | } | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | sub new_msg_w_id { | ||||||||||||||||||||||||||||
| 546 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
new_msg_w_id: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 547 | 0 | my($code, $sid) = @_; | |||||||||||||||||||||||||||
| 548 | 0 | my $msg = $sftp->new_msg($code); | |||||||||||||||||||||||||||
| 549 | 0 | ▶ | my $id = defined $sid ? $sid : $sftp->msg_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 550 | 0 | $msg->put_int32($id); | |||||||||||||||||||||||||||
| 551 | 0 | ($msg, $id); | |||||||||||||||||||||||||||
| 552 | } | ||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | sub send_msg { | ||||||||||||||||||||||||||||
| 555 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
send_msg: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 556 | 0 | my($buf) = @_; | |||||||||||||||||||||||||||
| 557 | 0 | my $b = Net::SFTP::Buffer->new; | |||||||||||||||||||||||||||
| 558 | 0 | $b->put_int32($buf->length); | |||||||||||||||||||||||||||
| 559 | 0 | $b->append($buf->bytes); | |||||||||||||||||||||||||||
| 560 | 0 | $sftp->{channel}->send_data($b->bytes); | |||||||||||||||||||||||||||
| 561 | } | ||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||
| 563 | sub get_msg { | ||||||||||||||||||||||||||||
| 564 | 0 | ▶ | my $sftp = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_msg: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 565 | 0 | my $buf = $sftp->{incoming}; | |||||||||||||||||||||||||||
| 566 | 0 | my $len; | |||||||||||||||||||||||||||
| 567 | 0 | ▶ | unless ($buf->length > 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 568 | 0 | $sftp->{ssh}->client_loop; | |||||||||||||||||||||||||||
| 569 | 0 | ▶ | croak "Connection closed" unless $buf->length > 4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 570 | 0 | $len = unpack "N", $buf->bytes(0, 4, ''); | |||||||||||||||||||||||||||
| 571 | 0 | ▶ | croak "Received message too long $len" if $len > 256 * 1024; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 572 | 0 | ▶ | while ($buf->length < $len) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 573 | 0 | $sftp->{ssh}->client_loop; | |||||||||||||||||||||||||||
| 574 | } | ||||||||||||||||||||||||||||
| 575 | } | ||||||||||||||||||||||||||||
| 576 | 0 | my $b = Net::SFTP::Buffer->new; | |||||||||||||||||||||||||||
| 577 | 0 | $b->append( $buf->bytes(0, $len, '') ); | |||||||||||||||||||||||||||
| 578 | 0 | $b; | |||||||||||||||||||||||||||
| 579 | } | ||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | 2 | 1; | |||||||||||||||||||||||||||
| 582 | __END__ | ||||||||||||||||||||||||||||