Ensure termux files directory is accessible before bootstrap installation and provide better info when running as secondary user/profile

Termux will check if termux files directory `/data/data/com.termux/files` has rwx permission access before installing bootstrap or starting terminal. Missing permission will automatically be set if possible. The `/data/data/com.termux` directory will also be created if it did not already exist, like if android did not already create it.

Users will now also be shown a crash notification if they attempt to start termux as a secondary user or in a work profile with info of the "alternate" termux files directory `/data/user/<id>/com.termux` set by android and the profile owner app if running under work profile (not secondary user). A notification will also be shown if the termux files directory (not "alternate") is not accessible.

Related #2168
This commit is contained in:
agnostic-apollo
2021-07-10 15:56:52 +05:00
parent b2a071aad9
commit 6fa4b9b7cd
6 changed files with 144 additions and 11 deletions

View File

@@ -1,7 +1,10 @@
package com.termux.shared.file;
import android.content.Context;
import android.os.Environment;
import androidx.annotation.NonNull;
import com.termux.shared.models.errors.Error;
import com.termux.shared.termux.TermuxConstants;
@@ -9,6 +12,7 @@ import java.io.File;
import java.util.regex.Pattern;
public class TermuxFileUtils {
/**
* Replace "$PREFIX/" or "~/" prefix with termux absolute paths.
*
@@ -120,4 +124,41 @@ public class TermuxFileUtils {
ignoreErrorsIfPathIsInParentDirPath, ignoreIfNotExecutable);
}
/**
* Validate the existence and permissions of {@link TermuxConstants#TERMUX_FILES_DIR_PATH}.
*
* The directory will not be created manually but by calling {@link Context#getFilesDir()}
* so that android itself creates it. The `/data/data/[package_name]` directory cannot be
* created by an app itself. Note that the path returned by {@link Context#getFilesDir()} will
* be under `/data/user/[id]/[package_name]` instead of `/data/data/[package_name]`
* defined by default by {@link TermuxConstants#TERMUX_FILES_DIR_PATH}, where id will be 0 for
* primary user and a higher number for other users/profiles. If app is running under work profile
* or secondary user, then {@link TermuxConstants#TERMUX_FILES_DIR_PATH} will not be accessible
* and will not be automatically created, unless there is a bind mount from `/data/user/[id]`
* to `/data/data`, ideally in the right namespace.
*
* The permissions set to directory will be {@link FileUtils#APP_WORKING_DIRECTORY_PERMISSIONS}.
*
* https://source.android.com/devices/tech/admin/multi-user
*
* @param context The {@link Context} for operations.
* @param createDirectoryIfMissing The {@code boolean} that decides if directory file
* should be created if its missing.
* @param setMissingPermissions The {@code boolean} that decides if permissions are to be
* automatically set.
* @return Returns the {@code error} if path is not a directory file, failed to create it,
* or validating permissions failed, otherwise {@code null}.
*/
public static Error isTermuxFilesDirectoryAccessible(@NonNull final Context context, boolean createDirectoryIfMissing, boolean setMissingPermissions) {
if (createDirectoryIfMissing)
context.getFilesDir();
if (setMissingPermissions)
FileUtils.setMissingFilePermissions("Termux files directory", TermuxConstants.TERMUX_FILES_DIR_PATH,
FileUtils.APP_WORKING_DIRECTORY_PERMISSIONS);
return FileUtils.checkMissingFilePermissions("Termux files directory", TermuxConstants.TERMUX_FILES_DIR_PATH,
FileUtils.APP_WORKING_DIRECTORY_PERMISSIONS, false);
}
}