Commit b446bf27 authored by Skylot's avatar Skylot

refactor: move filesystem case sensitivity value to JadxArgs

parent b7109b1b
package jadx.cli;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jadx.api.JadxArgs;
import jadx.api.JadxDecompiler;
import jadx.core.utils.exceptions.JadxArgsValidateException;
import jadx.core.utils.files.FileUtils;
public class JadxCLI {
private static final Logger LOG = LoggerFactory.getLogger(JadxCLI.class);
......@@ -25,7 +31,9 @@ public class JadxCLI {
}
static int processAndSave(JadxCLIArgs inputArgs) {
JadxDecompiler jadx = new JadxDecompiler(inputArgs.toJadxArgs());
JadxArgs args = inputArgs.toJadxArgs();
args.setFsCaseSensitive(getFsCaseSensitivity(args));
JadxDecompiler jadx = new JadxDecompiler(args);
try {
jadx.load();
} catch (JadxArgsValidateException e) {
......@@ -42,4 +50,14 @@ public class JadxCLI {
}
return errorsCount;
}
private static boolean getFsCaseSensitivity(JadxArgs args) {
List<File> testDirList = new ArrayList<>(3);
testDirList.add(args.getOutDir());
testDirList.add(args.getOutDirSrc());
if (!args.getInputFiles().isEmpty()) {
testDirList.add(args.getInputFiles().get(0));
}
return FileUtils.isCaseSensitiveFS(testDirList);
}
}
......@@ -2,6 +2,7 @@ package jadx.api;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class JadxArgs {
......@@ -44,6 +45,8 @@ public class JadxArgs {
private boolean respectBytecodeAccModifiers = false;
private boolean exportAsGradleProject = false;
private boolean isFsCaseSensitive;
public JadxArgs() {
// use default options
}
......@@ -58,6 +61,10 @@ public class JadxArgs {
return inputFiles;
}
public void setInputFile(File inputFile) {
this.inputFiles = Collections.singletonList(inputFile);
}
public void setInputFiles(List<File> inputFiles) {
this.inputFiles = inputFiles;
}
......@@ -230,6 +237,14 @@ public class JadxArgs {
this.exportAsGradleProject = exportAsGradleProject;
}
public boolean isFsCaseSensitive() {
return isFsCaseSensitive;
}
public void setFsCaseSensitive(boolean fsCaseSensitive) {
isFsCaseSensitive = fsCaseSensitive;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("JadxArgs{");
......
......@@ -20,7 +20,6 @@ import jadx.core.dex.nodes.DexNode;
import jadx.core.dex.nodes.FieldNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.nodes.RootNode;
import jadx.core.utils.files.FileUtils;
import jadx.core.utils.files.InputFile;
public class RenameVisitor extends AbstractVisitor {
......@@ -45,8 +44,7 @@ public class RenameVisitor extends AbstractVisitor {
if (deobfuscationOn) {
deobfuscator.execute();
}
boolean isCaseSensitive = FileUtils.isCaseSensitiveFS(new File(inputPath)); // args.getOutDir() - not set in gui
checkClasses(root, isCaseSensitive);
checkClasses(root, args.isFsCaseSensitive());
}
private void checkClasses(RootNode root, boolean caseSensitive) {
......
......@@ -13,6 +13,7 @@ import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;
......@@ -198,8 +199,26 @@ public class FileUtils {
}
}
/**
* Checks dirs in order, fist success result returned
*/
public static boolean isCaseSensitiveFS(List<File> testDirList) {
for (File dir : testDirList) {
Optional<Boolean> result = isCaseSensitiveFSInternal(dir);
if (result.isPresent()) {
return result.get();
}
}
return IOCase.SYSTEM.isCaseSensitive();
}
public static boolean isCaseSensitiveFS(File testDir) {
if (testDir != null) {
Optional<Boolean> result = isCaseSensitiveFSInternal(testDir);
return result.orElseGet(IOCase.SYSTEM::isCaseSensitive);
}
private static Optional<Boolean> isCaseSensitiveFSInternal(@Nullable File testDir) {
if (testDir != null && testDir.exists() && testDir.isDirectory()) {
File caseCheckUpper = new File(testDir, "CaseCheck");
File caseCheckLow = new File(testDir, "casecheck");
try {
......@@ -208,7 +227,7 @@ public class FileUtils {
boolean caseSensitive = !caseCheckLow.exists();
LOG.debug("Filesystem at {} is {}case-sensitive", testDir.getAbsolutePath(),
(caseSensitive ? "" : "NOT "));
return caseSensitive;
return Optional.of(caseSensitive);
} else {
LOG.debug("Failed to create file: {}", caseCheckUpper.getAbsolutePath());
}
......@@ -223,7 +242,7 @@ public class FileUtils {
}
}
}
return IOCase.SYSTEM.isCaseSensitive();
return Optional.empty();
}
public static File toFile(String path) {
......
......@@ -78,6 +78,7 @@ public abstract class IntegrationTest extends TestUtils {
args.setShowInconsistentCode(true);
args.setThreadsCount(1);
args.setSkipResources(true);
args.setFsCaseSensitive(false); // use same value on all systems
}
public ClassNode getClassNode(Class<?> clazz) {
......
package jadx.gui;
import javax.swing.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -8,8 +9,6 @@ import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;
import javax.swing.ProgressMonitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -18,6 +17,7 @@ import jadx.api.JadxDecompiler;
import jadx.api.JavaClass;
import jadx.api.JavaPackage;
import jadx.api.ResourceFile;
import jadx.core.utils.files.FileUtils;
import jadx.gui.settings.JadxSettings;
public class JadxWrapper {
......@@ -34,8 +34,12 @@ public class JadxWrapper {
public void openFile(File file) {
this.openFile = file;
try {
this.decompiler = new JadxDecompiler(settings.toJadxArgs());
this.decompiler.getArgs().setInputFiles(Collections.singletonList(file));
JadxArgs jadxArgs = settings.toJadxArgs();
jadxArgs.setInputFile(file);
// output folder not known yet => use input dir as a best choice
jadxArgs.setFsCaseSensitive(FileUtils.isCaseSensitiveFS(file.getParentFile()));
this.decompiler = new JadxDecompiler(jadxArgs);
this.decompiler.load();
} catch (Exception e) {
LOG.error("Jadx init error", e);
......@@ -97,6 +101,7 @@ public class JadxWrapper {
}).collect(Collectors.toList());
}
// TODO: move to CLI and filter classes in JadxDecompiler
public List<String> getExcludedPackages() {
String excludedPackages = settings.getExcludedPackages().trim();
if (excludedPackages.isEmpty()) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment