From 36c1b389cfaf841678e524bffb9e72dad812526b Mon Sep 17 00:00:00 2001 From: Matthieu Ghilain Date: Tue, 10 Dec 2024 14:16:47 +0100 Subject: [PATCH 1/2] Sporadic Gradle failure: access-filter.json already exists (still exist in 0.10.4) #653 --- .../buildtools/agent/AgentConfiguration.java | 51 ++++++++----------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/common/utils/src/main/java/org/graalvm/buildtools/agent/AgentConfiguration.java b/common/utils/src/main/java/org/graalvm/buildtools/agent/AgentConfiguration.java index 4ed13d01..92be22ae 100644 --- a/common/utils/src/main/java/org/graalvm/buildtools/agent/AgentConfiguration.java +++ b/common/utils/src/main/java/org/graalvm/buildtools/agent/AgentConfiguration.java @@ -47,6 +47,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Objects; import java.util.logging.Logger; public class AgentConfiguration implements Serializable { @@ -143,41 +144,29 @@ private void addDefaultAccessFilter() { // this could only happen if we instantiated disabled agent configuration return; } + Path defaultAgentConfigTempFile = createDefaultAgentConfigTempFile(); + try { + Files.writeString(defaultAgentConfigTempFile, getDefaultAgentConfigContent()); + accessFilterFiles.add(defaultAgentConfigTempFile.toString()); + } catch (IOException e) { + throw new RuntimeException("Cannot write default access filter to temporary file "+ defaultAgentConfigTempFile.toAbsolutePath(), e); + } + } - String tempDir = System.getProperty("java.io.tmpdir"); - Path agentDir = Path.of(tempDir).resolve("agent-config"); - Path accessFilterFile = agentDir.resolve(ACCESS_FILTER_PREFIX + ACCESS_FILTER_SUFFIX); - if (Files.exists(accessFilterFile)) { - accessFilterFiles.add(accessFilterFile.toString()); - return; + private static Path createDefaultAgentConfigTempFile() { + try { + return Files.createTempFile(ACCESS_FILTER_PREFIX, ACCESS_FILTER_SUFFIX); + } catch (IOException e) { + throw new RuntimeException("Cannot create temporary file for access filter", e); } + } - try(InputStream accessFilterData = AgentConfiguration.class.getResourceAsStream(DEFAULT_ACCESS_FILTER_FILE_LOCATION)) { - if (accessFilterData == null) { - throw new IOException("Cannot access data from: " + DEFAULT_ACCESS_FILTER_FILE_LOCATION); - } - - try { - Files.createDirectory(agentDir); - } catch (FileAlreadyExistsException e) { - logger.info("Skip creation of directory " + agentDir + " (already created)."); - } - - long pid = ProcessHandle.current().pid(); - long time = System.currentTimeMillis(); - Path tmpAccessFilter = agentDir.resolve(ACCESS_FILTER_PREFIX + '_' + pid + '_' + time + '_' + ACCESS_FILTER_SUFFIX); - Files.copy(accessFilterData, tmpAccessFilter); - - try { - Files.move(tmpAccessFilter, accessFilterFile, StandardCopyOption.ATOMIC_MOVE); - } catch (FileAlreadyExistsException e) { - Files.delete(tmpAccessFilter); - logger.info(accessFilterFile + " already exists. Delete " + tmpAccessFilter); - } - - accessFilterFiles.add(accessFilterFile.toString()); + private static String getDefaultAgentConfigContent() { + try (InputStream accessFilterData = AgentConfiguration.class.getResourceAsStream(DEFAULT_ACCESS_FILTER_FILE_LOCATION)) { + Objects.requireNonNull(accessFilterData, "Cannot access data from: " + DEFAULT_ACCESS_FILTER_FILE_LOCATION); + return new String(accessFilterData.readAllBytes()); } catch (IOException e) { - throw new RuntimeException("Cannot add default access-filter.json" ,e); + throw new RuntimeException("Cannot access default "+DEFAULT_ACCESS_FILTER_FILE_LOCATION+" from the classpath" ,e); } } From 89c66bf7503193323d54f3abc10f3025873f0868 Mon Sep 17 00:00:00 2001 From: Matthieu Ghilain Date: Tue, 10 Dec 2024 16:08:49 +0100 Subject: [PATCH 2/2] Delete file on exit --- .../java/org/graalvm/buildtools/agent/AgentConfiguration.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/common/utils/src/main/java/org/graalvm/buildtools/agent/AgentConfiguration.java b/common/utils/src/main/java/org/graalvm/buildtools/agent/AgentConfiguration.java index 92be22ae..0adf59a3 100644 --- a/common/utils/src/main/java/org/graalvm/buildtools/agent/AgentConfiguration.java +++ b/common/utils/src/main/java/org/graalvm/buildtools/agent/AgentConfiguration.java @@ -155,7 +155,9 @@ private void addDefaultAccessFilter() { private static Path createDefaultAgentConfigTempFile() { try { - return Files.createTempFile(ACCESS_FILTER_PREFIX, ACCESS_FILTER_SUFFIX); + Path tempFile = Files.createTempFile(ACCESS_FILTER_PREFIX, ACCESS_FILTER_SUFFIX); + tempFile.toFile().deleteOnExit(); + return tempFile; } catch (IOException e) { throw new RuntimeException("Cannot create temporary file for access filter", e); }