From 1c6bc6238b820f9a9bb20c55ced1902ad25ed988 Mon Sep 17 00:00:00 2001 From: Rob Greene Date: Thu, 26 Oct 2023 18:53:09 -0500 Subject: [PATCH] Fixing omission in 'acx copy' command where target directory is utterly ignored. #104 --- .../acx/command/CopyFileCommand.java | 25 +++++++++++++------ .../webcodepro/applecommander/util/Name.java | 12 ++++----- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/app/cli-acx/src/main/java/io/github/applecommander/acx/command/CopyFileCommand.java b/app/cli-acx/src/main/java/io/github/applecommander/acx/command/CopyFileCommand.java index d9d8343..a38d8c4 100644 --- a/app/cli-acx/src/main/java/io/github/applecommander/acx/command/CopyFileCommand.java +++ b/app/cli-acx/src/main/java/io/github/applecommander/acx/command/CopyFileCommand.java @@ -23,9 +23,8 @@ import java.util.List; import java.util.logging.Logger; import java.util.stream.Collectors; -import com.webcodepro.applecommander.storage.Disk; -import com.webcodepro.applecommander.storage.DiskException; -import com.webcodepro.applecommander.storage.FormattedDisk; +import com.webcodepro.applecommander.storage.*; +import com.webcodepro.applecommander.util.Name; import com.webcodepro.applecommander.util.filestreamer.FileStreamer; import com.webcodepro.applecommander.util.filestreamer.FileTuple; import com.webcodepro.applecommander.util.filestreamer.TypeOfFile; @@ -72,19 +71,29 @@ public class CopyFileCommand extends ReadWriteDiskCommandOptions { if (files.isEmpty()) { LOG.warning(() -> String.format("No matches found for %s.", String.join(",", globs))); } else { - files.forEach(this::fileHandler); + DirectoryEntry targetDirectory = disk.getFormattedDisks()[0]; + if (targetPath != null) { + Name name = new Name(targetPath); + FileEntry found = name.getEntry(targetDirectory); + if (found == null || !found.isDirectory()) { + throw new RuntimeException("unable to find directory: " + targetPath); + } + targetDirectory = (DirectoryEntry) found; + } + for (FileTuple tuple : files) { + fileHandler(targetDirectory, tuple); + } } return 0; } - private void fileHandler(FileTuple tuple) { + private void fileHandler(DirectoryEntry directoryEntry, FileTuple tuple) { try { - FormattedDisk formattedDisk = disk.getFormattedDisks()[0]; if (!recursiveFlag && tuple.fileEntry.isDirectory()) { - formattedDisk.createDirectory(tuple.fileEntry.getFilename()); + directoryEntry.createDirectory(tuple.fileEntry.getFilename()); } else { FileUtils copier = new FileUtils(overwriteFlag); - copier.copy(formattedDisk, tuple.fileEntry); + copier.copy(directoryEntry, tuple.fileEntry); } } catch (DiskException ex) { LOG.severe(ex.getMessage()); diff --git a/lib/ac-api/src/main/java/com/webcodepro/applecommander/util/Name.java b/lib/ac-api/src/main/java/com/webcodepro/applecommander/util/Name.java index 3394720..0193f91 100644 --- a/lib/ac-api/src/main/java/com/webcodepro/applecommander/util/Name.java +++ b/lib/ac-api/src/main/java/com/webcodepro/applecommander/util/Name.java @@ -45,8 +45,8 @@ public class Name { this.name = path[path.length - 1]; } - public FileEntry getEntry(FormattedDisk formattedDisk) throws DiskException { - List files = formattedDisk.getFiles(); + public FileEntry getEntry(DirectoryEntry directoryEntry) throws DiskException { + List files = directoryEntry.getFiles(); FileEntry entry = null; for (int i = 0; i < path.length - 1; i++) { String dirName = path[i]; @@ -68,11 +68,11 @@ public class Name { return null; } - public FileEntry createEntry(FormattedDisk formattedDisk) throws DiskException { + public FileEntry createEntry(DirectoryEntry directoryEntry) throws DiskException { if (path.length == 1) { - return formattedDisk.createFile(); + return directoryEntry.createFile(); } - List files = formattedDisk.getFiles(); + List files = directoryEntry.getFiles(); DirectoryEntry dir = null, parentDir = null; for (int i = 0; i < path.length - 1; i++) { String dirName = path[i]; @@ -94,7 +94,7 @@ public class Name { parentDir = dir; } else { // Add the directory to the root of the filesystem - dir = formattedDisk.createDirectory(dirName); + dir = directoryEntry.createDirectory(dirName); parentDir = dir; } }