blob: be7734da6725365378a51c95f33bc159cd4bfe94 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package com.keuin.kbackupfabric.operation;
import com.keuin.kbackupfabric.util.PrintUtil;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import java.io.File;
import java.io.IOException;
import static com.keuin.kbackupfabric.util.BackupFilesystemUtil.getBackupFileName;
import static com.keuin.kbackupfabric.util.BackupFilesystemUtil.getBackupSaveDirectory;
import static com.keuin.kbackupfabric.util.PrintUtil.msgErr;
import static com.keuin.kbackupfabric.util.PrintUtil.msgInfo;
import static org.apache.commons.io.FileUtils.forceDelete;
class DeleteOperation extends AbstractConfirmableOperation {
//private static final Logger LOGGER = LogManager.getLogger();
private final String backupName;
private final CommandContext<ServerCommandSource> context;
DeleteOperation(CommandContext<ServerCommandSource> context, String backupName) {
this.backupName = backupName;
this.context = context;
}
@Override
public boolean confirm() {
MinecraftServer server = context.getSource().getMinecraftServer();
String backupFileName = getBackupFileName(backupName);
PrintUtil.info("Deleting backup " + backupName);
File backupFile = new File(getBackupSaveDirectory(server), backupFileName);
int tryCounter = 0;
do {
if (tryCounter == 5) {
String msg = "Failed to delete file " + backupFileName;
PrintUtil.error(msg);
msgErr(context, msg);
return false;
}
try {
if (!backupFile.delete())
forceDelete(backupFile);
} catch (SecurityException | NullPointerException | IOException ignored) {
}
++tryCounter;
} while (backupFile.exists());
PrintUtil.info("Deleted backup " + backupName);
msgInfo(context, "Deleted backup " + backupName);
return true;
}
@Override
public String toString() {
return String.format("deletion of %s", backupName);
}
}
|