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:
		@@ -75,19 +75,19 @@ public class BackupGUI implements ActionListener {
 | 
			
		||||
     * Destroys the backup GUI
 | 
			
		||||
     */
 | 
			
		||||
    public static void destroy() {
 | 
			
		||||
        BackupUtil.abortBackup();
 | 
			
		||||
        progressTextArea = null;
 | 
			
		||||
        progressBar = null;
 | 
			
		||||
        cancelButton = null;
 | 
			
		||||
        if (frame != null) {
 | 
			
		||||
            frame.dispose();
 | 
			
		||||
            frame = null;
 | 
			
		||||
            progressBar = null;
 | 
			
		||||
            progressTextArea = null;
 | 
			
		||||
            cancelButton = null;
 | 
			
		||||
        }
 | 
			
		||||
        frame = null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void actionPerformed(ActionEvent actionEvent) {
 | 
			
		||||
        if (actionEvent.getSource() == cancelButton) {
 | 
			
		||||
            BackupUtil.abortBackup();
 | 
			
		||||
            destroy();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -36,18 +36,21 @@ public class BackupUtil {
 | 
			
		||||
    /**
 | 
			
		||||
     * Recursively copies a folder to another location
 | 
			
		||||
     *
 | 
			
		||||
     * @param source      <p>The folder to copy</p>
 | 
			
		||||
     * @param destination <p>Target destination</p>
 | 
			
		||||
     * @param source         <p>The folder to copy</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>
 | 
			
		||||
     */
 | 
			
		||||
    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) {
 | 
			
		||||
            return 0L;
 | 
			
		||||
        }
 | 
			
		||||
        if (!source.isDirectory()) {
 | 
			
		||||
            long copiedFileSize = copyFile(source, destination);
 | 
			
		||||
            BackupGUI.updateProgress("Copying " + source + "\n to " + destination,
 | 
			
		||||
                    (int)((alreadyCopied + copiedFileSize) * 100 / backupFileSize));
 | 
			
		||||
                    (int) ((alreadyCopied + copiedFileSize) * 100 / backupFileSize));
 | 
			
		||||
            return copiedFileSize;
 | 
			
		||||
        } else {
 | 
			
		||||
            if (!destination.exists() && !destination.mkdir()) {
 | 
			
		||||
@@ -62,7 +65,7 @@ public class BackupUtil {
 | 
			
		||||
                    copiedFilesSize += backupFolder(srcFile, destinationFile, backupFileSize,
 | 
			
		||||
                            alreadyCopied + copiedFilesSize);
 | 
			
		||||
                    BackupGUI.updateProgress("Copying " + source + "\n to " + destination,
 | 
			
		||||
                            (int)((alreadyCopied + copiedFilesSize) * 100 / backupFileSize));
 | 
			
		||||
                            (int) ((alreadyCopied + copiedFilesSize) * 100 / backupFileSize));
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            return copiedFilesSize;
 | 
			
		||||
@@ -72,8 +75,8 @@ public class BackupUtil {
 | 
			
		||||
    /**
 | 
			
		||||
     * Copies a file from one location to another
 | 
			
		||||
     *
 | 
			
		||||
     * @param source            <p>The file to copy</p>
 | 
			
		||||
     * @param destination       <p>The location of the copied file</p>
 | 
			
		||||
     * @param source      <p>The file to copy</p>
 | 
			
		||||
     * @param destination <p>The location of the copied file</p>
 | 
			
		||||
     * @throws IOException <p>If reading or writing fails</p>
 | 
			
		||||
     */
 | 
			
		||||
    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
 | 
			
		||||
     *
 | 
			
		||||
     * @param gui <p>The GUI to use for informing the user</p>
 | 
			
		||||
     */
 | 
			
		||||
    public synchronized static void backup(GUI gui) {
 | 
			
		||||
        backupAborted = false;
 | 
			
		||||
@@ -116,46 +121,70 @@ public class BackupUtil {
 | 
			
		||||
 | 
			
		||||
        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();
 | 
			
		||||
        BackupGUI.updateProgress("Backup starting...", 0);
 | 
			
		||||
        long alreadyCopied = 0;
 | 
			
		||||
        for (List<File> serverFolder : serverFolders) {
 | 
			
		||||
            if (backupAborted) {
 | 
			
		||||
            if (backupAborted || !backupRunning) {
 | 
			
		||||
                gui.setStatus("Backup aborted");
 | 
			
		||||
                backupRunning = false;
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            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;
 | 
			
		||||
            }
 | 
			
		||||
            alreadyCopied = backupServerFiles(gui, serverFolder, backupFileSize, alreadyCopied);
 | 
			
		||||
        }
 | 
			
		||||
        backupRunning = false;
 | 
			
		||||
        BackupGUI.destroy();
 | 
			
		||||
        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 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
 | 
			
		||||
     *
 | 
			
		||||
     * @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>
 | 
			
		||||
     * @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
 | 
			
		||||
     *
 | 
			
		||||
     * @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>
 | 
			
		||||
     */
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user