diff --git a/src/main/java/net/szum123321/textile_backup/ConfigHandler.java b/src/main/java/net/szum123321/textile_backup/ConfigHandler.java index c9ca13c..e37e4e7 100644 --- a/src/main/java/net/szum123321/textile_backup/ConfigHandler.java +++ b/src/main/java/net/szum123321/textile_backup/ConfigHandler.java @@ -45,11 +45,11 @@ public class ConfigHandler { @Comment("\nShould every world has its won backup folder?\n") public boolean perWorldBackup = false; - @Comment("\nMaximum number of backups to keep. if 0 then no backup will be deleted based on its amount\n") + @Comment("\nMaximum number of backups to keep. If 0 then no backup will be deleted based on its amount\n") public 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 int maxAge = 0; + public long maxAge = 0; @Comment("\nMaximum size of backup folder in kilo bytes. \n") public int maxSize = 0; @@ -70,7 +70,7 @@ public class ConfigHandler { public Set playerWhitelist = new HashSet<>(); @Comment("\nPlayers banned from running backup commands besides their sufficient permission level\n") - public Set playerBlocklist = new HashSet<>(); + public Set playerBlacklist = new HashSet<>(); @Comment("\nFormat of date&time used to name backup files.\n") public String dateTimeFormat = "dd.MM.yyyy_HH-mm-ss"; diff --git a/src/main/java/net/szum123321/textile_backup/TextileBackup.java b/src/main/java/net/szum123321/textile_backup/TextileBackup.java index ff0b631..054f932 100644 --- a/src/main/java/net/szum123321/textile_backup/TextileBackup.java +++ b/src/main/java/net/szum123321/textile_backup/TextileBackup.java @@ -54,7 +54,7 @@ public class TextileBackup implements ModInitializer { try { return ((config.playerWhitelist.contains(ctx.getEntityOrThrow().getEntityName()) || ctx.hasPermissionLevel(config.permissionLevel)) && - !config.playerBlocklist.contains(ctx.getEntityOrThrow().getEntityName())) || + !config.playerBlacklist.contains(ctx.getEntityOrThrow().getEntityName())) || (ctx.getMinecraftServer().isSinglePlayer() && config.alwaysSingleplayerAllowed); }catch (Exception e){ //Command was called from server console. diff --git a/src/main/java/net/szum123321/textile_backup/commands/BlacklistCommand.java b/src/main/java/net/szum123321/textile_backup/commands/BlacklistCommand.java index a9a3521..5a4b926 100644 --- a/src/main/java/net/szum123321/textile_backup/commands/BlacklistCommand.java +++ b/src/main/java/net/szum123321/textile_backup/commands/BlacklistCommand.java @@ -40,7 +40,7 @@ public class BlacklistCommand { builder.append("Currently on the blacklist are: "); - for(String name : TextileBackup.config.playerBlocklist){ + for(String name : TextileBackup.config.playerBlacklist){ builder.append(name); builder.append(", "); } @@ -53,10 +53,10 @@ public class BlacklistCommand { private static int executeAdd(CommandContext ctx) throws CommandSyntaxException { PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player"); - if(TextileBackup.config.playerBlocklist.contains(player.getEntityName())) { + if(TextileBackup.config.playerBlacklist.contains(player.getEntityName())) { ctx.getSource().sendFeedback(new TranslatableText("Player: %s is already blacklisted.", player.getEntityName()), false); }else{ - TextileBackup.config.playerBlocklist.add(player.getEntityName()); + TextileBackup.config.playerBlacklist.add(player.getEntityName()); ConfigManager.saveConfig(TextileBackup.config); StringBuilder builder = new StringBuilder(); @@ -81,10 +81,10 @@ public class BlacklistCommand { private static int executeRemove(CommandContext ctx) throws CommandSyntaxException { PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player"); - if(!TextileBackup.config.playerBlocklist.contains(player.getEntityName())) { + if(!TextileBackup.config.playerBlacklist.contains(player.getEntityName())) { ctx.getSource().sendFeedback(new TranslatableText("Player: %s newer was blacklisted.", player.getEntityName()), false); }else{ - TextileBackup.config.playerBlocklist.remove(player.getEntityName()); + TextileBackup.config.playerBlacklist.remove(player.getEntityName()); ConfigManager.saveConfig(TextileBackup.config); StringBuilder builder = new StringBuilder(); diff --git a/src/main/java/net/szum123321/textile_backup/commands/WhitelistCommand.java b/src/main/java/net/szum123321/textile_backup/commands/WhitelistCommand.java index 83947e1..c85a33b 100644 --- a/src/main/java/net/szum123321/textile_backup/commands/WhitelistCommand.java +++ b/src/main/java/net/szum123321/textile_backup/commands/WhitelistCommand.java @@ -65,8 +65,8 @@ public class WhitelistCommand { builder.append(player.getEntityName()); builder.append(" added to the whitelist"); - if(TextileBackup.config.playerBlocklist.contains(player.getEntityName())){ - TextileBackup.config.playerBlocklist.remove(player.getEntityName()); + if(TextileBackup.config.playerBlacklist.contains(player.getEntityName())){ + TextileBackup.config.playerBlacklist.remove(player.getEntityName()); builder.append(" and removed form the blacklist"); } 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 8d88fc1..8d40756 100644 --- a/src/main/java/net/szum123321/textile_backup/core/MakeBackupThread.java +++ b/src/main/java/net/szum123321/textile_backup/core/MakeBackupThread.java @@ -59,7 +59,7 @@ public class MakeBackupThread implements Runnable { return; } - Compressor.createArchive(world, outFile, ctx); + ZipCompressor.createArchive(world, outFile, ctx); BackupHelper.executeFileLimit(ctx, server.getWorld(DimensionType.OVERWORLD).getLevelProperties().getLevelName()); diff --git a/src/main/java/net/szum123321/textile_backup/core/ZipCompressor.java b/src/main/java/net/szum123321/textile_backup/core/ZipCompressor.java new file mode 100644 index 0000000..a4f2ded --- /dev/null +++ b/src/main/java/net/szum123321/textile_backup/core/ZipCompressor.java @@ -0,0 +1,68 @@ +/* + A simple backup mod for Fabric + Copyright (C) 2020 Szum123321 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package net.szum123321.textile_backup.core; + +import net.minecraft.server.command.ServerCommandSource; +import net.szum123321.textile_backup.TextileBackup; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; +import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream; +import org.apache.commons.compress.utils.IOUtils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.time.LocalDateTime; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +public class ZipCompressor { + public static void createArchive(File in, File out, ServerCommandSource ctx){ + Utilities.log("Starting compression...", ctx); + + try (ZipOutputStream arc = new ZipOutputStream(new FileOutputStream(out))){ + arc.setLevel(TextileBackup.config.compression); + arc.setComment("Created on: " + Utilities.getDateTimeFormatter().format(LocalDateTime.now())); + + 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 -> { + try{ + File file = path.toAbsolutePath().toFile(); + + ZipEntry entry = new ZipEntry(file.getAbsolutePath().substring(rootPathLength)); + arc.putNextEntry(entry); + entry.setSize(file.length()); + IOUtils.copy(new FileInputStream(file), arc); + arc.closeEntry(); + }catch (IOException e){ + TextileBackup.logger.error(e.getMessage()); + } + }); + + } catch (IOException e) { + TextileBackup.logger.error(e.getMessage()); + } + + Utilities.log("Compression finished", ctx); + } +}