|
32 | 32 | import java.io.RandomAccessFile;
|
33 | 33 | import java.nio.channels.FileChannel;
|
34 | 34 | import java.nio.channels.FileLock;
|
| 35 | +import java.nio.file.Files; |
35 | 36 | import java.nio.file.Path;
|
| 37 | +import java.nio.file.StandardCopyOption; |
36 | 38 | import java.security.MessageDigest;
|
37 | 39 | import java.security.NoSuchAlgorithmException;
|
38 | 40 | import java.util.Base64;
|
@@ -86,19 +88,40 @@ static File doUnpackNar(final File nar, final File baseWorkingDirectory, Runnabl
|
86 | 88 | try (FileChannel channel = new RandomAccessFile(lockFile, "rw").getChannel();
|
87 | 89 | FileLock lock = channel.lock()) {
|
88 | 90 | File narWorkingDirectory = new File(parentDirectory, md5Sum);
|
89 |
| - if (narWorkingDirectory.mkdir()) { |
| 91 | + if (!narWorkingDirectory.exists()) { |
| 92 | + File narExtractionTempDirectory = new File(parentDirectory, md5Sum + ".tmp"); |
| 93 | + if (narExtractionTempDirectory.exists()) { |
| 94 | + FileUtils.deleteFile(narExtractionTempDirectory, true); |
| 95 | + } |
| 96 | + if (!narExtractionTempDirectory.mkdir()) { |
| 97 | + throw new IOException("Cannot create " + narExtractionTempDirectory); |
| 98 | + } |
| 99 | + if (!narWorkingDirectory.mkdir()) { |
| 100 | + throw new IOException("Cannot create " + narWorkingDirectory); |
| 101 | + } |
90 | 102 | try {
|
91 |
| - log.info("Extracting {} to {}", nar, narWorkingDirectory); |
| 103 | + log.info("Extracting {} to {}", nar, narExtractionTempDirectory); |
92 | 104 | if (extractCallback != null) {
|
93 | 105 | extractCallback.run();
|
94 | 106 | }
|
95 |
| - unpack(nar, narWorkingDirectory); |
| 107 | + unpack(nar, narExtractionTempDirectory); |
96 | 108 | } catch (IOException e) {
|
97 |
| - log.error("There was a problem extracting the nar file. Deleting {} to clean up state.", |
98 |
| - narWorkingDirectory, e); |
99 |
| - FileUtils.deleteFile(narWorkingDirectory, true); |
| 109 | + log.error("There was a problem extracting the nar file. Deleting {} and {} to clean up state.", |
| 110 | + narExtractionTempDirectory, narWorkingDirectory, e); |
| 111 | + try { |
| 112 | + FileUtils.deleteFile(narExtractionTempDirectory, true); |
| 113 | + } catch (IOException e2) { |
| 114 | + log.error("Failed to delete temporary directory {}", narExtractionTempDirectory, e2); |
| 115 | + } |
| 116 | + try { |
| 117 | + FileUtils.deleteFile(narWorkingDirectory, true); |
| 118 | + } catch (IOException e2) { |
| 119 | + log.error("Failed to delete working directory {}", narWorkingDirectory, e2); |
| 120 | + } |
100 | 121 | throw e;
|
101 | 122 | }
|
| 123 | + Files.move(narExtractionTempDirectory.toPath(), narWorkingDirectory.toPath(), |
| 124 | + StandardCopyOption.ATOMIC_MOVE); |
102 | 125 | }
|
103 | 126 | return narWorkingDirectory;
|
104 | 127 | }
|
|
0 commit comments