From 063ef02f2b5b95efc5961d4fbe90e8196f1bbc5e Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Sat, 17 Nov 2018 22:50:27 +0100 Subject: [PATCH] Let the installer create directories when necessary By creating directories when necessary before trying to install files we depend on less details in how the bootstrap zip is constructed. --- .../java/com/termux/app/TermuxInstaller.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/termux/app/TermuxInstaller.java b/app/src/main/java/com/termux/app/TermuxInstaller.java index 3ca3bf75..f1818650 100644 --- a/app/src/main/java/com/termux/app/TermuxInstaller.java +++ b/app/src/main/java/com/termux/app/TermuxInstaller.java @@ -96,14 +96,17 @@ final class TermuxInstaller { String oldPath = parts[0]; String newPath = STAGING_PREFIX_PATH + "/" + parts[1]; symlinks.add(Pair.create(oldPath, newPath)); + + ensureDirectoryExists(new File(newPath).getParentFile()); } } else { String zipEntryName = zipEntry.getName(); File targetFile = new File(STAGING_PREFIX_PATH, zipEntryName); - if (zipEntry.isDirectory()) { - if (!targetFile.mkdirs()) - throw new RuntimeException("Failed to create directory: " + targetFile.getAbsolutePath()); - } else { + boolean isDirectory = zipEntry.isDirectory(); + + ensureDirectoryExists(isDirectory ? targetFile : targetFile.getParentFile()); + + if (!isDirectory) { try (FileOutputStream outStream = new FileOutputStream(targetFile)) { int readBytes; while ((readBytes = zipInput.read(buffer)) != -1) @@ -158,8 +161,14 @@ final class TermuxInstaller { }.start(); } + private static void ensureDirectoryExists(File directory) { + if (!directory.isDirectory() && !directory.mkdirs()) { + throw new RuntimeException("Unable to create directory: " + directory.getAbsolutePath()); + } + } + /** Get bootstrap zip url for this systems cpu architecture. */ - static URL determineZipUrl() throws MalformedURLException { + private static URL determineZipUrl() throws MalformedURLException { String archName = determineTermuxArchName(); return new URL("https://termux.net/bootstrap/bootstrap-" + archName + ".zip"); } @@ -201,7 +210,7 @@ final class TermuxInstaller { } } - public static void setupStorageSymlinks(final Context context) { + static void setupStorageSymlinks(final Context context) { final String LOG_TAG = "termux-storage"; new Thread() { public void run() {