Skip to content

Commit

Permalink
Add logging for termux bootstrap package installation and setup of st…
Browse files Browse the repository at this point in the history
…orage symlinks
  • Loading branch information
agnostic-apollo committed Mar 23, 2021
1 parent 1b5e5b5 commit 92b804d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
47 changes: 33 additions & 14 deletions app/src/main/java/com/termux/app/TermuxInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
* Install the Termux bootstrap packages if necessary by following the below steps:
* <p/>
* (1) If $PREFIX already exist, assume that it is correct and be done. Note that this relies on that we do not create a
* broken $PREFIX folder below.
* broken $PREFIX directory below.
* <p/>
* (2) A progress dialog is shown with "Installing..." message and a spinner.
* <p/>
* (3) A staging folder, $STAGING_PREFIX, is {@link #deleteFolder(File)} if left over from broken installation below.
* (3) A staging directory, $STAGING_PREFIX, is {@link #deleteDirectory(File)} if left over from broken installation below.
* <p/>
* (4) The zip file is loaded from a shared library.
* <p/>
Expand All @@ -49,16 +49,21 @@ final class TermuxInstaller {

/** Performs setup if necessary. */
static void setupIfNeeded(final Activity activity, final Runnable whenDone) {
Logger.logInfo(LOG_TAG, "Installing " + TermuxConstants.TERMUX_APP_NAME + " bootstrap packages.");

// Termux can only be run as the primary user (device owner) since only that
// account has the expected file system paths. Verify that:
UserManager um = (UserManager) activity.getSystemService(Context.USER_SERVICE);
boolean isPrimaryUser = um.getSerialNumberForUser(android.os.Process.myUserHandle()) == 0;
if (!isPrimaryUser) {
new AlertDialog.Builder(activity).setTitle(R.string.bootstrap_error_title).setMessage(R.string.bootstrap_error_not_primary_user_message)
String bootstrapErrorMessage = activity.getString(R.string.bootstrap_error_not_primary_user_message, TermuxConstants.TERMUX_PREFIX_DIR_PATH);
Logger.logError(LOG_TAG, bootstrapErrorMessage);
new AlertDialog.Builder(activity).setTitle(R.string.bootstrap_error_title).setMessage(bootstrapErrorMessage)
.setOnDismissListener(dialog -> System.exit(0)).setPositiveButton(android.R.string.ok, null).show();
return;
}

Logger.logInfo(LOG_TAG, "Creating prefix directory \"" + TermuxConstants.TERMUX_PREFIX_DIR_PATH + "\".");
final File PREFIX_FILE = TermuxConstants.TERMUX_PREFIX_DIR;
if (PREFIX_FILE.isDirectory()) {
whenDone.run();
Expand All @@ -74,9 +79,12 @@ public void run() {
final File STAGING_PREFIX_FILE = new File(STAGING_PREFIX_PATH);

if (STAGING_PREFIX_FILE.exists()) {
deleteFolder(STAGING_PREFIX_FILE);
Logger.logInfo(LOG_TAG, "Deleting prefix staging directory \"" + TermuxConstants.TERMUX_STAGING_PREFIX_DIR_PATH + "\".");
deleteDirectory(STAGING_PREFIX_FILE);
}

Logger.logInfo(LOG_TAG, "Extracting bootstrap zip to prefix staging directory \"" + TermuxConstants.TERMUX_STAGING_PREFIX_DIR_PATH + "\".");

final byte[] buffer = new byte[8096];
final List<Pair<String, String>> symlinks = new ArrayList<>(50);

Expand Down Expand Up @@ -125,10 +133,13 @@ public void run() {
Os.symlink(symlink.first, symlink.second);
}

Logger.logInfo(LOG_TAG, "Moving prefix staging to prefix directory.");

if (!STAGING_PREFIX_FILE.renameTo(PREFIX_FILE)) {
throw new RuntimeException("Unable to rename staging folder");
throw new RuntimeException("Moving prefix staging to prefix directory failed");
}

Logger.logInfo(LOG_TAG, "Bootstrap packages installed successfully.");
activity.runOnUiThread(whenDone);
} catch (final Exception e) {
Logger.logStackTraceWithMessage(LOG_TAG, "Bootstrap error", e);
Expand All @@ -139,9 +150,9 @@ public void run() {
dialog.dismiss();
activity.finish();
}).setPositiveButton(R.string.bootstrap_error_try_again, (dialog, which) -> {
dialog.dismiss();
TermuxInstaller.setupIfNeeded(activity, whenDone);
}).show();
dialog.dismiss();
TermuxInstaller.setupIfNeeded(activity, whenDone);
}).show();
} catch (WindowManager.BadTokenException e1) {
// Activity already dismissed - ignore.
}
Expand Down Expand Up @@ -173,14 +184,14 @@ public static byte[] loadZipBytes() {

public static native byte[] getZip();

/** Delete a folder and all its content or throw. Don't follow symlinks. */
static void deleteFolder(File fileOrDirectory) throws IOException {
/** Delete a directory and all its content or throw. Don't follow symlinks. */
static void deleteDirectory(File fileOrDirectory) throws IOException {
if (fileOrDirectory.getCanonicalPath().equals(fileOrDirectory.getAbsolutePath()) && fileOrDirectory.isDirectory()) {
File[] children = fileOrDirectory.listFiles();

if (children != null) {
for (File child : children) {
deleteFolder(child);
deleteDirectory(child);
}
}
}
Expand All @@ -192,25 +203,30 @@ static void deleteFolder(File fileOrDirectory) throws IOException {

static void setupStorageSymlinks(final Context context) {
final String LOG_TAG = "termux-storage";

Logger.logInfo(LOG_TAG, "Setting up storage symlinks.");

new Thread() {
public void run() {
try {
File storageDir = TermuxConstants.TERMUX_STORAGE_HOME_DIR;

if (storageDir.exists()) {
try {
deleteFolder(storageDir);
deleteDirectory(storageDir);
} catch (IOException e) {
Logger.logError(LOG_TAG, "Could not delete old $HOME/storage, " + e.getMessage());
Logger.logStackTraceWithMessage(LOG_TAG, "Failed to delete old ~/storage directory", e);
return;
}
}

if (!storageDir.mkdirs()) {
Logger.logError(LOG_TAG, "Unable to mkdirs() for $HOME/storage");
Logger.logError(LOG_TAG, "Unable to create ~/storage directory.");
return;
}

Logger.logInfo(LOG_TAG, "Setting up storage symlinks at ~/storage/shared, ~/storage/downloads, ~/storage/dcim, ~/storage/pictures, ~/storage/music and ~/storage/movies for directories in \"" + Environment.getExternalStorageDirectory().getAbsolutePath() + "\".");

File sharedDir = Environment.getExternalStorageDirectory();
Os.symlink(sharedDir.getAbsolutePath(), new File(storageDir, "shared").getAbsolutePath());

Expand All @@ -235,9 +251,12 @@ public void run() {
File dir = dirs[i];
if (dir == null) continue;
String symlinkName = "external-" + i;
Logger.logInfo(LOG_TAG, "Setting up storage symlinks at ~/storage/" + symlinkName + " for \"" + dir.getAbsolutePath() + "\".");
Os.symlink(dir.getAbsolutePath(), new File(storageDir, symlinkName).getAbsolutePath());
}
}

Logger.logInfo(LOG_TAG, "Storage symlinks created successfully.");
} catch (Exception e) {
Logger.logStackTraceWithMessage(LOG_TAG, "Error setting up link", e);
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/termux/app/TermuxService.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void onDestroy() {

if (termuxTmpDir.exists()) {
try {
TermuxInstaller.deleteFolder(termuxTmpDir.getCanonicalFile());
TermuxInstaller.deleteDirectory(termuxTmpDir.getCanonicalFile());
} catch (Exception e) {
Logger.logStackTraceWithMessage(LOG_TAG, "Error while removing file at " + termuxTmpDir.getAbsolutePath(), e);
}
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
<string name="toggle_keep_screen_on">Keep screen on</string>
<string name="autofill_password">Autofill password</string>

<string name="bootstrap_installer_body">Installing…</string>
<string name="bootstrap_error_title">Unable to install</string>
<string name="bootstrap_installer_body">Installing bootstrap packages…</string>
<string name="bootstrap_error_title">Unable to install bootstrap</string>
<string name="bootstrap_error_body">&TERMUX_APP_NAME; was unable to install the bootstrap packages.</string>
<string name="bootstrap_error_abort">Abort</string>
<string name="bootstrap_error_try_again">Try again</string>
<string name="bootstrap_error_not_primary_user_message">&TERMUX_APP_NAME; can only be installed on the primary user account.</string>
<string name="bootstrap_error_not_primary_user_message">&TERMUX_APP_NAME; can only be run as the primary user.\nBootstrap binaries compiled for &TERMUX_APP_NAME; have hardcoded $PREFIX path and cannot be installed under any path other than \"%1$s\".</string>

<string name="max_terminals_reached_title">Max terminals reached</string>
<string name="max_terminals_reached_message">Close down existing ones before creating new.</string>
Expand Down

0 comments on commit 92b804d

Please sign in to comment.