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
201 lines (154 loc) · 7.42 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* 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 java.util.regex.Matcher;
import java.util.regex.Pattern;
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> optionForce = parser.accepts("force");
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);
OptionSpec<String> optionSecondsBetweenCleanups = parser.acceptsAll(asList("t", "time-between-cleanups")).withRequiredArg().ofType(String.class);
OptionSpec<Integer> optionMaxDatabaseFiles = parser.acceptsAll(asList("x", "max-database-files")).withRequiredArg().ofType(Integer.class);
OptionSet options = parser.parse(operationArgs);
// -F, --force
operationOptions.setForce(options.has(optionForce));
// -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));
}
// -t=<count>, --time-between-cleanups=<count>
if (options.has(optionSecondsBetweenCleanups)) {
long secondsBetweenCleanups = parseTimePeriod(options.valueOf(optionSecondsBetweenCleanups));
if (secondsBetweenCleanups < 0) {
throw new Exception("Invalid value for --time-between-cleanups="+secondsBetweenCleanups+"; must be >= 0");
}
operationOptions.setMinSecondsBetweenCleanups(secondsBetweenCleanups);
}
// -d=<count>, --max-database-files=<count>
if (options.has(optionMaxDatabaseFiles)) {
int maxDatabaseFiles = options.valueOf(optionMaxDatabaseFiles);
if (maxDatabaseFiles < 1) {
throw new Exception("Invalid value for --max-database-files="+maxDatabaseFiles+"; must be >= 1");
}
operationOptions.setMaxDatabaseFiles(maxDatabaseFiles);
}
// 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());
}
}
private long parseTimePeriod(String period) {
Pattern relativeDatePattern = Pattern.compile("(\\d+(?:[.,]\\d+)?)(mo|[smhdwy])");
Matcher relativeDateMatcher = relativeDatePattern.matcher(period);
relativeDateMatcher.reset();
long periodSeconds = 0;
while (relativeDateMatcher.find()) {
double time = Double.parseDouble(relativeDateMatcher.group(1));
String unitStr = relativeDateMatcher.group(2).toLowerCase();
int unitMultiplier = 0;
if (unitStr.startsWith("mo")) { unitMultiplier = 30*24*60*60; } // must be before "m"
else if (unitStr.startsWith("s")) { unitMultiplier = 1; }
else if (unitStr.startsWith("m")) { unitMultiplier = 60; }
else if (unitStr.startsWith("h")) { unitMultiplier = 60*60; }
else if (unitStr.startsWith("d")) { unitMultiplier = 24*60*60; }
else if (unitStr.startsWith("w")) { unitMultiplier = 7*24*60*60; }
else if (unitStr.startsWith("y")) { unitMultiplier = 365*24*60*60; }
periodSeconds += (long) ((double)time*unitMultiplier);
}
return periodSeconds;
}
}