Re: checking if a file is locked before trying to open it for write?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jeffry Killen wrote:

> I've been reading my copy of the manual regarding flock() and it
> appears that a script will have to try to open the file before finding
> out if it is locked.

Yes, because flock() expects a file handle as first parameter, not a
file name.

> So, if another script has a lock on the file and the current script
> does is_writable on the file, will it return false (and trying to open
> the file will generate a permission denied error ?).

AFAIK is_writable() ignores any locks set via flock().

> This assumes a multi user situation, like two or more admin users
> are logged into one cms system and are trying to editing  the same
> file at the same time.

Most likely you can't prevent that with flock() only, as the lock is
removed as soon as the file handle is closed, what happens at the end of
the script, at the latest.  So you'll need some kind of "offline"
locking strategy.  An optimistic approach might be simpler to implement
reliably (store a hash of the file contents in the edit form, and check
whether the hash of the current file contents has changed when the form
is submitted; if so, reject the submission), but might not be
appropriate.  DokuWiki[1] uses a pessimistic locking strategy, but I
don't know how it's implemented.  You may have a look at the implementation.

[1] <https://www.dokuwiki.org/>

-- 
Christoph M. Becker

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





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux