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
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2015-06-14 09:02:12 -04:00
|
|
|
abstract public function is_dir($path);
|
|
|
|
|
|
2015-06-15 09:41:52 -04:00
|
|
|
/**
|
|
|
|
|
* Check if a file or folder exists
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2015-06-14 09:02:12 -04:00
|
|
|
abstract public function file_exists($path);
|
|
|
|
|
|
2015-06-15 09:41:52 -04:00
|
|
|
/**
|
|
|
|
|
* Delete a file or folder
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2015-06-14 09:02:12 -04:00
|
|
|
abstract public function unlink($path);
|
|
|
|
|
|
2015-06-15 09:41:52 -04:00
|
|
|
/**
|
|
|
|
|
* Open a directory handle for a folder
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return resource | bool
|
|
|
|
|
*/
|
2015-06-14 09:02:12 -04:00
|
|
|
abstract public function opendir($path);
|
|
|
|
|
|
2015-06-15 09:41:52 -04:00
|
|
|
/**
|
|
|
|
|
* Create a new folder
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2015-06-14 09:02:12 -04:00
|
|
|
abstract public function mkdir($path);
|
|
|
|
|
|
2022-10-18 06:49:34 -04:00
|
|
|
public function copy($source, $target) {
|
|
|
|
|
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
|
|
|
*
|
|
|
|
|
* @param $source
|
|
|
|
|
* @param $target
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function copyRecursive($source, $target) {
|
|
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
}
|