fix(files): Fix mtime preservation in Local and Common storages

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
This commit is contained in:
Côme Chilliet 2024-02-22 10:40:47 +01:00
parent afcbdf53dd
commit a9c3a148f6
No known key found for this signature in database
GPG key ID: A3E2F658B28C760A
8 changed files with 113 additions and 35 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
/*
* @copyright Copyright (c) 2024 Côme Chilliet <come.chilliet@nextcloud.com>
*
* @author Côme Chilliet <come.chilliet@nextcloud.com>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/
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;
}

View file

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