Skip to content

Commit e2a331f

Browse files
authored
Merge pull request #52890 from nextcloud/chore/move-stream-copy-implementation
chore: move streamCopy implementation from `OC_Helper` to `OCP\Files`
2 parents 0c2934e + 0f69648 commit e2a331f

File tree

11 files changed

+81
-44
lines changed

11 files changed

+81
-44
lines changed

apps/dav/lib/Connector/Sabre/File.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use OCA\DAV\Connector\Sabre\Exception\UnsupportedMediaType;
2020
use OCP\App\IAppManager;
2121
use OCP\Encryption\Exceptions\GenericEncryptionException;
22+
use OCP\Files;
2223
use OCP\Files\EntityTooLargeException;
2324
use OCP\Files\FileInfo;
2425
use OCP\Files\ForbiddenException;
@@ -229,7 +230,7 @@ public function put($data) {
229230
// because we have no clue about the cause we can only throw back a 500/Internal Server Error
230231
throw new Exception($this->l10n->t('Could not write file contents'));
231232
}
232-
[$count, $result] = \OC_Helper::streamCopy($data, $target);
233+
[$count, $result] = Files::streamCopy($data, $target, true);
233234
fclose($target);
234235
}
235236

apps/files_versions/lib/Storage.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use OCP\AppFramework\Db\DoesNotExistException;
2424
use OCP\Command\IBus;
2525
use OCP\EventDispatcher\IEventDispatcher;
26+
use OCP\Files;
2627
use OCP\Files\FileInfo;
2728
use OCP\Files\Folder;
2829
use OCP\Files\IMimeTypeDetector;
@@ -32,6 +33,7 @@
3233
use OCP\Files\NotPermittedException;
3334
use OCP\Files\Search\ISearchBinaryOperator;
3435
use OCP\Files\Search\ISearchComparison;
36+
use OCP\Files\Storage\IWriteStreamStorage;
3537
use OCP\Files\StorageInvalidException;
3638
use OCP\Files\StorageNotAvailableException;
3739
use OCP\IURLGenerator;
@@ -416,12 +418,25 @@ private static function copyFileContents($view, $path1, $path2) {
416418

417419
try {
418420
// TODO add a proper way of overwriting a file while maintaining file ids
419-
if ($storage1->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage') || $storage2->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage')) {
421+
if ($storage1->instanceOfStorage(\OC\Files\ObjectStore\ObjectStoreStorage::class)
422+
|| $storage2->instanceOfStorage(\OC\Files\ObjectStore\ObjectStoreStorage::class)
423+
) {
420424
$source = $storage1->fopen($internalPath1, 'r');
421-
$target = $storage2->fopen($internalPath2, 'w');
422-
[, $result] = \OC_Helper::streamCopy($source, $target);
423-
fclose($source);
424-
fclose($target);
425+
$result = $source !== false;
426+
if ($result) {
427+
if ($storage2->instanceOfStorage(IWriteStreamStorage::class)) {
428+
/** @var IWriteStreamStorage $storage2 */
429+
$storage2->writeStream($internalPath2, $source);
430+
} else {
431+
$target = $storage2->fopen($internalPath2, 'w');
432+
$result = $target !== false;
433+
if ($target !== false) {
434+
[, $result] = Files::streamCopy($source, $target, true);
435+
fclose($target);
436+
}
437+
}
438+
fclose($source);
439+
}
425440

426441
if ($result !== false) {
427442
$storage1->unlink($internalPath1);

build/psalm-baseline.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,12 @@
675675
</ParamNameMismatch>
676676
</file>
677677
<file src="apps/dav/lib/Connector/Sabre/File.php">
678+
<DeprecatedClass>
679+
<code><![CDATA[Files::streamCopy($data, $target, true)]]></code>
680+
</DeprecatedClass>
681+
<DeprecatedMethod>
682+
<code><![CDATA[Files::streamCopy($data, $target, true)]]></code>
683+
</DeprecatedMethod>
678684
<LessSpecificReturnStatement>
679685
<code><![CDATA[$this->node]]></code>
680686
</LessSpecificReturnStatement>
@@ -1964,14 +1970,13 @@
19641970
</file>
19651971
<file src="apps/files_versions/lib/Storage.php">
19661972
<DeprecatedClass>
1973+
<code><![CDATA[Files::streamCopy($source, $target, true)]]></code>
19671974
<code><![CDATA[\OC_Util::setupFS($uid)]]></code>
19681975
</DeprecatedClass>
19691976
<DeprecatedMethod>
1977+
<code><![CDATA[Files::streamCopy($source, $target, true)]]></code>
19701978
<code><![CDATA[dispatch]]></code>
19711979
</DeprecatedMethod>
1972-
<RedundantCondition>
1973-
<code><![CDATA[$storage1->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage')]]></code>
1974-
</RedundantCondition>
19751980
</file>
19761981
<file src="apps/oauth2/lib/Controller/OauthApiController.php">
19771982
<NoInterfaceProperties>

lib/private/Files/Storage/Common.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use OC\Files\Storage\Wrapper\Encryption;
2020
use OC\Files\Storage\Wrapper\Jail;
2121
use OC\Files\Storage\Wrapper\Wrapper;
22+
use OCP\Files;
2223
use OCP\Files\Cache\ICache;
2324
use OCP\Files\Cache\IPropagator;
2425
use OCP\Files\Cache\IScanner;
@@ -205,7 +206,7 @@ public function copy(string $source, string $target): bool {
205206
} else {
206207
$sourceStream = $this->fopen($source, 'r');
207208
$targetStream = $this->fopen($target, 'w');
208-
[, $result] = \OC_Helper::streamCopy($sourceStream, $targetStream);
209+
[, $result] = Files::streamCopy($sourceStream, $targetStream, true);
209210
if (!$result) {
210211
Server::get(LoggerInterface::class)->warning("Failed to write data while copying $source to $target");
211212
}
@@ -734,7 +735,7 @@ public function writeStream(string $path, $stream, ?int $size = null): int {
734735
throw new GenericFileException("Failed to open $path for writing");
735736
}
736737
try {
737-
[$count, $result] = \OC_Helper::streamCopy($stream, $target);
738+
[$count, $result] = Files::streamCopy($stream, $target, true);
738739
if (!$result) {
739740
throw new GenericFileException('Failed to copy stream');
740741
}

lib/private/Files/Storage/LocalTempFileTrait.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
namespace OC\Files\Storage;
99

10+
use OCP\Files;
11+
1012
/**
1113
* Storage backend class for providing common filesystem operation methods
1214
* which are not storage-backend specific.
@@ -45,7 +47,7 @@ protected function toTmpFile(string $path): string|false { //no longer in the st
4547
}
4648
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension);
4749
$target = fopen($tmpFile, 'w');
48-
\OC_Helper::streamCopy($source, $target);
50+
Files::streamCopy($source, $target);
4951
fclose($target);
5052
return $tmpFile;
5153
}

lib/private/Files/Storage/Wrapper/Encryption.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use OCP\Encryption\IFile;
2222
use OCP\Encryption\IManager;
2323
use OCP\Encryption\Keys\IStorage;
24+
use OCP\Files;
2425
use OCP\Files\Cache\ICacheEntry;
2526
use OCP\Files\Mount\IMountPoint;
2627
use OCP\Files\Storage;
@@ -684,7 +685,7 @@ private function copyBetweenStorage(
684685
try {
685686
$source = $sourceStorage->fopen($sourceInternalPath, 'r');
686687
$target = $this->fopen($targetInternalPath, 'w');
687-
[, $result] = \OC_Helper::streamCopy($source, $target);
688+
[, $result] = Files::streamCopy($source, $target, true);
688689
} finally {
689690
if (is_resource($source)) {
690691
fclose($source);
@@ -893,7 +894,7 @@ protected function shouldEncrypt(string $path): bool {
893894
public function writeStream(string $path, $stream, ?int $size = null): int {
894895
// always fall back to fopen
895896
$target = $this->fopen($path, 'w');
896-
[$count, $result] = \OC_Helper::streamCopy($stream, $target);
897+
[$count, $result] = Files::streamCopy($stream, $target, true);
897898
fclose($stream);
898899
fclose($target);
899900

lib/private/Files/Storage/Wrapper/Jail.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use OC\Files\Cache\Wrapper\JailPropagator;
1212
use OC\Files\Cache\Wrapper\JailWatcher;
1313
use OC\Files\Filesystem;
14+
use OCP\Files;
1415
use OCP\Files\Cache\ICache;
1516
use OCP\Files\Cache\IPropagator;
1617
use OCP\Files\Cache\IWatcher;
@@ -253,7 +254,7 @@ public function writeStream(string $path, $stream, ?int $size = null): int {
253254
return $storage->writeStream($this->getUnjailedPath($path), $stream, $size);
254255
} else {
255256
$target = $this->fopen($path, 'w');
256-
[$count, $result] = \OC_Helper::streamCopy($stream, $target);
257+
$count = Files::streamCopy($stream, $target);
257258
fclose($stream);
258259
fclose($target);
259260
return $count;

lib/private/Files/Storage/Wrapper/Wrapper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use OC\Files\Storage\FailedStorage;
1111
use OC\Files\Storage\Storage;
12+
use OCP\Files;
1213
use OCP\Files\Cache\ICache;
1314
use OCP\Files\Cache\IPropagator;
1415
use OCP\Files\Cache\IScanner;
@@ -322,7 +323,7 @@ public function writeStream(string $path, $stream, ?int $size = null): int {
322323
return $storage->writeStream($path, $stream, $size);
323324
} else {
324325
$target = $this->fopen($path, 'w');
325-
[$count, $result] = \OC_Helper::streamCopy($stream, $target);
326+
$count = Files::streamCopy($stream, $target);
326327
fclose($stream);
327328
fclose($target);
328329
return $count;

lib/private/Files/View.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use OC\User\User;
1818
use OCA\Files_Sharing\SharedMount;
1919
use OCP\Constants;
20+
use OCP\Files;
2021
use OCP\Files\Cache\ICacheEntry;
2122
use OCP\Files\ConnectionLostException;
2223
use OCP\Files\EmptyFileNameException;
@@ -629,7 +630,7 @@ public function file_put_contents($path, $data) {
629630
[$storage, $internalPath] = $this->resolvePath($path);
630631
$target = $storage->fopen($internalPath, 'w');
631632
if ($target) {
632-
[, $result] = \OC_Helper::streamCopy($data, $target);
633+
[, $result] = Files::streamCopy($data, $target, true);
633634
fclose($target);
634635
fclose($data);
635636

lib/private/legacy/OC_Helper.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -123,31 +123,10 @@ public static function canExecute($name, $path = false) {
123123
* @param resource $source
124124
* @param resource $target
125125
* @return array the number of bytes copied and result
126+
* @deprecated 5.0.0 - Use \OCP\Files::streamCopy
126127
*/
127128
public static function streamCopy($source, $target) {
128-
if (!$source or !$target) {
129-
return [0, false];
130-
}
131-
$bufSize = 8192;
132-
$result = true;
133-
$count = 0;
134-
while (!feof($source)) {
135-
$buf = fread($source, $bufSize);
136-
$bytesWritten = fwrite($target, $buf);
137-
if ($bytesWritten !== false) {
138-
$count += $bytesWritten;
139-
}
140-
// note: strlen is expensive so only use it when necessary,
141-
// on the last block
142-
if ($bytesWritten === false
143-
|| ($bytesWritten < $bufSize && $bytesWritten < strlen($buf))
144-
) {
145-
// write error, could be disk full ?
146-
$result = false;
147-
break;
148-
}
149-
}
150-
return [$count, $result];
129+
return \OCP\Files::streamCopy($source, $target, true);
151130
}
152131

153132
/**

0 commit comments

Comments
 (0)