Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions Storage/src/StreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/**
* A streamWrapper implementation for handling `gs://bucket/path/to/file.jpg`.
* Note that you can only open a file with mode 'r', 'rb', 'rt', 'w', 'wb', 'wt', 'a', 'ab', or 'at'.
* Note that you can only open a file with mode 'r', 'rb', 'rt', 'w', 'wb', 'wt', 'a', 'ab', 'at', 'x', 'xb', or 'xt'.
*
* See: http://php.net/manual/en/class.streamwrapper.php
*/
Expand Down Expand Up @@ -211,7 +211,8 @@ public static function getClient($protocol = null)
* download the file to see if it can be opened.
*
* @param string $path The path of the resource to open
* @param string $mode The fopen mode. Currently supports ('r', 'rb', 'rt', 'w', 'wb', 'wt', 'a', 'ab', 'at')
* @param string $mode The fopen mode. Currently supports ('r', 'rb', 'rt',
* 'w', 'wb', 'wt', 'a', 'ab', 'at', 'x', 'xb', 'xt')
* @param int $flags Bitwise options STREAM_USE_PATH|STREAM_REPORT_ERRORS|STREAM_MUST_SEEK
* @param string $openedPath Will be set to the path on success if STREAM_USE_PATH option is set
* @return bool
Expand Down Expand Up @@ -264,6 +265,22 @@ public function stream_open($path, $mode, $flags, &$openedPath)
$options + ['name' => $name]
)
);
} elseif ($mode == 'x') {
try {
if ($this->bucket->object($this->file)->exists()) {
return $this->returnError('File already exists.', $flags);
}
} catch (ServiceException $ex) {
return $this->returnError($ex->getMessage(), $flags);
}

$this->stream = new WriteStream(null, $options);
$this->stream->setUploader(
$this->bucket->getStreamableUploader(
$this->stream,
$options + ['name' => $this->file, 'ifGenerationMatch' => 0]
)
);
} elseif ($mode == 'r') {
try {
// Lazy read from the source
Expand Down
26 changes: 26 additions & 0 deletions Storage/tests/System/StreamWrapper/WriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,30 @@ public function testStreamingWrite()

$this->assertFileExists($this->fileUrl);
}

public function testFwriteWithXMode()
{
$this->assertFileDoesNotExist($this->fileUrl);

$output = 'This is a test with x mode';
$fd = fopen($this->fileUrl, 'x');
$this->assertIsResource($fd);
$this->assertEquals(strlen($output), fwrite($fd, $output));
$this->assertTrue(fclose($fd));

$this->assertFileExists($this->fileUrl);
}

public function testFwriteWithXModeFailsIfExists()
{
$this->assertFileDoesNotExist($this->fileUrl);

// Create the file first
touch($this->fileUrl);
$this->assertFileExists($this->fileUrl);

// Try to open with 'x' mode, should fail
$fd = @fopen($this->fileUrl, 'x');
$this->assertFalse($fd);
}
}
57 changes: 57 additions & 0 deletions Storage/tests/Unit/StreamWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,67 @@ public function testOpeningNonExistentFileReturnsFalse()
*/
public function testUnknownOpenMode()
{
$fp = @fopen('gs://my_bucket/existing_file.txt', 'z');
$this->assertFalse($fp);
}

/**
* @group storageWrite
*/
public function testOpeningExistingFileWithXModeReturnsFalse()
{
$object = $this->prophesize(StorageObject::class);
$object->exists()->willReturn(true);
$this->bucket->object('existing_file.txt')->willReturn($object->reveal());

$fp = @fopen('gs://my_bucket/existing_file.txt', 'x');
$this->assertFalse($fp);
}

/**
* @group storageWrite
*/
public function testOpeningNonExistentFileWithXModeSucceeds()
{
$object = $this->prophesize(StorageObject::class);
$object->exists()->willReturn(false);
$this->bucket->object('new_file.txt')->willReturn($object->reveal());

$uploader = $this->prophesize(StreamableUploader::class);
$uploader->upload()->shouldBeCalled();
$uploader->getResumeUri()->willReturn('https://resume-uri/');

$this->bucket->getStreamableUploader(Argument::any(), Argument::withEntry('ifGenerationMatch', 0))
->willReturn($uploader->reveal());

$fp = fopen('gs://my_bucket/new_file.txt', 'x');
$this->assertIsResource($fp);
fwrite($fp, "some data");
fclose($fp);
}

/**
* @group storageWrite
*/
public function testOpeningNonExistentFileWithXbModeSucceeds()
{
$object = $this->prophesize(StorageObject::class);
$object->exists()->willReturn(false);
$this->bucket->object('new_file.txt')->willReturn($object->reveal());

$uploader = $this->prophesize(StreamableUploader::class);
$uploader->upload()->shouldBeCalled();
$uploader->getResumeUri()->willReturn('https://resume-uri/');

$this->bucket->getStreamableUploader(Argument::any(), Argument::withEntry('ifGenerationMatch', 0))
->willReturn($uploader->reveal());

$fp = fopen('gs://my_bucket/new_file.txt', 'xb');
$this->assertIsResource($fp);
fwrite($fp, "some data");
fclose($fp);
}

/**
* @group storageRead
*/
Expand Down
Loading