2023-08-08 11:52:21 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace OCA\GroupfolderFilesystemSnapshots\Manager;
|
|
|
|
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
use OCA\GroupFolders\Folder\FolderManager;
|
2024-01-23 22:02:48 +01:00
|
|
|
use OCA\GroupFolders\Mount\MountProvider;
|
2023-08-08 11:52:21 +02:00
|
|
|
|
|
|
|
class PathManager {
|
2024-01-23 22:02:48 +01:00
|
|
|
private IConfig $config;
|
|
|
|
private IRootFolder $rootFolder;
|
|
|
|
private FolderManager $groupfolderFolderManager;
|
|
|
|
/** @var MountProvider */
|
|
|
|
protected $mountProvider;
|
|
|
|
|
|
|
|
const FILESYSTEM_ROOT_PATH = "/srv/nextcloud/files/";
|
|
|
|
const FILESYSTEM_SNAPSHOT_PATH = "/srv/nextcloud/files/.zfs/snapshot/";
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
IConfig $config,
|
|
|
|
IRootFolder $rootFolder,
|
|
|
|
FolderManager $manager,
|
|
|
|
MountProvider $mountProvider,
|
|
|
|
){
|
|
|
|
$this->config = $config;
|
|
|
|
$this->groupfolderFolderManager = $manager;
|
|
|
|
$this->rootFolder = $rootFolder;
|
|
|
|
$this->mountProvider = $mountProvider;
|
|
|
|
}
|
2023-08-08 11:52:21 +02:00
|
|
|
|
2024-01-23 22:02:48 +01:00
|
|
|
public function getFilesystemSnapshotPath() {
|
|
|
|
return self::FILESYSTEM_SNAPSHOT_PATH;
|
|
|
|
}
|
2023-08-08 11:52:21 +02:00
|
|
|
|
2024-01-23 22:02:48 +01:00
|
|
|
// Nextcloud general
|
|
|
|
public function getDataDirectory() {
|
|
|
|
return $this->config->getSystemValue("datadirectory");
|
|
|
|
}
|
2023-08-08 11:52:21 +02:00
|
|
|
|
2024-01-23 22:02:48 +01:00
|
|
|
private function getRootFolderStorageId(): ?int {
|
2023-08-08 11:52:21 +02:00
|
|
|
return $this->rootFolder->getMountPoint()->getNumericStorageId();
|
|
|
|
}
|
|
|
|
|
2024-01-23 22:02:48 +01:00
|
|
|
// Snapshots
|
|
|
|
public function getSnapshotPath(string $snapshotId) {
|
|
|
|
return realpath(self::FILESYSTEM_SNAPSHOT_PATH . DIRECTORY_SEPARATOR . $snapshotId . DIRECTORY_SEPARATOR);
|
|
|
|
}
|
2023-08-08 11:52:21 +02:00
|
|
|
|
2024-01-23 22:02:48 +01:00
|
|
|
public function convertToSnapshotPath(string $filesystemPath, string $snapshotId) {
|
|
|
|
$filesystemPath = realpath($filesystemPath);
|
2023-08-08 11:52:21 +02:00
|
|
|
|
2024-01-23 22:02:48 +01:00
|
|
|
if(!str_starts_with($filesystemPath, self::FILESYSTEM_ROOT_PATH)) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-08 11:52:21 +02:00
|
|
|
|
2024-01-23 22:02:48 +01:00
|
|
|
return str_replace(self::FILESYSTEM_ROOT_PATH, $this->getSnapshotPath($snapshotId) . DIRECTORY_SEPARATOR, $filesystemPath);
|
|
|
|
}
|
2023-08-08 11:52:21 +02:00
|
|
|
|
2024-01-23 22:02:48 +01:00
|
|
|
// Groupfolders
|
|
|
|
public function getGroupfolderById(int $groupfolderId) {
|
|
|
|
$storageId = $this->getRootFolderStorageId();
|
|
|
|
if (!is_null($storageId)) {
|
|
|
|
return $this->groupfolderFolderManager->getFolder($groupfolderId, $storageId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function getGroupfolderMountById(int $groupfolderId) {
|
|
|
|
return $this->mountProvider->getFolder($groupfolderId, False);
|
|
|
|
}
|
2023-08-08 11:52:21 +02:00
|
|
|
private function checkIfGroupfolderExists(int $groupfolderId): bool {
|
|
|
|
$storageId = $this->getRootFolderStorageId();
|
|
|
|
if ($storageId === null) {
|
|
|
|
return "storage Id null";
|
|
|
|
}
|
|
|
|
|
|
|
|
$folder = $this->groupfolderFolderManager->getFolder($groupfolderId, $storageId);
|
|
|
|
if ($folder === false) {
|
|
|
|
return "Folder does not exist";
|
|
|
|
}
|
2024-01-23 22:02:48 +01:00
|
|
|
|
2023-08-08 11:52:21 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-01-23 22:02:48 +01:00
|
|
|
public function getGroupFolderDirectory(int $groupfolderId, string $relativePath) {
|
|
|
|
$groupFolder = $this->getGroupfolderMountById($groupfolderId);
|
|
|
|
$relativeToDataDirectoryPath = $groupFolder->get($relativePath)->getInternalPath();
|
|
|
|
$minPath = realpath($this->getDataDirectory() . DIRECTORY_SEPARATOR . $groupFolder->getInternalPath() . DIRECTORY_SEPARATOR);
|
|
|
|
$realPath = realpath($this->getDataDirectory() . DIRECTORY_SEPARATOR . $relativeToDataDirectoryPath . DIRECTORY_SEPARATOR);
|
|
|
|
|
|
|
|
// Data directory and groupfolder directory have to actually exist
|
|
|
|
if(!file_exists($minPath) || !file_exists($realPath)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the group subfolder directory always has to start with the groupfolder directory
|
|
|
|
// prevents usage of ".." in relativePath to escape out of groupfolder or even data directory
|
|
|
|
if(!str_starts_with($realPath, $minPath)) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-08 11:52:21 +02:00
|
|
|
|
2024-01-23 22:02:48 +01:00
|
|
|
return $realPath;
|
2023-08-08 11:52:21 +02:00
|
|
|
}
|
|
|
|
|
2024-01-23 22:02:48 +01:00
|
|
|
public function getGroupFolderSnapshotDirectory(int $groupfolderId, string $relativePath, string $snapshotId) {
|
|
|
|
return $this->convertToSnapshotPath($this->getGroupFolderDirectory($groupfolderId, $relativePath), $snapshotId);
|
|
|
|
}
|
2023-08-08 11:52:21 +02:00
|
|
|
}
|