Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mvomiero committed Mar 4, 2024
1 parent 7ee9352 commit 51ffff4
Showing 1 changed file with 9 additions and 22 deletions.
31 changes: 9 additions & 22 deletions cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 51ffff4

Please sign in to comment.