|
22 | 22 | import java.io.IOException;
|
23 | 23 | import java.io.PrintStream;
|
24 | 24 | import java.lang.reflect.Method;
|
| 25 | +import java.nio.file.FileAlreadyExistsException; |
25 | 26 | import java.nio.file.Files;
|
26 | 27 | import java.nio.file.Path;
|
27 | 28 | import java.nio.file.StandardCopyOption;
|
|
31 | 32 | import java.util.Enumeration;
|
32 | 33 | import java.util.List;
|
33 | 34 | import java.util.Locale;
|
| 35 | +import java.util.Random; |
34 | 36 | import java.util.TimeZone;
|
35 | 37 | import java.util.regex.Pattern;
|
36 | 38 | import java.util.zip.ZipEntry;
|
@@ -147,7 +149,7 @@ protected void postCreateArchive() throws ArchiverException {
|
147 | 149 | private void fixLastModifiedTimeZipEntries() throws IOException {
|
148 | 150 | long timeMillis = getLastModifiedTime().toMillis();
|
149 | 151 | Path destFile = getDestFile().toPath();
|
150 |
| - Path tmpZip = Files.createTempFile(destFile.getParent(), null, null); |
| 152 | + Path tmpZip = createTempFile(destFile.getParent()); |
151 | 153 | try (ZipFile zipFile = new ZipFile(getDestFile());
|
152 | 154 | ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(tmpZip))) {
|
153 | 155 | Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
@@ -263,4 +265,24 @@ private boolean isJarDateOptionSupported(Method runMethod) {
|
263 | 265 | return false;
|
264 | 266 | }
|
265 | 267 | }
|
| 268 | + |
| 269 | + /** |
| 270 | + * Create a temporary file in the provided directory. |
| 271 | + * |
| 272 | + * It is an unsecure replacement for {@code Files#createTempFile(Path, String, String, java.nio.file.attribute.FileAttribute...)}: |
| 273 | + * The new file permissions are controlled by the umask property instead of just being accessible to the current user. |
| 274 | + */ |
| 275 | + private Path createTempFile(Path dir) throws IOException { |
| 276 | + Random random = new Random(); |
| 277 | + for (; ; ) { |
| 278 | + |
| 279 | + String name = Long.toUnsignedString(random.nextLong()) + ".tmp"; |
| 280 | + Path path = dir.resolve(name); |
| 281 | + try { |
| 282 | + return Files.createFile(path); |
| 283 | + } catch (FileAlreadyExistsException e) { |
| 284 | + // retry; |
| 285 | + } |
| 286 | + } |
| 287 | + } |
266 | 288 | }
|
0 commit comments