2015-06-14 09:02:12 -04:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2015-06-14 09:02:12 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-06-14 09:02:12 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Files\Storage\PolyFill;
|
|
|
|
|
|
|
|
|
|
trait CopyDirectory {
|
2015-06-15 09:41:52 -04:00
|
|
|
/**
|
|
|
|
|
* Check if a path is a directory
|
|
|
|
|
*/
|
2024-10-01 10:12:30 -04:00
|
|
|
abstract public function is_dir(string $path): bool;
|
2015-06-14 09:02:12 -04:00
|
|
|
|
2015-06-15 09:41:52 -04:00
|
|
|
/**
|
|
|
|
|
* Check if a file or folder exists
|
|
|
|
|
*/
|
2024-10-01 10:12:30 -04:00
|
|
|
abstract public function file_exists(string $path): bool;
|
2015-06-14 09:02:12 -04:00
|
|
|
|
2015-06-15 09:41:52 -04:00
|
|
|
/**
|
|
|
|
|
* Delete a file or folder
|
|
|
|
|
*/
|
2024-10-01 10:12:30 -04:00
|
|
|
abstract public function unlink(string $path): bool;
|
2015-06-14 09:02:12 -04:00
|
|
|
|
2015-06-15 09:41:52 -04:00
|
|
|
/**
|
|
|
|
|
* Open a directory handle for a folder
|
|
|
|
|
*
|
2024-09-19 12:19:34 -04:00
|
|
|
* @return resource|false
|
2015-06-15 09:41:52 -04:00
|
|
|
*/
|
2024-10-01 10:12:30 -04:00
|
|
|
abstract public function opendir(string $path);
|
2015-06-14 09:02:12 -04:00
|
|
|
|
2015-06-15 09:41:52 -04:00
|
|
|
/**
|
|
|
|
|
* Create a new folder
|
|
|
|
|
*/
|
2024-10-01 10:12:30 -04:00
|
|
|
abstract public function mkdir(string $path): bool;
|
2015-06-14 09:02:12 -04:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function copy(string $source, string $target): bool {
|
2022-10-18 06:49:34 -04:00
|
|
|
if ($this->is_dir($source)) {
|
|
|
|
|
if ($this->file_exists($target)) {
|
|
|
|
|
$this->unlink($target);
|
2015-06-14 09:02:12 -04:00
|
|
|
}
|
2022-10-18 06:49:34 -04:00
|
|
|
$this->mkdir($target);
|
|
|
|
|
return $this->copyRecursive($source, $target);
|
2015-06-14 09:02:12 -04:00
|
|
|
} else {
|
2022-10-18 06:49:34 -04:00
|
|
|
return parent::copy($source, $target);
|
2015-06-14 09:02:12 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-04-07 13:51:27 -04:00
|
|
|
* For adapters that don't support copying folders natively
|
2015-06-14 09:02:12 -04:00
|
|
|
*/
|
2024-10-01 10:12:30 -04:00
|
|
|
protected function copyRecursive(string $source, string $target): bool {
|
2015-06-14 09:02:12 -04:00
|
|
|
$dh = $this->opendir($source);
|
|
|
|
|
$result = true;
|
2023-06-03 08:57:38 -04:00
|
|
|
while (($file = readdir($dh)) !== false) {
|
2015-09-21 08:09:28 -04:00
|
|
|
if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
|
2015-06-14 09:02:12 -04:00
|
|
|
if ($this->is_dir($source . '/' . $file)) {
|
|
|
|
|
$this->mkdir($target . '/' . $file);
|
|
|
|
|
$result = $this->copyRecursive($source . '/' . $file, $target . '/' . $file);
|
|
|
|
|
} else {
|
|
|
|
|
$result = parent::copy($source . '/' . $file, $target . '/' . $file);
|
|
|
|
|
}
|
|
|
|
|
if (!$result) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
}
|