0 | 0 |
package Net::SFTP::Foreign;
|
1 | 1 |
|
2 | |
our $VERSION = '1.91';
|
|
2 |
our $VERSION = '1.93';
|
3 | 3 |
|
4 | 4 |
use strict;
|
5 | 5 |
use warnings;
|
|
75 | 75 |
my $sftp = shift;
|
76 | 76 |
my $code = shift;
|
77 | 77 |
my $id = $sftp->_next_msg_id;
|
|
78 |
$sftp->{incomming}{$id} = undef;
|
78 | 79 |
my $msg = Net::SFTP::Foreign::Buffer->new(int8 => $code, int32 => $id, @_);
|
79 | 80 |
$sftp->_queue_msg($msg);
|
80 | 81 |
return $id;
|
|
181 | 182 |
_connected => 1,
|
182 | 183 |
_queued => 0,
|
183 | 184 |
_error => 0,
|
184 | |
_status => 0 };
|
|
185 |
_status => 0,
|
|
186 |
_incomming => {} };
|
185 | 187 |
|
186 | 188 |
bless $sftp, $class;
|
187 | 189 |
|
|
416 | 418 |
}
|
417 | 419 |
|
418 | 420 |
# helper methods:
|
|
421 |
|
|
422 |
sub _get_msg_by_id {
|
|
423 |
my ($sftp, $eid) = @_;
|
|
424 |
while (1) {
|
|
425 |
my $msg = delete($sftp->{incomming}{$eid}) || $sftp->_get_msg || return undef;
|
|
426 |
my $id = unpack xN => $$msg;
|
|
427 |
return $msg if $id == $eid;
|
|
428 |
unless (exists $sftp->{incomming}{$id}) {
|
|
429 |
$sftp->_conn_lost(SSH2_FX_BAD_MESSAGE,
|
|
430 |
SFTP_ERR_REMOTE_BAD_MESSAGE,
|
|
431 |
$_[2], "bad packet sequence, expected $eid, got $id");
|
|
432 |
return undef;
|
|
433 |
}
|
|
434 |
$sftp->{incomming}{$id} = $msg
|
|
435 |
}
|
|
436 |
}
|
|
437 |
|
419 | 438 |
sub _get_msg_and_check {
|
420 | 439 |
my ($sftp, $etype, $eid, $err, $errstr) = @_;
|
421 | |
my $msg = $sftp->_get_msg;
|
|
440 |
my $msg = $sftp->_get_msg_by_id($eid, $errstr);
|
422 | 441 |
if ($msg) {
|
423 | 442 |
my $type = $msg->get_int8;
|
424 | |
my $id = $msg->get_int32;
|
|
443 |
$msg->get_int32; # discard id, it has already been checked at _get_msg_by_id
|
425 | 444 |
|
426 | 445 |
$sftp->_clear_error_and_status;
|
427 | |
|
428 | |
if ($id != $eid) {
|
429 | |
$sftp->_conn_lost(SSH2_FX_BAD_MESSAGE,
|
430 | |
SFTP_ERR_REMOTE_BAD_MESSAGE,
|
431 | |
$errstr, "bad packet sequence, expected $eid, got $id");
|
432 | |
return undef;
|
433 | |
}
|
434 | 446 |
|
435 | 447 |
if ($type != $etype) {
|
436 | 448 |
if ($type == SSH2_FXP_STATUS) {
|
|
766 | 778 |
"Couldn't write to remote file")) {
|
767 | 779 |
|
768 | 780 |
# discard responses to queued requests:
|
769 | |
$sftp->_get_msg for @msgid;
|
|
781 |
$sftp->_get_msg_by_id($_) for @msgid;
|
770 | 782 |
return $last;
|
771 | 783 |
}
|
772 | 784 |
}
|
|
785 | 797 |
$$bout .= $_[2];
|
786 | 798 |
my $len = length $$bout;
|
787 | 799 |
|
788 | |
$sftp->flush($rfh, 'out')
|
789 | |
if ($len >= $sftp->{_write_delay} or ($len and $sftp->{_autoflush} ));
|
|
800 |
if ($len >= $sftp->{_write_delay} or ($len and $sftp->{_autoflush} )) {
|
|
801 |
$sftp->flush($rfh, 'out') or return undef;
|
|
802 |
}
|
790 | 803 |
|
791 | 804 |
return $datalen;
|
792 | 805 |
}
|
|
829 | 842 |
$rfh->_inc_pos($written)
|
830 | 843 |
unless $append;
|
831 | 844 |
|
832 | |
substr($$bout, 0, $written, '');
|
|
845 |
$$bout = ''; # The full buffer is discarded even when some error happens.
|
833 | 846 |
$written == $len or return undef;
|
834 | 847 |
}
|
835 | 848 |
}
|
|
894 | 907 |
}
|
895 | 908 |
}
|
896 | 909 |
|
897 | |
$sftp->_get_msg for @msgid;
|
|
910 |
$sftp->_get_msg_by_id($_) for @msgid;
|
898 | 911 |
|
899 | 912 |
if ($ensure_eof and
|
900 | 913 |
$sftp->_get_msg_and_check(SSH2_FXP_DATA, $ensure_eof,
|
|
1859 | 1872 |
}
|
1860 | 1873 |
}
|
1861 | 1874 |
|
1862 | |
$sftp->_get_msg for (@msgid);
|
|
1875 |
$sftp->_get_msg_by_id($_) for @msgid;
|
1863 | 1876 |
|
1864 | 1877 |
goto CLEANUP if $sftp->{_error};
|
1865 | 1878 |
|
|
2405 | 2418 |
|
2406 | 2419 |
CORE::close $fh unless $local_is_fh;
|
2407 | 2420 |
|
2408 | |
$sftp->_get_msg for (@msgid);
|
|
2421 |
$sftp->_get_msg_by_id($_) for @msgid;
|
2409 | 2422 |
|
2410 | 2423 |
$sftp->truncate($rfh, $writeoff)
|
2411 | 2424 |
if $last_block_was_zeros and not $sftp->{_error};
|
|
2452 | 2465 |
$sftp->_set_error(SFTP_ERR_LOCAL_OPEN_FAILED, "Can't open scalar as file handle", $!);
|
2453 | 2466 |
return undef;
|
2454 | 2467 |
}
|
2455 | |
$sftp->put($fh, $remote, %opts);
|
|
2468 |
$sftp->put($fh, $remote, %put_opts);
|
2456 | 2469 |
}
|
2457 | 2470 |
|
2458 | 2471 |
sub ls {
|
|
2562 | 2575 |
$queue_size++ if $queue_size < $max_queue_size;
|
2563 | 2576 |
}
|
2564 | 2577 |
$sftp->_set_error if $sftp->{_status} == SSH2_FX_EOF;
|
2565 | |
$sftp->_get_msg for @msgid;
|
|
2578 |
$sftp->_get_msg_by_id($_) for @msgid;
|
2566 | 2579 |
$sftp->_closedir_save_status($rdh) if $rdh;
|
2567 | 2580 |
};
|
2568 | 2581 |
unless ($sftp->{_error}) {
|
|
4990 | 5003 |
writes C<$data> to the remote file C<$handle>. Returns the number of
|
4991 | 5004 |
bytes written or undef on failure.
|
4992 | 5005 |
|
|
5006 |
Note that unless the file has been open in C<autoflush> mode, data
|
|
5007 |
will be cached until the buffer fills, the file is closed or C<flush>
|
|
5008 |
is explicitly called. That could also mask write errors that would
|
|
5009 |
become unnoticed until later when the write operation is actually
|
|
5010 |
performed.
|
|
5011 |
|
4993 | 5012 |
=item $sftp-E<gt>readline($handle)
|
4994 | 5013 |
|
4995 | 5014 |
=item $sftp-E<gt>readline($handle, $sep)
|
|
5630 | 5649 |
|
5631 | 5650 |
=head1 COPYRIGHT
|
5632 | 5651 |
|
5633 | |
Copyright (c) 2005-2020 Salvador FandiE<ntilde>o (sfandino@yahoo.com).
|
|
5652 |
Copyright (c) 2005-2021 Salvador FandiE<ntilde>o (sfandino@yahoo.com).
|
5634 | 5653 |
|
5635 | 5654 |
Copyright (c) 2001 Benjamin Trott, Copyright (c) 2003 David Rolsky.
|
5636 | 5655 |
|