Improves the readability of the backup code a bit
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good
This commit is contained in:
parent
60fdcf5ddc
commit
71e47acbb0
@ -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;
|
||||||
|
progressBar = null;
|
||||||
|
progressTextArea = null;
|
||||||
|
cancelButton = null;
|
||||||
}
|
}
|
||||||
frame = 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,18 +36,21 @@ public class BackupUtil {
|
|||||||
/**
|
/**
|
||||||
* Recursively copies a folder to another location
|
* Recursively copies a folder to another location
|
||||||
*
|
*
|
||||||
* @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;
|
||||||
}
|
}
|
||||||
if (!source.isDirectory()) {
|
if (!source.isDirectory()) {
|
||||||
long copiedFileSize = copyFile(source, destination);
|
long copiedFileSize = copyFile(source, destination);
|
||||||
BackupGUI.updateProgress("Copying " + source + "\n to " + destination,
|
BackupGUI.updateProgress("Copying " + source + "\n to " + destination,
|
||||||
(int)((alreadyCopied + copiedFileSize) * 100 / backupFileSize));
|
(int) ((alreadyCopied + copiedFileSize) * 100 / backupFileSize));
|
||||||
return copiedFileSize;
|
return copiedFileSize;
|
||||||
} else {
|
} else {
|
||||||
if (!destination.exists() && !destination.mkdir()) {
|
if (!destination.exists() && !destination.mkdir()) {
|
||||||
@ -62,7 +65,7 @@ public class BackupUtil {
|
|||||||
copiedFilesSize += backupFolder(srcFile, destinationFile, backupFileSize,
|
copiedFilesSize += backupFolder(srcFile, destinationFile, backupFileSize,
|
||||||
alreadyCopied + copiedFilesSize);
|
alreadyCopied + copiedFilesSize);
|
||||||
BackupGUI.updateProgress("Copying " + source + "\n to " + destination,
|
BackupGUI.updateProgress("Copying " + source + "\n to " + destination,
|
||||||
(int)((alreadyCopied + copiedFilesSize) * 100 / backupFileSize));
|
(int) ((alreadyCopied + copiedFilesSize) * 100 / backupFileSize));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return copiedFilesSize;
|
return copiedFilesSize;
|
||||||
@ -72,8 +75,8 @@ public class BackupUtil {
|
|||||||
/**
|
/**
|
||||||
* Copies a file from one location to another
|
* Copies a file from one location to another
|
||||||
*
|
*
|
||||||
* @param source <p>The file to copy</p>
|
* @param source <p>The file to copy</p>
|
||||||
* @param destination <p>The location of the copied file</p>
|
* @param destination <p>The location of the copied file</p>
|
||||||
* @throws IOException <p>If reading or writing fails</p>
|
* @throws IOException <p>If reading or writing fails</p>
|
||||||
*/
|
*/
|
||||||
private static long copyFile(File source, File destination) throws IOException {
|
private static long copyFile(File source, File destination) throws IOException {
|
||||||
@ -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,46 +121,70 @@ 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;
|
||||||
}
|
}
|
||||||
File srcFolder = serverFolder.get(0);
|
alreadyCopied = backupServerFiles(gui, serverFolder, backupFileSize, alreadyCopied);
|
||||||
File destinationFolder = serverFolder.get(1);
|
|
||||||
|
|
||||||
//Create child folders
|
|
||||||
if (!destinationFolder.exists() && !destinationFolder.mkdirs()) {
|
|
||||||
backupRunning = false;
|
|
||||||
gui.logError("Unable to create necessary sub-folder in the backup folder");
|
|
||||||
throw new IllegalArgumentException("Unable to create necessary sub-folder in the backup folder");
|
|
||||||
}
|
|
||||||
//Backup
|
|
||||||
try {
|
|
||||||
alreadyCopied += backupFolder(srcFolder, destinationFolder, backupFileSize, alreadyCopied);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
gui.setStatus("Backup caused an error");
|
|
||||||
backupRunning = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
backupRunning = false;
|
backupRunning = false;
|
||||||
BackupGUI.destroy();
|
|
||||||
if (backupAborted) {
|
if (backupAborted) {
|
||||||
gui.setStatus("Backup aborted");
|
gui.setStatus("Backup aborted");
|
||||||
} else {
|
} else {
|
||||||
|
BackupGUI.destroy();
|
||||||
gui.setStatus("Backup finished");
|
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 destinationFolder = serverFolder.get(1);
|
||||||
|
|
||||||
|
//Create child folders
|
||||||
|
if (!destinationFolder.exists() && !destinationFolder.mkdirs()) {
|
||||||
|
backupRunning = false;
|
||||||
|
gui.logError("Unable to create necessary sub-folder in the backup folder");
|
||||||
|
throw new IllegalArgumentException("Unable to create necessary sub-folder in the backup folder");
|
||||||
|
}
|
||||||
|
//Backup
|
||||||
|
try {
|
||||||
|
alreadyCopied += backupFolder(srcFolder, destinationFolder, backupFileSize, alreadyCopied);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
gui.setStatus("Backup caused an error");
|
||||||
|
backupRunning = false;
|
||||||
|
}
|
||||||
|
return alreadyCopied;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the size of a list of folders
|
* Gets the size of a list of folders
|
||||||
*
|
*
|
||||||
* @param gui <p>The GUI to write any errors to</p>
|
* @param gui <p>The GUI to write any errors to</p>
|
||||||
* @param serverFolders <p>The folder to find the size of</p>
|
* @param serverFolders <p>The folder to find the size of</p>
|
||||||
* @return <p>The size of the given folders</p>
|
* @return <p>The size of the given folders</p>
|
||||||
*/
|
*/
|
||||||
@ -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>
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user