optional truncate

This commit is contained in:
raffis 2019-10-09 10:10:07 +02:00
commit 7e49f35ae3
No known key found for this signature in database
GPG key ID: 5E0BF46A67AD81C4
2 changed files with 17 additions and 4 deletions

View file

@ -265,17 +265,24 @@ class NativeShare extends AbstractShare {
/** /**
* Open a writeable stream to a remote file * Open a writeable stream to a remote file
* Note: This method will truncate the file to 0bytes first * Note: The default will truncate the file to 0 bytes.
* *
* @param string $source * @param string $source
* @param bool $truncate
* @return resource a writeable stream * @return resource a writeable stream
* *
* @throws \Icewind\SMB\Exception\NotFoundException * @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException * @throws \Icewind\SMB\Exception\InvalidTypeException
*/ */
public function write($source) { public function write($source, $truncate=true) {
$url = $this->buildUrl($source); $url = $this->buildUrl($source);
$handle = $this->getState()->create($url);
if($truncate === true) {
$handle = $this->getState()->create($url);
} else {
$handle = $this->getState()->open($url, 'c');
}
return NativeWriteStream::wrap($this->getState(), $handle, 'w', $url); return NativeWriteStream::wrap($this->getState(), $handle, 'w', $url);
} }

View file

@ -326,12 +326,18 @@ class Share extends AbstractShare {
* Open a writable stream to a remote file * Open a writable stream to a remote file
* *
* @param string $target * @param string $target
* @param bool $truncate
* @return resource a write only stream to upload a remote file * @return resource a write only stream to upload a remote file
* *
* @throws \Icewind\SMB\Exception\DependencyException
* @throws \Icewind\SMB\Exception\NotFoundException * @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException * @throws \Icewind\SMB\Exception\InvalidTypeException
*/ */
public function write($target) { public function write($target, $truncate=true) {
if($truncate === false) {
throw new DependencyException('truncate required by smbclient, use php-libsmbclient instead');
}
$target = $this->escapePath($target); $target = $this->escapePath($target);
// since returned stream is closed by the caller we need to create a new instance // since returned stream is closed by the caller we need to create a new instance
// since we can't re-use the same file descriptor over multiple calls // since we can't re-use the same file descriptor over multiple calls