This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 218
/
CleanupCommand.java
143 lines (111 loc) · 5.12 KB
/
CleanupCommand.java
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Syncany, www.syncany.org
* Copyright (C) 2011-2014 Philipp C. Heckel <philipp.heckel@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.syncany.cli;
import static java.util.Arrays.asList;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.syncany.database.MultiChunkEntry;
import org.syncany.operations.cleanup.CleanupOperationOptions;
import org.syncany.operations.cleanup.CleanupOperationResult;
import org.syncany.operations.status.StatusOperationOptions;
public class CleanupCommand extends Command {
@Override
public CommandScope getRequiredCommandScope() {
return CommandScope.INITIALIZED_LOCALDIR;
}
@Override
public int execute(String[] operationArgs) throws Exception {
CleanupOperationOptions operationOptions = parseOptions(operationArgs);
CleanupOperationResult operationResult = client.cleanup(operationOptions);
printResults(operationResult);
return 0;
}
public CleanupOperationOptions parseOptions(String[] operationArgs) throws Exception {
CleanupOperationOptions operationOptions = new CleanupOperationOptions();
OptionParser parser = new OptionParser();
parser.allowsUnrecognizedOptions();
OptionSpec<Void> optionNoDatabaseMerge = parser.acceptsAll(asList("M", "no-database-merge"));
OptionSpec<Void> optionNoOldVersionRemoval = parser.acceptsAll(asList("V", "no-version-remove"));
OptionSpec<Integer> optionKeepVersions = parser.acceptsAll(asList("k", "keep-versions")).withRequiredArg().ofType(Integer.class);
OptionSet options = parser.parse(operationArgs);
// -M, --no-database-merge
operationOptions.setMergeRemoteFiles(!options.has(optionNoDatabaseMerge));
// -V, --no-version-removal
operationOptions.setRemoveOldVersions(!options.has(optionNoOldVersionRemoval));
// -k=<count>, --keep-versions=<count>
if (options.has(optionKeepVersions)) {
int keepVersionCount = options.valueOf(optionKeepVersions);
if (keepVersionCount < 1) {
throw new Exception("Invalid value for --keep-versions="+keepVersionCount+"; must be >= 1");
}
operationOptions.setKeepVersionsCount(options.valueOf(optionKeepVersions));
}
// Parse 'status' options
operationOptions.setStatusOptions(parseStatusOptions(operationArgs));
// Does this configuration make sense
boolean nothingToDo = !operationOptions.isMergeRemoteFiles() && operationOptions.isRemoveOldVersions();
if (nothingToDo) {
throw new Exception("Invalid parameter configuration: -M and -V cannot be set together. Nothing to do.");
}
return operationOptions;
}
private StatusOperationOptions parseStatusOptions(String[] operationArgs) {
StatusCommand statusCommand = new StatusCommand();
return statusCommand.parseOptions(operationArgs);
}
private void printResults(CleanupOperationResult operationResult) {
switch (operationResult.getResultCode()) {
case NOK_DIRTY_LOCAL:
out.println("Cannot cleanup database if local repository is in a dirty state; Call 'up' first.");
break;
case NOK_LOCAL_CHANGES:
out.println("Local changes detected. Please call 'up' first'.");
break;
case NOK_REMOTE_CHANGES:
out.println("Remote changes detected or repository is locked by another user. Please call 'down' first.");
break;
case NOK_OTHER_OPERATIONS_RUNNING:
out.println("Cannot run cleanup while other clients are performing up/down/cleanup. Try again later.");
break;
case OK:
if (operationResult.getMergedDatabaseFilesCount() > 0) {
out.println(operationResult.getMergedDatabaseFilesCount() + " database files merged.");
}
if (operationResult.getRemovedMultiChunks().size() > 0) {
long totalRemovedMultiChunkSize = 0;
for (MultiChunkEntry removedMultiChunk : operationResult.getRemovedMultiChunks().values()) {
totalRemovedMultiChunkSize += removedMultiChunk.getSize();
}
out.printf("%d multichunk(s) deleted on remote storage (freed %.2f MB)\n",
operationResult.getRemovedMultiChunks().size(), (double) totalRemovedMultiChunkSize / 1024 / 1024);
}
if (operationResult.getRemovedOldVersionsCount() > 0) {
out.println(operationResult.getRemovedOldVersionsCount() + " file histories shortened.");
// TODO [low] This counts only the file histories, not file versions; not very helpful!
}
out.println("Cleanup successful.");
break;
case OK_NOTHING_DONE:
out.println("Cleanup not necessary. Nothing done.");
break;
default:
throw new RuntimeException("Invalid result code: " + operationResult.getResultCode().toString());
}
}
}