Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jun 21, 2024
1 parent cb2fb00 commit 2255d49
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/main/java/org/apache/maven/plugins/clean/Cleaner.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.api.Event;
Expand Down Expand Up @@ -122,7 +121,7 @@ public void delete(
logInfo.log("Deleting " + basedir + (selector != null ? " (" + selector + ")" : ""));
}

Path file = followSymlinks ? basedir : basedir.toAbsolutePath().normalize();
Path file = followSymlinks ? basedir : getCanonicalPath(basedir);

if (selector == null && !followSymlinks && fastDir != null && session != null) {
// If anything wrong happens, we'll just use the usual deletion mechanism
Expand Down Expand Up @@ -221,12 +220,12 @@ private Result delete(

if (isDirectory) {
if (selector == null || selector.couldHoldSelected(pathname)) {
final boolean isSymlink = Files.isSymbolicLink(file);
Path canonical = followSymlinks ? file : file.toAbsolutePath().normalize();
final boolean isSymlink = isSymbolicLink(file);
Path canonical = followSymlinks ? file : getCanonicalPath(file);
if (followSymlinks || !isSymlink) {
String prefix = pathname.length() > 0 ? pathname + File.separatorChar : "";
String prefix = !pathname.isEmpty() ? pathname + File.separatorChar : "";
try (Stream<Path> children = Files.list(canonical)) {
for (Path child : children.collect(Collectors.toList())) {
for (Path child : children.toList()) {
result.update(delete(
child,
prefix + child.getFileName(),
Expand Down Expand Up @@ -262,6 +261,14 @@ private Result delete(
return result;
}

private static Path getCanonicalPath(Path path) {
try {
return path.toRealPath();
} catch (IOException e) {
return getCanonicalPath(path.getParent()).resolve(path.getFileName());
}
}

private boolean isSymbolicLink(Path path) throws IOException {
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
return attrs.isSymbolicLink()
Expand Down

0 comments on commit 2255d49

Please sign in to comment.