From 6b94dc2dbdfdec5dcd94fc735d15fdb14bb7c023 Mon Sep 17 00:00:00 2001 From: zzz Date: Wed, 24 Jul 2019 13:37:20 +0000 Subject: [PATCH] configclients: Only save files that have changed when in split config --- .../web/helpers/ConfigClientsHandler.java | 30 +++++++++++++++---- .../i2p/router/startup/ClientAppConfig.java | 17 +++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigClientsHandler.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigClientsHandler.java index bc860e626e..e05d91c093 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigClientsHandler.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigClientsHandler.java @@ -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 clients = ClientAppConfig.getClientApps(_context); + // will save if split config + List saveClients = new ArrayList(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,12 +310,21 @@ 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 - ClientAppConfig.writeClientAppConfig(_context, clients); + 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); + } } /** diff --git a/router/java/src/net/i2p/router/startup/ClientAppConfig.java b/router/java/src/net/i2p/router/startup/ClientAppConfig.java index 600bbbae4f..f66e527c1e 100644 --- a/router/java/src/net/i2p/router/startup/ClientAppConfig.java +++ b/router/java/src/net/i2p/router/startup/ClientAppConfig.java @@ -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