-----Original Message-----
From: Ashley Sheridan [mailto:a...@ashleysheridan.co.uk] 
Sent: 19 August 2009 01:00 PM
To: a...@dotcontent.net
Cc: 'Dengxule'; 'Php Maillist'
Subject: RE: [PHP] How to make sure that the target file to read is not
under writing by others?

On Wed, 2009-08-19 at 12:56 +0200, Arno Kuhl wrote:
> -----Original Message-----
> From: Ashley Sheridan [mailto:a...@ashleysheridan.co.uk]
> Sent: 19 August 2009 11:57 AM
> To: a...@dotcontent.net
> Cc: 'Dengxule'; 'Php Maillist'
> Subject: RE: [PHP] How to make sure that the target file to read is 
> not under writing by others?
> 
> On Wed, 2009-08-19 at 11:55 +0200, Arno Kuhl wrote:
> > -----Original Message-----
> > From: Dengxule [mailto:dengx...@gmail.com]
> > Sent: 19 August 2009 09:56 AM
> > To: Php Maillist
> > Subject: [PHP] How to make sure that the target file to read is not 
> > under writing by others?
> > 
> > Hi everyone:
> > 
> > I have a crontab command to execuate my php-script every half an hour.
> > 
> > The mission of the php-script is to open a file(log file), examine it.
> > 
> > The target file(log file) is transported to local every half an hour.
> > 
> > I've no idea how much time it will costs and on the other hand, i 
> > want to make sure the file i'm openning is Completely Written.
> > 
> > Any instruction will be grateful.
> > 
> > 
> > PS: I've made a test. My php-script just "fopen" a local file being 
> > transported in, and the "fopen" returns no FALSE value but a resource.
> > 
> > 
> > Dengxule
> > 2009/08/19
> > ----------------
> > 
> > Check the timestamp of the log file. Alternatively you can have a 
> > log control file that is updated by the program transferring the log 
> > file only when the transfer is completed, and work on that.
> > 
> > Cheers
> > Arno
> > 
> > 
> The log control file wouldn't work, as how would you know when to open 
> the control file?!
> 
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
> 
> ----------
> 
> You can open the control file any time you want. The info in the 
> control file tells you whether to proceed. The transfer program adds 
> the entry when the log has been transferred, the processing script 
> updates the entry to indicate it's been processed. You can either add 
> a new entry each time (useful for monitoring to fine-tune the timing, 
> or for keeping a history) or work on a single entry that's overwritten
each time the log is transferred.
> 
> Cheers
> Arno
> 
> 
No, what you're saying is 'use a log file in order to know when to look at
another log file'. What would happen if you tried to access the control log
file whilst it was in the process of being written to?
Admittedly, you reduce your chances of failure because there is less data
being written to the control log than the actual log, but the chance of
reading incomplete data is still there!

Thanks,
Ash
http://www.ashleysheridan.co.uk

----------------

A control file is used to control processing, a log file is used to log
events. I'm definitely talking about a control file. If the control file
happens to keep a log then it's a bonus, but not its primary purpose.

Access the control file while being written to? I'm talking about a short
single-line entry, and the file is only written to when the write command is
processed. Copying a file is very different to issuing a single write
command. Don't forget that PHP and webservers run on top of operating
systems, and the OS ultimately handles all file activity. The OS ensures
that another process can't read a file that is halfway through a write
command (any computer would instantly crash if that sort of basic
housekeeping wasn't done). 

If you don't trust the OS then file locking can be used (though pointless in
this case). For the ultra-paranoids who don't trust OS's or locks you can
either (a) choose a different career, or (b) add a time-stamp to the
beginning of the entry and check that the entry wasn't written in the last
few nanoseconds (if you get this far I'd recommend option a). You can even
go so far as to check the length of the timestamp string to be sure that you
got all of it, and if it's too short you know you're busy reading a file
that's halfway through a write command, and sleep for a millisecond and then
try again ;)

Cheers
Arno


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to