2023-08-08 11:52:21 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace OCA\GroupfolderFilesystemSnapshots\Manager;
|
|
|
|
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\Files\IRootFolder;
|
2024-04-23 19:51:41 +02:00
|
|
|
|
2023-08-08 11:52:21 +02:00
|
|
|
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
|
|
|
|
2024-04-23 19:51:41 +02:00
|
|
|
use OCA\GroupfolderFilesystemSnapshots\Service\SettingsService;
|
2024-01-23 22:02:48 +01:00
|
|
|
|
2024-04-23 19:51:41 +02:00
|
|
|
class PathManager {
|
2024-01-23 22:02:48 +01:00
|
|
|
|
|
|
|
public function __construct(
|
2024-04-23 19:51:41 +02:00
|
|
|
private IConfig $config,
|
|
|
|
private IRootFolder $rootFolder,
|
|
|
|
private FolderManager $groupfolderFolderManager,
|
|
|
|
private MountProvider $mountProvider,
|
|
|
|
private SettingsService $settingsService,
|
|
|
|
){}
|
|
|
|
|
|
|
|
public function getFilesystemMountpointPath() {
|
|
|
|
return $this->settingsService->getAppValue("filesystem_mountpoint_path");
|
2024-01-23 22:02:48 +01:00
|
|
|
}
|
2023-08-08 11:52:21 +02:00
|
|
|
|
2024-04-23 19:51:41 +02:00
|
|
|
public function getFilesystemSnapshotsPath() {
|
|
|
|
return $this->settingsService->getAppValue("filesystem_snapshots_path");
|
2024-01-23 22:02:48 +01:00
|
|
|
}
|
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) {
|
2024-04-23 19:51:41 +02:00
|
|
|
return realpath($this->getFilesystemSnapshotsPath() . DIRECTORY_SEPARATOR . $snapshotId . DIRECTORY_SEPARATOR);
|
2024-01-23 22:02:48 +01:00
|
|
|
}
|
2023-08-08 11:52:21 +02:00
|
|
|
|
2024-01-23 22:02:48 +01:00
|
|
|
public function convertToSnapshotPath(string $filesystemPath, string $snapshotId) {
|
2024-04-23 19:51:41 +02:00
|
|
|
$filesystemMountpointPath = $this->getFilesystemMountpointPath();
|
2024-01-23 22:02:48 +01:00
|
|
|
$filesystemPath = realpath($filesystemPath);
|
2023-08-08 11:52:21 +02:00
|
|
|
|
2024-04-23 19:51:41 +02:00
|
|
|
if($filesystemMountpointPath == '') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!str_starts_with($filesystemPath, $filesystemMountpointPath)) {
|
2024-01-23 22:02:48 +01:00
|
|
|
return false;
|
|
|
|
}
|
2023-08-08 11:52:21 +02:00
|
|
|
|
2024-04-23 19:51:41 +02:00
|
|
|
return str_replace($filesystemMountpointPath, $this->getSnapshotPath($snapshotId) . DIRECTORY_SEPARATOR, $filesystemPath);
|
2024-01-23 22:02:48 +01:00
|
|
|
}
|
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
|
|
|
}
|