Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Net:FTP Not overwrite files

by rethaew (Sexton)
on Jul 25, 2008 at 16:24 UTC ( #700178=perlquestion: print w/replies, xml ) Need Help??
rethaew has asked for the wisdom of the Perl Monks concerning the following question:

The default behavior of put() when using net::ftp is to overwrite the remote file. I need a quick and easy way of checking to see if the same file name already exists on the remote server and skip it if it exists. I know I can get a list of the remote files and compare that against the list of files I want to upload, but I looking for something a little cleaner and simpler. Something like:
use Net::FTP; my $ftp = Net::FTP->new("", Debug => 0); $ftp->login('user','pass'); foreach $file (@filelist) { $ftp-> put("$file") unless ($ftp-> fileexists($file));#fake code }

Replies are listed 'Best First'.
Re: Net:FTP Not overwrite files
by MidLifeXis (Monsignor) on Jul 25, 2008 at 16:35 UTC

    There is a race condition with this between the time a check is done vs the time the file is written.

    It would be better if there is a session variable that could be set that would not allow overwrite of existing files.

    Update: For example:

    • PureFTPD's -r option forces a rename on upload of existing files.
    • wu-ftpd is either by default or by a setting not able to overwrite files.
    and my guess is that there are others. My only question is if any of them are user / client configurable. Now the question is, what does your ftp server support?


Re: Net:FTP Not overwrite files
by Khen1950fx (Canon) on Jul 26, 2008 at 09:18 UTC
    Using an example from Network Programming with Perl - Ch.6 FTP and Telnet:

    #!/usr/bin/perl use strict; use warnings; use Net::FTP::File; use constant HOST => ''; use constant DIR => '/pub/CPAN'; use constant FILE => 'README'; my $ftp = Net::FTP->new(HOST, Debug => 1) or die "Couldn't connect: $@ +\n"; $ftp->login('anonymous'); $ftp->cwd(DIR); $ftp->put(FILE) unless $ftp->isfile(FILE); warn "File exists.\n"; $ftp->quit;

      Net-FTP-File has also the exists() method, which would be more appropiate because if the filename exists and is actually a directory the file put through FTP would be created under that directory, which doesn't seem to be the OP's desired behaviour.

        Hi i developed a very simples way to accomplish that it is so simple and work with the old Net::FTP
        $ftp->put("$file") unless $ftp->rename("$file","$file");
        well gonna put file only if it doens't exits otherwise it will rename it with the same actual name! it is genious regards Claudio
Re: Net:FTP Not overwrite files
by spivey49 (Monk) on Jul 25, 2008 at 18:20 UTC

    How about uploading the file to a temp name? After the temp file is done check to see if the real file is there. If it is do something with the temp file like delete it, or rename it to the real file name if it's not there. That "should" handle the race condition. Of course, on a busy server you might still run into issues there.

    If you're looking for a way to determine whether the file exists or not you could eval a $ftp->get for the file or query the output of a $ftp->dir.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://700178]
Approved by marto
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2016-10-23 00:48 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (299 votes). Check out past polls.