configclients: Only save files that have changed when in split config

This commit is contained in:
zzz
2019-07-24 13:37:20 +00:00
parent 3bfbb6aef6
commit 6b94dc2dbd
2 changed files with 42 additions and 5 deletions

View File

@ -7,6 +7,7 @@ import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@ -256,12 +257,21 @@ public class ConfigClientsHandler extends FormHandler {
}
private void saveClientChanges2() throws IOException {
// will save if not split config
List<ClientAppConfig> clients = ClientAppConfig.getClientApps(_context);
// will save if split config
List<ClientAppConfig> saveClients = new ArrayList<ClientAppConfig>(clients.size() + 1);
boolean isSplitConfig = ClientAppConfig.isSplitConfig(_context);
for (int cur = 0; cur < clients.size(); cur++) {
ClientAppConfig ca = clients.get(cur);
Object val = _settings.get(cur + ".enabled");
if (! (RouterConsoleRunner.class.getName().equals(ca.className)))
ca.disabled = val == null;
if (! (RouterConsoleRunner.class.getName().equals(ca.className))) {
boolean newval = val == null;
if (ca.disabled != newval) {
ca.disabled = newval;
saveClients.add(ca);
}
}
// edit of an existing entry
if (_context.getBooleanProperty(ConfigClientsHelper.PROP_ENABLE_CLIENT_CHANGE) ||
isAdvanced()) {
@ -277,6 +287,7 @@ public class ConfigClientsHandler extends FormHandler {
ca.className = clss;
ca.args = args;
ca.clientName = getJettyString("nofilter_name" + cur);
saveClients.add(ca);
}
}
}
@ -299,13 +310,22 @@ public class ConfigClientsHandler extends FormHandler {
if (name == null || name.trim().length() <= 0) name = "new client";
ClientAppConfig ca = new ClientAppConfig(clss, name, args, 2*60*1000,
_settings.get(newClient + ".enabled") == null); // true for disabled
ClientAppConfig.writeClientAppConfig(_context, ca);
clients.add(ca);
saveClients.add(ca);
addFormNotice(_t("New client added") + ": " + name + " (" + clss + ").");
}
}
// Always save, as any of the disabled flags could have changed
if (isSplitConfig) {
// Only save what changed
// TODO this will lose other clients in the same file
for (ClientAppConfig ca : saveClients) {
ClientAppConfig.writeClientAppConfig(_context, ca);
}
} else {
// Save all
ClientAppConfig.writeClientAppConfig(_context, clients);
}
}
/**
* @since Implemented in 0.9.6 using ClientAppManager

View File

@ -112,6 +112,19 @@ public class ClientAppConfig {
uninstallargs = ua;
}
/*
* Only valid for the router's clients (not plugins).
* Only valid after getClientApps(ctx) has been called.
* @since 0.9.42
*/
public synchronized static boolean isSplitConfig(RouterContext ctx) {
File dir = new File(ctx.getConfigDir(), CLIENT_CONFIG_DIR);
return dir.exists() && !configFile(ctx).exists();
}
/*
* This is the old config file. Only valid if not a split config.
*/
public static File configFile(I2PAppContext ctx) {
String clientConfigFile = ctx.getProperty(PROP_CLIENT_CONFIG_FILENAME, DEFAULT_CLIENT_CONFIG_FILENAME);
File cfgFile = new File(clientConfigFile);
@ -380,6 +393,10 @@ public class ClientAppConfig {
}
/**
* This works for both split and non-split config.
* If this is the only config in the file, the file will be deleted;
* otherwise the file will be saved with the remaining configs.
*
* @return success
* @throws IllegalArgumentException if cac has a null configfile
* @since 0.9.42