Skip to content

Commit

Permalink
Extract FileUtils updates by @ericvergnaud in antlr#4027 then he can …
Browse files Browse the repository at this point in the history
…rebase after we merge this into dev. All tests pass locally (didn't check python2 actually but python3 works).

Signed-off-by: Terence Parr <[email protected]>
  • Loading branch information
parrt committed Dec 20, 2022
1 parent 539ffaf commit d98378c
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions runtime-testsuite/test/org/antlr/v4/test/runtime/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@

import org.antlr.v4.runtime.misc.Utils;

import java.io.*;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.DosFileAttributes;

import static org.antlr.v4.test.runtime.RuntimeTestUtils.FileSeparator;

Expand Down Expand Up @@ -55,7 +61,7 @@ public static void mkdir(String dir) {
}

public static void deleteDirectory(File f) throws IOException {
if (f.isDirectory()) {
if (f.isDirectory() && !isLink(f.toPath())) {
File[] files = f.listFiles();
if (files != null) {
for (File c : files)
Expand All @@ -65,4 +71,13 @@ public static void deleteDirectory(File f) throws IOException {
if (!f.delete())
throw new IOException("Failed to delete file: " + f);
}

public static boolean isLink(Path path) throws IOException {
try {
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
return attrs.isSymbolicLink() || (attrs instanceof DosFileAttributes && attrs.isOther());
} catch (IOException ignored) {
return false;
}
}
}

1 comment on commit d98378c

@ericvergnaud
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parrt blessed

Please sign in to comment.