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

I am writing an ftp application .. the server is a NT machine and my perl code also runs on NT ... the program is always running and connects to the server once in 5 minutes and 1.checks if the file is being written (based on size) 2.if file has been completly written it downloads the same i am using NET::FTP when i connect to the server and do a ls i get the filename date and size of file the date is the that when the first byte is written to the file and the size keeps changing till the file is completly written ... i need to download all the files once it has been completly written and should not download the same file more than once (i dont have write access in the server so i cannot move or rename the same)...need some Ways(TMTOWTDI) in which i can do this ...

Replies are listed 'Best First'.
Re: ftp files (TMTOWTDI)
by robartes (Priest) on Oct 10, 2002 at 08:17 UTC
    You basically want to keep state. Two things make it easy for your application to keep state:

    * You are constantly running, meaning there is no need to keep a persistent state

    * For those times when your program has stopped and needs to rebuild state, you are lucky that you have your state automatically persistent: it's the files on your local system.

    To implement your state keeping, you could use a hash of filenames and array references as the data structure:

    $state_hash{$filename} = [$state,$size];
    The state hash would have filenames as a key, and a reference to an anonymous array as value. The anon array would contain a state and a size as elements. $state would have different values depending on whether the file in question is:

    i) growing on the server

    ii) no longer growing on the server but not yet downloaded

    iii) downloaded to the local system

    When the state is i) above, $size stores the current size of the file on the server.

    Every five minutes, your script would then get a directory listing, loop over the files and check whether they are in the hash. If they are not in the hash, it would put them in the hash in the appropriate state, and if they are in the hash will update their state and download them if necessary.

    Upon startup, the script would initialise the hash based on the files already on the local system.

    That's how I would implement it. You will undoubtedly have noticed that I have cunningly refrained from posting any useful code. Sarcastically inclined people would point out that this is because none of my code is ever useful, but my excuse is that this is really a design rather than implementation issue. Nah.

    CU
    Robartes-

Re: ftp files (TMTOWTDI)
by rob_au (Abbot) on Oct 10, 2002 at 11:14 UTC
    At the risk of sounding like a broken record, I would recommend an exploration of existing mirroring solutions - For example:

    • w3mir - This script can be used to perform recursive mirroring of HTTP and FTP resources, including access authorisation, proxy connections and regular expression matching and exclusion.
    • wget - Wget is a freely available network utility which can be used to retrieve and mirror files from the HTTP and FTP resources.
    • rsync - rsync is an open source utility that provides fast incremental file transfer - There is a Perl interface (File::Rsync) for rsync available from CPAN.

     

    perl -e 'print+unpack("N",pack("B32","00000000000000000000000111001101")),"\n"'

Re: ftp files (TMTOWTDI)
by princepawn (Parson) on Oct 11, 2002 at 14:11 UTC