diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 9c3bf0f7e9a..61a6b38b164 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -413,6 +413,7 @@ return array( 'OCP\\Files\\Storage\\IChunkedFileWrite' => $baseDir . '/lib/public/Files/Storage/IChunkedFileWrite.php', 'OCP\\Files\\Storage\\IDisableEncryptionStorage' => $baseDir . '/lib/public/Files/Storage/IDisableEncryptionStorage.php', 'OCP\\Files\\Storage\\ILockingStorage' => $baseDir . '/lib/public/Files/Storage/ILockingStorage.php', + 'OCP\\Files\\Storage\\IMtimePreserving' => $baseDir . '/lib/public/Files/Storage/IMtimePreserving.php', 'OCP\\Files\\Storage\\INotifyStorage' => $baseDir . '/lib/public/Files/Storage/INotifyStorage.php', 'OCP\\Files\\Storage\\IReliableEtagStorage' => $baseDir . '/lib/public/Files/Storage/IReliableEtagStorage.php', 'OCP\\Files\\Storage\\IStorage' => $baseDir . '/lib/public/Files/Storage/IStorage.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index b222594af75..3ce8e8675e7 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -446,6 +446,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Files\\Storage\\IChunkedFileWrite' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/IChunkedFileWrite.php', 'OCP\\Files\\Storage\\IDisableEncryptionStorage' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/IDisableEncryptionStorage.php', 'OCP\\Files\\Storage\\ILockingStorage' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/ILockingStorage.php', + 'OCP\\Files\\Storage\\IMtimePreserving' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/IMtimePreserving.php', 'OCP\\Files\\Storage\\INotifyStorage' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/INotifyStorage.php', 'OCP\\Files\\Storage\\IReliableEtagStorage' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/IReliableEtagStorage.php', 'OCP\\Files\\Storage\\IStorage' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/IStorage.php', diff --git a/lib/private/Files/Storage/Common.php b/lib/private/Files/Storage/Common.php index 65c2580e61c..8be0ae56699 100644 --- a/lib/private/Files/Storage/Common.php +++ b/lib/private/Files/Storage/Common.php @@ -60,6 +60,7 @@ use OCP\Files\InvalidDirectoryException; use OCP\Files\InvalidPathException; use OCP\Files\ReservedWordException; use OCP\Files\Storage\ILockingStorage; +use OCP\Files\Storage\IMtimePreserving; use OCP\Files\Storage\IStorage; use OCP\Files\Storage\IWriteStreamStorage; use OCP\Lock\ILockingProvider; @@ -78,7 +79,7 @@ use Psr\Log\LoggerInterface; * Some \OC\Files\Storage\Common methods call functions which are first defined * in classes which extend it, e.g. $this->stat() . */ -abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { +abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, IMtimePreserving { use LocalTempFileTrait; protected $cache; @@ -222,17 +223,17 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { $this->remove($target); $this->removeCachedFile($source); - return $this->copy($source, $target) and $this->remove($source); + return $this->copy($source, $target, true) && $this->remove($source); } - public function copy($source, $target) { + public function copy($source, $target, bool $preserveMtime = false): bool { if ($this->is_dir($source)) { $this->remove($target); $dir = $this->opendir($source); $this->mkdir($target); while ($file = readdir($dir)) { if (!Filesystem::isIgnoredDir($file)) { - if (!$this->copy($source . '/' . $file, $target . '/' . $file)) { + if (!$this->copy($source . '/' . $file, $target . '/' . $file, $preserveMtime)) { closedir($dir); return false; } @@ -246,6 +247,9 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { [, $result] = \OC_Helper::streamCopy($sourceStream, $targetStream); if (!$result) { \OCP\Server::get(LoggerInterface::class)->warning("Failed to write data while copying $source to $target"); + } elseif ($preserveMtime) { + $mtime = $this->filemtime($source); + $this->touch($target, is_int($mtime) ? $mtime : null); } $this->removeCachedFile($target); return $result; @@ -615,19 +619,22 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { * @param bool $preserveMtime * @return bool */ - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) { + public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, bool $preserveMtime = false): bool { if ($sourceStorage === $this) { - return $this->copy($sourceInternalPath, $targetInternalPath); + return $this->copy($sourceInternalPath, $targetInternalPath, $preserveMtime); } if ($sourceStorage->is_dir($sourceInternalPath)) { $dh = $sourceStorage->opendir($sourceInternalPath); - $result = $this->mkdir($targetInternalPath); if (is_resource($dh)) { - $result = true; - while ($result and ($file = readdir($dh)) !== false) { + if (!$this->is_dir($targetInternalPath)) { + $result = $this->mkdir($targetInternalPath); + } else { + $result = true; + } + while ($result && ($file = readdir($dh)) !== false) { if (!Filesystem::isIgnoredDir($file)) { - $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file); + $result = $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file, $preserveMtime); } } } @@ -655,7 +662,7 @@ abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage { $this->getCache()->remove($targetInternalPath); } } - return (bool)$result; + return $result; } /** diff --git a/lib/private/Files/Storage/Local.php b/lib/private/Files/Storage/Local.php index 0fca853da59..560d59eb373 100644 --- a/lib/private/Files/Storage/Local.php +++ b/lib/private/Files/Storage/Local.php @@ -398,21 +398,27 @@ class Local extends \OC\Files\Storage\Common { return $this->copy($source, $target) && $this->unlink($source); } - public function copy($source, $target) { + public function copy($source, $target, bool $preserveMtime = false): bool { if ($this->is_dir($source)) { - return parent::copy($source, $target); + return parent::copy($source, $target, $preserveMtime); } else { $oldMask = umask($this->defUMask); if ($this->unlinkOnTruncate) { $this->unlink($target); } - $result = copy($this->getSourcePath($source), $this->getSourcePath($target)); + $sourceInternalPath = $this->getSourcePath($source); + $targetInternalPath = $this->getSourcePath($target); + $result = copy($sourceInternalPath, $targetInternalPath); umask($oldMask); if ($this->caseInsensitive) { if (mb_strtolower($target) === mb_strtolower($source) && !$this->file_exists($target)) { return false; } } + if ($result && $preserveMtime) { + $mtime = $this->filemtime($sourceInternalPath); + $this->touch($targetInternalPath, is_int($mtime) ? $mtime : null); + } return $result; } } @@ -590,13 +596,10 @@ class Local extends \OC\Files\Storage\Common { } /** - * @param IStorage $sourceStorage * @param string $sourceInternalPath * @param string $targetInternalPath - * @param bool $preserveMtime - * @return bool */ - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) { + public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, bool $preserveMtime = false): bool { if ($this->canDoCrossStorageMove($sourceStorage)) { if ($sourceStorage->instanceOfStorage(Jail::class)) { /** @@ -608,9 +611,9 @@ class Local extends \OC\Files\Storage\Common { * @var \OC\Files\Storage\Local $sourceStorage */ $rootStorage = new Local(['datadir' => '/']); - return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath)); + return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath), $preserveMtime); } else { - return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); + return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime); } } diff --git a/lib/private/Files/Storage/PolyFill/CopyDirectory.php b/lib/private/Files/Storage/PolyFill/CopyDirectory.php index ff05eecb134..170bcffea9e 100644 --- a/lib/private/Files/Storage/PolyFill/CopyDirectory.php +++ b/lib/private/Files/Storage/PolyFill/CopyDirectory.php @@ -64,35 +64,37 @@ trait CopyDirectory { */ abstract public function mkdir($path); - public function copy($source, $target) { + /** + * Copy file or folder + * + * @param string $source + * @param string $target + */ + public function copy($source, $target, bool $preserveMtime = false): bool { if ($this->is_dir($source)) { if ($this->file_exists($target)) { $this->unlink($target); } $this->mkdir($target); - return $this->copyRecursive($source, $target); + return $this->copyRecursive($source, $target, $preserveMtime); } else { - return parent::copy($source, $target); + return parent::copy($source, $target, $preserveMtime); } } /** * For adapters that don't support copying folders natively - * - * @param $source - * @param $target - * @return bool */ - protected function copyRecursive($source, $target) { + protected function copyRecursive(string $source, string $target, bool $preserveMtime = false): bool { $dh = $this->opendir($source); $result = true; while ($file = readdir($dh)) { if (!\OC\Files\Filesystem::isIgnoredDir($file)) { if ($this->is_dir($source . '/' . $file)) { $this->mkdir($target . '/' . $file); - $result = $this->copyRecursive($source . '/' . $file, $target . '/' . $file); + $result = $this->copyRecursive($source . '/' . $file, $target . '/' . $file, $preserveMtime); } else { - $result = parent::copy($source . '/' . $file, $target . '/' . $file); + $result = parent::copy($source . '/' . $file, $target . '/' . $file, $preserveMtime); } if (!$result) { break; diff --git a/lib/private/Files/Storage/Wrapper/Encryption.php b/lib/private/Files/Storage/Wrapper/Encryption.php index 7ce4338256f..39bf9b4e06d 100644 --- a/lib/private/Files/Storage/Wrapper/Encryption.php +++ b/lib/private/Files/Storage/Wrapper/Encryption.php @@ -824,9 +824,9 @@ class Encryption extends Wrapper { $result = true; } if (is_resource($dh)) { - while ($result and ($file = readdir($dh)) !== false) { + while ($result && ($file = readdir($dh)) !== false) { if (!Filesystem::isIgnoredDir($file)) { - $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file, false, $isRename); + $result = $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file, $preserveMtime, $isRename); } } } diff --git a/lib/public/Files/Storage/IMtimePreserving.php b/lib/public/Files/Storage/IMtimePreserving.php new file mode 100644 index 00000000000..3b64bd6afb5 --- /dev/null +++ b/lib/public/Files/Storage/IMtimePreserving.php @@ -0,0 +1,64 @@ + + * + * @author Côme Chilliet + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCP\Files\Storage; + +/** + * @since 29.0.0 + */ +interface IMtimePreserving extends IStorage { + + /** + * see https://www.php.net/manual/en/function.copy.php + * + * @param string $source + * @param string $target + * @return bool + * @since 9.0.0 + * @deprecated 29.0.0 see copyWithMtime + */ + public function copy($source, $target, bool $preserveMtime = false): bool; + + /** + * @param string $sourceInternalPath + * @param string $targetInternalPath + * @since 29.0.0 + */ + public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, bool $preserveMtime = false): bool; + + /** + * see https://www.php.net/manual/en/function.copy.php + * + * @since 29.0.0 + */ + // public function copyWithMtime(string $source, string $target, bool $preserveMtime = false): bool; + + /** + * @since 29.0.0 + */ + // public function copyFromStorageWithMtime(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath, bool $preserveMtime = false): bool; +} diff --git a/tests/lib/Files/Storage/CopyDirectoryTest.php b/tests/lib/Files/Storage/CopyDirectoryTest.php index 25bdb016ead..fb3d411e2ff 100644 --- a/tests/lib/Files/Storage/CopyDirectoryTest.php +++ b/tests/lib/Files/Storage/CopyDirectoryTest.php @@ -24,11 +24,11 @@ namespace Test\Files\Storage; use OC\Files\Storage\Temporary; class StorageNoRecursiveCopy extends Temporary { - public function copy($path1, $path2) { - if ($this->is_dir($path1)) { + public function copy($source, $target, bool $preserveMtime = false): bool { + if ($this->is_dir($source)) { return false; } - return copy($this->getSourcePath($path1), $this->getSourcePath($path2)); + return copy($this->getSourcePath($source), $this->getSourcePath($target)); } }