Skip to content

Commit d617825

Browse files
committed
Check for the existence of a file or directory when pasting
For now we don't have conflict resolution implemented yet for copy and paste so disallow pasting when any of the targets exist.
1 parent 76ec022 commit d617825

File tree

5 files changed

+32
-7
lines changed

5 files changed

+32
-7
lines changed

src/common.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,12 @@ export function * map_permissions<T>(func: (value: number, label: string) => T)
5151
yield func(value, label);
5252
}
5353
}
54+
55+
export function basename(path: string) {
56+
const elements = path.split('/');
57+
if (elements.length === 0) {
58+
return '/';
59+
} else {
60+
return elements[elements.length - 1];
61+
}
62+
}

src/fileActions.jsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
} from "pam_user_parser.js";
4141
import { superuser } from "superuser";
4242

43-
import { map_permissions, inode_types } from "./common";
43+
import { map_permissions, inode_types, basename } from "./common";
4444
import { useFilesContext } from "./app";
4545

4646
const _ = cockpit.gettext;
@@ -467,11 +467,15 @@ const downloadFile = (currentPath, selected) => {
467467
window.open(`${prefix}?${query}`);
468468
};
469469

470-
export const fileActions = (path, selected, setSelected, clipboard, setClipboard, addAlert, Dialogs) => {
470+
export const fileActions = (path, selected, setSelected, clipboard, setClipboard, cwdInfo, addAlert, Dialogs) => {
471471
const currentPath = path.join("/") + "/";
472472
const menuItems = [];
473473

474474
const spawnPaste = (sourcePaths, targetPath) => {
475+
if (sourcePaths.some(sourcePath => cwdInfo?.entries[basename(sourcePath)])) {
476+
addAlert(_("Paste not overwriting existing files"), "danger", "paste-error");
477+
return;
478+
}
475479
cockpit.spawn([
476480
"cp",
477481
"-R",

src/files-card-body.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ const compare = (sortBy) => {
8080

8181
const ContextMenuItems = ({ path, selected, setSelected, clipboard, setClipboard }) => {
8282
const Dialogs = useDialogs();
83-
const { addAlert } = useFilesContext();
83+
const { addAlert, cwdInfo } = useFilesContext();
8484
const menuItems = fileActions(path, selected, setSelected,
85-
clipboard, setClipboard, addAlert, Dialogs);
85+
clipboard, setClipboard, cwdInfo, addAlert, Dialogs);
8686

8787
return (
8888
<MenuList>

src/sidebar.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export const SidebarPanelDetails = ({
123123

124124
const menuItems = fileActions(path, selected, setSelected,
125125
clipboard, setClipboard,
126-
addAlert, Dialogs).map((option, i) => {
126+
cwdInfo, addAlert, Dialogs).map((option, i) => {
127127
if (option.type === "divider")
128128
return <Divider key={i} />;
129129
return (

test/check-application

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,19 +924,31 @@ class TestFiles(testlib.MachineCase):
924924
b.wait_visible("[data-item='newfile']")
925925
self.assertEqual(m.execute("head -n 1 /home/admin/newdir/newfile"), "test_text\n")
926926
b.click(".breadcrumb-button:nth-of-type(4)")
927+
self.browser.wait_not_present(".pf-c-empty-state")
927928

928929
# Copy/paste directory
929-
self.browser.wait_not_present(".pf-c-empty-state")
930930
m.execute("runuser -u admin mkdir /home/admin/copyDir")
931+
b.click("[data-item='copyDir']")
932+
b.click("#dropdown-menu")
933+
b.click("#copy-item")
934+
b.mouse("[data-item='newdir']", "dblclick")
935+
b.click("#dropdown-menu")
936+
b.click("#paste-item")
937+
b.wait_visible("[data-item='copyDir']")
938+
b.click(".breadcrumb-button:nth-of-type(4)")
939+
self.browser.wait_not_present(".pf-c-empty-state")
940+
941+
# File already exists error
931942
b.click("[data-item='newfile']")
932943
b.click("#dropdown-menu")
933944
b.click("#copy-item")
934945
b.mouse("[data-item='newdir']", "dblclick")
935946
b.click("#dropdown-menu")
936947
b.click("#paste-item")
937948
b.wait_visible("[data-item='newfile']")
938-
self.assertEqual(m.execute("head -n 1 /home/admin/newdir/newfile"), "test_text\n")
949+
b.wait_in_text("h4.pf-v5-c-alert__title", "Paste not overwriting existing files")
939950
b.click(".breadcrumb-button:nth-of-type(4)")
951+
self.browser.wait_not_present(".pf-c-empty-state")
940952

941953
@testlib.skipBrowser(".upload_files() doesn't work on Firefox", "firefox")
942954
@testlib.skipImage("Debian-testing has Cockpit 311 without new upload support in fsreplace1", "debian-testing")

0 commit comments

Comments
 (0)