|
6 | 6 | import com.github.benmanes.caffeine.cache.Caffeine; |
7 | 7 | import java.io.BufferedInputStream; |
8 | 8 | import java.io.BufferedOutputStream; |
9 | | -import java.io.File; |
10 | | -import java.io.FileInputStream; |
11 | | -import java.io.FileOutputStream; |
12 | 9 | import java.io.IOException; |
13 | 10 | import java.io.InputStream; |
14 | 11 | import java.io.OutputStream; |
15 | 12 | import java.nio.charset.StandardCharsets; |
16 | 13 | import java.nio.file.AtomicMoveNotSupportedException; |
17 | 14 | import java.nio.file.Files; |
| 15 | +import java.nio.file.Path; |
18 | 16 | import java.nio.file.StandardCopyOption; |
19 | 17 | import java.util.function.Supplier; |
20 | 18 | import org.jenkinsci.plugins.workflow.job.WorkflowRun; |
@@ -78,12 +76,12 @@ public boolean tryServeAllSteps(WorkflowRun run, OutputStream out) throws IOExce |
78 | 76 | return tryServe(allStepsFile(run), out); |
79 | 77 | } |
80 | 78 |
|
81 | | - private boolean tryServe(File file, OutputStream out) throws IOException { |
82 | | - if (!file.isFile()) { |
| 79 | + private boolean tryServe(Path file, OutputStream out) throws IOException { |
| 80 | + if (!Files.exists(file)) { |
83 | 81 | return false; |
84 | 82 | } |
85 | 83 | out.write(ENVELOPE_PREFIX); |
86 | | - try (InputStream in = new BufferedInputStream(new FileInputStream(file))) { |
| 84 | + try (InputStream in = new BufferedInputStream(Files.newInputStream(file))) { |
87 | 85 | in.transferTo(out); |
88 | 86 | } |
89 | 87 | out.write(ENVELOPE_SUFFIX); |
@@ -141,55 +139,59 @@ public void seed(WorkflowRun run, PipelineGraph graph, PipelineStepList allSteps |
141 | 139 | } |
142 | 140 |
|
143 | 141 | /** Returns the JSON-decoded value at {@code source} or {@code null} if the file is absent or unreadable. */ |
144 | | - private <T> T readJson(File source, Class<T> type) { |
145 | | - if (!source.isFile()) { |
| 142 | + private <T> T readJson(Path source, Class<T> type) { |
| 143 | + if (!Files.exists(source)) { |
146 | 144 | return null; |
147 | 145 | } |
148 | | - try (InputStream in = new BufferedInputStream(new FileInputStream(source))) { |
| 146 | + try (InputStream in = new BufferedInputStream(Files.newInputStream(source))) { |
149 | 147 | return MAPPER.readValue(in, type); |
150 | 148 | } catch (IOException e) { |
151 | 149 | // A corrupt/older file shouldn't wedge the cache: drop it and fall back to compute. |
152 | | - logger.warn("Failed to read pipeline graph cache for {}; recomputing", source.getName(), e); |
| 150 | + logger.warn("Failed to read pipeline graph cache for {}; recomputing", source.getFileName(), e); |
153 | 151 | return null; |
154 | 152 | } |
155 | 153 | } |
156 | 154 |
|
157 | | - private void writeJson(File target, Object data) { |
158 | | - File dir = target.getParentFile(); |
159 | | - File tmp = null; |
| 155 | + private void writeJson(Path target, Object data) { |
| 156 | + Path dir = target.getParent(); |
| 157 | + Path tmp = null; |
160 | 158 | try { |
161 | | - tmp = File.createTempFile(target.getName() + ".", ".tmp", dir); |
162 | | - try (OutputStream os = new BufferedOutputStream(new FileOutputStream(tmp))) { |
| 159 | + tmp = Files.createTempFile(dir, target.getFileName() + ".", ".tmp"); |
| 160 | + try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(tmp))) { |
163 | 161 | MAPPER.writeValue(os, data); |
164 | 162 | } |
165 | 163 | try { |
166 | 164 | Files.move( |
167 | | - tmp.toPath(), |
168 | | - target.toPath(), |
| 165 | + tmp, |
| 166 | + target, |
169 | 167 | StandardCopyOption.ATOMIC_MOVE, |
170 | 168 | StandardCopyOption.REPLACE_EXISTING); |
171 | 169 | } catch (AtomicMoveNotSupportedException e) { |
172 | | - Files.move(tmp.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING); |
| 170 | + Files.move(tmp, target, StandardCopyOption.REPLACE_EXISTING); |
173 | 171 | } |
174 | 172 | tmp = null; |
175 | 173 | // Best-effort cleanup of any pre-v1 XStream cache left behind by older versions |
176 | 174 | // of the plugin. |
177 | | - new File(dir, LEGACY_XSTREAM_FILE_NAME).delete(); |
| 175 | + Files.deleteIfExists(dir.resolve(LEGACY_XSTREAM_FILE_NAME)); |
178 | 176 | } catch (IOException e) { |
179 | | - logger.warn("Failed to write pipeline graph cache for {}", target.getName(), e); |
| 177 | + logger.warn("Failed to write pipeline graph cache for {}", target.getFileName(), e); |
180 | 178 | } finally { |
181 | 179 | if (tmp != null) { |
182 | | - tmp.delete(); |
| 180 | + try { |
| 181 | + Files.deleteIfExists(tmp); |
| 182 | + } catch (IOException e) { |
| 183 | + logger.warn("Failed to delete temporary pipeline graph cache file", e); |
| 184 | + } |
183 | 185 | } |
184 | 186 | } |
185 | 187 | } |
186 | 188 |
|
187 | | - private File treeFile(WorkflowRun run) { |
188 | | - return new File(run.getRootDir(), TREE_FILE_NAME); |
| 189 | + private Path treeFile(WorkflowRun run) { |
| 190 | + return run.getRootDir().toPath().resolve(TREE_FILE_NAME); |
189 | 191 | } |
190 | 192 |
|
191 | | - private File allStepsFile(WorkflowRun run) { |
192 | | - return new File(run.getRootDir(), ALL_STEPS_FILE_NAME); |
| 193 | + private Path allStepsFile(WorkflowRun run) { |
| 194 | + return run.getRootDir().toPath().resolve(ALL_STEPS_FILE_NAME); |
193 | 195 | } |
194 | 196 |
|
195 | 197 | /** Test hook: drop in-memory entries so the next call re-runs the supplier. */ |
|
0 commit comments