Skip to content

Commit

Permalink
Fix static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
wiwa committed Feb 1, 2023
1 parent 0fa20a1 commit b47177d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
import java.util.logging.Logger;

// Utility for concurrent move/copy/link of files
public class FileAccessUtils {
public final class FileAccessUtils {
// singleton class with only static methods
private FileAccessUtils() {}

private static final Logger logger = Logger.getLogger(FileAccessUtils.class.getName());

private static final ConcurrentHashMap<Path, EasyMonitor> fileLocks = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<Path, PathLock> fileLocks = new ConcurrentHashMap<>();

// Used here for locking "files"
private static class EasyMonitor {
public EasyMonitor() {}
private static class PathLock {
// Not used elsewhere
private PathLock() {}
}

/**
Expand Down Expand Up @@ -130,7 +134,7 @@ public static void linkFile(Path from, Path to) throws IOException {
*/
public static void deleteFileIfExists(Path toDelete) throws IOException {
Path absTo = toDelete.toAbsolutePath();
EasyMonitor toLock = fileLock(absTo);
PathLock toLock = fileLock(absTo);
synchronized (toLock) {
try {
Files.deleteIfExists(absTo);
Expand All @@ -148,7 +152,7 @@ public static void deleteFileIfExists(Path toDelete) throws IOException {
* <p>It is up to the write operation to specify whether or not to overwrite existing files.
*/
private static IOException writeFileSafe(Path absTo, Supplier<IOException> writeOp) {
EasyMonitor toLock = fileLock(absTo);
PathLock toLock = fileLock(absTo);
synchronized (toLock) {
try {
// If 'absTo' is a symlink, checks if its target file exists
Expand All @@ -164,7 +168,7 @@ private static IOException writeFileSafe(Path absTo, Supplier<IOException> write
}

// "Logical" file lock
private static EasyMonitor fileLock(Path writeTo) {
return fileLocks.computeIfAbsent(writeTo, k -> new EasyMonitor());
private static PathLock fileLock(Path writeTo) {
return fileLocks.computeIfAbsent(writeTo, k -> new PathLock());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ public PersistentWorker create(WorkerKey workerKey) throws Exception {
StringBuilder initArgs = new StringBuilder();
for (String s : workerKey.getCmd()) {
initArgs.append(s);
initArgs.append("\n");
initArgs.append('\n');
}
for (String s : workerKey.getArgs()) {
initArgs.append(s);
initArgs.append("\n");
initArgs.append('\n');
}

Files.write(initArgsLogFile, initArgs.toString().getBytes());
Expand Down Expand Up @@ -178,17 +178,16 @@ public ResponseCtx postWorkCleanup(
private IOException logBadCleanup(RequestCtx request, IOException e) {
WorkFilesContext context = request.filesContext;

StringBuilder sb = new StringBuilder();
sb.append(
"Output files failure debug for request with args<"
+ request.request.getArgumentsList()
+ ">:\n");
sb.append("getOutputPathsList:\n");
sb.append(context.outputPaths);
sb.append("getOutputFilesList:\n");
sb.append(context.outputFiles);
sb.append("getOutputDirectoriesList:\n");
sb.append(context.outputDirectories);
StringBuilder sb = new StringBuilder(122);
sb.append("Output files failure debug for request with args<")
.append(request.request.getArgumentsList())
.append(">:\ngetOutputPathsList:\n")
.append(context.outputPaths)
.append("getOutputFilesList:\n")
.append(context.outputFiles)
.append("getOutputDirectoriesList:\n")
.append(context.outputDirectories);

logger.severe(sb.toString());

e.printStackTrace();
Expand Down
16 changes: 0 additions & 16 deletions src/main/java/build/buildfarm/worker/persistent/WorkerInputs.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import persistent.common.util.Args;

public class WorkerInputs {
private static final Logger logger = Logger.getLogger(WorkerInputs.class.getName());
Expand Down Expand Up @@ -114,17 +111,4 @@ public static WorkerInputs from(WorkFilesContext workFilesContext, List<String>

return new WorkerInputs(workFilesContext.opRoot, absToolInputs, toolInputs, pathInputs);
}

private static List<Path> argsFiles(Path opRoot, List<String> reqArgs) {
List<Path> files = new ArrayList<>();
for (String a : reqArgs) {
if (Args.isArgsFile(a)) {
try {
files.add(opRoot.resolve(Paths.get(a.substring(1))));
} catch (Exception ignored) {
}
}
}
return files;
}
}

0 comments on commit b47177d

Please sign in to comment.