2011-03-02 16:18:22 -05:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2011-03-02 16:18:22 -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
|
2011-03-13 12:25:34 -04:00
|
|
|
*/
|
2020-08-20 08:08:18 -04:00
|
|
|
use bantu\IniGetWrapper\IniGetWrapper;
|
2024-07-16 20:03:18 -04:00
|
|
|
use OC\Files\FilenameValidator;
|
2022-04-21 10:48:01 -04:00
|
|
|
use OC\Files\Filesystem;
|
2021-05-20 07:55:49 -04:00
|
|
|
use OCP\Files\Mount\IMountPoint;
|
2022-08-18 04:47:49 -04:00
|
|
|
use OCP\IBinaryFinder;
|
2022-04-21 10:48:01 -04:00
|
|
|
use OCP\ICacheFactory;
|
2021-05-20 07:55:49 -04:00
|
|
|
use OCP\IUser;
|
2025-05-16 10:03:26 -04:00
|
|
|
use OCP\Server;
|
2023-01-23 05:11:26 -05:00
|
|
|
use OCP\Util;
|
2022-04-22 08:59:15 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2015-02-26 05:37:37 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Collection of useful functions
|
2023-02-15 13:27:04 -05:00
|
|
|
*
|
|
|
|
|
* @psalm-type StorageInfo = array{
|
|
|
|
|
* free: float|int,
|
|
|
|
|
* mountPoint: string,
|
|
|
|
|
* mountType: string,
|
|
|
|
|
* owner: string,
|
|
|
|
|
* ownerDisplayName: string,
|
|
|
|
|
* quota: float|int,
|
|
|
|
|
* relative: float|int,
|
|
|
|
|
* total: float|int,
|
|
|
|
|
* used: float|int,
|
|
|
|
|
* }
|
2015-02-26 05:37:37 -05:00
|
|
|
*/
|
2011-07-29 15:36:03 -04:00
|
|
|
class OC_Helper {
|
2013-08-07 10:53:09 -04:00
|
|
|
private static $templateManager;
|
2023-08-15 12:31:10 -04:00
|
|
|
private static ?ICacheFactory $cacheFactory = null;
|
|
|
|
|
private static ?bool $quotaIncludeExternalStorage = null;
|
2012-04-13 16:59:47 -04:00
|
|
|
|
2011-05-28 11:33:25 -04:00
|
|
|
/**
|
2014-05-19 11:50:53 -04:00
|
|
|
* Recursive copying of folders
|
2011-05-28 11:33:25 -04:00
|
|
|
* @param string $src source folder
|
|
|
|
|
* @param string $dest target folder
|
2023-01-23 04:05:20 -05:00
|
|
|
* @return void
|
2025-05-16 10:44:38 -04:00
|
|
|
* @deprecated 32.0.0 - use \OCP\Files\Folder::copy
|
2011-05-28 11:33:25 -04:00
|
|
|
*/
|
2020-04-10 10:51:06 -04:00
|
|
|
public static function copyr($src, $dest) {
|
2024-07-16 20:03:18 -04:00
|
|
|
if (!file_exists($src)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-07 10:38:57 -04:00
|
|
|
if (is_dir($src)) {
|
|
|
|
|
if (!is_dir($dest)) {
|
2011-05-28 11:33:25 -04:00
|
|
|
mkdir($dest);
|
|
|
|
|
}
|
|
|
|
|
$files = scandir($src);
|
2012-09-07 09:22:01 -04:00
|
|
|
foreach ($files as $file) {
|
|
|
|
|
if ($file != '.' && $file != '..') {
|
2011-05-28 11:33:25 -04:00
|
|
|
self::copyr("$src/$file", "$dest/$file");
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-16 20:03:18 -04:00
|
|
|
} else {
|
|
|
|
|
$validator = \OCP\Server::get(FilenameValidator::class);
|
|
|
|
|
if (!$validator->isForbidden($src)) {
|
|
|
|
|
copy($src, $dest);
|
|
|
|
|
}
|
2011-05-28 11:33:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
2012-04-13 16:59:47 -04:00
|
|
|
|
2013-08-07 10:53:09 -04:00
|
|
|
/**
|
2019-10-16 03:54:17 -04:00
|
|
|
* @deprecated 18.0.0
|
2013-08-07 10:53:09 -04:00
|
|
|
* @return \OC\Files\Type\TemplateManager
|
|
|
|
|
*/
|
2020-04-10 10:51:06 -04:00
|
|
|
public static function getFileTemplateManager() {
|
2013-08-07 10:53:09 -04:00
|
|
|
if (!self::$templateManager) {
|
|
|
|
|
self::$templateManager = new \OC\Files\Type\TemplateManager();
|
|
|
|
|
}
|
|
|
|
|
return self::$templateManager;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-28 14:10:58 -04:00
|
|
|
/**
|
2013-01-04 17:00:51 -05:00
|
|
|
* detect if a given program is found in the search PATH
|
|
|
|
|
*
|
2014-05-11 16:51:30 -04:00
|
|
|
* @param string $name
|
2013-01-04 17:00:51 -05:00
|
|
|
* @param bool $path
|
|
|
|
|
* @internal param string $program name
|
|
|
|
|
* @internal param string $optional search path, defaults to $PATH
|
|
|
|
|
* @return bool true if executable program found in path
|
2025-05-14 03:55:14 -04:00
|
|
|
* @deprecated 32.0.0 use the \OCP\IBinaryFinder
|
2013-01-04 17:00:51 -05:00
|
|
|
*/
|
2012-09-07 09:22:01 -04:00
|
|
|
public static function canExecute($name, $path = false) {
|
2011-07-28 14:10:58 -04:00
|
|
|
// path defaults to PATH from environment if not set
|
|
|
|
|
if ($path === false) {
|
|
|
|
|
$path = getenv('PATH');
|
|
|
|
|
}
|
2016-10-15 07:22:25 -04:00
|
|
|
// we look for an executable file of that name
|
|
|
|
|
$exts = [''];
|
|
|
|
|
$check_fn = 'is_executable';
|
2011-07-28 14:10:58 -04:00
|
|
|
// Default check will be done with $path directories :
|
2025-05-16 10:44:38 -04:00
|
|
|
$dirs = explode(PATH_SEPARATOR, (string)$path);
|
2011-07-28 14:10:58 -04:00
|
|
|
// WARNING : We have to check if open_basedir is enabled :
|
2020-08-20 08:08:18 -04:00
|
|
|
$obd = OC::$server->get(IniGetWrapper::class)->getString('open_basedir');
|
2013-08-07 10:38:57 -04:00
|
|
|
if ($obd != 'none') {
|
2011-07-28 14:10:58 -04:00
|
|
|
$obd_values = explode(PATH_SEPARATOR, $obd);
|
2013-08-07 10:38:57 -04:00
|
|
|
if (count($obd_values) > 0 and $obd_values[0]) {
|
2012-09-22 20:39:11 -04:00
|
|
|
// open_basedir is in effect !
|
|
|
|
|
// We need to check if the program is in one of these dirs :
|
|
|
|
|
$dirs = $obd_values;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-07 10:38:57 -04:00
|
|
|
foreach ($dirs as $dir) {
|
|
|
|
|
foreach ($exts as $ext) {
|
|
|
|
|
if ($check_fn("$dir/$name" . $ext)) {
|
2011-07-28 14:10:58 -04:00
|
|
|
return true;
|
2020-04-10 08:19:56 -04:00
|
|
|
}
|
2011-07-28 14:10:58 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2012-04-13 16:59:47 -04:00
|
|
|
|
2012-02-27 06:04:04 -05:00
|
|
|
/**
|
|
|
|
|
* copy the contents of one stream to another
|
2013-08-07 10:38:57 -04:00
|
|
|
*
|
2012-09-22 20:39:11 -04:00
|
|
|
* @param resource $source
|
|
|
|
|
* @param resource $target
|
2013-10-14 15:33:23 -04:00
|
|
|
* @return array the number of bytes copied and result
|
2025-05-15 18:07:33 -04:00
|
|
|
* @deprecated 5.0.0 - Use \OCP\Files::streamCopy
|
2012-02-27 06:04:04 -05:00
|
|
|
*/
|
2012-11-02 14:53:02 -04:00
|
|
|
public static function streamCopy($source, $target) {
|
2025-05-15 18:07:33 -04:00
|
|
|
return \OCP\Files::streamCopy($source, $target, true);
|
2012-02-27 06:04:04 -05:00
|
|
|
}
|
2012-04-13 16:59:47 -04:00
|
|
|
|
2012-06-14 06:57:30 -04:00
|
|
|
/**
|
2013-08-07 10:38:57 -04:00
|
|
|
* Adds a suffix to the name in case the file exists
|
|
|
|
|
*
|
2014-05-11 16:51:30 -04:00
|
|
|
* @param string $path
|
|
|
|
|
* @param string $filename
|
2013-08-07 10:38:57 -04:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2012-09-07 09:22:01 -04:00
|
|
|
public static function buildNotExistingFileName($path, $filename) {
|
2013-03-08 08:22:04 -05:00
|
|
|
$view = \OC\Files\Filesystem::getView();
|
|
|
|
|
return self::buildNotExistingFileNameForView($path, $filename, $view);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-08-07 10:38:57 -04:00
|
|
|
* Adds a suffix to the name in case the file exists
|
|
|
|
|
*
|
2014-05-11 16:51:30 -04:00
|
|
|
* @param string $path
|
|
|
|
|
* @param string $filename
|
2013-08-07 10:38:57 -04:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2013-03-08 08:22:04 -05:00
|
|
|
public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) {
|
2013-08-07 10:38:57 -04:00
|
|
|
if ($path === '/') {
|
|
|
|
|
$path = '';
|
2012-06-14 06:57:30 -04:00
|
|
|
}
|
|
|
|
|
if ($pos = strrpos($filename, '.')) {
|
|
|
|
|
$name = substr($filename, 0, $pos);
|
|
|
|
|
$ext = substr($filename, $pos);
|
|
|
|
|
} else {
|
|
|
|
|
$name = $filename;
|
2012-09-22 20:39:11 -04:00
|
|
|
$ext = '';
|
2012-06-14 06:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$newpath = $path . '/' . $filename;
|
2013-07-05 09:38:09 -04:00
|
|
|
if ($view->file_exists($newpath)) {
|
2013-08-07 10:38:57 -04:00
|
|
|
if (preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) {
|
2013-09-05 05:58:57 -04:00
|
|
|
//Replace the last "(number)" with "(number+1)"
|
2013-08-07 10:38:57 -04:00
|
|
|
$last_match = count($matches[0]) - 1;
|
|
|
|
|
$counter = $matches[1][$last_match][0] + 1;
|
2013-07-05 09:38:09 -04:00
|
|
|
$offset = $matches[0][$last_match][1];
|
|
|
|
|
$match_length = strlen($matches[0][$last_match][0]);
|
|
|
|
|
} else {
|
|
|
|
|
$counter = 2;
|
2015-04-27 08:45:05 -04:00
|
|
|
$match_length = 0;
|
2013-07-05 09:38:09 -04:00
|
|
|
$offset = false;
|
|
|
|
|
}
|
|
|
|
|
do {
|
2013-08-07 10:38:57 -04:00
|
|
|
if ($offset) {
|
2013-09-05 05:58:57 -04:00
|
|
|
//Replace the last "(number)" with "(number+1)"
|
2013-08-07 10:38:57 -04:00
|
|
|
$newname = substr_replace($name, '(' . $counter . ')', $offset, $match_length);
|
2013-07-05 09:38:09 -04:00
|
|
|
} else {
|
|
|
|
|
$newname = $name . ' (' . $counter . ')';
|
|
|
|
|
}
|
|
|
|
|
$newpath = $path . '/' . $newname . $ext;
|
|
|
|
|
$counter++;
|
|
|
|
|
} while ($view->file_exists($newpath));
|
2012-06-14 06:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $newpath;
|
|
|
|
|
}
|
2012-07-02 14:24:26 -04:00
|
|
|
|
2013-01-04 17:00:51 -05:00
|
|
|
/**
|
|
|
|
|
* Checks if a function is available
|
2013-08-07 10:38:57 -04:00
|
|
|
*
|
2024-09-18 17:51:06 -04:00
|
|
|
* @deprecated 25.0.0 use \OCP\Util::isFunctionEnabled instead
|
2013-01-04 17:00:51 -05:00
|
|
|
*/
|
2022-08-18 04:47:49 -04:00
|
|
|
public static function is_function_enabled(string $function_name): bool {
|
2025-05-16 10:03:26 -04:00
|
|
|
return Util::isFunctionEnabled($function_name);
|
2013-01-04 17:00:51 -05:00
|
|
|
}
|
2013-01-06 06:18:21 -05:00
|
|
|
|
2014-11-20 06:37:59 -05:00
|
|
|
/**
|
|
|
|
|
* Try to find a program
|
2025-05-17 07:18:49 -04:00
|
|
|
* @deprecated 25.0.0 Use \OCP\IBinaryFinder directly
|
2014-11-20 06:37:59 -05:00
|
|
|
*/
|
2022-08-18 04:47:49 -04:00
|
|
|
public static function findBinaryPath(string $program): ?string {
|
2025-05-16 10:03:26 -04:00
|
|
|
$result = Server::get(IBinaryFinder::class)->findBinaryPath($program);
|
2022-08-18 04:47:49 -04:00
|
|
|
return $result !== false ? $result : null;
|
2014-11-20 06:37:59 -05:00
|
|
|
}
|
|
|
|
|
|
2013-01-02 08:35:45 -05:00
|
|
|
/**
|
2013-08-26 18:59:58 -04:00
|
|
|
* Calculate the disc space for the given path
|
|
|
|
|
*
|
2020-11-13 10:52:08 -05:00
|
|
|
* BEWARE: this requires that Util::setupFS() was called
|
|
|
|
|
* already !
|
|
|
|
|
*
|
2013-08-26 18:59:58 -04:00
|
|
|
* @param string $path
|
2014-04-02 11:10:57 -04:00
|
|
|
* @param \OCP\Files\FileInfo $rootInfo (optional)
|
2022-11-21 10:21:25 -05:00
|
|
|
* @param bool $includeMountPoints whether to include mount points in the size calculation
|
|
|
|
|
* @param bool $useCache whether to use the cached quota values
|
2023-02-15 13:27:04 -05:00
|
|
|
* @psalm-suppress LessSpecificReturnStatement Legacy code outputs weird types - manually validated that they are correct
|
|
|
|
|
* @return StorageInfo
|
2015-04-27 08:45:05 -04:00
|
|
|
* @throws \OCP\Files\NotFoundException
|
2013-01-02 08:35:45 -05:00
|
|
|
*/
|
2022-11-21 10:21:25 -05:00
|
|
|
public static function getStorageInfo($path, $rootInfo = null, $includeMountPoints = true, $useCache = true) {
|
2023-08-15 12:31:10 -04:00
|
|
|
if (!self::$cacheFactory) {
|
2025-05-16 10:03:26 -04:00
|
|
|
self::$cacheFactory = Server::get(ICacheFactory::class);
|
2023-08-15 12:31:10 -04:00
|
|
|
}
|
|
|
|
|
$memcache = self::$cacheFactory->createLocal('storage_info');
|
2022-04-21 10:48:01 -04:00
|
|
|
|
2013-11-18 11:29:30 -05:00
|
|
|
// return storage info without adding mount points
|
2023-08-15 12:31:10 -04:00
|
|
|
if (self::$quotaIncludeExternalStorage === null) {
|
|
|
|
|
self::$quotaIncludeExternalStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false);
|
|
|
|
|
}
|
2014-03-25 11:37:46 -04:00
|
|
|
|
2023-01-04 13:06:52 -05:00
|
|
|
$view = Filesystem::getView();
|
|
|
|
|
if (!$view) {
|
|
|
|
|
throw new \OCP\Files\NotFoundException();
|
|
|
|
|
}
|
2023-02-18 09:41:01 -05:00
|
|
|
$fullPath = Filesystem::normalizePath($view->getAbsolutePath($path));
|
2023-01-04 13:06:52 -05:00
|
|
|
|
2022-04-21 10:48:01 -04:00
|
|
|
$cacheKey = $fullPath . '::' . ($includeMountPoints ? 'include' : 'exclude');
|
2022-11-21 10:21:25 -05:00
|
|
|
if ($useCache) {
|
|
|
|
|
$cached = $memcache->get($cacheKey);
|
|
|
|
|
if ($cached) {
|
|
|
|
|
return $cached;
|
|
|
|
|
}
|
2022-04-21 10:48:01 -04:00
|
|
|
}
|
|
|
|
|
|
2014-04-29 09:14:48 -04:00
|
|
|
if (!$rootInfo) {
|
2023-08-15 12:31:10 -04:00
|
|
|
$rootInfo = \OC\Files\Filesystem::getFileInfo($path, self::$quotaIncludeExternalStorage ? 'ext' : false);
|
2014-04-02 11:10:57 -04:00
|
|
|
}
|
2015-01-06 09:56:06 -05:00
|
|
|
if (!$rootInfo instanceof \OCP\Files\FileInfo) {
|
2023-05-16 04:31:52 -04:00
|
|
|
throw new \OCP\Files\NotFoundException('The root directory of the user\'s files is missing');
|
2015-01-06 09:56:06 -05:00
|
|
|
}
|
2022-03-28 09:32:34 -04:00
|
|
|
$used = $rootInfo->getSize($includeMountPoints);
|
2013-01-02 08:35:45 -05:00
|
|
|
if ($used < 0) {
|
2023-02-15 13:27:04 -05:00
|
|
|
$used = 0.0;
|
2013-01-02 08:35:45 -05:00
|
|
|
}
|
2023-02-15 13:27:04 -05:00
|
|
|
/** @var int|float $quota */
|
2016-02-24 04:39:04 -05:00
|
|
|
$quota = \OCP\Files\FileInfo::SPACE_UNLIMITED;
|
2020-08-25 10:05:16 -04:00
|
|
|
$mount = $rootInfo->getMountPoint();
|
|
|
|
|
$storage = $mount->getStorage();
|
2016-02-24 04:39:04 -05:00
|
|
|
$sourceStorage = $storage;
|
2016-06-01 08:29:31 -04:00
|
|
|
if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) {
|
2023-08-15 12:31:10 -04:00
|
|
|
self::$quotaIncludeExternalStorage = false;
|
2014-03-25 11:37:46 -04:00
|
|
|
}
|
2023-08-15 12:31:10 -04:00
|
|
|
if (self::$quotaIncludeExternalStorage) {
|
2016-08-12 09:59:19 -04:00
|
|
|
if ($storage->instanceOfStorage('\OC\Files\Storage\Home')
|
|
|
|
|
|| $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')
|
|
|
|
|
) {
|
|
|
|
|
/** @var \OC\Files\Storage\Home $storage */
|
2020-03-04 11:20:27 -05:00
|
|
|
$user = $storage->getUser();
|
2016-08-12 09:59:19 -04:00
|
|
|
} else {
|
2020-03-04 11:20:27 -05:00
|
|
|
$user = \OC::$server->getUserSession()->getUser();
|
2016-08-12 09:59:19 -04:00
|
|
|
}
|
2025-05-22 06:44:08 -04:00
|
|
|
$quota = $user?->getQuotaBytes() ?? \OCP\Files\FileInfo::SPACE_UNKNOWN;
|
2014-08-19 08:05:08 -04:00
|
|
|
if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) {
|
2014-03-25 11:37:46 -04:00
|
|
|
// always get free space / total space from root + mount points
|
2021-05-20 07:55:49 -04:00
|
|
|
return self::getGlobalStorageInfo($quota, $user, $mount);
|
2014-03-25 11:37:46 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: need a better way to get total space from storage
|
2016-02-24 04:39:04 -05:00
|
|
|
if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota')) {
|
2015-04-27 08:45:05 -04:00
|
|
|
/** @var \OC\Files\Storage\Wrapper\Quota $storage */
|
2016-02-24 04:39:04 -05:00
|
|
|
$quota = $sourceStorage->getQuota();
|
2014-03-10 10:19:18 -04:00
|
|
|
}
|
2022-04-22 08:59:15 -04:00
|
|
|
try {
|
2022-05-11 05:41:00 -04:00
|
|
|
$free = $sourceStorage->free_space($rootInfo->getInternalPath());
|
2023-02-15 13:27:04 -05:00
|
|
|
if (is_bool($free)) {
|
|
|
|
|
$free = 0.0;
|
|
|
|
|
}
|
2022-04-22 08:59:15 -04:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
if ($path === '') {
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
/** @var LoggerInterface $logger */
|
|
|
|
|
$logger = \OC::$server->get(LoggerInterface::class);
|
|
|
|
|
$logger->warning('Error while getting quota info, using root quota', ['exception' => $e]);
|
|
|
|
|
$rootInfo = self::getStorageInfo('');
|
|
|
|
|
$memcache->set($cacheKey, $rootInfo, 5 * 60);
|
|
|
|
|
return $rootInfo;
|
|
|
|
|
}
|
2013-08-07 10:38:57 -04:00
|
|
|
if ($free >= 0) {
|
2013-03-15 11:46:20 -04:00
|
|
|
$total = $free + $used;
|
|
|
|
|
} else {
|
|
|
|
|
$total = $free; //either unknown or unlimited
|
|
|
|
|
}
|
2013-08-27 04:58:17 -04:00
|
|
|
if ($total > 0) {
|
2014-03-10 10:19:18 -04:00
|
|
|
if ($quota > 0 && $total > $quota) {
|
|
|
|
|
$total = $quota;
|
|
|
|
|
}
|
2013-08-27 04:58:17 -04:00
|
|
|
// prevent division by zero or error codes (negative values)
|
2013-03-15 11:46:20 -04:00
|
|
|
$relative = round(($used / $total) * 10000) / 100;
|
|
|
|
|
} else {
|
|
|
|
|
$relative = 0;
|
|
|
|
|
}
|
2013-01-02 08:35:45 -05:00
|
|
|
|
2025-03-11 07:24:18 -04:00
|
|
|
/*
|
|
|
|
|
* \OCA\Files_Sharing\External\Storage returns the cloud ID as the owner for the storage.
|
|
|
|
|
* It is unnecessary to query the user manager for the display name, as it won't have this information.
|
|
|
|
|
*/
|
|
|
|
|
$isRemoteShare = $storage->instanceOfStorage(\OCA\Files_Sharing\External\Storage::class);
|
|
|
|
|
|
2015-06-05 12:21:41 -04:00
|
|
|
$ownerId = $storage->getOwner($path);
|
|
|
|
|
$ownerDisplayName = '';
|
2025-03-11 07:24:18 -04:00
|
|
|
|
|
|
|
|
if ($isRemoteShare === false && $ownerId !== false) {
|
2022-08-26 02:34:15 -04:00
|
|
|
$ownerDisplayName = \OC::$server->getUserManager()->getDisplayName($ownerId) ?? '';
|
2015-06-05 12:21:41 -04:00
|
|
|
}
|
2022-08-26 02:34:15 -04:00
|
|
|
|
2020-10-21 07:37:56 -04:00
|
|
|
if (substr_count($mount->getMountPoint(), '/') < 3) {
|
|
|
|
|
$mountPoint = '';
|
|
|
|
|
} else {
|
|
|
|
|
[,,,$mountPoint] = explode('/', $mount->getMountPoint(), 4);
|
|
|
|
|
}
|
2015-06-05 12:21:41 -04:00
|
|
|
|
2022-04-21 10:48:01 -04:00
|
|
|
$info = [
|
2015-06-05 12:21:41 -04:00
|
|
|
'free' => $free,
|
|
|
|
|
'used' => $used,
|
2016-02-24 04:39:04 -05:00
|
|
|
'quota' => $quota,
|
2015-06-05 12:21:41 -04:00
|
|
|
'total' => $total,
|
|
|
|
|
'relative' => $relative,
|
|
|
|
|
'owner' => $ownerId,
|
|
|
|
|
'ownerDisplayName' => $ownerDisplayName,
|
2020-09-17 10:19:41 -04:00
|
|
|
'mountType' => $mount->getMountType(),
|
|
|
|
|
'mountPoint' => trim($mountPoint, '/'),
|
2015-06-05 12:21:41 -04:00
|
|
|
];
|
2022-04-21 10:48:01 -04:00
|
|
|
|
2025-03-11 07:24:18 -04:00
|
|
|
if ($isRemoteShare === false && $ownerId !== false && $path === '/') {
|
2024-03-05 06:09:36 -05:00
|
|
|
// If path is root, store this as last known quota usage for this user
|
|
|
|
|
\OCP\Server::get(\OCP\IConfig::class)->setUserValue($ownerId, 'files', 'lastSeenQuotaUsage', (string)$relative);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 10:48:01 -04:00
|
|
|
$memcache->set($cacheKey, $info, 5 * 60);
|
|
|
|
|
|
|
|
|
|
return $info;
|
2013-01-02 08:35:45 -05:00
|
|
|
}
|
2014-03-25 11:37:46 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get storage info including all mount points and quota
|
2023-02-15 13:27:04 -05:00
|
|
|
*
|
|
|
|
|
* @psalm-suppress LessSpecificReturnStatement Legacy code outputs weird types - manually validated that they are correct
|
|
|
|
|
* @return StorageInfo
|
2014-03-25 11:37:46 -04:00
|
|
|
*/
|
2023-01-23 05:11:26 -05:00
|
|
|
private static function getGlobalStorageInfo(int|float $quota, IUser $user, IMountPoint $mount): array {
|
2023-01-24 05:39:47 -05:00
|
|
|
$rootInfo = \OC\Files\Filesystem::getFileInfo('', 'ext');
|
2023-02-15 13:27:04 -05:00
|
|
|
/** @var int|float $used */
|
2014-03-25 11:37:46 -04:00
|
|
|
$used = $rootInfo['size'];
|
|
|
|
|
if ($used < 0) {
|
2023-02-15 13:27:04 -05:00
|
|
|
$used = 0.0;
|
2014-03-25 11:37:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$total = $quota;
|
2023-02-15 13:27:04 -05:00
|
|
|
/** @var int|float $free */
|
2014-03-25 11:37:46 -04:00
|
|
|
$free = $quota - $used;
|
|
|
|
|
|
|
|
|
|
if ($total > 0) {
|
|
|
|
|
if ($quota > 0 && $total > $quota) {
|
|
|
|
|
$total = $quota;
|
|
|
|
|
}
|
|
|
|
|
// prevent division by zero or error codes (negative values)
|
|
|
|
|
$relative = round(($used / $total) * 10000) / 100;
|
|
|
|
|
} else {
|
2023-02-15 13:27:04 -05:00
|
|
|
$relative = 0.0;
|
2014-03-25 11:37:46 -04:00
|
|
|
}
|
|
|
|
|
|
2021-05-20 07:55:49 -04:00
|
|
|
if (substr_count($mount->getMountPoint(), '/') < 3) {
|
|
|
|
|
$mountPoint = '';
|
|
|
|
|
} else {
|
|
|
|
|
[,,,$mountPoint] = explode('/', $mount->getMountPoint(), 4);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-06 04:18:47 -05:00
|
|
|
return [
|
|
|
|
|
'free' => $free,
|
|
|
|
|
'used' => $used,
|
|
|
|
|
'total' => $total,
|
|
|
|
|
'relative' => $relative,
|
2021-05-20 07:55:49 -04:00
|
|
|
'quota' => $quota,
|
|
|
|
|
'owner' => $user->getUID(),
|
|
|
|
|
'ownerDisplayName' => $user->getDisplayName(),
|
|
|
|
|
'mountType' => $mount->getMountType(),
|
|
|
|
|
'mountPoint' => trim($mountPoint, '/'),
|
2019-11-06 04:18:47 -05:00
|
|
|
];
|
2014-03-25 11:37:46 -04:00
|
|
|
}
|
2014-11-25 10:12:12 -05:00
|
|
|
|
2023-02-16 02:43:14 -05:00
|
|
|
public static function clearStorageInfo(string $absolutePath): void {
|
|
|
|
|
/** @var ICacheFactory $cacheFactory */
|
|
|
|
|
$cacheFactory = \OC::$server->get(ICacheFactory::class);
|
|
|
|
|
$memcache = $cacheFactory->createLocal('storage_info');
|
2023-02-18 09:41:01 -05:00
|
|
|
$cacheKeyPrefix = Filesystem::normalizePath($absolutePath) . '::';
|
|
|
|
|
$memcache->remove($cacheKeyPrefix . 'include');
|
|
|
|
|
$memcache->remove($cacheKeyPrefix . 'exclude');
|
2023-02-16 02:43:14 -05:00
|
|
|
}
|
|
|
|
|
|
2014-11-25 10:12:12 -05:00
|
|
|
/**
|
|
|
|
|
* Returns whether the config file is set manually to read-only
|
|
|
|
|
* @return bool
|
2025-05-17 07:18:49 -04:00
|
|
|
* @deprecated 32.0.0 use the `config_is_read_only` system config directly
|
2014-11-25 10:12:12 -05:00
|
|
|
*/
|
|
|
|
|
public static function isReadOnlyConfigEnabled() {
|
2023-04-05 06:50:08 -04:00
|
|
|
return \OC::$server->getConfig()->getSystemValueBool('config_is_read_only', false);
|
2014-11-25 10:12:12 -05:00
|
|
|
}
|
2011-03-02 16:18:22 -05:00
|
|
|
}
|