|
37 | 37 | import io.github.guacsec.trustifyda.tools.Operations; |
38 | 38 | import io.github.guacsec.trustifyda.utils.IgnorePatternDetector; |
39 | 39 | import java.io.IOException; |
| 40 | +import java.io.InputStream; |
40 | 41 | import java.nio.charset.StandardCharsets; |
41 | 42 | import java.nio.file.Files; |
42 | 43 | import java.nio.file.Path; |
@@ -143,59 +144,36 @@ private CargoMetadata executeCargoMetadata() throws IOException, InterruptedExce |
143 | 144 | log.info("Timeout: " + TIMEOUT + " seconds"); |
144 | 145 | } |
145 | 146 |
|
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 | + } |
170 | 157 |
|
171 | 158 | boolean finished = process.waitFor(TIMEOUT, TimeUnit.SECONDS); |
172 | 159 |
|
173 | 160 | if (!finished) { |
174 | 161 | process.destroyForcibly(); |
175 | | - try { |
176 | | - process.waitFor(5, TimeUnit.SECONDS); |
177 | | - } catch (InterruptedException ignored) { |
178 | | - } |
179 | | - readerThread.interrupt(); |
180 | 162 | throw new InterruptedException("cargo metadata timed out after " + TIMEOUT + " seconds"); |
181 | 163 | } |
182 | 164 |
|
183 | 165 | 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; |
195 | 171 | } |
196 | 172 |
|
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 | + } |
199 | 177 | return null; |
200 | 178 | } |
201 | 179 |
|
|
0 commit comments