fix(gui): handle config save error, don't write partial config
This commit is contained in:
@@ -15,6 +15,8 @@ import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.beust.jcommander.DynamicParameter;
|
||||
import com.beust.jcommander.IStringConverter;
|
||||
@@ -43,6 +45,7 @@ import jadx.core.utils.exceptions.JadxRuntimeException;
|
||||
import jadx.core.utils.files.FileUtils;
|
||||
|
||||
public class JadxCLIArgs implements IJadxConfig {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(JadxCLIArgs.class);
|
||||
|
||||
@JadxConfigExclude
|
||||
@Parameter(description = "<input files> (.apk, .dex, .jar, .class, .smali, .zip, .aar, .arsc, .aab, .xapk, .apkm, .jadx.kts)")
|
||||
@@ -376,11 +379,15 @@ public class JadxCLIArgs implements IJadxConfig {
|
||||
}
|
||||
if (!argsObj.config.equalsIgnoreCase("none")) {
|
||||
// load config file and merge with command line args
|
||||
configAdapter.useConfigRef(argsObj.config);
|
||||
T configObj = configAdapter.load();
|
||||
if (configObj != null) {
|
||||
jcw.overrideProvided(configObj);
|
||||
argsObj = configObj;
|
||||
try {
|
||||
configAdapter.useConfigRef(argsObj.config);
|
||||
T configObj = configAdapter.load();
|
||||
if (configObj != null) {
|
||||
jcw.overrideProvided(configObj);
|
||||
argsObj = configObj;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.error("Config load failed, continue with default values", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package jadx.cli.config;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.function.Consumer;
|
||||
@@ -12,17 +11,12 @@ import com.google.gson.FieldAttributes;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
import jadx.commons.app.JadxCommonFiles;
|
||||
import jadx.core.utils.GsonUtils;
|
||||
import jadx.core.utils.exceptions.JadxArgsValidateException;
|
||||
import jadx.core.utils.exceptions.JadxRuntimeException;
|
||||
|
||||
import static java.nio.file.StandardOpenOption.CREATE;
|
||||
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
|
||||
import static java.nio.file.StandardOpenOption.WRITE;
|
||||
|
||||
public class JadxConfigAdapter<T extends IJadxConfig> {
|
||||
private static final ExclusionStrategy GSON_EXCLUSION_STRATEGY = new ExclusionStrategy() {
|
||||
@Override
|
||||
@@ -81,9 +75,10 @@ public class JadxConfigAdapter<T extends IJadxConfig> {
|
||||
}
|
||||
|
||||
public void save(T configObject) {
|
||||
try (JsonWriter writer = gson.newJsonWriter(
|
||||
Files.newBufferedWriter(configPath, StandardCharsets.UTF_8, WRITE, CREATE, TRUNCATE_EXISTING))) {
|
||||
gson.toJson(configObject, configCls, writer);
|
||||
try {
|
||||
String jsonStr = gson.toJson(configObject, configCls);
|
||||
// don't use stream writer here because serialization errors will corrupt config
|
||||
Files.writeString(configPath, jsonStr);
|
||||
} catch (Exception e) {
|
||||
throw new JadxRuntimeException("Failed to save config file: " + configPath, e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user