From 3f571bebefdf1bfccba7ec9c942cd1808dd5e8ff Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Thu, 12 Feb 2015 08:18:53 -0600 Subject: [PATCH] Handles unreadable $HOME gracefully - superseeds PR 98 (rscarvalho) --- CHANGES | 1 + .../brut/androlib/res/AndrolibResources.java | 30 +++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGES b/CHANGES index c9036af2..9869547d 100644 --- a/CHANGES +++ b/CHANGES @@ -58,6 +58,7 @@ v2.0.0 (TBA) -Fixed (issue #702) - Fixed improper handling of MNC_ZERO which caused dupe`d resources -Fixed (issue #744) - Fixed warnings of "Cleaning up unclosed ZipFile..." -Fixed (issue #757) - Download gradle binaries over https +-Fixed (issue #402) - Fix issues when running user has no access to $HOME. -Fixed issue with APKs with multiple dex files. -Fixed issue with using Apktool without smali/baksmali for ApktoolProperties (Thanks teprrr) -Fixed issue with non-URI standard characters in apk name (Thanks rover12421) diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/AndrolibResources.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/AndrolibResources.java index b8effa33..a3c66b88 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/AndrolibResources.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/AndrolibResources.java @@ -742,34 +742,50 @@ final public class AndrolibResources { } private File getFrameworkDir() throws AndrolibException { + if (mFrameworkDirectory != null) { + return mFrameworkDirectory; + } + String path; // if a framework path was specified on the command line, use it if (apkOptions.frameworkFolderLocation != null) { path = apkOptions.frameworkFolderLocation; - } else if (OSDetection.isMacOSX()) { - path = System.getProperty("user.home") + File.separatorChar + "Library" + File.separatorChar + - "apktool" + File.separatorChar + "framework"; } else { - path = System.getProperty("user.home") + File.separatorChar + "apktool" + File.separatorChar + "framework"; + File parentPath = new File(System.getProperty("user.home")); + if (! parentPath.canWrite()) { + LOGGER.severe(String.format("WARNING: Could not write to $HOME (%s), using %s instead...", + parentPath.getAbsolutePath(), System.getProperty("java.io.tmpdir"))); + LOGGER.severe("Please be aware this is a volatile directory and frameworks could go missing, " + + "please utilize --frame-path if the default storage directory is unavailable"); + + parentPath = new File(System.getProperty("java.io.tmpdir")); + } + + if (OSDetection.isMacOSX()) { + path = parentPath.getAbsolutePath() + String.format("%1$sLibrary%1$sapktool%1$sframework", File.separatorChar); + } else { + path = parentPath.getAbsolutePath() + String.format("%1$sapktool%1$sframework", File.separatorChar); + } } File dir = new File(path); if (dir.getParentFile() != null && dir.getParentFile().isFile()) { - System.err.println("Please remove file at " + dir.getParentFile()); + LOGGER.severe("Please remove file at " + dir.getParentFile()); System.exit(1); } if (! dir.exists()) { if (! dir.mkdirs()) { if (apkOptions.frameworkFolderLocation != null) { - System.err.println("Can't create Framework directory: " + dir); + LOGGER.severe("Can't create Framework directory: " + dir); } throw new AndrolibException("Can't create directory: " + dir); } } + mFrameworkDirectory = dir; return dir; } @@ -820,6 +836,8 @@ final public class AndrolibResources { private final static Logger LOGGER = Logger.getLogger(AndrolibResources.class.getName()); + private File mFrameworkDirectory = null; + private String mMinSdkVersion = null; private String mMaxSdkVersion = null; private String mTargetSdkVersion = null;