Fixing omission in 'acx copy' command where target directory is utterly ignored. #104

This commit is contained in:
Rob Greene 2023-10-26 18:53:09 -05:00
parent 745895d553
commit 1c6bc6238b
2 changed files with 23 additions and 14 deletions

View File

@ -23,9 +23,8 @@ import java.util.List;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.webcodepro.applecommander.storage.Disk; import com.webcodepro.applecommander.storage.*;
import com.webcodepro.applecommander.storage.DiskException; import com.webcodepro.applecommander.util.Name;
import com.webcodepro.applecommander.storage.FormattedDisk;
import com.webcodepro.applecommander.util.filestreamer.FileStreamer; import com.webcodepro.applecommander.util.filestreamer.FileStreamer;
import com.webcodepro.applecommander.util.filestreamer.FileTuple; import com.webcodepro.applecommander.util.filestreamer.FileTuple;
import com.webcodepro.applecommander.util.filestreamer.TypeOfFile; import com.webcodepro.applecommander.util.filestreamer.TypeOfFile;
@ -72,19 +71,29 @@ public class CopyFileCommand extends ReadWriteDiskCommandOptions {
if (files.isEmpty()) { if (files.isEmpty()) {
LOG.warning(() -> String.format("No matches found for %s.", String.join(",", globs))); LOG.warning(() -> String.format("No matches found for %s.", String.join(",", globs)));
} else { } 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; return 0;
} }
private void fileHandler(FileTuple tuple) { private void fileHandler(DirectoryEntry directoryEntry, FileTuple tuple) {
try { try {
FormattedDisk formattedDisk = disk.getFormattedDisks()[0];
if (!recursiveFlag && tuple.fileEntry.isDirectory()) { if (!recursiveFlag && tuple.fileEntry.isDirectory()) {
formattedDisk.createDirectory(tuple.fileEntry.getFilename()); directoryEntry.createDirectory(tuple.fileEntry.getFilename());
} else { } else {
FileUtils copier = new FileUtils(overwriteFlag); FileUtils copier = new FileUtils(overwriteFlag);
copier.copy(formattedDisk, tuple.fileEntry); copier.copy(directoryEntry, tuple.fileEntry);
} }
} catch (DiskException ex) { } catch (DiskException ex) {
LOG.severe(ex.getMessage()); LOG.severe(ex.getMessage());

View File

@ -45,8 +45,8 @@ public class Name {
this.name = path[path.length - 1]; this.name = path[path.length - 1];
} }
public FileEntry getEntry(FormattedDisk formattedDisk) throws DiskException { public FileEntry getEntry(DirectoryEntry directoryEntry) throws DiskException {
List<FileEntry> files = formattedDisk.getFiles(); List<FileEntry> files = directoryEntry.getFiles();
FileEntry entry = null; FileEntry entry = null;
for (int i = 0; i < path.length - 1; i++) { for (int i = 0; i < path.length - 1; i++) {
String dirName = path[i]; String dirName = path[i];
@ -68,11 +68,11 @@ public class Name {
return null; return null;
} }
public FileEntry createEntry(FormattedDisk formattedDisk) throws DiskException { public FileEntry createEntry(DirectoryEntry directoryEntry) throws DiskException {
if (path.length == 1) { if (path.length == 1) {
return formattedDisk.createFile(); return directoryEntry.createFile();
} }
List<FileEntry> files = formattedDisk.getFiles(); List<FileEntry> files = directoryEntry.getFiles();
DirectoryEntry dir = null, parentDir = null; DirectoryEntry dir = null, parentDir = null;
for (int i = 0; i < path.length - 1; i++) { for (int i = 0; i < path.length - 1; i++) {
String dirName = path[i]; String dirName = path[i];
@ -94,7 +94,7 @@ public class Name {
parentDir = dir; parentDir = dir;
} else { } else {
// Add the directory to the root of the filesystem // Add the directory to the root of the filesystem
dir = formattedDisk.createDirectory(dirName); dir = directoryEntry.createDirectory(dirName);
parentDir = dir; parentDir = dir;
} }
} }