Skip to content

Commit

Permalink
Update IOUtils.java
Browse files Browse the repository at this point in the history
Refactor code around file system to synchronously access file system when using JAR.

Fixes grails/grails-core#12589
  • Loading branch information
puneetbehl committed Jun 28, 2022
1 parent 26a5273 commit 583948f
Showing 1 changed file with 44 additions and 22 deletions.
66 changes: 44 additions & 22 deletions core/src/main/java/io/micronaut/core/io/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.ClosedFileSystemException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemAlreadyExistsException;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
Expand Down Expand Up @@ -82,39 +84,59 @@ public static void eachFile(@NonNull URI uri, String path, @NonNull Consumer<Pat

try {
if ("jar".equals(scheme)) {
try {
fileSystem = FileSystems.getFileSystem(uri);
} catch (FileSystemNotFoundException e) {
fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap());
synchronized (IOUtils.class) {
try {
fileSystem = FileSystems.getFileSystem(uri);
} catch (FileSystemNotFoundException e) {
//no-op
}
if (fileSystem == null || !fileSystem.isOpen()) {
try {
fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap());
} catch (FileSystemAlreadyExistsException e) {
fileSystem = FileSystems.getFileSystem(uri);
}
}
try {
walkFiles(consumer, fileSystem.getPath(path));
} finally {
if (fileSystem != null && fileSystem.isOpen()) {
try {
fileSystem.close();
} catch (ClosedFileSystemException e) {
// no-op, because it is already closed
}
}
}
}
myPath = fileSystem.getPath(path);

} else if ("file".equals(scheme)) {
myPath = Paths.get(uri).resolve(path);
walkFiles(consumer, Paths.get(uri).resolve(path));
} else {
// graal resource: case
myPath = Paths.get(uri);
walkFiles(consumer, Paths.get(uri));
}
} catch (FileSystemNotFoundException e) {
myPath = null;
// no-op, can't log because class is used in compiler
}

if (myPath != null) {
try (Stream<Path> walk = Files.walk(myPath, 1)) {
for (Iterator<Path> it = walk.iterator(); it.hasNext();) {
final Path currentPath = it.next();
if (currentPath.equals(myPath) || Files.isHidden(currentPath) || currentPath.getFileName().startsWith(".")) {
continue;
}
consumer.accept(currentPath);
}
} finally {
if (fileSystem != null && fileSystem.isOpen()) {
fileSystem.close();

} catch (IOException e) {
// ignore, can't do anything here and can't log because class used in compiler
}
}

private static void walkFiles(Consumer<Path> consumer, Path myPath) throws IOException {
if (myPath != null) {
try (Stream<Path> walk = Files.walk(myPath, 1)) {
for (Iterator<Path> it = walk.iterator(); it.hasNext();) {
final Path currentPath = it.next();
if (currentPath.equals(myPath) || Files.isHidden(currentPath) || currentPath.getFileName().startsWith(".")) {
continue;
}
consumer.accept(currentPath);
}
}
} catch (IOException e) {
// ignore, can't do anything here and can't log because class used in compiler
}
}

Expand Down

0 comments on commit 583948f

Please sign in to comment.