From c74fc6c99950f61221547d70809f7f649e27c9a5 Mon Sep 17 00:00:00 2001 From: Szum123321 Date: Fri, 17 Apr 2020 10:20:47 +0200 Subject: [PATCH] Some additional tweaks to the compression system. Also file blacklist works now! --- README.md | 6 +- build.gradle | 4 -- gradle.properties | 10 +-- .../textile_backup/ConfigHandler.java | 38 +++++------ .../textile_backup/core/BackupHelper.java | 9 ++- .../textile_backup/core/MakeBackupThread.java | 11 ++-- .../textile_backup/core/Utilities.java | 10 +++ .../compressors/GenericTarCompressor.java | 28 +++----- .../core/compressors/ZipCompressor.java | 65 ++++++++++--------- src/main/resources/fabric.mod.json | 2 +- 10 files changed, 92 insertions(+), 91 deletions(-) diff --git a/README.md b/README.md index 8bfb764..7f68f91 100644 --- a/README.md +++ b/README.md @@ -15,14 +15,14 @@ Available operations are: * whitelist - here you can add, remove and list player that are allowed to run any operation within this mod despite not having high enough permission level* * whitelist - here you can add, remove and list player that are not allowed to run any operation within this mod despite having high enough permission level* -All of above can only be done by server admins(permission level 4 - configurable*) / player on single player with cheats on. +All of the above can only be done by server admins(permission level 4 - configurable*) or player on a single player. -Feel free to use this mod in your modpack or on server! +Feel free to use this mod in your modpack or on a server! ### Important * Time format defaultly used by this mod is: dd.MM.yyyy_HH-mm-ss although it is configurable*. -* _This mod contains **Cotton Config** and its dependencies as jars in jar, which are property of **CottonMC**_. +* _This mod contains **Cotton Config** and its dependencies as jars in a jar, which are property of **CottonMC**_. \* - feature available since 1.1.0 diff --git a/build.gradle b/build.gradle index ab628e5..10eb4c7 100644 --- a/build.gradle +++ b/build.gradle @@ -35,10 +35,6 @@ dependencies { modCompile "org.apache.commons:commons-compress:1.20" include "org.apache.commons:commons-compress:1.20" - //include "org.apache.commons:commons-io:1.3.2" - - // PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs. - // You may need to force-disable transitiveness on them. } processResources { diff --git a/gradle.properties b/gradle.properties index 5e339a3..eb2fd34 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,14 +1,14 @@ # Done to increase the memory available to gradle. org.gradle.jvmargs=-Xmx1G -minecraft_version=20w14a -yarn_mappings=20w14a+build.1 -loader_version=0.7.9+build.190 +minecraft_version=1.15.2 +yarn_mappings=1.15.2+build.15 +loader_version=0.8.2+build.194 #Fabric api -fabric_version=0.5.7+build.314-1.16 +fabric_version=0.5.1+build.294-1.15 # Mod Properties - mod_version = 1.2.0-1.15 + mod_version = 1.2.0B-1.15 maven_group = net.szum123321 archives_base_name = textile_backup \ No newline at end of file diff --git a/src/main/java/net/szum123321/textile_backup/ConfigHandler.java b/src/main/java/net/szum123321/textile_backup/ConfigHandler.java index 70fde08..dba67b4 100644 --- a/src/main/java/net/szum123321/textile_backup/ConfigHandler.java +++ b/src/main/java/net/szum123321/textile_backup/ConfigHandler.java @@ -27,60 +27,60 @@ import java.util.Set; @ConfigFile(name = TextileBackup.MOD_ID) public class ConfigHandler { @Comment("\nTime between backups in seconds\n") - public long backupInterval = 3600; + public final long backupInterval = 3600; @Comment("\nShould backups be done even if there is no players?\n") - public boolean doBackupsOnEmptyServer = false; + public final boolean doBackupsOnEmptyServer = false; @Comment("\nShould backups be made on server shutdown\n") - public boolean shutdownBackup = true; + public final boolean shutdownBackup = true; @Comment("\nA path to backup folder\n") - public String path = "backup/"; + public final String path = "backup/"; @Comment("\nThis setting allows you to exclude files form being backuped.\n"+ "Be very careful when setting it, as it is easy to make your backuped world unusable!\n") - public Set fileBlacklist = new HashSet<>(); + public final Set fileBlacklist = new HashSet<>(); @Comment("\nShould every world has its won backup folder?\n") - public boolean perWorldBackup = false; + public final boolean perWorldBackup = false; @Comment("\nMaximum number of backups to keep. If 0 then no backup will be deleted based on its amount\n") - public int backupsToKeep = 10; + public final int backupsToKeep = 10; @Comment("\nMaximum age of backups to keep in seconds.\n if 0 then backups will not be deleted based on its age \n") - public long maxAge = 0; + public final long maxAge = 0; @Comment("\nMaximum size of backup folder in kilo bytes. \n") - public int maxSize = 0; + public final int maxSize = 0; @Comment("\nCompression level \n0 - 9\n Only available for zip compression.\n") - public int compression = 1; + public final int compression = 1; @Comment(value = "\nAvailable formats are:\n" + "ZIP - normal zip archive using standard deflate compression\n" + "BZIP2 - tar.bz2 archive using bzip2 compression\n" + "GIZP - tar.gz using gzip compression\n" + "LZ4 - tar.lz using lz4 compression\n") - public ArchiveFormat format = ArchiveFormat.ZIP; + public final ArchiveFormat format = ArchiveFormat.ZIP; @Comment("\nPrint info to game out\n") - public boolean log = true; + public final boolean log = true; @Comment("\nMinimal permission level required to run commands\n") - public int permissionLevel = 4; + public final int permissionLevel = 4; @Comment("\nPlayer on singleplayer is always allowed to run command. Warning! On lan party everyone will be allowed to run it.\n") - public boolean alwaysSingleplayerAllowed = true; + public final boolean alwaysSingleplayerAllowed = true; @Comment("\nPlayers allowed to run backup commands without sufficient permission level\n") - public Set playerWhitelist = new HashSet<>(); + public final Set playerWhitelist = new HashSet<>(); @Comment("\nPlayers banned from running backup commands besides their sufficient permission level\n") - public Set playerBlacklist = new HashSet<>(); + public final Set playerBlacklist = new HashSet<>(); @Comment("\nFormat of date&time used to name backup files.\n") - public String dateTimeFormat = "dd.MM.yyyy_HH-mm-ss"; + public final String dateTimeFormat = "dd.MM.yyyy_HH-mm-ss"; public enum ArchiveFormat { ZIP(".zip"), @@ -88,7 +88,7 @@ public class ConfigHandler { GZIP(".tar.gz"), LZ4(".tar.lz4"); - private String extension; + private final String extension; private ArchiveFormat(String extension){ this.extension = extension; @@ -97,5 +97,5 @@ public class ConfigHandler { public String getExtension() { return extension; } - }; + } } diff --git a/src/main/java/net/szum123321/textile_backup/core/BackupHelper.java b/src/main/java/net/szum123321/textile_backup/core/BackupHelper.java index 4f2bdf6..259604d 100644 --- a/src/main/java/net/szum123321/textile_backup/core/BackupHelper.java +++ b/src/main/java/net/szum123321/textile_backup/core/BackupHelper.java @@ -71,13 +71,13 @@ public class BackupHelper { LocalDateTime now = LocalDateTime.now(); Arrays.stream(root.listFiles()).filter(f -> f.exists() && f.isFile()).forEach(f -> { - LocalDateTime creationTime = null; + LocalDateTime creationTime; try { try { - FileTime ct = (FileTime) Files.getAttribute(f.toPath(), "creationTime"); + FileTime fileTime = (FileTime) Files.getAttribute(f.toPath(), "creationTime"); - creationTime = LocalDateTime.ofInstant(ct.toInstant(), ZoneOffset.UTC); + creationTime = LocalDateTime.ofInstant(fileTime.toInstant(), ZoneOffset.UTC); } catch (IOException ignored) { try { creationTime = LocalDateTime.from( @@ -98,8 +98,7 @@ public class BackupHelper { Utilities.log("Deleting: " + f.getName(), ctx); f.delete(); } - } catch (NullPointerException ignored3) { - } + } catch (NullPointerException ignored3) {} }); } diff --git a/src/main/java/net/szum123321/textile_backup/core/MakeBackupThread.java b/src/main/java/net/szum123321/textile_backup/core/MakeBackupThread.java index f3454ba..09780d3 100644 --- a/src/main/java/net/szum123321/textile_backup/core/MakeBackupThread.java +++ b/src/main/java/net/szum123321/textile_backup/core/MakeBackupThread.java @@ -33,9 +33,9 @@ import java.io.IOException; import java.time.LocalDateTime; public class MakeBackupThread implements Runnable { - private MinecraftServer server; - private ServerCommandSource ctx; - private String comment; + private final MinecraftServer server; + private final ServerCommandSource ctx; + private final String comment; public MakeBackupThread(MinecraftServer server, ServerCommandSource ctx, String comment){ this.server = server; @@ -83,12 +83,11 @@ public class MakeBackupThread implements Runnable { break; default: - Utilities.log("Error! No compression format specified! using default compressor!", ctx); + Utilities.log("Error! No correct compression format specified! using default compressor!", ctx); ZipCompressor.createArchive(world, outFile, ctx); break; } - BackupHelper.executeFileLimit(ctx, server.getWorld(DimensionType.OVERWORLD).getLevelProperties().getLevelName()); Utilities.log("Done!", ctx); @@ -97,6 +96,6 @@ public class MakeBackupThread implements Runnable { private String getFileName(){ LocalDateTime now = LocalDateTime.now(); - return Utilities.getDateTimeFormatter().format(now) + (comment != null ? "#" + comment.replace("#", ""): "") + TextileBackup.config.format.getExtension(); + return Utilities.getDateTimeFormatter().format(now) + (comment != null ? "#" + comment.replace("#", "") : "") + TextileBackup.config.format.getExtension(); } } diff --git a/src/main/java/net/szum123321/textile_backup/core/Utilities.java b/src/main/java/net/szum123321/textile_backup/core/Utilities.java index 66ade81..c211054 100644 --- a/src/main/java/net/szum123321/textile_backup/core/Utilities.java +++ b/src/main/java/net/szum123321/textile_backup/core/Utilities.java @@ -4,6 +4,7 @@ import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.LiteralText; import net.szum123321.textile_backup.TextileBackup; +import java.nio.file.Path; import java.time.format.DateTimeFormatter; public class Utilities { @@ -12,6 +13,15 @@ public class Utilities { return os.toLowerCase().startsWith("win"); } + public static boolean isBlacklisted(Path path) { + for(String i : TextileBackup.config.fileBlacklist) { + if(path.startsWith(i)) + return true; + } + + return false; + } + public static DateTimeFormatter getDateTimeFormatter(){ if(!TextileBackup.config.dateTimeFormat.equals("")) return DateTimeFormatter.ofPattern(TextileBackup.config.dateTimeFormat); diff --git a/src/main/java/net/szum123321/textile_backup/core/compressors/GenericTarCompressor.java b/src/main/java/net/szum123321/textile_backup/core/compressors/GenericTarCompressor.java index a115dbd..d59881b 100644 --- a/src/main/java/net/szum123321/textile_backup/core/compressors/GenericTarCompressor.java +++ b/src/main/java/net/szum123321/textile_backup/core/compressors/GenericTarCompressor.java @@ -3,7 +3,7 @@ package net.szum123321.textile_backup.core.compressors; import net.minecraft.server.command.ServerCommandSource; import net.szum123321.textile_backup.TextileBackup; import net.szum123321.textile_backup.core.Utilities; -import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.apache.commons.compress.compressors.CompressorOutputStream; import org.apache.commons.compress.utils.IOUtils; @@ -18,10 +18,8 @@ public class GenericTarCompressor { try (FileOutputStream outStream = new FileOutputStream(out); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outStream); - CompressorOutputStream lz4Stream = CompressorStreamClass.getDeclaredConstructor(OutputStream.class).newInstance(bufferedOutputStream);// CompressorStreamClass.getConstructor().newInstance(bufferedOutputStream); - TarArchiveOutputStream arc = new TarArchiveOutputStream(lz4Stream)) { - - System.out.println(lz4Stream.getClass().toString()); + CompressorOutputStream compressorStream = CompressorStreamClass.getDeclaredConstructor(OutputStream.class).newInstance(bufferedOutputStream);// CompressorStreamClass.getConstructor().newInstance(bufferedOutputStream); + TarArchiveOutputStream arc = new TarArchiveOutputStream(compressorStream)) { arc.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); arc.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX); @@ -32,15 +30,14 @@ public class GenericTarCompressor { Files.walk(input.toPath()).filter( path -> !path.equals(input.toPath()) && path.toFile().isFile() && - !TextileBackup.config.fileBlacklist.contains(path.toString().substring(rootPathLength)) + !Utilities.isBlacklisted(input.toPath().relativize(path)) ).forEach(path -> { File file = path.toAbsolutePath().toFile(); try (FileInputStream fin = new FileInputStream(file); BufferedInputStream bfin = new BufferedInputStream(fin)){ - TarArchiveEntry entry = new TarArchiveEntry(file, file.getAbsolutePath().substring(rootPathLength)); + ArchiveEntry entry = arc.createArchiveEntry(file, file.getAbsolutePath().substring(rootPathLength)); - entry.setSize(file.length()); arc.putArchiveEntry(entry); IOUtils.copy(bfin, arc); @@ -49,18 +46,11 @@ public class GenericTarCompressor { TextileBackup.logger.error(e.getMessage()); } }); - } catch (IOException e) { - TextileBackup.logger.error(e.getMessage()); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); + arc.finish(); + } catch (IOException | IllegalAccessException | NoSuchMethodException | InstantiationException | InvocationTargetException e) { + TextileBackup.logger.error(e.toString()); } Utilities.log("Compression finished", ctx); } -} +} \ No newline at end of file diff --git a/src/main/java/net/szum123321/textile_backup/core/compressors/ZipCompressor.java b/src/main/java/net/szum123321/textile_backup/core/compressors/ZipCompressor.java index 522e04c..a69b6f7 100644 --- a/src/main/java/net/szum123321/textile_backup/core/compressors/ZipCompressor.java +++ b/src/main/java/net/szum123321/textile_backup/core/compressors/ZipCompressor.java @@ -21,7 +21,7 @@ package net.szum123321.textile_backup.core.compressors; import net.minecraft.server.command.ServerCommandSource; import net.szum123321.textile_backup.TextileBackup; import net.szum123321.textile_backup.core.Utilities; -import org.apache.commons.compress.archivers.ArchiveEntry; +import org.apache.commons.compress.archivers.zip.Zip64Mode; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.utils.IOUtils; @@ -31,39 +31,46 @@ import java.nio.file.Files; import java.time.LocalDateTime; public class ZipCompressor { - public static void createArchive(File in, File out, ServerCommandSource ctx){ - Utilities.log("Starting compression...", ctx); + public static void createArchive(File in, File out, ServerCommandSource ctx) { + Utilities.log("Starting compression...", ctx); - try (FileOutputStream fileOutputStream = new FileOutputStream(out); - BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); - ZipArchiveOutputStream arc = new ZipArchiveOutputStream(bufferedOutputStream)){ + try (FileOutputStream fileOutputStream = new FileOutputStream(out); + BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); + ZipArchiveOutputStream arc = new ZipArchiveOutputStream(bufferedOutputStream)) { - arc.setMethod(ZipArchiveOutputStream.DEFLATED); - arc.setLevel(TextileBackup.config.compression); - arc.setComment("Created on: " + Utilities.getDateTimeFormatter().format(LocalDateTime.now())); + arc.setMethod(ZipArchiveOutputStream.DEFLATED); + arc.setUseZip64(Zip64Mode.AsNeeded); + arc.setLevel(TextileBackup.config.compression); + arc.setComment("Created on: " + Utilities.getDateTimeFormatter().format(LocalDateTime.now())); - File input = in.getCanonicalFile(); - int rootPathLength = input.toString().length() + 1; + File input = in.getCanonicalFile(); + int rootPathLength = input.toString().length() + 1; - Files.walk(input.toPath()).filter(path -> !path.equals(input.toPath()) && path.toFile().isFile() && !TextileBackup.config.fileBlacklist.contains(path.toString().substring(rootPathLength))).forEach(path -> { - File file = path.toAbsolutePath().toFile(); - try (FileInputStream fstream = new FileInputStream(file)) { - ZipArchiveEntry entry = new ZipArchiveEntry(file, file.getAbsolutePath().substring(rootPathLength)); - arc.putArchiveEntry(entry); + Files.walk(input.toPath()).filter( + path -> !path.equals(input.toPath()) && + path.toFile().isFile() && + !Utilities.isBlacklisted(input.toPath().relativize(path)) + ).forEach(path -> { + File file = path.toAbsolutePath().toFile(); - IOUtils.copy(fstream, arc); + try (FileInputStream fin = new FileInputStream(file); + BufferedInputStream bfin = new BufferedInputStream(fin)) { + ZipArchiveEntry entry = new ZipArchiveEntry(file, file.getAbsolutePath().substring(rootPathLength)); - arc.closeArchiveEntry(); - }catch (IOException e){ - TextileBackup.logger.error(e.getMessage()); - } - }); + arc.putArchiveEntry(entry); + IOUtils.copy(bfin, arc); - arc.finish(); - } catch (IOException e) { - TextileBackup.logger.error(e.getMessage()); - } + arc.closeArchiveEntry(); + } catch (IOException e) { + TextileBackup.logger.error(e.getMessage()); + } + }); - Utilities.log("Compression finished", ctx); - } -} + arc.finish(); + } catch (IOException e) { + TextileBackup.logger.error(e.getMessage()); + } + + Utilities.log("Compression finished", ctx); + } +} \ No newline at end of file diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 73e90a0..2c8d3d6 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -29,6 +29,6 @@ "depends": { "fabricloader": ">=0.7.2", "fabric": "*", - "minecraft": "1.16.*" + "minecraft": "1.15.*" } }