Improves the readability of the backup code a bit
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2021-09-27 22:06:56 +02:00
parent 60fdcf5ddc
commit 71e47acbb0
2 changed files with 62 additions and 32 deletions

View File

@ -75,19 +75,19 @@ public class BackupGUI implements ActionListener {
* Destroys the backup GUI * Destroys the backup GUI
*/ */
public static void destroy() { public static void destroy() {
BackupUtil.abortBackup();
progressTextArea = null;
progressBar = null;
cancelButton = null;
if (frame != null) { if (frame != null) {
frame.dispose(); frame.dispose();
}
frame = null; frame = null;
progressBar = null;
progressTextArea = null;
cancelButton = null;
}
} }
@Override @Override
public void actionPerformed(ActionEvent actionEvent) { public void actionPerformed(ActionEvent actionEvent) {
if (actionEvent.getSource() == cancelButton) { if (actionEvent.getSource() == cancelButton) {
BackupUtil.abortBackup();
destroy(); destroy();
} }
} }

View File

@ -38,9 +38,12 @@ public class BackupUtil {
* *
* @param source <p>The folder to copy</p> * @param source <p>The folder to copy</p>
* @param destination <p>Target destination</p> * @param destination <p>Target destination</p>
* @param backupFileSize <p>The total file size of the backup in progress</p>
* @param alreadyCopied <p>The amount of bytes already copied</p>
* @throws IOException <p>If we can't start a file stream</p> * @throws IOException <p>If we can't start a file stream</p>
*/ */
private static synchronized long backupFolder(File source, File destination, long backupFileSize, long alreadyCopied) throws IOException { private static synchronized long backupFolder(File source, File destination, long backupFileSize,
long alreadyCopied) throws IOException {
if (backupAborted) { if (backupAborted) {
return 0L; return 0L;
} }
@ -91,6 +94,8 @@ public class BackupUtil {
/** /**
* Copies all server directories to a folder specified by the user * Copies all server directories to a folder specified by the user
*
* @param gui <p>The GUI to use for informing the user</p>
*/ */
public synchronized static void backup(GUI gui) { public synchronized static void backup(GUI gui) {
backupAborted = false; backupAborted = false;
@ -116,15 +121,46 @@ public class BackupUtil {
gui.setStatus("Backing up " + (backupFileSize / 1000000) + "MB"); gui.setStatus("Backing up " + (backupFileSize / 1000000) + "MB");
performBackup(gui, serverFolders, backupFileSize);
}
/**
* Performs the actual backup after checks have passed and necessary info is available
* @param gui <p>The GUI to use for informing the user</p>
* @param serverFolders <p>The folders of the servers to backup</p>
* @param backupFileSize <p>The total size of the folders to backup</p>
*/
private static void performBackup(GUI gui, List<List<File>> serverFolders, long backupFileSize) {
new BackupGUI(); new BackupGUI();
BackupGUI.updateProgress("Backup starting...", 0); BackupGUI.updateProgress("Backup starting...", 0);
long alreadyCopied = 0; long alreadyCopied = 0;
for (List<File> serverFolder : serverFolders) { for (List<File> serverFolder : serverFolders) {
if (backupAborted) { if (backupAborted || !backupRunning) {
gui.setStatus("Backup aborted"); gui.setStatus("Backup aborted");
backupRunning = false; backupRunning = false;
return; return;
} }
alreadyCopied = backupServerFiles(gui, serverFolder, backupFileSize, alreadyCopied);
}
backupRunning = false;
if (backupAborted) {
gui.setStatus("Backup aborted");
} else {
BackupGUI.destroy();
gui.setStatus("Backup finished");
}
}
/**
* Backs up the files for a single server
*
* @param gui <p>The GUI to use for informing the user</p>
* @param serverFolder <p>The server's input and output folders</p>
* @param backupFileSize <p>The total size of the files to backup</p>
* @param alreadyCopied <p>The amount of bytes already copied</p>
* @return <p>The new amount of bytes copied</p>
*/
private static long backupServerFiles(GUI gui, List<File> serverFolder, long backupFileSize, long alreadyCopied) {
File srcFolder = serverFolder.get(0); File srcFolder = serverFolder.get(0);
File destinationFolder = serverFolder.get(1); File destinationFolder = serverFolder.get(1);
@ -142,14 +178,7 @@ public class BackupUtil {
gui.setStatus("Backup caused an error"); gui.setStatus("Backup caused an error");
backupRunning = false; backupRunning = false;
} }
} return alreadyCopied;
backupRunning = false;
BackupGUI.destroy();
if (backupAborted) {
gui.setStatus("Backup aborted");
} else {
gui.setStatus("Backup finished");
}
} }
/** /**
@ -208,6 +237,7 @@ public class BackupUtil {
/** /**
* Gets the size of a file given its path * Gets the size of a file given its path
*
* @param path <p>The path to a file</p> * @param path <p>The path to a file</p>
* @return <p>The size of the file in bytes, or 0 if an exception is thrown</p> * @return <p>The size of the file in bytes, or 0 if an exception is thrown</p>
*/ */