Skip to content

Commit 73ca4aa

Browse files
committed
fix: simplify cargo metadata execution and remove threading
1 parent 84882ee commit 73ca4aa

1 file changed

Lines changed: 20 additions & 42 deletions

File tree

src/main/java/io/github/guacsec/trustifyda/providers/CargoProvider.java

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import io.github.guacsec.trustifyda.tools.Operations;
3838
import io.github.guacsec.trustifyda.utils.IgnorePatternDetector;
3939
import java.io.IOException;
40+
import java.io.InputStream;
4041
import java.nio.charset.StandardCharsets;
4142
import java.nio.file.Files;
4243
import java.nio.file.Path;
@@ -143,59 +144,36 @@ private CargoMetadata executeCargoMetadata() throws IOException, InterruptedExce
143144
log.info("Timeout: " + TIMEOUT + " seconds");
144145
}
145146

146-
ProcessBuilder pb = new ProcessBuilder(cargoExecutable, "metadata", "--format-version", "1");
147-
pb.directory(workingDir.toFile());
148-
Process process = pb.start();
149-
150-
final StringBuilder outputBuilder = new StringBuilder();
151-
final Exception[] readException = {null};
152-
153-
Thread readerThread =
154-
new Thread(
155-
() -> {
156-
try (var reader =
157-
new java.io.BufferedReader(
158-
new java.io.InputStreamReader(
159-
process.getInputStream(), StandardCharsets.UTF_8))) {
160-
String line;
161-
while ((line = reader.readLine()) != null) {
162-
outputBuilder.append(line).append('\n');
163-
}
164-
} catch (IOException e) {
165-
readException[0] = e;
166-
}
167-
});
168-
readerThread.setDaemon(true);
169-
readerThread.start();
147+
Process process =
148+
new ProcessBuilder(cargoExecutable, "metadata", "--format-version", "1")
149+
.directory(workingDir.toFile())
150+
.redirectError(ProcessBuilder.Redirect.DISCARD)
151+
.start();
152+
153+
String output;
154+
try (InputStream is = process.getInputStream()) {
155+
output = new String(is.readAllBytes(), StandardCharsets.UTF_8);
156+
}
170157

171158
boolean finished = process.waitFor(TIMEOUT, TimeUnit.SECONDS);
172159

173160
if (!finished) {
174161
process.destroyForcibly();
175-
try {
176-
process.waitFor(5, TimeUnit.SECONDS);
177-
} catch (InterruptedException ignored) {
178-
}
179-
readerThread.interrupt();
180162
throw new InterruptedException("cargo metadata timed out after " + TIMEOUT + " seconds");
181163
}
182164

183165
int exitCode = process.exitValue();
184-
185-
try {
186-
readerThread.join(5000);
187-
} catch (InterruptedException e) {
188-
Thread.currentThread().interrupt();
189-
}
190-
191-
if (readException[0] != null) {
192-
throw new IOException(
193-
"Failed to read cargo metadata output: " + readException[0].getMessage(),
194-
readException[0]);
166+
if (exitCode != 0) {
167+
if (debugLoggingIsNeeded()) {
168+
log.warning("cargo metadata failed with exit code: " + exitCode);
169+
}
170+
return null;
195171
}
196172

197-
String output = outputBuilder.toString();
198-
if (exitCode != 0 || output.trim().isEmpty()) {
173+
if (output.isBlank()) {
174+
if (debugLoggingIsNeeded()) {
175+
log.warning("cargo metadata returned empty output");
176+
}
199177
return null;
200178
}
201179

0 commit comments

Comments
 (0)