Bug #51969 [Opn->Nab]: fwrite returns 1 when trying to write to a locked file
| From: | cmb@php.net | Date: | Sun, 12 Apr 2015 21:19:42 +0000 |
| Subject: | Bug #51969 [Opn->Nab]: fwrite returns 1 when trying to write to a locked file | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-192003@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=51969&edit=1
ID: 51969
Updated by: cmb@php.net
Reported by: scarduzio at gmail dot com
Summary: fwrite returns 1 when trying to write to a locked
file
-Status: Open
+Status: Not a bug
Type: Bug
Package: Filesystem function related
Operating System: Windows 7 x86
PHP Version: 5.3.2
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
You can't lock a file that you're opening in "w" mode reliably. See
also the respective note in the flock man page[1] and the
documentation of the $mode parameter of fopen[2].
On Windows locking a file opened in "w" mode might work reliably.
However, testing on PHP 5.4.19 revealed that fwrite() returned 0 in
the test script.
[1] <http://php.net/manual/en/function.flock.php#refsect1-function.flock-notes>
[2] <http://php.net/manual/en/function.fopen.php>
Previous Comments:
------------------------------------------------------------------------
[2010-06-10 14:50:19] fa@php.net
Ah, ok, thank you for the clarification.
Tried to reproduce now, indeed fwrite in the else clause ignores the flock() - but then again you
already checked if there is a lock, that is exactly why you are in the else code path, so I'm
not exactly sure how flock *should* behave ...
Trying to open the locked file gives a windows warning that the file is indeed locked, though.
Same result with PHP 5.3.2 (cli) on Debian lenny, btw - minus the warning on accessing the file
directly.
------------------------------------------------------------------------
[2010-06-10 12:26:50] scarduzio at gmail dot com
Hi, thanks for replying.
The OS in use is Windows 7 x86 genuine with all windows updates.
The path with forward slash that I use in my code is unix style because I
noticed
PHP always translates it correctly into "C:\cache\lockable.txt", and it's always
been fine. Also this time, the file is always found.
About the manual line stating LOCK_NB bitmask is not supported: as evinced from
my tests, including this one, the use of LOCK_NB is behaving perfectly since it
detects file is locked, and executes my "else".
I also do not think LOCK_NB is the core of this problem since I could split this
script in two (one with the flock+sleep and the other with the "else" content)
and reproduce the same bug.
I later tested this in linux environment, fwrite will return 5 (not 1). Which is
still funny, since writing in a locked file should in my opinion return false.
I look forward for your further analysis.
BR,
Simone
------------------------------------------------------------------------
[2010-06-10 12:04:33] fa@php.net
From the manual:
"It is also possible to add LOCK_NB as a bitmask to one of the above operations if you
don't want flock() to block while locking. (not supported on Windows)"
Then again path "/cache/lockable.txt" doesn't look like windows - so what OS are you
using?
And while I know that's possibly not directly related to your problem - please clarify.
------------------------------------------------------------------------
[2010-06-01 23:45:32] scarduzio at gmail dot com
Description:
------------
When I try to write any length string with fwrite using a file handle I previously
locked with flock, fwrite is of course unable to write, but will return
unexplicable value 1 instead of false.
Test script:
---------------
/*
Run two instances of this script in a rapid sequence
*/
$fp1 = fopen("/cache/lockable.txt","w");
if(flock($fp1, LOCK_EX | LOCK_NB)){
echo "locked fp1\n";
sleep(20);
}else{
$res = fwrite($fp1, "ABCDE");
if(false === $res){
echo "failed the write\n";
}
echo "I managed! fwrite returned: "; print_r($res);
}
Expected result:
----------------
I expect to read "failed to write" when a second instance of the script is run,
since fwrite should return false as documentation says when fwrite is not able to
write.
Actual result:
--------------
fwrite returns integer 1.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=51969&edit=1