Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: how to tell if a file is still being modified

by blue_cowdawg (Monsignor)
on Sep 15, 2003 at 19:55 UTC ( [id://291639]=note: print w/replies, xml ) Need Help??


in reply to how to tell if a file is still being modified

Along the same lines of what halley had to say in his reply I once solved that problem by using an algorithm described in the following psuedocode:

open file for reading slurp in file into a scalar close file calculate the MD5 of slurp buffer wait X seconds open file for reading slurp into second scalar close file calculate MD5 of second slurp buffer compare and repeat if not equal.
Another scheme I have used in the past is to let the FTP client that is putting the files into the directory bit-twiddle the permission bits so that when the client is done copying the permission bits are of some pre-agreed upon value, (444 being my favorite) and then opening the file.


Peter L. Berghold -- Unix Professional
Peter at Berghold dot Net
   Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.

Replies are listed 'Best First'.
Re: Re: how to tell if a file is still being modified
by bunnyman (Hermit) on Sep 16, 2003 at 14:12 UTC

    That scheme is inefficient and fragile. Why bother to read the entire file and calculate a hash on it, when checking the file size is a whole lot faster and for an FTP upload, is just as good?

    It is fragile; imagine what would happen if the upload stalled for X+1 seconds and then resumed. If you make X large to try to avoid this, it makes the processing slower.

    The permission bits scheme is better, if your system supports permission bits.

          That scheme is inefficient and fragile. Why bother to read the entire file and calculate a hash on it, when checking the file size is a whole lot faster and for an FTP upload, is just as good?
      If my scheme of checking a hash is fragile then checking file sizes is just as bad if not worse for the same reasons you stated mine was bad.

      As you say if the upload stalls for X+1 seconds then you are going to end up colliding with the upload when you act on what you assume is a finished file.

      I personally like the bit banging method much better but unfortuneatly that is UNIX-centric and is not portable to say Win32 and friends.

      As others have said there are no really clean and portable ways of doing this and YMMV no matter what method you use. Generating MD5 hashes worked for me and in a batch environment are not that expensive.


      Peter L. Berghold -- Unix Professional
      Peter at Berghold dot Net
         Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.
Re: Re: how to tell if a file is still being modified
by Aighearach (Initiate) on Sep 18, 2003 at 10:39 UTC
    There is no reason to slurp the whole file into memory just to calculate an MD5. See Digest::MD5 for the correct way to do it; efficiently. It's quite common for a disk file to be larger than the available RAM on a machine, so that's just about the worst algorithm error you could get in there (on an algorithm that works at all). Though the usefullness of an MD5 here is somewhat suspect in the first place; expensive, and gains nothing over the sollution it would replace.
    --
    Snazzy tagline here

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (9)
As of 2024-03-28 12:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found