Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hello
I'm trying to move files from one server to a new one, so I made a simple script using ftp, problem is Net::FTP broke files, here is my script:
#!/usr/bin/perl use strict; use warnings; use Cwd; use File::Spec; use Getopt::Long; use Net::FTP; use DBI; use Digest::MD5::File qw(-nofatals file_md5_hex); use File::Basename; my $path = File::Spec->catdir(getcwd()); my $host, my $username, my $password; GetOptions ( "dir=s" => \$path, "host=s" => \$host, "user=s" => \$username, "pass=s" => \$password ) or die("Error in command line arguments\n"); $path = File::Spec->catdir(getcwd(),$path); # Connect my $ftp = Net::FTP->new($host,Passive => 0) or die("Can't connect to: +$!"); $ftp->login($username,$password) or die("Couldn't authenticate ftp: $! +"); #$ftp->pasv(); $ftp->binary(); my $dbh = DBI->connect("DBI:SQLite:dbname=moving.db", "", "", { RaiseE +rror => 1 }) or die $DBI::errstr; $dbh->do(q{ CREATE TABLE IF NOT EXISTS "files"( hash TEXT, path TEXT, size INT )}); my @tree = ($path); loop: { $path = pop(@tree); opendir(DIR, $path) or die $!; unless ($ftp->cwd($path)) { $ftp->mkdir($path,1) or die "$! ", $ftp->message; $ftp->cwd($path) or die "$! ", $ftp->message; } while (my $file = readdir(DIR)) { next if $file =~ m/^\./ or ! $file; $file = File::Spec->catdir($path,$file); if (-d $file) { push @tree,$file; next; } $ftp->put($file) or die "$! ", $ftp->message; die(sprintf("$file didn't match size %s <> %s",$ftp->size( +$file),-s $file)) if $ftp->size($file) != -s $file; $dbh->do('INSERT INTO "files"(hash,path,size) VALUES (\''. +file_md5_hex($file).'\',\''.$dbh->quote($path).'\',\''.$dbh->quote(-s + $path).'\')') or die $DBI::errstr; print "Moved: $path\n"; } closedir(DIR); if (scalar @tree) { goto loop; } } $ftp->quit;
When its send a file it add some extra space, even with binary mode, both OS are the same Ubuntu 12.04, do you have an idea what could be wrong? because I'm a bit lost
Thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Net::FTP bug or is my error?
by kcott (Archbishop) on Aug 29, 2013 at 09:48 UTC | |
by Anonymous Monk on Aug 29, 2013 at 13:54 UTC |