Re: Remote directory <-> local directory comparison


Subject: Re: Remote directory <-> local directory comparison
From: Arthur Corliss (arthur@corlissfamily.org)
Date: Wed Dec 18 2002 - 14:45:22 AKST


On Wed, 18 Dec 2002, Brian ThunderEagle wrote:

>
> PERL:
>
> #!/usr/bin/perl
> $file="/var/log/messages";
> $size=(stat("".$file.""))[7];
> print "size of ".$file." is ".$size." bytes\n";
>
> or directly on the command line
>
> perl -e '$file="/var/log/messages"; $size=(stat("".$file.""))[7]; print "size
> of ".$file." is $size bytes\n"'
>
> This will get you the size of a file directly on the system. Getting the size
> of the file on a remote though I am not sure. I will have to look into that.

A shortcut for size is -s in Perl. You can use the Net::FTP module, which
supports a size method as well. The only problem you're likely to run into is
if you have to compare the sizes of ASCII files on different platforms which
use different newline termination. I did something like so to compare a
transfer from UNIX to NT:

# Get a list of all the local files to transfer
@files = <*>;

# Start FTP connection
$ftp = Net::FTP->new(DESTHOST);
$ftp->login(LOGON, PSSWD);
$ftp->ascii;
$ftp->cwd(DESTPATH);
unless ($ftp->pwd eq DESTPATH) {
  die "Couldn't switch to remote @{[ DESTPATH ]}!\n";
}

# Start transfering files
$err = 0;
while (defined($file = shift @files)) {
  die "Too many transfer errors!\n" if $err == MAXERR;

  $success = 1;

  # Open the file for transfer
  if (open(FILE, "< $file")) {
    flock FILE, LOCK_EX;

    # Send the file
    if ($ftp->put(*FILE, $file)) {

      # Calculate the DOS file size since UNIX is different
      $size = -s $file;
      chomp($tmp =`wc -l $file`);
      $size += (($tmp =~ /^\s*(\d+)/)[0] - 1);

      # Test the size of the transferred file, and error if incorrect
      unless ($ftp->size($file) >= $size) {
        warn "Remote file size isn't correct!\n";
        $ftp->delete($file);
        ++$err;
        $success = 0;

      # Put it back into the array so we can try again
        unshift @files, $file;
      }

    # The xfer failed, so clean up and account
    } else {
      $ftp->delete($file);
      warn "Unsuccessful transfer of $file!\n";
      ++$err;
        $success = 0;

      # Put it back into the array so we can try again
      unshift @files, $file;
    }

    flock FILE, LOCK_UN;
    close(FILE);

    unlink $file if $success;

  } else {
    warn "Couldn't open $file for transfer: $!\n";
    ++$err;

    # Put it back into the array so we can try again
    unshift @files, $file;
  }
}

Not a complete example, but you should get the idea. . .

        --Arthur Corliss
          Bolverk's Lair -- http://arthur.corlissfamily.org/
          Digital Mages -- http://www.digitalmages.com/
          "Live Free or Die, the Only Way to Live" -- NH State Motto

---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.



This archive was generated by hypermail 2a23 : Wed Dec 18 2002 - 15:19:34 AKST