Skip to content

Commit d4fc34c

Browse files
Move FileUtils to file package and define more file util functions
A lot of utils have been defined now that can be used to safely manage files. The java java.io.File API has poor support for detecting symlinks including broken symlinks. Android implementation also has issues. Check FileTypes.getFileType() function for more info. For this reason, the UnixFileAttributes and related classes has been ported from AOSP to get file attributes and type. Some file utils and android versions use google's Guava com.google.common.io.MoreFiles library for managing files, specially for safer directory deletion with SecureDirectoryStream. Some file utils and android versions use org.apache.commons.io.FileUtils for managing files. The library version used is 2.5 and it must not be incremented for compatibility with android version < 8, otherwise runtime crashes will occur.
1 parent c0323fe commit d4fc34c

15 files changed

+3219
-411
lines changed

app/build.gradle

+15-13
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@ android {
99
ndkVersion project.properties.ndkVersion
1010

1111
dependencies {
12-
implementation "androidx.annotation:annotation:1.1.0"
13-
implementation 'androidx.appcompat:appcompat:1.2.0'
14-
implementation 'androidx.core:core:1.5.0-beta03'
12+
implementation "androidx.annotation:annotation:1.2.0"
13+
implementation "androidx.core:core:1.5.0-rc01"
1514
implementation "androidx.drawerlayout:drawerlayout:1.1.1"
16-
implementation 'androidx.preference:preference:1.1.1'
15+
implementation "androidx.preference:preference:1.1.1"
1716
implementation "androidx.viewpager:viewpager:1.0.0"
18-
implementation 'com.google.guava:guava:24.1-jre'
19-
implementation "io.noties.markwon:core:$markwon_version"
20-
implementation "io.noties.markwon:ext-strikethrough:$markwon_version"
21-
implementation "io.noties.markwon:linkify:$markwon_version"
22-
implementation "io.noties.markwon:recycler:$markwon_version"
17+
implementation "com.google.guava:guava:24.1-jre"
18+
implementation "io.noties.markwon:core:$markwon_version"
19+
implementation "io.noties.markwon:ext-strikethrough:$markwon_version"
20+
implementation "io.noties.markwon:linkify:$markwon_version"
21+
implementation "io.noties.markwon:recycler:$markwon_version"
2322
implementation project(":terminal-view")
23+
24+
// Do not increment version higher than 2.5 or there
25+
// will be runtime exceptions on android < 8
26+
// due to missing classes like java.nio.file.Path.
27+
implementation "commons-io:commons-io:2.5"
2428
}
2529

2630
defaultConfig {
@@ -95,10 +99,8 @@ android {
9599
}
96100

97101
dependencies {
98-
implementation 'androidx.appcompat:appcompat:1.2.0'
99-
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
100-
testImplementation 'junit:junit:4.13.1'
101-
testImplementation 'org.robolectric:robolectric:4.4'
102+
testImplementation "junit:junit:4.13.1"
103+
testImplementation "org.robolectric:robolectric:4.4"
102104
}
103105

104106
task versionName {

app/src/main/java/com/termux/app/RunCommandService.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import com.termux.R;
1515
import com.termux.app.TermuxConstants.TERMUX_APP.RUN_COMMAND_SERVICE;
1616
import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
17-
import com.termux.app.utils.FileUtils;
17+
import com.termux.app.file.FileUtils;
1818
import com.termux.app.utils.Logger;
1919
import com.termux.app.utils.NotificationUtils;
2020
import com.termux.app.utils.PluginUtils;
@@ -354,9 +354,9 @@ public int onStartCommand(Intent intent, int flags, int startId) {
354354

355355
// If executable is not a regular file, or is not readable or executable, then just return
356356
// Setting of missing read and execute permissions is not done
357-
errmsg = FileUtils.validateRegularFileExistenceAndPermissions(this, executionCommand.executable,
358-
null, PluginUtils.PLUGIN_EXECUTABLE_FILE_PERMISSIONS,
359-
false, false);
357+
errmsg = FileUtils.validateRegularFileExistenceAndPermissions(this, "executable", executionCommand.executable, null,
358+
PluginUtils.PLUGIN_EXECUTABLE_FILE_PERMISSIONS, true, true,
359+
false);
360360
if (errmsg != null) {
361361
errmsg += "\n" + this.getString(R.string.msg_executable_absolute_path, executionCommand.executable);
362362
executionCommand.setStateFailed(ExecutionCommand.RESULT_CODE_FAILED, errmsg, null);
@@ -376,10 +376,9 @@ public int onStartCommand(Intent intent, int flags, int startId) {
376376
// under {@link TermuxConstants#TERMUX_FILES_DIR_PATH}
377377
// We try to set execute permissions, but ignore if they are missing, since only read and write permissions are required
378378
// for working directories.
379-
errmsg = FileUtils.validateDirectoryExistenceAndPermissions(this, executionCommand.workingDirectory,
380-
TermuxConstants.TERMUX_FILES_DIR_PATH, PluginUtils.PLUGIN_WORKING_DIRECTORY_PERMISSIONS,
381-
true, true, false,
382-
true);
379+
errmsg = FileUtils.validateDirectoryFileExistenceAndPermissions(this, "working", executionCommand.workingDirectory, TermuxConstants.TERMUX_FILES_DIR_PATH, true,
380+
PluginUtils.PLUGIN_WORKING_DIRECTORY_PERMISSIONS, true, true,
381+
true, true);
383382
if (errmsg != null) {
384383
errmsg += "\n" + this.getString(R.string.msg_working_directory_absolute_path, executionCommand.workingDirectory);
385384
executionCommand.setStateFailed(ExecutionCommand.RESULT_CODE_FAILED, errmsg, null);

0 commit comments

Comments
 (0)