2012-02-27 06:04:04 -05:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2012-02-27 06:04:04 -05: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
|
2012-09-07 12:30:48 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Files\Storage;
|
2014-05-29 07:45:50 -04:00
|
|
|
|
2015-02-18 11:44:13 -05:00
|
|
|
use OC\Files\Cache\Cache;
|
2023-08-15 12:41:53 -04:00
|
|
|
use OC\Files\Cache\CacheDependencies;
|
2015-11-10 10:14:08 -05:00
|
|
|
use OC\Files\Cache\Propagator;
|
2015-02-18 11:44:13 -05:00
|
|
|
use OC\Files\Cache\Scanner;
|
2015-11-25 07:53:31 -05:00
|
|
|
use OC\Files\Cache\Updater;
|
2014-05-22 07:45:55 -04:00
|
|
|
use OC\Files\Cache\Watcher;
|
2024-08-27 08:07:44 -04:00
|
|
|
use OC\Files\FilenameValidator;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OC\Files\Filesystem;
|
2024-09-25 11:52:42 -04:00
|
|
|
use OC\Files\ObjectStore\ObjectStoreStorage;
|
2024-11-13 12:47:13 -05:00
|
|
|
use OC\Files\Storage\Wrapper\Encryption;
|
2019-09-25 13:17:06 -04:00
|
|
|
use OC\Files\Storage\Wrapper\Jail;
|
|
|
|
|
use OC\Files\Storage\Wrapper\Wrapper;
|
2025-05-15 18:07:33 -04:00
|
|
|
use OCP\Files;
|
2024-09-19 12:19:34 -04:00
|
|
|
use OCP\Files\Cache\ICache;
|
|
|
|
|
use OCP\Files\Cache\IPropagator;
|
|
|
|
|
use OCP\Files\Cache\IScanner;
|
|
|
|
|
use OCP\Files\Cache\IUpdater;
|
|
|
|
|
use OCP\Files\Cache\IWatcher;
|
2022-03-17 11:05:52 -04:00
|
|
|
use OCP\Files\ForbiddenException;
|
2020-07-01 09:37:47 -04:00
|
|
|
use OCP\Files\GenericFileException;
|
2024-07-15 10:28:43 -04:00
|
|
|
use OCP\Files\IFilenameValidator;
|
2015-02-18 11:44:13 -05:00
|
|
|
use OCP\Files\InvalidPathException;
|
2024-09-16 15:48:18 -04:00
|
|
|
use OCP\Files\Storage\IConstructableStorage;
|
2016-01-04 08:07:03 -05:00
|
|
|
use OCP\Files\Storage\ILockingStorage;
|
2017-07-19 13:44:10 -04:00
|
|
|
use OCP\Files\Storage\IStorage;
|
2018-10-26 13:15:23 -04:00
|
|
|
use OCP\Files\Storage\IWriteStreamStorage;
|
2024-02-23 10:07:43 -05:00
|
|
|
use OCP\Files\StorageNotAvailableException;
|
2024-11-21 02:38:50 -05:00
|
|
|
use OCP\IConfig;
|
2015-05-04 08:21:34 -04:00
|
|
|
use OCP\Lock\ILockingProvider;
|
2017-02-13 09:03:46 -05:00
|
|
|
use OCP\Lock\LockedException;
|
2024-02-12 10:38:08 -05:00
|
|
|
use OCP\Server;
|
2022-03-31 04:57:10 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2012-02-27 06:04:04 -05:00
|
|
|
|
2012-06-21 13:07:21 -04:00
|
|
|
/**
|
2012-08-29 02:38:33 -04:00
|
|
|
* Storage backend class for providing common filesystem operation methods
|
2012-06-21 13:07:21 -04:00
|
|
|
* which are not storage-backend specific.
|
|
|
|
|
*
|
2012-09-07 12:30:48 -04:00
|
|
|
* \OC\Files\Storage\Common is never used directly; it is extended by all other
|
2012-08-29 02:38:33 -04:00
|
|
|
* storage backends, where its methods may be overridden, and additional
|
2012-06-21 13:07:21 -04:00
|
|
|
* (backend-specific) methods are defined.
|
|
|
|
|
*
|
2012-09-07 12:30:48 -04:00
|
|
|
* Some \OC\Files\Storage\Common methods call functions which are first defined
|
2012-06-21 13:07:21 -04:00
|
|
|
* in classes which extend it, e.g. $this->stat() .
|
|
|
|
|
*/
|
2024-09-16 15:48:18 -04:00
|
|
|
abstract class Common implements Storage, ILockingStorage, IWriteStreamStorage, IConstructableStorage {
|
2015-04-02 08:44:25 -04:00
|
|
|
use LocalTempFileTrait;
|
|
|
|
|
|
2024-09-15 11:14:37 -04:00
|
|
|
protected ?Cache $cache = null;
|
|
|
|
|
protected ?Scanner $scanner = null;
|
|
|
|
|
protected ?Watcher $watcher = null;
|
|
|
|
|
protected ?Propagator $propagator = null;
|
2013-11-08 06:57:28 -05:00
|
|
|
protected $storageCache;
|
2024-09-15 11:14:37 -04:00
|
|
|
protected ?Updater $updater = null;
|
2012-02-27 06:04:04 -05:00
|
|
|
|
2024-09-15 11:14:37 -04:00
|
|
|
protected array $mountOptions = [];
|
2016-02-18 04:12:23 -05:00
|
|
|
protected $owner = null;
|
2015-03-05 11:22:48 -05:00
|
|
|
|
2024-07-15 10:28:43 -04:00
|
|
|
private ?bool $shouldLogLocks = null;
|
|
|
|
|
private ?LoggerInterface $logger = null;
|
|
|
|
|
private ?IFilenameValidator $filenameValidator = null;
|
2017-02-13 09:03:46 -05:00
|
|
|
|
2024-10-08 09:13:16 -04:00
|
|
|
public function __construct(array $parameters) {
|
2013-02-25 20:51:57 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
protected function remove(string $path): bool {
|
2024-11-15 13:50:16 -05:00
|
|
|
if ($this->file_exists($path)) {
|
|
|
|
|
if ($this->is_dir($path)) {
|
|
|
|
|
return $this->rmdir($path);
|
|
|
|
|
} elseif ($this->is_file($path)) {
|
|
|
|
|
return $this->unlink($path);
|
|
|
|
|
}
|
2014-05-23 06:46:12 -04:00
|
|
|
}
|
2024-11-15 13:50:16 -05:00
|
|
|
return false;
|
2014-05-23 06:46:12 -04:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function is_dir(string $path): bool {
|
2014-10-01 07:14:08 -04:00
|
|
|
return $this->filetype($path) === 'dir';
|
2012-02-27 06:04:04 -05:00
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function is_file(string $path): bool {
|
2014-10-01 07:14:08 -04:00
|
|
|
return $this->filetype($path) === 'file';
|
2012-02-27 06:04:04 -05:00
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function filesize(string $path): int|float|false {
|
2013-02-25 20:51:57 -05:00
|
|
|
if ($this->is_dir($path)) {
|
|
|
|
|
return 0; //by definition
|
|
|
|
|
} else {
|
2012-02-27 06:04:04 -05:00
|
|
|
$stat = $this->stat($path);
|
2024-11-21 02:38:50 -05:00
|
|
|
return isset($stat['size']) ? $stat['size'] : 0;
|
2012-02-27 06:04:04 -05:00
|
|
|
}
|
|
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function isReadable(string $path): bool {
|
2013-11-22 09:24:23 -05:00
|
|
|
// at least check whether it exists
|
|
|
|
|
// subclasses might want to implement this more thoroughly
|
|
|
|
|
return $this->file_exists($path);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function isUpdatable(string $path): bool {
|
2013-11-22 09:24:23 -05:00
|
|
|
// at least check whether it exists
|
|
|
|
|
// subclasses might want to implement this more thoroughly
|
|
|
|
|
// a non-existing file/folder isn't updatable
|
|
|
|
|
return $this->file_exists($path);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function isCreatable(string $path): bool {
|
2012-12-26 15:36:50 -05:00
|
|
|
if ($this->is_dir($path) && $this->isUpdatable($path)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2012-07-24 17:42:07 -04:00
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function isDeletable(string $path): bool {
|
2014-08-25 08:28:35 -04:00
|
|
|
if ($path === '' || $path === '/') {
|
2021-04-27 09:43:34 -04:00
|
|
|
return $this->isUpdatable($path);
|
2014-08-25 08:06:27 -04:00
|
|
|
}
|
|
|
|
|
$parent = dirname($path);
|
2014-09-17 05:36:08 -04:00
|
|
|
return $this->isUpdatable($parent) && $this->isUpdatable($path);
|
2012-07-24 17:42:07 -04:00
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function isSharable(string $path): bool {
|
2012-07-24 17:42:07 -04:00
|
|
|
return $this->isReadable($path);
|
|
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getPermissions(string $path): int {
|
2012-09-12 17:25:57 -04:00
|
|
|
$permissions = 0;
|
2013-02-25 20:51:57 -05:00
|
|
|
if ($this->isCreatable($path)) {
|
2014-11-25 10:28:41 -05:00
|
|
|
$permissions |= \OCP\Constants::PERMISSION_CREATE;
|
2012-09-12 17:25:57 -04:00
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
if ($this->isReadable($path)) {
|
2014-11-25 10:28:41 -05:00
|
|
|
$permissions |= \OCP\Constants::PERMISSION_READ;
|
2012-09-12 17:25:57 -04:00
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
if ($this->isUpdatable($path)) {
|
2014-11-25 10:28:41 -05:00
|
|
|
$permissions |= \OCP\Constants::PERMISSION_UPDATE;
|
2012-09-12 17:25:57 -04:00
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
if ($this->isDeletable($path)) {
|
2014-11-25 10:28:41 -05:00
|
|
|
$permissions |= \OCP\Constants::PERMISSION_DELETE;
|
2012-09-12 17:25:57 -04:00
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
if ($this->isSharable($path)) {
|
2014-11-25 10:28:41 -05:00
|
|
|
$permissions |= \OCP\Constants::PERMISSION_SHARE;
|
2012-09-12 17:25:57 -04:00
|
|
|
}
|
|
|
|
|
return $permissions;
|
2012-02-27 06:04:04 -05:00
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function filemtime(string $path): int|false {
|
2012-02-27 06:04:04 -05:00
|
|
|
$stat = $this->stat($path);
|
2014-12-05 08:54:43 -05:00
|
|
|
if (isset($stat['mtime']) && $stat['mtime'] > 0) {
|
2013-02-25 20:53:02 -05:00
|
|
|
return $stat['mtime'];
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2012-02-27 06:04:04 -05:00
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function file_get_contents(string $path): string|false {
|
2024-08-23 09:10:27 -04:00
|
|
|
$handle = $this->fopen($path, 'r');
|
2013-02-25 20:51:57 -05:00
|
|
|
if (!$handle) {
|
2012-03-02 18:55:17 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
2014-03-31 12:36:52 -04:00
|
|
|
$data = stream_get_contents($handle);
|
2014-03-31 12:37:52 -04:00
|
|
|
fclose($handle);
|
2014-03-31 12:36:52 -04:00
|
|
|
return $data;
|
2012-02-27 06:04:04 -05:00
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function file_put_contents(string $path, mixed $data): int|float|false {
|
2024-08-23 09:10:27 -04:00
|
|
|
$handle = $this->fopen($path, 'w');
|
2023-04-06 05:52:22 -04:00
|
|
|
if (!$handle) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-03-04 09:44:58 -05:00
|
|
|
$this->removeCachedFile($path);
|
2014-03-31 12:37:52 -04:00
|
|
|
$count = fwrite($handle, $data);
|
|
|
|
|
fclose($handle);
|
|
|
|
|
return $count;
|
2012-02-27 06:04:04 -05:00
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function rename(string $source, string $target): bool {
|
2022-10-18 06:49:34 -04:00
|
|
|
$this->remove($target);
|
2013-07-01 12:11:05 -04:00
|
|
|
|
2022-10-18 06:49:34 -04:00
|
|
|
$this->removeCachedFile($source);
|
2025-09-27 16:56:38 -04:00
|
|
|
return $this->copy($source, $target) && $this->remove($source);
|
2012-02-27 06:04:04 -05:00
|
|
|
}
|
2013-02-25 20:51:57 -05: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)) {
|
|
|
|
|
$this->remove($target);
|
|
|
|
|
$dir = $this->opendir($source);
|
|
|
|
|
$this->mkdir($target);
|
2023-06-03 08:57:38 -04:00
|
|
|
while (($file = readdir($dir)) !== false) {
|
2014-05-23 06:46:12 -04:00
|
|
|
if (!Filesystem::isIgnoredDir($file)) {
|
2022-10-18 06:49:34 -04:00
|
|
|
if (!$this->copy($source . '/' . $file, $target . '/' . $file)) {
|
2022-03-07 04:19:58 -05:00
|
|
|
closedir($dir);
|
2013-07-01 12:11:05 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
closedir($dir);
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
2022-10-18 06:49:34 -04:00
|
|
|
$sourceStream = $this->fopen($source, 'r');
|
|
|
|
|
$targetStream = $this->fopen($target, 'w');
|
2025-05-15 18:07:33 -04:00
|
|
|
[, $result] = Files::streamCopy($sourceStream, $targetStream, true);
|
2017-12-18 07:59:58 -05:00
|
|
|
if (!$result) {
|
2024-11-21 02:38:50 -05:00
|
|
|
Server::get(LoggerInterface::class)->warning("Failed to write data while copying $source to $target");
|
2017-12-18 07:59:58 -05:00
|
|
|
}
|
2022-10-18 06:49:34 -04:00
|
|
|
$this->removeCachedFile($target);
|
2013-07-01 12:11:05 -04:00
|
|
|
return $result;
|
|
|
|
|
}
|
2012-02-27 06:04:04 -05:00
|
|
|
}
|
2012-06-21 13:07:21 -04:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getMimeType(string $path): string|false {
|
2013-02-25 20:51:57 -05:00
|
|
|
if ($this->is_dir($path)) {
|
2012-02-27 06:04:04 -05:00
|
|
|
return 'httpd/unix-directory';
|
2013-11-20 09:25:29 -05:00
|
|
|
} elseif ($this->file_exists($path)) {
|
2015-12-17 11:07:48 -05:00
|
|
|
return \OC::$server->getMimeTypeDetector()->detectPath($path);
|
2013-02-25 20:51:57 -05:00
|
|
|
} else {
|
2013-11-20 09:25:29 -05:00
|
|
|
return false;
|
2012-03-03 12:24:10 -05:00
|
|
|
}
|
2012-02-27 06:04:04 -05:00
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function hash(string $type, string $path, bool $raw = false): string|false {
|
2014-03-20 11:45:24 -04:00
|
|
|
$fh = $this->fopen($path, 'rb');
|
2024-11-21 02:38:50 -05:00
|
|
|
if (!$fh) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-03-20 10:35:01 -04:00
|
|
|
$ctx = hash_init($type);
|
|
|
|
|
hash_update_stream($ctx, $fh);
|
|
|
|
|
fclose($fh);
|
|
|
|
|
return hash_final($ctx, $raw);
|
2012-02-27 06:04:04 -05:00
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getLocalFile(string $path): string|false {
|
2014-03-04 09:44:58 -05:00
|
|
|
return $this->getCachedFile($path);
|
2012-02-27 06:04:04 -05:00
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
private function addLocalFolder(string $path, string $target): void {
|
2013-09-04 07:06:04 -04:00
|
|
|
$dh = $this->opendir($path);
|
2013-11-20 09:25:29 -05:00
|
|
|
if (is_resource($dh)) {
|
2013-08-19 06:04:53 -04:00
|
|
|
while (($file = readdir($dh)) !== false) {
|
2024-11-21 02:38:50 -05:00
|
|
|
if (!Filesystem::isIgnoredDir($file)) {
|
2013-02-25 20:51:57 -05:00
|
|
|
if ($this->is_dir($path . '/' . $file)) {
|
|
|
|
|
mkdir($target . '/' . $file);
|
|
|
|
|
$this->addLocalFolder($path . '/' . $file, $target . '/' . $file);
|
|
|
|
|
} else {
|
|
|
|
|
$tmp = $this->toTmpFile($path . '/' . $file);
|
|
|
|
|
rename($tmp, $target . '/' . $file);
|
2012-08-18 20:30:33 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-03-02 12:42:04 -05:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
protected function searchInDir(string $query, string $dir = ''): array {
|
2020-03-26 04:30:18 -04:00
|
|
|
$files = [];
|
2013-02-25 20:51:57 -05:00
|
|
|
$dh = $this->opendir($dir);
|
2013-09-04 07:06:04 -04:00
|
|
|
if (is_resource($dh)) {
|
2013-08-19 06:04:53 -04:00
|
|
|
while (($item = readdir($dh)) !== false) {
|
2024-11-21 02:38:50 -05:00
|
|
|
if (Filesystem::isIgnoredDir($item)) {
|
2020-04-10 08:19:56 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
if (strstr(strtolower($item), strtolower($query)) !== false) {
|
|
|
|
|
$files[] = $dir . '/' . $item;
|
2012-03-02 12:42:04 -05:00
|
|
|
}
|
2013-02-25 20:51:57 -05:00
|
|
|
if ($this->is_dir($dir . '/' . $item)) {
|
|
|
|
|
$files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item));
|
2012-03-02 12:42:04 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-11 06:58:22 -05:00
|
|
|
closedir($dh);
|
2012-03-02 12:42:04 -05:00
|
|
|
return $files;
|
|
|
|
|
}
|
2012-06-15 10:43:24 -04:00
|
|
|
|
|
|
|
|
/**
|
2024-09-19 12:19:34 -04:00
|
|
|
* @inheritDoc
|
2021-07-29 09:56:30 -04:00
|
|
|
* Check if a file or folder has been updated since $time
|
2013-02-25 20:51:57 -05:00
|
|
|
*
|
2014-06-27 06:14:31 -04:00
|
|
|
* The method is only used to check if the cache needs to be updated. Storage backends that don't support checking
|
|
|
|
|
* the mtime should always return false here. As a result storage implementations that always return false expect
|
|
|
|
|
* exclusive access to the backend and will not pick up files that have been added in a way that circumvents
|
2021-07-29 09:56:30 -04:00
|
|
|
* Nextcloud filesystem.
|
2012-06-15 10:43:24 -04:00
|
|
|
*/
|
2024-10-01 10:12:30 -04:00
|
|
|
public function hasUpdated(string $path, int $time): bool {
|
2013-02-25 20:51:57 -05:00
|
|
|
return $this->filemtime($path) > $time;
|
2012-06-15 10:43:24 -04:00
|
|
|
}
|
2012-07-06 06:22:21 -04:00
|
|
|
|
2023-08-15 12:41:53 -04:00
|
|
|
protected function getCacheDependencies(): CacheDependencies {
|
|
|
|
|
static $dependencies = null;
|
|
|
|
|
if (!$dependencies) {
|
2024-02-12 10:38:08 -05:00
|
|
|
$dependencies = Server::get(CacheDependencies::class);
|
2023-08-15 12:41:53 -04:00
|
|
|
}
|
|
|
|
|
return $dependencies;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getCache(string $path = '', ?IStorage $storage = null): ICache {
|
2014-06-12 11:23:34 -04:00
|
|
|
if (!$storage) {
|
|
|
|
|
$storage = $this;
|
|
|
|
|
}
|
2024-09-19 12:19:34 -04:00
|
|
|
/** @var self $storage */
|
2016-04-13 09:23:37 -04:00
|
|
|
if (!isset($storage->cache)) {
|
2023-08-15 12:41:53 -04:00
|
|
|
$storage->cache = new Cache($storage, $this->getCacheDependencies());
|
2013-04-05 11:20:08 -04:00
|
|
|
}
|
2016-04-13 09:23:37 -04:00
|
|
|
return $storage->cache;
|
2012-10-20 18:31:32 -04:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getScanner(string $path = '', ?IStorage $storage = null): IScanner {
|
2014-06-12 11:23:34 -04:00
|
|
|
if (!$storage) {
|
|
|
|
|
$storage = $this;
|
|
|
|
|
}
|
2024-09-16 12:07:03 -04:00
|
|
|
if (!$storage->instanceOfStorage(self::class)) {
|
|
|
|
|
throw new \InvalidArgumentException('Storage is not of the correct class');
|
|
|
|
|
}
|
2016-04-13 09:23:37 -04:00
|
|
|
if (!isset($storage->scanner)) {
|
|
|
|
|
$storage->scanner = new Scanner($storage);
|
2013-04-05 11:20:08 -04:00
|
|
|
}
|
2016-04-13 09:23:37 -04:00
|
|
|
return $storage->scanner;
|
2012-10-20 18:31:32 -04:00
|
|
|
}
|
2012-10-23 10:16:46 -04:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getWatcher(string $path = '', ?IStorage $storage = null): IWatcher {
|
2014-06-12 11:23:34 -04:00
|
|
|
if (!$storage) {
|
|
|
|
|
$storage = $this;
|
|
|
|
|
}
|
2013-04-05 11:20:08 -04:00
|
|
|
if (!isset($this->watcher)) {
|
2015-02-18 11:44:13 -05:00
|
|
|
$this->watcher = new Watcher($storage);
|
2024-11-21 02:38:50 -05:00
|
|
|
$globalPolicy = Server::get(IConfig::class)->getSystemValueInt('filesystem_check_changes', Watcher::CHECK_NEVER);
|
2015-03-26 14:24:37 -04:00
|
|
|
$this->watcher->setPolicy((int)$this->getMountOption('filesystem_check_changes', $globalPolicy));
|
2013-04-05 11:20:08 -04:00
|
|
|
}
|
|
|
|
|
return $this->watcher;
|
2013-01-01 12:04:29 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getPropagator(?IStorage $storage = null): IPropagator {
|
2015-11-10 10:14:08 -05:00
|
|
|
if (!$storage) {
|
|
|
|
|
$storage = $this;
|
|
|
|
|
}
|
2024-09-16 12:07:03 -04:00
|
|
|
if (!$storage->instanceOfStorage(self::class)) {
|
|
|
|
|
throw new \InvalidArgumentException('Storage is not of the correct class');
|
|
|
|
|
}
|
2024-09-16 07:04:15 -04:00
|
|
|
/** @var self $storage */
|
2016-04-13 09:23:37 -04:00
|
|
|
if (!isset($storage->propagator)) {
|
2024-11-21 02:38:50 -05:00
|
|
|
$config = Server::get(IConfig::class);
|
|
|
|
|
$storage->propagator = new Propagator($storage, \OC::$server->getDatabaseConnection(), ['appdata_' . $config->getSystemValueString('instanceid')]);
|
2015-11-10 10:14:08 -05:00
|
|
|
}
|
2016-04-13 09:23:37 -04:00
|
|
|
return $storage->propagator;
|
2015-11-10 10:14:08 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getUpdater(?IStorage $storage = null): IUpdater {
|
2015-11-25 07:53:31 -05:00
|
|
|
if (!$storage) {
|
|
|
|
|
$storage = $this;
|
|
|
|
|
}
|
2024-09-16 12:07:03 -04:00
|
|
|
if (!$storage->instanceOfStorage(self::class)) {
|
|
|
|
|
throw new \InvalidArgumentException('Storage is not of the correct class');
|
|
|
|
|
}
|
2024-09-19 12:19:34 -04:00
|
|
|
/** @var self $storage */
|
2016-04-13 09:23:37 -04:00
|
|
|
if (!isset($storage->updater)) {
|
|
|
|
|
$storage->updater = new Updater($storage);
|
2015-11-25 07:53:31 -05:00
|
|
|
}
|
2016-04-13 09:23:37 -04:00
|
|
|
return $storage->updater;
|
2015-11-25 07:53:31 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getStorageCache(?IStorage $storage = null): \OC\Files\Cache\Storage {
|
2024-09-19 12:19:34 -04:00
|
|
|
/** @var Cache $cache */
|
2024-10-01 10:12:30 -04:00
|
|
|
$cache = $this->getCache(storage: $storage);
|
2024-09-19 12:19:34 -04:00
|
|
|
return $cache->getStorageCache();
|
2013-04-25 18:00:18 -04:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getOwner(string $path): string|false {
|
2016-02-18 04:12:23 -05:00
|
|
|
if ($this->owner === null) {
|
|
|
|
|
$this->owner = \OC_User::getUser();
|
2016-02-17 13:14:46 -05:00
|
|
|
}
|
|
|
|
|
|
2016-02-18 04:12:23 -05:00
|
|
|
return $this->owner;
|
2012-07-06 06:22:21 -04:00
|
|
|
}
|
2012-11-08 11:47:00 -05:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getETag(string $path): string|false {
|
2015-10-14 08:39:25 -04:00
|
|
|
return uniqid();
|
2012-07-06 06:22:21 -04:00
|
|
|
}
|
2013-02-22 11:21:57 -05:00
|
|
|
|
2013-01-26 13:44:09 -05:00
|
|
|
/**
|
|
|
|
|
* clean a path, i.e. remove all redundant '.' and '..'
|
|
|
|
|
* making sure that it can't point to higher than '/'
|
2013-02-25 20:51:57 -05:00
|
|
|
*
|
2014-05-11 16:51:30 -04:00
|
|
|
* @param string $path The path to clean
|
2013-01-26 13:44:09 -05:00
|
|
|
* @return string cleaned path
|
|
|
|
|
*/
|
2024-10-01 10:12:30 -04:00
|
|
|
public function cleanPath(string $path): string {
|
2024-11-21 02:38:50 -05:00
|
|
|
if (strlen($path) == 0 || $path[0] != '/') {
|
2013-01-26 13:44:09 -05:00
|
|
|
$path = '/' . $path;
|
|
|
|
|
}
|
2013-02-22 11:21:57 -05:00
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$output = [];
|
2013-01-27 10:07:21 -05:00
|
|
|
foreach (explode('/', $path) as $chunk) {
|
2013-01-26 13:44:09 -05:00
|
|
|
if ($chunk == '..') {
|
|
|
|
|
array_pop($output);
|
2020-04-10 04:35:09 -04:00
|
|
|
} elseif ($chunk == '.') {
|
2013-01-26 13:44:09 -05:00
|
|
|
} else {
|
|
|
|
|
$output[] = $chunk;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return implode('/', $output);
|
|
|
|
|
}
|
2013-02-15 21:27:50 -05:00
|
|
|
|
2015-01-23 16:53:21 -05:00
|
|
|
/**
|
|
|
|
|
* Test a storage for availability
|
|
|
|
|
*/
|
2024-09-19 12:19:34 -04:00
|
|
|
public function test(): bool {
|
2017-01-24 10:56:24 -05:00
|
|
|
try {
|
|
|
|
|
if ($this->stat('')) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-11-21 02:38:50 -05:00
|
|
|
Server::get(LoggerInterface::class)->info('External storage not available: stat() failed');
|
2017-01-24 10:56:24 -05:00
|
|
|
return false;
|
|
|
|
|
} catch (\Exception $e) {
|
2024-11-21 02:38:50 -05:00
|
|
|
Server::get(LoggerInterface::class)->warning(
|
2024-08-23 09:10:27 -04:00
|
|
|
'External storage not available: ' . $e->getMessage(),
|
2022-03-31 04:57:10 -04:00
|
|
|
['exception' => $e]
|
|
|
|
|
);
|
2017-01-24 10:56:24 -05:00
|
|
|
return false;
|
2012-12-28 12:00:48 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function free_space(string $path): int|float|false {
|
2014-08-19 08:05:08 -04:00
|
|
|
return \OCP\Files\FileInfo::SPACE_UNKNOWN;
|
2013-02-15 21:27:50 -05:00
|
|
|
}
|
2014-02-04 13:58:49 -05:00
|
|
|
|
2024-09-19 12:19:34 -04:00
|
|
|
public function isLocal(): bool {
|
2014-02-04 13:58:49 -05:00
|
|
|
// the common implementation returns a temporary file by
|
|
|
|
|
// default, which is not local
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-03-04 09:44:58 -05:00
|
|
|
|
2014-05-29 07:45:50 -04:00
|
|
|
/**
|
|
|
|
|
* Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class
|
|
|
|
|
*/
|
2024-10-01 10:12:30 -04:00
|
|
|
public function instanceOfStorage(string $class): bool {
|
2016-06-01 08:29:31 -04:00
|
|
|
if (ltrim($class, '\\') === 'OC\Files\Storage\Shared') {
|
|
|
|
|
// FIXME Temporary fix to keep existing checks working
|
|
|
|
|
$class = '\OCA\Files_Sharing\SharedStorage';
|
|
|
|
|
}
|
2014-05-29 07:45:50 -04:00
|
|
|
return is_a($this, $class);
|
|
|
|
|
}
|
2014-12-15 09:20:24 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A custom storage implementation can return an url for direct download of a give file.
|
|
|
|
|
*
|
|
|
|
|
* For now the returned array can hold the parameter url - in future more attributes might follow.
|
|
|
|
|
*/
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getDirectDownload(string $path): array|false {
|
2014-12-15 09:20:24 -05:00
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function verifyPath(string $path, string $fileName): void {
|
2024-07-15 10:28:43 -04:00
|
|
|
$this->getFilenameValidator()
|
|
|
|
|
->validateFilename($fileName);
|
2015-03-10 08:08:22 -04:00
|
|
|
|
2024-08-27 08:07:44 -04:00
|
|
|
// verify also the path is valid
|
|
|
|
|
if ($path && $path !== '/' && $path !== '.') {
|
|
|
|
|
try {
|
|
|
|
|
$this->verifyPath(dirname($path), basename($path));
|
|
|
|
|
} catch (InvalidPathException $e) {
|
|
|
|
|
// Ignore invalid file type exceptions on directories
|
|
|
|
|
if ($e->getCode() !== FilenameValidator::INVALID_FILE_TYPE) {
|
|
|
|
|
$l = \OCP\Util::getL10N('lib');
|
|
|
|
|
throw new InvalidPathException($l->t('Invalid parent path'), previous: $e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-18 11:44:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-07-15 10:28:43 -04:00
|
|
|
* Get the filename validator
|
|
|
|
|
* (cached for performance)
|
2015-02-18 11:44:13 -05:00
|
|
|
*/
|
2024-07-15 10:28:43 -04:00
|
|
|
protected function getFilenameValidator(): IFilenameValidator {
|
|
|
|
|
if ($this->filenameValidator === null) {
|
2024-11-21 02:38:50 -05:00
|
|
|
$this->filenameValidator = Server::get(IFilenameValidator::class);
|
2015-02-20 12:19:14 -05:00
|
|
|
}
|
2024-07-15 10:28:43 -04:00
|
|
|
return $this->filenameValidator;
|
2015-02-18 11:44:13 -05:00
|
|
|
}
|
2015-05-19 08:21:58 -04:00
|
|
|
|
2024-09-19 12:19:34 -04:00
|
|
|
public function setMountOptions(array $options): void {
|
2015-03-05 11:22:48 -05:00
|
|
|
$this->mountOptions = $options;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getMountOption(string $name, mixed $default = null): mixed {
|
2023-07-07 06:13:21 -04:00
|
|
|
return $this->mountOptions[$name] ?? $default;
|
2015-03-05 11:22:48 -05:00
|
|
|
}
|
2015-05-19 08:21:58 -04:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath, bool $preserveMtime = false): bool {
|
2015-05-19 08:21:58 -04:00
|
|
|
if ($sourceStorage === $this) {
|
|
|
|
|
return $this->copy($sourceInternalPath, $targetInternalPath);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-16 07:32:42 -05:00
|
|
|
if ($sourceStorage->is_dir($sourceInternalPath)) {
|
|
|
|
|
$dh = $sourceStorage->opendir($sourceInternalPath);
|
|
|
|
|
$result = $this->mkdir($targetInternalPath);
|
|
|
|
|
if (is_resource($dh)) {
|
2020-11-17 08:29:56 -05:00
|
|
|
$result = true;
|
2024-11-21 02:38:50 -05:00
|
|
|
while ($result && ($file = readdir($dh)) !== false) {
|
2015-01-16 07:32:42 -05:00
|
|
|
if (!Filesystem::isIgnoredDir($file)) {
|
|
|
|
|
$result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$source = $sourceStorage->fopen($sourceInternalPath, 'r');
|
2020-07-01 09:37:47 -04:00
|
|
|
$result = false;
|
2020-06-29 12:14:47 -04:00
|
|
|
if ($source) {
|
2020-07-01 09:37:47 -04:00
|
|
|
try {
|
|
|
|
|
$this->writeStream($targetInternalPath, $source);
|
|
|
|
|
$result = true;
|
|
|
|
|
} catch (\Exception $e) {
|
2024-11-21 02:38:50 -05:00
|
|
|
Server::get(LoggerInterface::class)->warning('Failed to copy stream to storage', ['exception' => $e]);
|
2020-07-01 09:37:47 -04:00
|
|
|
}
|
2020-06-29 12:14:47 -04:00
|
|
|
}
|
|
|
|
|
|
2020-12-03 05:03:17 -05:00
|
|
|
if ($result && $preserveMtime) {
|
|
|
|
|
$mtime = $sourceStorage->filemtime($sourceInternalPath);
|
|
|
|
|
$this->touch($targetInternalPath, is_int($mtime) ? $mtime : null);
|
2015-01-16 07:32:42 -05:00
|
|
|
}
|
2015-03-05 05:49:31 -05:00
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
|
// delete partially written target file
|
|
|
|
|
$this->unlink($targetInternalPath);
|
|
|
|
|
// delete cache entry that was created by fopen
|
|
|
|
|
$this->getCache()->remove($targetInternalPath);
|
|
|
|
|
}
|
2015-01-16 07:32:42 -05:00
|
|
|
}
|
2015-03-05 05:49:31 -05:00
|
|
|
return (bool)$result;
|
2015-01-16 07:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
2019-09-25 13:17:06 -04:00
|
|
|
/**
|
|
|
|
|
* Check if a storage is the same as the current one, including wrapped storages
|
|
|
|
|
*/
|
|
|
|
|
private function isSameStorage(IStorage $storage): bool {
|
|
|
|
|
while ($storage->instanceOfStorage(Wrapper::class)) {
|
|
|
|
|
/**
|
2024-02-22 20:22:12 -05:00
|
|
|
* @var Wrapper $storage
|
2019-09-25 13:17:06 -04:00
|
|
|
*/
|
|
|
|
|
$storage = $storage->getWrapperStorage();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $storage === $this;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool {
|
2024-11-13 12:47:13 -05:00
|
|
|
if (
|
2025-06-30 09:04:05 -04:00
|
|
|
!$sourceStorage->instanceOfStorage(Encryption::class)
|
|
|
|
|
&& $this->isSameStorage($sourceStorage)
|
2024-11-13 12:47:13 -05:00
|
|
|
) {
|
2019-09-25 13:17:06 -04:00
|
|
|
// resolve any jailed paths
|
|
|
|
|
while ($sourceStorage->instanceOfStorage(Jail::class)) {
|
|
|
|
|
/**
|
|
|
|
|
* @var Jail $sourceStorage
|
|
|
|
|
*/
|
|
|
|
|
$sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
|
|
|
|
|
$sourceStorage = $sourceStorage->getUnjailedStorage();
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-19 08:21:58 -04:00
|
|
|
return $this->rename($sourceInternalPath, $targetInternalPath);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-19 06:59:10 -05:00
|
|
|
if (!$sourceStorage->isDeletable($sourceInternalPath)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-16 07:32:42 -05:00
|
|
|
$result = $this->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, true);
|
|
|
|
|
if ($result) {
|
2024-09-25 11:52:42 -04:00
|
|
|
if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class)) {
|
|
|
|
|
/** @var ObjectStoreStorage $sourceStorage */
|
|
|
|
|
$sourceStorage->setPreserveCacheOnDelete(true);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
if ($sourceStorage->is_dir($sourceInternalPath)) {
|
|
|
|
|
$result = $sourceStorage->rmdir($sourceInternalPath);
|
|
|
|
|
} else {
|
|
|
|
|
$result = $sourceStorage->unlink($sourceInternalPath);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class)) {
|
|
|
|
|
/** @var ObjectStoreStorage $sourceStorage */
|
|
|
|
|
$sourceStorage->setPreserveCacheOnDelete(false);
|
|
|
|
|
}
|
2015-01-16 07:32:42 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2015-04-20 08:25:39 -04:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getMetaData(string $path): ?array {
|
2022-03-17 11:05:52 -04:00
|
|
|
if (Filesystem::isFileBlacklisted($path)) {
|
|
|
|
|
throw new ForbiddenException('Invalid path: ' . $path, false);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-20 10:50:12 -04:00
|
|
|
$permissions = $this->getPermissions($path);
|
|
|
|
|
if (!$permissions & \OCP\Constants::PERMISSION_READ) {
|
2016-04-07 13:51:27 -04:00
|
|
|
//can't read, nothing we can do
|
2015-04-20 10:50:12 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-20 08:25:39 -04:00
|
|
|
$data = [];
|
|
|
|
|
$data['mimetype'] = $this->getMimeType($path);
|
|
|
|
|
$data['mtime'] = $this->filemtime($path);
|
2016-06-07 11:09:24 -04:00
|
|
|
if ($data['mtime'] === false) {
|
|
|
|
|
$data['mtime'] = time();
|
|
|
|
|
}
|
2015-04-20 08:25:39 -04:00
|
|
|
if ($data['mimetype'] == 'httpd/unix-directory') {
|
|
|
|
|
$data['size'] = -1; //unknown
|
|
|
|
|
} else {
|
|
|
|
|
$data['size'] = $this->filesize($path);
|
|
|
|
|
}
|
|
|
|
|
$data['etag'] = $this->getETag($path);
|
|
|
|
|
$data['storage_mtime'] = $data['mtime'];
|
2016-04-12 03:45:59 -04:00
|
|
|
$data['permissions'] = $permissions;
|
2020-03-27 12:47:20 -04:00
|
|
|
$data['name'] = basename($path);
|
2015-04-20 10:50:12 -04:00
|
|
|
|
2015-04-20 08:25:39 -04:00
|
|
|
return $data;
|
|
|
|
|
}
|
2015-05-04 08:21:34 -04:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function acquireLock(string $path, int $type, ILockingProvider $provider): void {
|
2017-02-13 09:03:46 -05:00
|
|
|
$logger = $this->getLockLogger();
|
|
|
|
|
if ($logger) {
|
|
|
|
|
$typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive';
|
2017-02-13 16:49:44 -05:00
|
|
|
$logger->info(
|
|
|
|
|
sprintf(
|
|
|
|
|
'acquire %s lock on "%s" on storage "%s"',
|
|
|
|
|
$typeString,
|
|
|
|
|
$path,
|
|
|
|
|
$this->getId()
|
|
|
|
|
),
|
|
|
|
|
[
|
|
|
|
|
'app' => 'locking',
|
|
|
|
|
]
|
|
|
|
|
);
|
2017-02-13 09:03:46 -05:00
|
|
|
}
|
|
|
|
|
try {
|
2020-06-30 12:10:42 -04:00
|
|
|
$provider->acquireLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type, $this->getId() . '::' . $path);
|
2017-02-13 09:03:46 -05:00
|
|
|
} catch (LockedException $e) {
|
2023-03-28 07:19:05 -04:00
|
|
|
$e = new LockedException($e->getPath(), $e, $e->getExistingLock(), $path);
|
2017-02-13 09:03:46 -05:00
|
|
|
if ($logger) {
|
2022-03-31 04:57:10 -04:00
|
|
|
$logger->info($e->getMessage(), ['exception' => $e]);
|
2017-02-13 09:03:46 -05:00
|
|
|
}
|
2017-02-13 19:03:35 -05:00
|
|
|
throw $e;
|
2017-02-13 09:03:46 -05:00
|
|
|
}
|
2015-05-04 08:21:34 -04:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function releaseLock(string $path, int $type, ILockingProvider $provider): void {
|
2017-02-13 09:03:46 -05:00
|
|
|
$logger = $this->getLockLogger();
|
|
|
|
|
if ($logger) {
|
|
|
|
|
$typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive';
|
2017-02-13 16:49:44 -05:00
|
|
|
$logger->info(
|
|
|
|
|
sprintf(
|
|
|
|
|
'release %s lock on "%s" on storage "%s"',
|
|
|
|
|
$typeString,
|
|
|
|
|
$path,
|
|
|
|
|
$this->getId()
|
|
|
|
|
),
|
|
|
|
|
[
|
|
|
|
|
'app' => 'locking',
|
|
|
|
|
]
|
|
|
|
|
);
|
2017-02-13 09:03:46 -05:00
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$provider->releaseLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type);
|
|
|
|
|
} catch (LockedException $e) {
|
2023-03-28 07:19:05 -04:00
|
|
|
$e = new LockedException($e->getPath(), $e, $e->getExistingLock(), $path);
|
2017-02-13 09:03:46 -05:00
|
|
|
if ($logger) {
|
2022-03-31 04:57:10 -04:00
|
|
|
$logger->info($e->getMessage(), ['exception' => $e]);
|
2017-02-13 09:03:46 -05:00
|
|
|
}
|
2017-02-13 19:03:35 -05:00
|
|
|
throw $e;
|
2017-02-13 09:03:46 -05:00
|
|
|
}
|
2015-05-04 08:21:34 -04:00
|
|
|
}
|
2015-05-29 08:40:06 -04:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function changeLock(string $path, int $type, ILockingProvider $provider): void {
|
2017-02-13 09:03:46 -05:00
|
|
|
$logger = $this->getLockLogger();
|
|
|
|
|
if ($logger) {
|
|
|
|
|
$typeString = ($type === ILockingProvider::LOCK_SHARED) ? 'shared' : 'exclusive';
|
2017-02-13 16:49:44 -05:00
|
|
|
$logger->info(
|
|
|
|
|
sprintf(
|
|
|
|
|
'change lock on "%s" to %s on storage "%s"',
|
|
|
|
|
$path,
|
|
|
|
|
$typeString,
|
|
|
|
|
$this->getId()
|
|
|
|
|
),
|
|
|
|
|
[
|
|
|
|
|
'app' => 'locking',
|
|
|
|
|
]
|
|
|
|
|
);
|
2017-02-13 09:03:46 -05:00
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$provider->changeLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type);
|
|
|
|
|
} catch (LockedException $e) {
|
2023-03-28 07:19:05 -04:00
|
|
|
$e = new LockedException($e->getPath(), $e, $e->getExistingLock(), $path);
|
2022-03-31 04:57:10 -04:00
|
|
|
if ($logger) {
|
|
|
|
|
$logger->info($e->getMessage(), ['exception' => $e]);
|
|
|
|
|
}
|
2017-02-13 19:03:35 -05:00
|
|
|
throw $e;
|
2017-02-13 09:03:46 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-31 04:57:10 -04:00
|
|
|
private function getLockLogger(): ?LoggerInterface {
|
2017-02-13 09:03:46 -05:00
|
|
|
if (is_null($this->shouldLogLocks)) {
|
2024-11-21 02:38:50 -05:00
|
|
|
$this->shouldLogLocks = Server::get(IConfig::class)->getSystemValueBool('filelocking.debug', false);
|
|
|
|
|
$this->logger = $this->shouldLogLocks ? Server::get(LoggerInterface::class) : null;
|
2017-02-13 09:03:46 -05:00
|
|
|
}
|
|
|
|
|
return $this->logger;
|
2015-05-29 08:40:06 -04:00
|
|
|
}
|
2015-01-23 16:53:21 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array [ available, last_checked ]
|
|
|
|
|
*/
|
2024-09-19 12:19:34 -04:00
|
|
|
public function getAvailability(): array {
|
2015-01-23 16:53:21 -05:00
|
|
|
return $this->getStorageCache()->getAvailability();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function setAvailability(bool $isAvailable): void {
|
2015-01-23 16:53:21 -05:00
|
|
|
$this->getStorageCache()->setAvailability($isAvailable);
|
|
|
|
|
}
|
2017-02-23 11:32:16 -05:00
|
|
|
|
2024-03-19 04:33:16 -04:00
|
|
|
public function setOwner(?string $user): void {
|
|
|
|
|
$this->owner = $user;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 12:19:34 -04:00
|
|
|
public function needsPartFile(): bool {
|
2017-02-23 11:32:16 -05:00
|
|
|
return true;
|
|
|
|
|
}
|
2018-10-26 13:15:23 -04:00
|
|
|
|
2024-03-28 11:13:19 -04:00
|
|
|
public function writeStream(string $path, $stream, ?int $size = null): int {
|
2018-10-26 13:15:23 -04:00
|
|
|
$target = $this->fopen($path, 'w');
|
2018-10-31 14:41:55 -04:00
|
|
|
if (!$target) {
|
2020-07-01 09:37:47 -04:00
|
|
|
throw new GenericFileException("Failed to open $path for writing");
|
2018-10-31 14:41:55 -04:00
|
|
|
}
|
2020-04-08 10:40:56 -04:00
|
|
|
try {
|
2025-05-15 18:07:33 -04:00
|
|
|
[$count, $result] = Files::streamCopy($stream, $target, true);
|
2020-07-01 09:37:47 -04:00
|
|
|
if (!$result) {
|
2024-08-23 09:10:27 -04:00
|
|
|
throw new GenericFileException('Failed to copy stream');
|
2020-07-01 09:37:47 -04:00
|
|
|
}
|
2020-04-08 10:40:56 -04:00
|
|
|
} finally {
|
|
|
|
|
fclose($target);
|
|
|
|
|
fclose($stream);
|
|
|
|
|
}
|
2018-10-26 13:15:23 -04:00
|
|
|
return $count;
|
|
|
|
|
}
|
2020-03-27 12:47:20 -04:00
|
|
|
|
2024-10-01 10:12:30 -04:00
|
|
|
public function getDirectoryContent(string $directory): \Traversable {
|
2020-03-27 12:47:20 -04:00
|
|
|
$dh = $this->opendir($directory);
|
2024-02-23 10:07:43 -05:00
|
|
|
|
|
|
|
|
if ($dh === false) {
|
|
|
|
|
throw new StorageNotAvailableException('Directory listing failed');
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 12:47:20 -04:00
|
|
|
if (is_resource($dh)) {
|
|
|
|
|
$basePath = rtrim($directory, '/');
|
|
|
|
|
while (($file = readdir($dh)) !== false) {
|
2022-03-17 11:05:52 -04:00
|
|
|
if (!Filesystem::isIgnoredDir($file)) {
|
2020-03-27 12:47:20 -04:00
|
|
|
$childPath = $basePath . '/' . trim($file, '/');
|
2020-07-10 08:18:40 -04:00
|
|
|
$metadata = $this->getMetaData($childPath);
|
|
|
|
|
if ($metadata !== null) {
|
|
|
|
|
yield $metadata;
|
|
|
|
|
}
|
2020-03-27 12:47:20 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-02-27 06:04:04 -05:00
|
|
|
}
|