Skip to content

Commit

Permalink
Check for the existence of a file or directory when pasting
Browse files Browse the repository at this point in the history
For now we don't have conflict resolution implemented yet for copy and
paste so disallow pasting when any of the targets exist.
  • Loading branch information
jelly committed May 29, 2024
1 parent 76ec022 commit d617825
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,12 @@ export function * map_permissions<T>(func: (value: number, label: string) => T)
yield func(value, label);
}
}

export function basename(path: string) {
const elements = path.split('/');
if (elements.length === 0) {
return '/';
} else {
return elements[elements.length - 1];
}
}
8 changes: 6 additions & 2 deletions src/fileActions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
} from "pam_user_parser.js";
import { superuser } from "superuser";

import { map_permissions, inode_types } from "./common";
import { map_permissions, inode_types, basename } from "./common";
import { useFilesContext } from "./app";

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

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

const spawnPaste = (sourcePaths, targetPath) => {
if (sourcePaths.some(sourcePath => cwdInfo?.entries[basename(sourcePath)])) {
addAlert(_("Paste not overwriting existing files"), "danger", "paste-error");
return;
}
cockpit.spawn([
"cp",
"-R",
Expand Down
4 changes: 2 additions & 2 deletions src/files-card-body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ const compare = (sortBy) => {

const ContextMenuItems = ({ path, selected, setSelected, clipboard, setClipboard }) => {
const Dialogs = useDialogs();
const { addAlert } = useFilesContext();
const { addAlert, cwdInfo } = useFilesContext();
const menuItems = fileActions(path, selected, setSelected,
clipboard, setClipboard, addAlert, Dialogs);
clipboard, setClipboard, cwdInfo, addAlert, Dialogs);

return (
<MenuList>
Expand Down
2 changes: 1 addition & 1 deletion src/sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export const SidebarPanelDetails = ({

const menuItems = fileActions(path, selected, setSelected,
clipboard, setClipboard,
addAlert, Dialogs).map((option, i) => {
cwdInfo, addAlert, Dialogs).map((option, i) => {
if (option.type === "divider")
return <Divider key={i} />;
return (
Expand Down
16 changes: 14 additions & 2 deletions test/check-application
Original file line number Diff line number Diff line change
Expand Up @@ -924,19 +924,31 @@ class TestFiles(testlib.MachineCase):
b.wait_visible("[data-item='newfile']")
self.assertEqual(m.execute("head -n 1 /home/admin/newdir/newfile"), "test_text\n")
b.click(".breadcrumb-button:nth-of-type(4)")
self.browser.wait_not_present(".pf-c-empty-state")

# Copy/paste directory
self.browser.wait_not_present(".pf-c-empty-state")
m.execute("runuser -u admin mkdir /home/admin/copyDir")
b.click("[data-item='copyDir']")
b.click("#dropdown-menu")
b.click("#copy-item")
b.mouse("[data-item='newdir']", "dblclick")
b.click("#dropdown-menu")
b.click("#paste-item")
b.wait_visible("[data-item='copyDir']")
b.click(".breadcrumb-button:nth-of-type(4)")
self.browser.wait_not_present(".pf-c-empty-state")

# File already exists error
b.click("[data-item='newfile']")
b.click("#dropdown-menu")
b.click("#copy-item")
b.mouse("[data-item='newdir']", "dblclick")
b.click("#dropdown-menu")
b.click("#paste-item")
b.wait_visible("[data-item='newfile']")
self.assertEqual(m.execute("head -n 1 /home/admin/newdir/newfile"), "test_text\n")
b.wait_in_text("h4.pf-v5-c-alert__title", "Paste not overwriting existing files")
b.click(".breadcrumb-button:nth-of-type(4)")
self.browser.wait_not_present(".pf-c-empty-state")

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

0 comments on commit d617825

Please sign in to comment.