From e3cd505199bbbe638348538765b61eddfbeffe37 Mon Sep 17 00:00:00 2001 From: Raffael Sahli Date: Thu, 16 Aug 2018 13:28:49 +0200 Subject: [PATCH] Implemented IShare::append(), leave ::write() due backwards compatibility --- src/IShare.php | 13 +++++++++++++ src/Native/NativeShare.php | 35 +++++++++++++++++++++++++++++++---- src/Wrapped/Share.php | 12 ++++++++++++ tests/NativeShareTest.php | 17 ++++++++++++++++- tests/ShareTest.php | 7 +++++++ 5 files changed, 79 insertions(+), 5 deletions(-) diff --git a/src/IShare.php b/src/IShare.php index 8f6d505..88aa676 100644 --- a/src/IShare.php +++ b/src/IShare.php @@ -52,6 +52,7 @@ interface IShare { /** * Open a writable stream to a remote file + * Note: This method will truncate the file to 0bytes * * @param string $target * @return resource a write only stream to upload a remote file @@ -61,6 +62,18 @@ interface IShare { */ public function write($target); + /** + * Open a writable stream to a remote file and set the cursor to the end of the file + * + * @param string $target + * @return resource a write only stream to upload a remote file + * + * @throws \Icewind\SMB\Exception\NotFoundException + * @throws \Icewind\SMB\Exception\InvalidTypeException + * @throws \Icewind\SMB\Exception\InvalidRequestException + */ + public function append($target); + /** * Rename a remote file * diff --git a/src/Native/NativeShare.php b/src/Native/NativeShare.php index 3b5cca8..a757d7f 100644 --- a/src/Native/NativeShare.php +++ b/src/Native/NativeShare.php @@ -109,6 +109,12 @@ class NativeShare extends AbstractShare { return new NativeFileInfo($this, $path, basename($path), $this->getStat($path)); } + /** + * Get fstat + * + * @param string $path + * @return array + */ public function getStat($path) { return $this->getState()->stat($this->buildUrl($path)); } @@ -228,7 +234,7 @@ class NativeShare extends AbstractShare { } /** - * Open a readable stream top a remote file + * Open a readable stream to a remote file * * @param string $source * @return resource a read only stream with the contents of the remote file @@ -243,10 +249,11 @@ class NativeShare extends AbstractShare { } /** - * Open a readable stream top a remote file + * Open a writeable stream to a remote file + * Note: This method will truncate the file to 0bytes first * * @param string $source - * @return resource a read only stream with the contents of the remote file + * @return resource a writeable stream * * @throws \Icewind\SMB\Exception\NotFoundException * @throws \Icewind\SMB\Exception\InvalidTypeException @@ -257,6 +264,21 @@ class NativeShare extends AbstractShare { return NativeWriteStream::wrap($this->getState(), $handle, 'w', $url); } + /** + * Open a writeable stream and set the cursor to the end of the stream + * + * @param string $source + * @return resource a writeable stream + * + * @throws \Icewind\SMB\Exception\NotFoundException + * @throws \Icewind\SMB\Exception\InvalidTypeException + */ + public function append($source) { + $url = $this->buildUrl($source); + $handle = $this->getState()->open($url, "a"); + return NativeWriteStream::wrap($this->getState(), $handle, "a", $url); + } + /** * Get extended attributes for the path * @@ -269,7 +291,7 @@ class NativeShare extends AbstractShare { } /** - * Get extended attributes for the path + * Set extended attributes for the given path * * @param string $path * @param string $attribute attribute to get the info @@ -286,6 +308,8 @@ class NativeShare extends AbstractShare { } /** + * Set DOS comaptible node mode + * * @param string $path * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL * @return mixed @@ -295,6 +319,9 @@ class NativeShare extends AbstractShare { } /** + * Start smb notify listener + * Note: This is a blocking call + * * @param string $path * @return INotifyHandler */ diff --git a/src/Wrapped/Share.php b/src/Wrapped/Share.php index ec27815..909c8a3 100644 --- a/src/Wrapped/Share.php +++ b/src/Wrapped/Share.php @@ -13,6 +13,7 @@ use Icewind\SMB\Exception\DependencyException; use Icewind\SMB\Exception\FileInUseException; use Icewind\SMB\Exception\InvalidTypeException; use Icewind\SMB\Exception\NotFoundException; +use Icewind\SMB\Exception\InvalidRequestException; use Icewind\SMB\INotifyHandler; use Icewind\SMB\IServer; use Icewind\SMB\ISystem; @@ -328,6 +329,17 @@ class Share extends AbstractShare { }); } + /** + * Append on wrapped smbclient not supported + * + * @param string $source + * + * @throws \Icewind\SMB\Exception\InvalidRequestException + */ + public function append($source) { + throw new InvalidRequestException("Not supported, use Icewind\SMB\Native\NativeShare"); + } + /** * @param string $path * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL diff --git a/tests/NativeShareTest.php b/tests/NativeShareTest.php index 667da9e..f75e61e 100644 --- a/tests/NativeShareTest.php +++ b/tests/NativeShareTest.php @@ -14,7 +14,7 @@ use Icewind\SMB\System; use Icewind\SMB\TimeZoneProvider; class NativeShareTest extends AbstractShareTest { - public function setUp() { + public function setUp() { $this->requireBackendEnv('libsmbclient'); if (!function_exists('smbclient_state_new')) { $this->markTestSkipped('libsmbclient php extension not installed'); @@ -39,4 +39,19 @@ class NativeShareTest extends AbstractShareTest { } $this->share->mkdir($this->root); } + + public function testAppendStream() { + $fh = $this->share->append($this->root . '/' . $name); + fwrite($fh, 'foo'); + fclose($fh); + + $fh = $this->share->append($this->root . '/' . $name); + fwrite($fh, 'bar'); + fclose($fh); + + $tmpFile1 = tempnam('/tmp', 'smb_test_'); + $this->assertEquals('foobar', file_get_contents($tmpFile1)); + $this->share->del($this->root . '/' . $name); + unlink($tmpFile1); + } } diff --git a/tests/ShareTest.php b/tests/ShareTest.php index 640b6aa..0916c82 100644 --- a/tests/ShareTest.php +++ b/tests/ShareTest.php @@ -57,4 +57,11 @@ class ShareTest extends AbstractShareTest { $share = $this->server->getShare($this->config->share); $share->dir($this->root); } + + /** + * @expectedException \Icewind\SMB\Exception\InvalidRequestException + */ + public function testAppendStream() { + $fh = $this->share->append($this->root . '/' . $name); + } }