Skip to content

Commit

Permalink
Workaround AV software making File.renameTo() not work.
Browse files Browse the repository at this point in the history
  • Loading branch information
asvitkine committed Dec 26, 2021
1 parent e621583 commit 4e7ba96
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
15 changes: 2 additions & 13 deletions src/impsave/SaveDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import crockford.CrockfordBase32;
Expand Down Expand Up @@ -327,16 +326,6 @@ private String generateBackupSuffix() {
return ".old" + new CrockfordBase32().encodeToString(arr);
}

private static void renameFileOrDie(File from, File to) {
try {
from.renameTo(to);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Could not rename file: " + from + " to: " + to);
System.exit(-1);
}
}

private void updateGameFileCache(GameFileContents game) {
gameFileCache.put(new File(saveFolder + "/" + game.filePath), game);
}
Expand All @@ -352,8 +341,8 @@ private void load() {
} catch (Exception e) {
System.out.println("Error loading file: " + file);
File backupFile = Utils.addFileNameSuffix(file, generateBackupSuffix());
System.out.println("Renaming existing file to: " + backupFile);
renameFileOrDie(file, backupFile);
System.out.println("Backing up existing file to: " + backupFile);
Utils.copyFile(file, backupFile);
savedGames = new SavedGames();
}

Expand Down
27 changes: 27 additions & 0 deletions src/impsave/Utils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package impsave;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -69,6 +70,32 @@ public static void copy(InputStream in, OutputStream out) throws IOException {
}
}

private static void close(Closeable in) {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void copyFile(File from, File to) {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(from);
out = new FileOutputStream(to);
copy(in, new FileOutputStream(to));
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
close(in);
close(out);
}
}

public static File addFileNameSuffix(File file, String suffix) {
String name = file.getName();
int dotIndex = name.lastIndexOf('.');
Expand Down

0 comments on commit 4e7ba96

Please sign in to comment.