From 51ffff4e9480338c95b2d52d60048c1959024b4b Mon Sep 17 00:00:00 2001 From: Marco Vomiero Date: Mon, 4 Mar 2024 15:48:51 +0100 Subject: [PATCH] code refactoring --- .../devonfw/tools/ide/io/FileAccessImpl.java | 31 ++++++------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java b/cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java index b5b39966b..eaa02a1ff 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java +++ b/cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java @@ -228,7 +228,10 @@ private boolean isJunction(Path path) { try { BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); return attr.isOther() && attr.isDirectory(); + } catch (NoSuchFileException e) { + return false; // file doesn't exist } catch (IOException e) { + // errors in reading the attributes of the file throw new IllegalStateException( "An unexpected error occurred whilst checking if the file: " + path + " is a junction", e); } @@ -240,6 +243,7 @@ public void backup(Path fileOrFolder) { if (Files.isSymbolicLink(fileOrFolder) || isJunction(fileOrFolder)) { delete(fileOrFolder); } else { + // fileOrFolder is a directory Path backupPath = this.context.getIdeHome().resolve(IdeContext.FOLDER_UPDATES).resolve(IdeContext.FOLDER_BACKUPS); LocalDateTime now = LocalDateTime.now(); String date = DateTimeUtil.formatDate(now); @@ -321,31 +325,14 @@ private void copyRecursive(Path source, Path target, FileCopyMode mode) throws I */ private void deleteLinkIfExists(Path path) throws IOException { - boolean exists = false; - boolean isJunction = false; - if (this.context.getSystemInfo().isWindows()) { - try { // since broken junctions are not detected by Files.exists(brokenJunction) - BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); - exists = true; - isJunction = attr.isOther() && attr.isDirectory(); - } catch (NoSuchFileException e) { - // ignore, since there is no previous file at the location, so nothing to delete - return; - } - } - exists = exists || Files.exists(path); - boolean isSymlink = exists && Files.isSymbolicLink(path); + boolean isJunction = isJunction(path); // since broken junctions are not detected by Files.exists() + boolean isSymlink = Files.exists(path) && Files.isSymbolicLink(path); assert !(isSymlink && isJunction); - if (exists) { - if (isJunction || isSymlink) { - this.context.info("Deleting previous " + (isJunction ? "junction" : "symlink") + " at " + path); - Files.delete(path); - } else { - throw new IllegalStateException( - "The file at " + path + " was not deleted since it is not a symlink or a Windows junction"); - } + if (isJunction || isSymlink) { + this.context.info("Deleting previous " + (isJunction ? "junction" : "symlink") + " at " + path); + Files.delete(path); } }