Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Net::FTP and corrupted uploads

by wfsp (Abbot)
on Jun 06, 2008 at 16:23 UTC ( [id://690696]=perlquestion: print w/replies, xml ) Need Help??

wfsp has asked for the wisdom of the Perl Monks concerning the following question:

I recently rebuilt Vista following a hard disk failure and I can no longer upload files without them being corrupted (lines truncated). It was fine before. My FileZilla FTP client works fine. But with Net::FTP there is some weirdness going on.
#!/usr/local/bin/perl use strict; use warnings; use Config::Simple; use Net::FTP; my $success; my %cnf; Config::Simple->import_from(q{cnf/sw.cnf}, \%cnf); my $ftp = Net::FTP->new($cnf{host}, Passive => 1) or die qq{cant connect: $!\n}; print $ftp->message; $success = $ftp->login($cnf{login}, $cnf{pwd}); print $ftp->message; $ftp->ascii; #$ftp->binary; print $ftp->message; my $local = q{c:/www/032/docroot/test.html}; my $remote = q{032/docroot/test.html}; $success = $ftp->put($local, $remote); print $ftp->message;
output
FTP Server ready. User u41576016 logged in Type set to A Opening ASCII mode data connection for 032/docroot/test.html Transfer complete
result
<html <head <title>ftp test</title <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1 +" </head <body <h1>ftp test</h1 </body </html>
With $ftp->ascii commented out and $ftp->binary uncommented I get
<html> <head> <title>ftp test</title> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1 +"> </head> <body> <h1>ftp test</h1> </body> </html>
Changing passive to 0 doesn't affect the outcome.

Now, I admit this more than likely a combination of my ineptitude and Vista. A lethal combination! :-)

Can anyone shed any light on it?

Replies are listed 'Best First'.
Re: Net::FTP and corrupted uploads
by sgifford (Prior) on Jun 06, 2008 at 20:03 UTC
    What does the file you are transferring look like? In particular, does it contain DOS-style (CR/LF) or Unix-style (LF) line endings? There are several parts of this process that will try to fix the line endings, and if the file doesn't look how they expect, they may truncate lines. In this case they are expecting DOS-style CR/LF line endings.

    Beyond that, it could be Net::FTP screwing up the file, the underlying I/O libraries, or the FTP server. Using a packet sniffer to look at what's being sent across the wire would be a good way to see if the client is behaving correctly. A debugger would be the easiest way to see whether it's the I/O library or the FTP module.

    Is there a reason you don't want to transfer the file in binary? It's much less error-prone.

    Update: Fixed formatting.

Re: Net::FTP and corrupted uploads
by monarch (Priest) on Jun 06, 2008 at 17:57 UTC
    At first guess I'd say Net::FTP could be using chomp?

    Let's say, you're on a Win32 machine, chomp is designed to remove the last 2 characters (CR,LF) right? Then say you feed a unix-formatted file through, then it is conceivable that chomp does the "wrong thing" and takes the last character of text from each line as well as the LF.

    Personally I never use chomp. Instead I prefer to correct for line endings by doing the following:

    $line =~ s/(\r\n|\n\r|\r|\n)//;

    The regular expression forces a test for CR,LF and vice-versa before considering that the line may end with just a single LF. Portable across operating systems and text file formats.

    It seems in this situation you are safest with just sending in binary format, and failing that maybe try hacking the source of Net::FTP yourself?

Net::FTP and corrupted uploads has a patch
by thoglette (Scribe) on Feb 02, 2009 at 22:50 UTC
    There is a known bug with recent Net::FTP - for which a patch exists.
    • CPAN Bug rt://25019
    • Moving from Net::FTP to 2.75 to 2.77 (in 5.8.6) sees problem
    • Around line 75 in Net/FTP/A.pm
      $tmp =~ s/[^\015]\012/\015\012/sg if $nr;

      The regexp says "Search for any character followed by a \012, and replace it with \015\012". Since the character is not in the replacement string, it is deleted. This is one fix:

      $tmp =~ s/([^\015];)\012/$1\015\012/sg if $nr;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://690696]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-03-29 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found