VpnService: Tweaks for ConfigUpdater to fix adding

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Samuel Holland 2017-08-16 00:56:53 -05:00
parent c3203ce90a
commit b60536222d

View File

@ -288,8 +288,9 @@ public class VpnService extends Service {
this.oldConfig = oldConfig; this.oldConfig = oldConfig;
this.shouldConnect = shouldConnect; this.shouldConnect = shouldConnect;
newName = newConfig.getName(); newName = newConfig.getName();
oldName = oldConfig.getName(); // When adding a config, "old file" and "new file" are the same thing.
if (isRename() && configurations.containsKey(newName)) oldName = oldConfig != null ? oldConfig.getName() : newName;
if (isAddOrRename() && configurations.containsKey(newName))
throw new IllegalStateException("Config " + newName + " already exists"); throw new IllegalStateException("Config " + newName + " already exists");
} }
@ -298,7 +299,7 @@ public class VpnService extends Service {
Log.i(TAG, (oldConfig == null ? "Adding" : "Updating") + " config " + newName); Log.i(TAG, (oldConfig == null ? "Adding" : "Updating") + " config " + newName);
final File newFile = new File(getFilesDir(), newName + ".conf"); final File newFile = new File(getFilesDir(), newName + ".conf");
final File oldFile = new File(getFilesDir(), oldName + ".conf"); final File oldFile = new File(getFilesDir(), oldName + ".conf");
if (isRename() && newFile.exists()) { if (isAddOrRename() && newFile.exists()) {
Log.w(TAG, "Refusing to overwrite existing config configuration"); Log.w(TAG, "Refusing to overwrite existing config configuration");
return false; return false;
} }
@ -317,8 +318,12 @@ public class VpnService extends Service {
return true; return true;
} }
private boolean isAddOrRename() {
return oldConfig == null || !newName.equals(oldName);
}
private boolean isRename() { private boolean isRename() {
return oldConfig != null && !newConfig.getName().equals(oldConfig.getName()); return oldConfig != null && !newName.equals(oldName);
} }
@Override @Override