From 5323e45d78d962e93ee31225e2c9052a1edc0565 Mon Sep 17 00:00:00 2001 From: tomasmatus Date: Fri, 3 Jan 2025 12:01:39 +0100 Subject: [PATCH] add cleanup in case chown fails --- src/dialogs/copyPasteOwnership.tsx | 15 +++++++++++++-- test/check-application | 2 -- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/dialogs/copyPasteOwnership.tsx b/src/dialogs/copyPasteOwnership.tsx index 5d7bc484..f24ffbcf 100644 --- a/src/dialogs/copyPasteOwnership.tsx +++ b/src/dialogs/copyPasteOwnership.tsx @@ -41,7 +41,7 @@ async function pasteAsOwner(clipboard: ClipboardInfo, dstPath: string, owner: st await cockpit.spawn([ "cp", "--recursive", - ...clipboard.files.map(file => clipboard.path + file.name), + ...clipboard.files.map(file => clipboard.path + "/" + file.name), dstPath ], { superuser: "try" }); @@ -49,11 +49,22 @@ async function pasteAsOwner(clipboard: ClipboardInfo, dstPath: string, owner: st "chown", "--recursive", owner, - ...clipboard.files.map(file => dstPath + file.name), + ...clipboard.files.map(file => dstPath + "/" + file.name), ], { superuser: "try" }); } catch (err) { const e = err as cockpit.BasicError; addAlert(e.message, AlertVariant.danger, `${new Date().getTime()}`); + + // cleanup potentially copied files in case of "chown" fail + try { + await cockpit.spawn([ + "rm", + "-rf", + ...clipboard.files.map(file => dstPath + "/" + file.name) + ], { superuser: "try" }); + } catch (ex) { + console.warn(`Failed to clean up copied files in ${dstPath}`, ex); + } } } diff --git a/test/check-application b/test/check-application index 01b31306..c6d9faf4 100755 --- a/test/check-application +++ b/test/check-application @@ -1957,8 +1957,6 @@ class TestFiles(testlib.MachineCase): # Copy/paste file m.execute("runuser -u admin mkdir /home/admin/newdir") m.write('/home/admin/newfile', 'test_text\n', owner='admin:admin') - # "hack" test is too quick and UI doesnt reflect the chown done in m.write so it still - # thinks that file owner is root b.wait_in_text("[data-item='newfile'] .item-owner", "admin") b.click("[data-item='newfile']") b.click("#dropdown-menu")