Implemented IShare::append(), leave ::write() due backwards

compatibility
This commit is contained in:
Raffael Sahli 2018-08-16 13:28:49 +02:00
commit e3cd505199
5 changed files with 79 additions and 5 deletions

View file

@ -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
*

View file

@ -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
*/

View file

@ -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

View file

@ -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);
}
}

View file

@ -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);
}
}