Skip to content

Commit

Permalink
Raise warning when put_contents on non-regular file with exclusive lock.
Browse files Browse the repository at this point in the history
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 #4238

Reviewed By: @paulbiss

Differential Revision: D1678644

Signature: t1:1678644:1416464069:78ae002251c6b6aa966520daef0d4b924f9e8b59
  • Loading branch information
kevinxucs authored and hhvm-bot committed Nov 20, 2014
1 parent fbdd08d commit dcc8da3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
14 changes: 12 additions & 2 deletions hphp/runtime/ext/std/ext_std_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlainFile*>(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;
Expand Down
2 changes: 1 addition & 1 deletion hphp/test/frameworks/results/vfsstream.expect
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions hphp/test/slow/ext_file/file_put_contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
1 change: 1 addition & 0 deletions hphp/test/slow/ext_file/file_put_contents.php.expect
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
string(25) "testing file_put_contents"
string(54) "testing file_put_contents with File:// url and LOCK_EX"

0 comments on commit dcc8da3

Please sign in to comment.