[PHP-BUG] Bug #63908 [NEW]: file_put_contents is not atomic.
From: awm086 at gmail dot com Operating system: LINUX PHP version: Irrelevant Package: Filesystem function related Bug Type: Bug Bug description:file_put_contents is not atomic. Description: The function file_put_contents is not atomic. When the function fails a file should not be created. However, when the reason for failure is full directory, (full diskspace, 0 empty), the function return FALSE, but a file is still created in the directory with size 0. The function should either complete the job successfully or have no effects at all (atomic). Test script: --- $data = 'somedata'; $temp_name = '/tmp/myfile'; if (file_put_contents($temp_name, $data) === FALSE) { // the message print that the file could not be created. print 'The file could not be created.'; } // after this a file named myfile will exist in the directory. Expected result: No file should be created Actual result: -- File is created in the directory. -- Edit bug report at https://bugs.php.net/bug.php?id=63908&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=63908&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=63908&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=63908&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=63908&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=63908&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=63908&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=63908&r=needscript Try newer version: https://bugs.php.net/fix.php?id=63908&r=oldversion Not developer issue:https://bugs.php.net/fix.php?id=63908&r=support Expected behavior: https://bugs.php.net/fix.php?id=63908&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=63908&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=63908&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=63908&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=63908&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=63908&r=dst IIS Stability: https://bugs.php.net/fix.php?id=63908&r=isapi Install GNU Sed:https://bugs.php.net/fix.php?id=63908&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=63908&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=63908&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=63908&r=mysqlcfg
Bug #63908 [Com]: file_put_contents is not atomic.
Edit report at https://bugs.php.net/bug.php?id=63908&edit=1 ID: 63908 Comment by: awm086 at gmail dot com Reported by:awm086 at gmail dot com Summary:file_put_contents is not atomic. Status: Not a bug Type: Bug Package:Filesystem function related Operating System: LINUX PHP Version:Irrelevant Block user comment: N Private report: N New Comment: I cannot belive that a function returns false, indicating that a file failed to be written, and still write to the file system. There must be a way. Previous Comments: [2013-01-04 23:56:46] ras...@php.net Sorry, there is no way to guarantee that. Even if we were to check before starting to write, there is no way to do an atomic check+write, so in between checking and writing even though the check said there should be space to write to completion, the filesystem could have filled up by the time we actually do the write. If you need this, you have to do it in userspace and apply whatever logic makes sense for your environment to ensure that the write is safe. [2013-01-04 22:12:57] awm086 at gmail dot com Description: The function file_put_contents is not atomic. When the function fails a file should not be created. However, when the reason for failure is full directory, (full diskspace, 0 empty), the function return FALSE, but a file is still created in the directory with size 0. The function should either complete the job successfully or have no effects at all (atomic). Test script: --- $data = 'somedata'; $temp_name = '/tmp/myfile'; if (file_put_contents($temp_name, $data) === FALSE) { // the message print that the file could not be created. print 'The file could not be created.'; } // after this a file named myfile will exist in the directory. Expected result: No file should be created Actual result: -- File is created in the directory. -- Edit this bug report at https://bugs.php.net/bug.php?id=63908&edit=1
Bug #63908 [Com]: file_put_contents is not atomic.
Edit report at https://bugs.php.net/bug.php?id=63908&edit=1 ID: 63908 Comment by: awm086 at gmail dot com Reported by:awm086 at gmail dot com Summary:file_put_contents is not atomic. Status: Not a bug Type: Bug Package:Filesystem function related Operating System: LINUX PHP Version:Irrelevant Block user comment: N Private report: N New Comment: Where can I find the C implementation of the function. I am just curious now. Previous Comments: [2013-01-06 00:09:16] ras...@php.net Nope, sorry, there isn't. The false indicates that the function wasn't able to complete its operation. Much like the underlying filesystem calls, there is no such thing as transactions or anything along those lines where you can group a bunch of operations into a single atomic unit. That's simply not how filesystem operations work. [2013-01-05 23:40:34] awm086 at gmail dot com I cannot belive that a function returns false, indicating that a file failed to be written, and still write to the file system. There must be a way. [2013-01-04 23:56:46] ras...@php.net Sorry, there is no way to guarantee that. Even if we were to check before starting to write, there is no way to do an atomic check+write, so in between checking and writing even though the check said there should be space to write to completion, the filesystem could have filled up by the time we actually do the write. If you need this, you have to do it in userspace and apply whatever logic makes sense for your environment to ensure that the write is safe. [2013-01-04 22:12:57] awm086 at gmail dot com Description: The function file_put_contents is not atomic. When the function fails a file should not be created. However, when the reason for failure is full directory, (full diskspace, 0 empty), the function return FALSE, but a file is still created in the directory with size 0. The function should either complete the job successfully or have no effects at all (atomic). Test script: --- $data = 'somedata'; $temp_name = '/tmp/myfile'; if (file_put_contents($temp_name, $data) === FALSE) { // the message print that the file could not be created. print 'The file could not be created.'; } // after this a file named myfile will exist in the directory. Expected result: No file should be created Actual result: -- File is created in the directory. -- Edit this bug report at https://bugs.php.net/bug.php?id=63908&edit=1
Bug #63908 [Com]: file_put_contents is not atomic.
Edit report at https://bugs.php.net/bug.php?id=63908&edit=1 ID: 63908 Comment by: awm086 at gmail dot com Reported by:awm086 at gmail dot com Summary:file_put_contents is not atomic. Status: Not a bug Type: Bug Package:Filesystem function related Operating System: LINUX PHP Version:Irrelevant Block user comment: N Private report: N New Comment: I noticed that function tempname does the same thing. Is that the case? Previous Comments: [2013-01-06 00:33:34] ras...@php.net http://lxr.php.net/xref/PHP_5_4/ext/standard/file.c#564 [2013-01-06 00:24:42] awm086 at gmail dot com Where can I find the C implementation of the function. I am just curious now. [2013-01-06 00:09:16] ras...@php.net Nope, sorry, there isn't. The false indicates that the function wasn't able to complete its operation. Much like the underlying filesystem calls, there is no such thing as transactions or anything along those lines where you can group a bunch of operations into a single atomic unit. That's simply not how filesystem operations work. [2013-01-05 23:40:34] awm086 at gmail dot com I cannot belive that a function returns false, indicating that a file failed to be written, and still write to the file system. There must be a way. [2013-01-04 23:56:46] ras...@php.net Sorry, there is no way to guarantee that. Even if we were to check before starting to write, there is no way to do an atomic check+write, so in between checking and writing even though the check said there should be space to write to completion, the filesystem could have filled up by the time we actually do the write. If you need this, you have to do it in userspace and apply whatever logic makes sense for your environment to ensure that the write is safe. The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=63908 -- Edit this bug report at https://bugs.php.net/bug.php?id=63908&edit=1
Bug #63908 [Com]: file_put_contents is not atomic.
Edit report at https://bugs.php.net/bug.php?id=63908&edit=1 ID: 63908 Comment by: Awm086 at gmail dot com Reported by:awm086 at gmail dot com Summary:file_put_contents is not atomic. Status: Not a bug Type: Bug Package:Filesystem function related Operating System: LINUX PHP Version:Irrelevant Block user comment: N Private report: N New Comment: Sorry I meant tempnam does creat a file even if the directory is full. Previous Comments: [2013-01-08 00:22:20] ras...@php.net You mean tempnam and no, not at all. tempnam() creates an empty file, it doesn't put anything in it. You can't check disk space, create a file and write to it, all in one atomic operation. [2013-01-07 23:48:20] awm086 at gmail dot com I noticed that function tempname does the same thing. Is that the case? [2013-01-06 00:33:34] ras...@php.net http://lxr.php.net/xref/PHP_5_4/ext/standard/file.c#564 [2013-01-06 00:24:42] awm086 at gmail dot com Where can I find the C implementation of the function. I am just curious now. [2013-01-06 00:09:16] ras...@php.net Nope, sorry, there isn't. The false indicates that the function wasn't able to complete its operation. Much like the underlying filesystem calls, there is no such thing as transactions or anything along those lines where you can group a bunch of operations into a single atomic unit. That's simply not how filesystem operations work. The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=63908 -- Edit this bug report at https://bugs.php.net/bug.php?id=63908&edit=1