Issue #1954180 by fietserwin, jthorson: LocalReadOnlyStream::stream_open() refuses to open a file when 'rb' flag is passed in.

8.0.x
Alex Pott 2013-06-30 02:12:07 +01:00
parent e5b7fb4b67
commit 9dbbb4dd98
3 changed files with 24 additions and 12 deletions

View File

@ -28,19 +28,20 @@ abstract class LocalReadOnlyStream extends LocalStream {
* @param string $uri
* A string containing the URI to the file to open.
* @param int $mode
* The file mode (only "r" is supported for the read-only stream wrapper.)
* The file mode, only strict readonly modes are supported.
* @param int $options
* A bit mask of STREAM_USE_PATH and STREAM_REPORT_ERRORS.
* @param string $opened_path
* A string containing the path actually opened.
*
* @return bool
* Returns TRUE if $mode == "r" and the file was opened successfully.
* TRUE if $mode denotes a readonly mode and the file was opened
* successfully, FALSE otherwise.
*
* @see http://php.net/manual/streamwrapper.stream-open.php
*/
public function stream_open($uri, $mode, $options, &$opened_path) {
if ($mode != "r") {
if (!in_array($mode, array('r', 'rb', 'rt'))) {
if ($options & STREAM_REPORT_ERRORS) {
trigger_error('stream_open() write modes not supported for read-only stream wrappers', E_USER_WARNING);
}
@ -114,6 +115,8 @@ abstract class LocalReadOnlyStream extends LocalStream {
* Support for fflush().
*
* Nothing will be output to the file, as this is a read-only stream wrapper.
* However as stream_flush is called during stream_close we should not trigger
* an error.
*
* @return bool
* FALSE, as no data will be stored.
@ -121,7 +124,6 @@ abstract class LocalReadOnlyStream extends LocalStream {
* @see http://php.net/manual/streamwrapper.stream-flush.php
*/
public function stream_flush() {
trigger_error('stream_flush() not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE;
}

View File

@ -63,19 +63,20 @@ abstract class ReadOnlyStream implements StreamWrapperInterface {
* @param string $uri
* A string containing the URI to the file to open.
* @param int $mode
* The file mode (only "r" is supported for the read-only stream wrapper.)
* The file mode, only strict readonly modes are supported.
* @param int $options
* A bit mask of STREAM_USE_PATH and STREAM_REPORT_ERRORS.
* @param string $opened_path
* A string containing the path actually opened.
*
* @return bool
* Returns TRUE if $mode == "r" and the file was opened successfully.
* TRUE if $mode denotes a readonly mode and the file was opened
* successfully, FALSE otherwise.
*
* @see http://php.net/manual/streamwrapper.stream-open.php
*/
public function stream_open($uri, $mode, $options, &$opened_path) {
if ($mode != "r") {
if (!in_array($mode, array('r', 'rb', 'rt'))) {
if ($options & STREAM_REPORT_ERRORS) {
trigger_error('stream_open() write modes not supported for read-only stream wrappers', E_USER_WARNING);
}
@ -148,6 +149,8 @@ abstract class ReadOnlyStream implements StreamWrapperInterface {
* Support for fflush().
*
* Nothing will be output to the file, as this is a read-only stream wrapper.
* However as stream_flush is called during stream_close we should not trigger
* an error.
*
* @return bool
* FALSE, as no data will be stored.
@ -155,7 +158,6 @@ abstract class ReadOnlyStream implements StreamWrapperInterface {
* @see http://php.net/manual/streamwrapper.stream-flush.php
*/
public function stream_flush() {
trigger_error('stream_flush() not supported for read-only stream wrappers', E_USER_WARNING);
return FALSE;
}

View File

@ -53,9 +53,17 @@ class ReadOnlyStreamWrapperTest extends FileTestBase {
$uri = $this->scheme . '://' . $filename;
$instance = file_stream_wrapper_get_instance_by_scheme($this->scheme);
// Attempt to open a file in write mode
$handle = @fopen($uri, 'w+');
$this->assertFalse($handle, 'Unable to open a file for writing with the read-only stream wrapper.');
// Attempt to open a file in read/write mode
$handle = @fopen($uri, 'r+');
$this->assertFalse($handle, 'Unable to open a file for reading and writing with the read-only stream wrapper.');
// Attempt to open a file in binary read mode
$handle = fopen($uri, 'rb');
$this->assertTrue($handle, 'Able to open a file for reading in binary mode with the read-only stream wrapper.');
$this->assertTrue(fclose($handle), 'Able to close file opened in binary mode using the read_only stream wrapper.');
// Attempt to open a file in text read mode
$handle = fopen($uri, 'rt');
$this->assertTrue($handle, 'Able to open a file for reading in text mode with the read-only stream wrapper.');
$this->assertTrue(fclose($handle), 'Able to close file opened in text mode using the read_only stream wrapper.');
// Attempt to open a file in read mode
$handle = fopen($uri, 'r');
$this->assertTrue($handle, 'Able to open a file for reading with the read-only stream wrapper.');
@ -72,7 +80,7 @@ class ReadOnlyStreamWrapperTest extends FileTestBase {
// Attempt to flush output to the file
$this->assertFalse(@fflush($handle), 'Unable to flush output to file using the read-only stream wrapper.');
// Attempt to close the stream. (Suppress errors, as fclose triggers fflush.)
$this->assertTrue(@fclose($handle), 'Able to close file using the read_only stream wrapper.');
$this->assertTrue(fclose($handle), 'Able to close file using the read_only stream wrapper.');
// Test the rename() function
$this->assertFalse(@rename($uri, $this->scheme . '://newname.txt'), 'Unable to rename files using the read-only stream wrapper.');
// Test the unlink() function