Skip to content

Commit

Permalink
Fix temporary file already exists issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dnestoro committed Nov 4, 2024
1 parent 97e96e6 commit bc7f9af
Showing 1 changed file with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;

public class AgentConfiguration implements Serializable {

private static final String DEFAULT_ACCESS_FILTER_FILE = "/access-filter.json";
private static final String ACCESS_FILTER_PREFIX = "access-filter";
private static final String ACCESS_FILTER_SUFFIX = ".json";
private static final String DEFAULT_ACCESS_FILTER_FILE_LOCATION = "/" + ACCESS_FILTER_PREFIX + ACCESS_FILTER_SUFFIX;

private static final Logger logger = Logger.getGlobal();

private final Collection<String> callerFilterFiles;
private final Collection<String> accessFilterFiles;
private final Boolean builtinCallerFilter;
Expand Down Expand Up @@ -138,36 +144,42 @@ private void addDefaultAccessFilter() {
return;
}

String accessFilterPrefix = "access-filter";
String accessFilterSuffix = ".json";

String tempDir = System.getProperty("java.io.tmpdir");
Path agentDir = Path.of(tempDir).resolve("agent-config");
Path accessFilterFile = agentDir.resolve(accessFilterPrefix + accessFilterSuffix);
Path accessFilterFile = agentDir.resolve(ACCESS_FILTER_PREFIX + ACCESS_FILTER_SUFFIX);
if (Files.exists(accessFilterFile)) {
accessFilterFiles.add(accessFilterFile.toString());
return;
}

try(InputStream accessFilterData = AgentConfiguration.class.getResourceAsStream(DEFAULT_ACCESS_FILTER_FILE)) {
try(InputStream accessFilterData = AgentConfiguration.class.getResourceAsStream(DEFAULT_ACCESS_FILTER_FILE_LOCATION)) {
if (accessFilterData == null) {
throw new IOException("Cannot find access-filter.json on default location: " + DEFAULT_ACCESS_FILTER_FILE);
throw new IOException("Cannot find access-filter.json on default location: " + DEFAULT_ACCESS_FILTER_FILE_LOCATION);
}

try {
Files.createDirectory(agentDir);
} catch (FileAlreadyExistsException e) {
System.out.println("Ignore directory creation because " + agentDir + " directory is already created.");
logger.info("Ignore directory creation because " + agentDir + " directory is already created.");
}

Path tmpAccessFilter = null;
try {
tmpAccessFilter = Files.createTempFile(agentDir, ACCESS_FILTER_PREFIX, ACCESS_FILTER_SUFFIX);
Files.copy(accessFilterData, tmpAccessFilter);
} catch (FileAlreadyExistsException e) {
logger.info("Temporary access-filter file already exists and we should just rename it.");
}

Path tmpAccessFilter = Files.createTempFile(agentDir, accessFilterPrefix, accessFilterSuffix);
Files.copy(accessFilterData, tmpAccessFilter);
if (tmpAccessFilter == null) {
throw new IOException("Cannot create temporary access-filter file.");
}

try {
Files.move(tmpAccessFilter, accessFilterFile, StandardCopyOption.ATOMIC_MOVE);
} catch (FileAlreadyExistsException e) {
Files.delete(tmpAccessFilter);
System.out.println("Access-filter file already exists. Delete temporary one.");
logger.info("Access-filter file already exists. Delete the temporary one.");
}

accessFilterFiles.add(accessFilterFile.toString());
Expand Down

0 comments on commit bc7f9af

Please sign in to comment.