Add permission update jobs

This commit is contained in:
Mikei386
2026-06-23 14:35:33 +02:00
parent 69367fc0d8
commit 1e20799499
8 changed files with 137 additions and 9 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
set -eu set -eu
PLUGIN="u-navigator" PLUGIN="u-navigator"
VERSION="2026.06.23.r037" VERSION="2026.06.23.r038"
PKG_DIR="packages" PKG_DIR="packages"
WORK_DIR=".plugin-build" WORK_DIR=".plugin-build"
PKG_NAME="${PLUGIN}-${VERSION}.tgz" PKG_NAME="${PLUGIN}-${VERSION}.tgz"
Binary file not shown.
Binary file not shown.
@@ -211,6 +211,33 @@ function unav_guard_destination(string $source, string $destination): void {
} }
} }
function unav_permission_options(array $data): array {
$mode = trim((string)($data['mode'] ?? ''));
$owner = trim((string)($data['owner'] ?? ''));
$group = trim((string)($data['group'] ?? ''));
$recursive = filter_var($data['recursive'] ?? false, FILTER_VALIDATE_BOOLEAN);
if ($mode === '' && $owner === '' && $group === '') {
unav_error(400, 'No permission changes requested');
}
if ($mode !== '' && !preg_match('/^[0-7]{3,4}$/', $mode)) {
unav_error(400, 'Invalid permission mode');
}
if ($owner !== '' && !preg_match('/^[A-Za-z0-9._-]+$/', $owner)) {
unav_error(400, 'Invalid owner');
}
if ($group !== '' && !preg_match('/^[A-Za-z0-9._-]+$/', $group)) {
unav_error(400, 'Invalid group');
}
return [
'mode' => $mode === '' ? null : $mode,
'owner' => $owner === '' ? null : $owner,
'group' => $group === '' ? null : $group,
'recursive' => $recursive,
];
}
function unav_job_dir(): string { function unav_job_dir(): string {
if (!is_dir(UNAV_JOB_DIR) && !mkdir(UNAV_JOB_DIR, 0777, true) && !is_dir(UNAV_JOB_DIR)) { if (!is_dir(UNAV_JOB_DIR) && !mkdir(UNAV_JOB_DIR, 0777, true) && !is_dir(UNAV_JOB_DIR)) {
unav_error(500, 'Could not create job directory'); unav_error(500, 'Could not create job directory');
@@ -6,17 +6,21 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
} }
$action = $_GET['action'] ?? ''; $action = $_GET['action'] ?? '';
if (!in_array($action, ['move', 'copy', 'size'], true)) { if (!in_array($action, ['move', 'copy', 'size', 'permissions'], true)) {
unav_error(404, 'Unknown job action'); unav_error(404, 'Unknown job action');
} }
$data = unav_request_data(); $data = unav_request_data();
$source = unav_existing_path($data['source'] ?? null); $source = unav_existing_path($data['source'] ?? null);
$destination = null; $destination = null;
$permissions = null;
if (in_array($action, ['move', 'copy'], true)) { if (in_array($action, ['move', 'copy'], true)) {
unav_assert_writable(); unav_assert_writable();
$destination = unav_destination_path($data['destination'] ?? null); $destination = unav_destination_path($data['destination'] ?? null);
unav_guard_destination($source['path'], $destination['path']); unav_guard_destination($source['path'], $destination['path']);
} elseif ($action === 'permissions') {
unav_assert_writable();
$permissions = unav_permission_options($data);
} }
$id = str_replace('.', '', uniqid($action . '-', true)); $id = str_replace('.', '', uniqid($action . '-', true));
@@ -30,6 +34,7 @@ $job = [
'error' => null, 'error' => null,
'source' => $source['path'], 'source' => $source['path'],
'destination' => $destination['path'] ?? null, 'destination' => $destination['path'] ?? null,
'permissions' => $permissions,
'createdAt' => $now, 'createdAt' => $now,
'updatedAt' => $now, 'updatedAt' => $now,
]; ];
@@ -19,6 +19,8 @@ function unav_run_job(string $id): void {
} elseif ($job['type'] === 'size') { } elseif ($job['type'] === 'size') {
$result = unav_worker_size($job['source'], $job); $result = unav_worker_size($job['source'], $job);
$job['result'] = $result; $job['result'] = $result;
} elseif ($job['type'] === 'permissions') {
unav_worker_permissions($job['source'], $job['permissions'] ?? [], $job);
} else { } else {
throw new RuntimeException('Unknown job action'); throw new RuntimeException('Unknown job action');
} }
@@ -120,6 +122,57 @@ function unav_worker_size_recursive(string $path): array {
return $total; return $total;
} }
function unav_worker_permissions(string $source, array $options, array &$job): void {
$sourceReal = unav_worker_guard_source($source);
$mode = $options['mode'] ?? null;
$owner = $options['owner'] ?? null;
$group = $options['group'] ?? null;
$recursive = filter_var($options['recursive'] ?? false, FILTER_VALIDATE_BOOLEAN);
if ($mode === null && $owner === null && $group === null) {
throw new RuntimeException('No permission changes requested');
}
unav_worker_update($job, 'running', 10, 'Updating permissions');
$count = 0;
unav_worker_permissions_apply($sourceReal, $mode, $owner, $group, $recursive, $job, $count);
$job['result'] = ['changed' => $count];
unav_worker_update($job, 'running', 95, 'Permissions updated');
}
function unav_worker_permissions_apply(string $path, ?string $mode, ?string $owner, ?string $group, bool $recursive, array &$job, int &$count): void {
if (is_link($path)) {
return;
}
if (!file_exists($path)) {
throw new RuntimeException('Path not found');
}
if ($mode !== null && !chmod($path, intval($mode, 8))) {
throw new RuntimeException('Could not change permissions');
}
if ($owner !== null && !chown($path, $owner)) {
throw new RuntimeException('Could not change owner');
}
if ($group !== null && !chgrp($path, $group)) {
throw new RuntimeException('Could not change group');
}
$count++;
if ($count % 100 === 0) {
unav_worker_update($job, 'running', 50, 'Updating permissions');
}
if (!$recursive || !is_dir($path)) {
return;
}
foreach (scandir($path) ?: [] as $entry) {
if ($entry === '.' || $entry === '..') {
continue;
}
unav_worker_permissions_apply($path . '/' . $entry, $mode, $owner, $group, true, $job, $count);
}
}
function unav_worker_copy(string $source, string $destination, array &$job): void { function unav_worker_copy(string $source, string $destination, array &$job): void {
if (is_link($source)) { if (is_link($source)) {
throw new RuntimeException('Copying symlinks is not allowed'); throw new RuntimeException('Copying symlinks is not allowed');
+46 -3
View File
@@ -343,7 +343,13 @@ function renderExplorer(body, win) {
row.addEventListener('contextmenu', (event) => { row.addEventListener('contextmenu', (event) => {
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
if (selectedPathSet(win).has(entry.path)) {
state.selectedEntry = entry;
focusWindow(win);
updatePropertiesWindows();
} else {
selectEntry(win, entry, null, event); selectEntry(win, entry, null, event);
}
showContextMenu(event, win, entry); showContextMenu(event, win, entry);
}); });
} }
@@ -875,15 +881,22 @@ async function handleDrop(event, win, targetPath) {
} }
} }
async function createJob(type, source, destination, refreshWindowIds = []) { async function createJob(type, source, destination = null, refreshWindowIds = [], extra = {}) {
if (['move', 'copy'].includes(type) && isReadOnly()) { if (['move', 'copy', 'permissions'].includes(type) && isReadOnly()) {
showError(new Error('Read-only mode ist aktiv.')); showError(new Error('Read-only mode ist aktiv.'));
return null; return null;
} }
recordDebug('job.create.request', { type, source, destination, refreshWindowIds }); recordDebug('job.create.request', { type, source, destination, refreshWindowIds, extra });
const body = new URLSearchParams(); const body = new URLSearchParams();
body.set('source', source); body.set('source', source);
if (destination !== null) {
body.set('destination', destination); body.set('destination', destination);
}
for (const [key, value] of Object.entries(extra)) {
if (value !== null && value !== undefined) {
body.set(key, String(value));
}
}
appendCsrf(body); appendCsrf(body);
const job = await api(apiUrl(`job.php?action=${encodeURIComponent(type)}`), { const job = await api(apiUrl(`job.php?action=${encodeURIComponent(type)}`), {
method: 'POST', method: 'POST',
@@ -999,6 +1012,35 @@ async function promptTransfer(win, entry, type) {
await createJob(type, entry.path, `${targetDir.replace(/\/$/, '')}/${entry.name}`, []); await createJob(type, entry.path, `${targetDir.replace(/\/$/, '')}/${entry.name}`, []);
} }
async function promptPermissions(win, entry) {
if (isReadOnly()) {
showError(new Error('Read-only mode ist aktiv.'));
return;
}
const entries = selectedPathSet(win).has(entry.path) ? selectedEntries(win) : [entry];
const mode = prompt('Berechtigungen als Oktalmodus, z.B. 0644 oder 0755', entry.permissions ? `0${entry.permissions}`.slice(-4) : '');
if (mode === null) return;
const owner = prompt('Besitzer ändern, leer lassen für unverändert', entry.owner || '');
if (owner === null) return;
const group = prompt('Gruppe ändern, leer lassen für unverändert', entry.group || '');
if (group === null) return;
const hasDirectory = entries.some((item) => item.type === 'directory');
const recursive = hasDirectory && confirm('Rekursiv auf Unterordner und Dateien anwenden?');
const options = {
mode: mode.trim(),
owner: owner.trim(),
group: group.trim(),
recursive: recursive ? '1' : '0'
};
if (!options.mode && !options.owner && !options.group) {
showError(new Error('Keine Änderung angegeben.'));
return;
}
for (const item of entries) {
await createJob('permissions', item.path, null, [win.id], options);
}
}
function showPropertiesForEntry(win, entry) { function showPropertiesForEntry(win, entry) {
selectEntry(win, entry); selectEntry(win, entry);
openProperties(entry); openProperties(entry);
@@ -1034,6 +1076,7 @@ function renderContextMenu() {
actions.push(['Eigenschaften', () => showPropertiesForEntry(win, entry)]); actions.push(['Eigenschaften', () => showPropertiesForEntry(win, entry)]);
if (!isReadOnly()) { if (!isReadOnly()) {
actions.push(['Umbenennen', () => renameEntry(win, entry)]); actions.push(['Umbenennen', () => renameEntry(win, entry)]);
actions.push(['Berechtigungen...', () => promptPermissions(win, entry)]);
actions.push(['Kopieren nach...', () => promptTransfer(win, entry, 'copy')]); actions.push(['Kopieren nach...', () => promptTransfer(win, entry, 'copy')]);
actions.push(['Verschieben nach...', () => promptTransfer(win, entry, 'move')]); actions.push(['Verschieben nach...', () => promptTransfer(win, entry, 'move')]);
} }
+2 -2
View File
@@ -1,7 +1,7 @@
<!DOCTYPE PLUGIN [ <!DOCTYPE PLUGIN [
<!ENTITY name "u-navigator"> <!ENTITY name "u-navigator">
<!ENTITY author "Michael Roll"> <!ENTITY author "Michael Roll">
<!ENTITY version "2026.06.23.r037"> <!ENTITY version "2026.06.23.r038">
<!ENTITY package "&name;-&version;.tgz"> <!ENTITY package "&name;-&version;.tgz">
<!ENTITY pluginURL "https://git.casaderoll.de/michael/Unraid-Navigator/raw/branch/main/u-navigator.plg"> <!ENTITY pluginURL "https://git.casaderoll.de/michael/Unraid-Navigator/raw/branch/main/u-navigator.plg">
<!ENTITY support "https://git.casaderoll.de/michael/Unraid-Navigator"> <!ENTITY support "https://git.casaderoll.de/michael/Unraid-Navigator">
@@ -24,7 +24,7 @@
<FILE Name="/boot/config/plugins/&name;/&package;"> <FILE Name="/boot/config/plugins/&name;/&package;">
<URL>https://git.casaderoll.de/michael/Unraid-Navigator/raw/branch/main/packages/&package;</URL> <URL>https://git.casaderoll.de/michael/Unraid-Navigator/raw/branch/main/packages/&package;</URL>
<MD5>6ae4016e68bf99a8cb0c64714b8cb647</MD5> <MD5>87661843082c0f41bd627a315278e346</MD5>
</FILE> </FILE>
<FILE Run="/bin/bash"> <FILE Run="/bin/bash">