Hi fellow Perlmonks!
I am currently having fun with Net::SFTP::Foreign. I am modifying a deamon which monitors a directory and automatically uploads files via sftp to a server. If there are multiple files, the deamon forks a child process for every file and transfers them in parallel.
Now I want to do some better error handling. Wenn a transfer aborts, I want to print the number of bytes which were transferred. I can quite get this to STDOUT with the "more => -v" option.
my $sftp = Net::SFTP::Foreign->new(host =>$host, user => $user, key_pa
+th => [$privkey, $pubkey] , more => '-v');
[... debug output ...]
Transferred: sent 32700048, received 60544 bytes, in 1.6 seconds
Bytes per second: sent 20997978.1, received 38877.7
[... more debug output ..]
My first approach was, to grep that output for the "Transferred" line. While this does work, that seems a bit overkill.
I also tried utilizing the callback function of sftp->put like this:
my $BYTES = 0;
$sftp->put(
"testfile",
"testfile",
cleanup => 1,
callback => sub {
my ( $sftp, $data, $offset, $size ) = @_;
print $offset ." of ". $size ." bytes copied\n";
last_bytes($offset);
+
} );
print "\nTransferred $BYTES bytes\n";
+
sub last_bytes {
my ($bytes) = @_;
$BYTES = $bytes;
}
However this is giving me a hard time, as I would have to track a variable per child I fork.
In contrast, WWW::Curl has a method for getting the number of transferred bytes:
$curl->getinfo( CURLINFO_SIZE_UPLOAD );
I hope I am not missing something from the documentation for getting the transferred bytes from Net::SFTP::Forerign....
Any Ideas? I get the feeling I am nearly there with the callback-solution...
Kind regards,
yulivee
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.