From dcc8da3e1d3a7f37c4b18f7130c1b45879f53d21 Mon Sep 17 00:00:00 2001 From: Kaiwen Xu Date: Wed, 19 Nov 2014 22:15:36 -0800 Subject: [PATCH] Raise warning when put_contents on non-regular file with exclusive lock. Summary: In file_put_contents(), raise warning if attempting to add an exclusive lock on non-regular file. Passing vfsstream's filePutContentsLockShouldReportError test case. Closes https://github.com/facebook/hhvm/pull/4238 Reviewed By: @paulbiss Differential Revision: D1678644 Signature: t1:1678644:1416464069:78ae002251c6b6aa966520daef0d4b924f9e8b59 --- hphp/runtime/ext/std/ext_std_file.cpp | 14 ++++++++++++-- hphp/test/frameworks/results/vfsstream.expect | 2 +- hphp/test/slow/ext_file/file_put_contents.php | 7 +++++++ .../slow/ext_file/file_put_contents.php.expect | 1 + 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/hphp/runtime/ext/std/ext_std_file.cpp b/hphp/runtime/ext/std/ext_std_file.cpp index c922629ba07fb3..565097848e168e 100644 --- a/hphp/runtime/ext/std/ext_std_file.cpp +++ b/hphp/runtime/ext/std/ext_std_file.cpp @@ -577,8 +577,18 @@ Variant HHVM_FUNCTION(file_put_contents, return false; } - if ((flags & LOCK_EX) && !f->lock(LOCK_EX)) { - return false; + if (flags & LOCK_EX) { + // Check to make sure we are dealing with a regular file + if (!dynamic_cast(f)) { + raise_warning( + "%s(): Exclusive locks may only be set for regular files", + __FUNCTION__ + 2); + return false; + } + + if (!f->lock(LOCK_EX)) { + return false; + } } int numbytes = 0; diff --git a/hphp/test/frameworks/results/vfsstream.expect b/hphp/test/frameworks/results/vfsstream.expect index 6bd3debef334d8..1a17941c4b8a6c 100644 --- a/hphp/test/frameworks/results/vfsstream.expect +++ b/hphp/test/frameworks/results/vfsstream.expect @@ -219,7 +219,7 @@ org\bovigo\vfs\vfsStreamDirectoryTestCase::rename org\bovigo\vfs\vfsStreamDirectoryTestCase::renameToInvalidNameThrowsvfsStreamException . org\bovigo\vfs\vfsStreamExLockTestCase::filePutContentsLockShouldReportError -F +. org\bovigo\vfs\vfsStreamExLockTestCase::flockSouldPass . org\bovigo\vfs\vfsStreamFileTestCase::content diff --git a/hphp/test/slow/ext_file/file_put_contents.php b/hphp/test/slow/ext_file/file_put_contents.php index 8d7189bd778079..f310ab53d8f942 100644 --- a/hphp/test/slow/ext_file/file_put_contents.php +++ b/hphp/test/slow/ext_file/file_put_contents.php @@ -6,3 +6,10 @@ var_dump(file_get_contents($tempfile)); unlink($tempfile); + +$tempfile2 = tempnam('/tmp', 'vmextfiletest'); + +file_put_contents('File://' . $tempfile2, 'testing file_put_contents with File:// url and LOCK_EX', LOCK_EX); +var_dump(file_get_contents($tempfile2)); + +unlink($tempfile2); diff --git a/hphp/test/slow/ext_file/file_put_contents.php.expect b/hphp/test/slow/ext_file/file_put_contents.php.expect index 75f1783a8c7e81..2a1f22359f2abd 100644 --- a/hphp/test/slow/ext_file/file_put_contents.php.expect +++ b/hphp/test/slow/ext_file/file_put_contents.php.expect @@ -1 +1,2 @@ string(25) "testing file_put_contents" +string(54) "testing file_put_contents with File:// url and LOCK_EX"