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.
This commit is contained in:
Fredrik Fornwall
2018-11-17 22:50:27 +01:00
parent 6e6e4fd212
commit 063ef02f2b

View File

@@ -96,14 +96,17 @@ final class TermuxInstaller {
String oldPath = parts[0]; String oldPath = parts[0];
String newPath = STAGING_PREFIX_PATH + "/" + parts[1]; String newPath = STAGING_PREFIX_PATH + "/" + parts[1];
symlinks.add(Pair.create(oldPath, newPath)); symlinks.add(Pair.create(oldPath, newPath));
ensureDirectoryExists(new File(newPath).getParentFile());
} }
} else { } else {
String zipEntryName = zipEntry.getName(); String zipEntryName = zipEntry.getName();
File targetFile = new File(STAGING_PREFIX_PATH, zipEntryName); File targetFile = new File(STAGING_PREFIX_PATH, zipEntryName);
if (zipEntry.isDirectory()) { boolean isDirectory = zipEntry.isDirectory();
if (!targetFile.mkdirs())
throw new RuntimeException("Failed to create directory: " + targetFile.getAbsolutePath()); ensureDirectoryExists(isDirectory ? targetFile : targetFile.getParentFile());
} else {
if (!isDirectory) {
try (FileOutputStream outStream = new FileOutputStream(targetFile)) { try (FileOutputStream outStream = new FileOutputStream(targetFile)) {
int readBytes; int readBytes;
while ((readBytes = zipInput.read(buffer)) != -1) while ((readBytes = zipInput.read(buffer)) != -1)
@@ -158,8 +161,14 @@ final class TermuxInstaller {
}.start(); }.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. */ /** Get bootstrap zip url for this systems cpu architecture. */
static URL determineZipUrl() throws MalformedURLException { private static URL determineZipUrl() throws MalformedURLException {
String archName = determineTermuxArchName(); String archName = determineTermuxArchName();
return new URL("https://termux.net/bootstrap/bootstrap-" + archName + ".zip"); 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"; final String LOG_TAG = "termux-storage";
new Thread() { new Thread() {
public void run() { public void run() {