mirror of
https://codeberg.org/icewind/SMB.git
synced 2026-06-03 17:24:07 +02:00
Only open target if source can be opened
This prevents empty files from being created if a remote file can't be opened.
This commit is contained in:
parent
2877a6e3da
commit
c67e9b9222
1 changed files with 7 additions and 6 deletions
|
|
@ -223,6 +223,12 @@ class NativeShare extends AbstractShare {
|
||||||
if (!$target) {
|
if (!$target) {
|
||||||
throw new InvalidPathException('Invalid target path: Filename cannot be empty');
|
throw new InvalidPathException('Invalid target path: Filename cannot be empty');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
|
||||||
|
if (!$sourceHandle) {
|
||||||
|
throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
|
||||||
|
}
|
||||||
|
|
||||||
$targetHandle = @fopen($target, 'wb');
|
$targetHandle = @fopen($target, 'wb');
|
||||||
if (!$targetHandle) {
|
if (!$targetHandle) {
|
||||||
$error = error_get_last();
|
$error = error_get_last();
|
||||||
|
|
@ -231,15 +237,10 @@ class NativeShare extends AbstractShare {
|
||||||
} else {
|
} else {
|
||||||
$reason = 'Unknown error';
|
$reason = 'Unknown error';
|
||||||
}
|
}
|
||||||
|
$this->getState()->close($sourceHandle);
|
||||||
throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason);
|
throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
$sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
|
|
||||||
if (!$sourceHandle) {
|
|
||||||
fclose($targetHandle);
|
|
||||||
throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
|
|
||||||
}
|
|
||||||
|
|
||||||
while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
|
while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
|
||||||
fwrite($targetHandle, $data);
|
fwrite($targetHandle, $data);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue