Skip to content

Commit

Permalink
fix(update): Delete old files on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Davies committed Mar 30, 2020
1 parent 533f06a commit 5353e39
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions core/src/main/java/uk/co/bjdavies/core/UpdateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import java.io.*;
import java.net.URL;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

Expand Down Expand Up @@ -103,6 +106,7 @@ private boolean swapScripts(File unZipTmp) throws IOException {
private boolean swapLibs(File unZipTmp) {
File libsFolder = new File(unZipTmp + File.separator + "lib/");
File ourLibsFolder = new File("../lib/");
log.info(ourLibsFolder.getAbsolutePath());

if (!ourLibsFolder.exists()) {
if (!ourLibsFolder.mkdirs()) {
Expand All @@ -117,22 +121,24 @@ private boolean swapLibs(File unZipTmp) {
return false;
}
}

if (ourLibsFolder.delete()) {
if (ourLibsFolder.mkdirs()) {
AtomicBoolean copy = new AtomicBoolean(true);
try {
Arrays.stream(Objects.requireNonNull(ourLibsFolder.listFiles())).forEach(File::deleteOnExit);
Arrays.stream(Objects.requireNonNull(libsFolder.listFiles())).forEach(f -> {
try {
FileUtils.copyDirectory(libsFolder, ourLibsFolder, true);
FileUtils.copyFileToDirectory(f, ourLibsFolder);
} catch (IOException e) {
log.error("Cant copy", e);
return false;
copy.set(false);
}
} else {
return false;
}
});
} catch (NullPointerException e) {
log.error("Cant copy", e);
return false;
}


return true;
return copy.get();
}

private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
Expand Down

0 comments on commit 5353e39

Please sign in to comment.