From 5e820ad2491acac8b07cc662cb24012cf82f52da Mon Sep 17 00:00:00 2001 From: agnostic-apollo Date: Thu, 17 Mar 2022 22:35:31 +0500 Subject: [PATCH] Added: Allow users to adjust `$TMPDIR` clear mechanism on termux exit The `delete-tmpdir-files-older-than-x-days-on-exit` key can be used to adjust how many days old the access time should be of files that should be deleted from `$TMPDIR` on termux exit. The user can set an integer value between `-1` and `100000`. Set `-1` to delete no files, `0` to delete all files and `> 0` for `x` days. The default value is `3` days. So adding an entry like `delete-tmpdir-files-older-than-x-days-on-exit=10` to `termux.properties` file will make termux delete files older than `10` when termux is exited. After updating the value, either restart termux or run `termux-reload-settings` for changes to take effect. Note that currently `> 0` will revert back to `0` since deletion is currently broken for empty sub directories and deletion needs to be done based on access time instead of modified time. It will need to be fixed in a later commit. Check `FileUtils.deleteFilesOlderThanXDays()`. Related issue #2350 --- .../com/termux/shared/file/FileUtils.java | 4 +++ .../properties/TermuxPropertyConstants.java | 20 +++++++++++- .../properties/TermuxSharedProperties.java | 24 ++++++++++++++ .../shared/termux/shell/TermuxShellUtils.java | 32 +++++++++++++++++-- 4 files changed, 76 insertions(+), 4 deletions(-) diff --git a/termux-shared/src/main/java/com/termux/shared/file/FileUtils.java b/termux-shared/src/main/java/com/termux/shared/file/FileUtils.java index 0ca949ae..512aca2f 100644 --- a/termux-shared/src/main/java/com/termux/shared/file/FileUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/file/FileUtils.java @@ -1352,6 +1352,10 @@ public class FileUtils { } } + // TODO: Use FileAttributes with support for atime (default), mtime, ctime. Add regex for ignoring file and dir absolute paths. + // FIXME: iterateFiles() does not return subdirectories even with TrueFileFilter for file and dir. + // FIXME: Empty directories remain + // If directory exists, delete its contents Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -(days)); diff --git a/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxPropertyConstants.java b/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxPropertyConstants.java index f7dc825e..34e3dfed 100644 --- a/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxPropertyConstants.java +++ b/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxPropertyConstants.java @@ -17,7 +17,7 @@ import java.util.List; import java.util.Set; /* - * Version: v0.16.0 + * Version: v0.17.0 * SPDX-License-Identifier: MIT * * Changelog @@ -73,6 +73,9 @@ import java.util.Set; * * - 0.16.0 (2021-10-21) * - Add `KEY_NIGHT_MODE`. + * + * - 0.17.0 (2022-03-17) + * - Add `KEY_DELETE_TMPDIR_FILES_OLDER_THAN_X_DAYS_ON_EXIT`. */ /** @@ -96,6 +99,7 @@ public final class TermuxPropertyConstants { public static final String KEY_DISABLE_HARDWARE_KEYBOARD_SHORTCUTS = "disable-hardware-keyboard-shortcuts"; // Default: "disable-hardware-keyboard-shortcuts" + /** Defines the key for whether a toast will be shown when user changes the terminal session */ public static final String KEY_DISABLE_TERMINAL_SESSION_CHANGE_TOAST = "disable-terminal-session-change-toast"; // Default: "disable-terminal-session-change-toast" @@ -199,6 +203,19 @@ public final class TermuxPropertyConstants { + + /** + * Defines the key for how many days old the access time should be of files that should be + * deleted from $TMPDIR on termux exit. + * `-1` for none, `0` for all and `> 0` for x days. + */ + public static final String KEY_DELETE_TMPDIR_FILES_OLDER_THAN_X_DAYS_ON_EXIT = "delete-tmpdir-files-older-than-x-days-on-exit"; // Default: "delete-tmpdir-files-older-than-x-days-on-exit" + public static final int IVALUE_DELETE_TMPDIR_FILES_OLDER_THAN_X_DAYS_ON_EXIT_MIN = -1; + public static final int IVALUE_DELETE_TMPDIR_FILES_OLDER_THAN_X_DAYS_ON_EXIT_MAX = 100000; + public static final int DEFAULT_IVALUE_DELETE_TMPDIR_FILES_OLDER_THAN_X_DAYS_ON_EXIT = 3; + + + /** Defines the key for the terminal margin on left and right in dp units */ public static final String KEY_TERMINAL_MARGIN_HORIZONTAL = "terminal-margin-horizontal"; // Default: "terminal-margin-horizontal" public static final int IVALUE_TERMINAL_MARGIN_HORIZONTAL_MIN = 0; @@ -370,6 +387,7 @@ public final class TermuxPropertyConstants { /* int */ KEY_BELL_BEHAVIOUR, + KEY_DELETE_TMPDIR_FILES_OLDER_THAN_X_DAYS_ON_EXIT, KEY_TERMINAL_CURSOR_BLINK_RATE, KEY_TERMINAL_CURSOR_STYLE, KEY_TERMINAL_MARGIN_HORIZONTAL, diff --git a/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxSharedProperties.java b/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxSharedProperties.java index 4297c532..4b1b7bfd 100644 --- a/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxSharedProperties.java +++ b/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxSharedProperties.java @@ -247,6 +247,8 @@ public abstract class TermuxSharedProperties { /* int */ case TermuxPropertyConstants.KEY_BELL_BEHAVIOUR: return (int) getBellBehaviourInternalPropertyValueFromValue(value); + case TermuxPropertyConstants.KEY_DELETE_TMPDIR_FILES_OLDER_THAN_X_DAYS_ON_EXIT: + return (int) getDeleteTMPDIRFilesOlderThanXDaysOnExitInternalPropertyValueFromValue(value); case TermuxPropertyConstants.KEY_TERMINAL_CURSOR_BLINK_RATE: return (int) getTerminalCursorBlinkRateInternalPropertyValueFromValue(value); case TermuxPropertyConstants.KEY_TERMINAL_CURSOR_STYLE: @@ -320,6 +322,24 @@ public abstract class TermuxSharedProperties { return (int) SharedProperties.getDefaultIfNotInMap(TermuxPropertyConstants.KEY_BELL_BEHAVIOUR, TermuxPropertyConstants.MAP_BELL_BEHAVIOUR, SharedProperties.toLowerCase(value), TermuxPropertyConstants.DEFAULT_IVALUE_BELL_BEHAVIOUR, true, LOG_TAG); } + /** + * Returns the int for the value if its not null and is between + * {@link TermuxPropertyConstants#IVALUE_DELETE_TMPDIR_FILES_OLDER_THAN_X_DAYS_ON_EXIT_MIN} and + * {@link TermuxPropertyConstants#IVALUE_DELETE_TMPDIR_FILES_OLDER_THAN_X_DAYS_ON_EXIT_MAX}, + * otherwise returns {@link TermuxPropertyConstants#DEFAULT_IVALUE_DELETE_TMPDIR_FILES_OLDER_THAN_X_DAYS_ON_EXIT}. + * + * @param value The {@link String} value to convert. + * @return Returns the internal value for value. + */ + public static int getDeleteTMPDIRFilesOlderThanXDaysOnExitInternalPropertyValueFromValue(String value) { + return SharedProperties.getDefaultIfNotInRange(TermuxPropertyConstants.KEY_DELETE_TMPDIR_FILES_OLDER_THAN_X_DAYS_ON_EXIT, + DataUtils.getIntFromString(value, TermuxPropertyConstants.DEFAULT_IVALUE_DELETE_TMPDIR_FILES_OLDER_THAN_X_DAYS_ON_EXIT), + TermuxPropertyConstants.DEFAULT_IVALUE_DELETE_TMPDIR_FILES_OLDER_THAN_X_DAYS_ON_EXIT, + TermuxPropertyConstants.IVALUE_DELETE_TMPDIR_FILES_OLDER_THAN_X_DAYS_ON_EXIT_MIN, + TermuxPropertyConstants.IVALUE_DELETE_TMPDIR_FILES_OLDER_THAN_X_DAYS_ON_EXIT_MAX, + true, true, LOG_TAG); + } + /** * Returns the int for the value if its not null and is between * {@link TermuxPropertyConstants#IVALUE_TERMINAL_CURSOR_BLINK_RATE_MIN} and @@ -583,6 +603,10 @@ public abstract class TermuxSharedProperties { return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_BELL_BEHAVIOUR, true); } + public int getDeleteTMPDIRFilesOlderThanXDaysOnExit() { + return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_DELETE_TMPDIR_FILES_OLDER_THAN_X_DAYS_ON_EXIT, true); + } + public int getTerminalCursorBlinkRate() { return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_TERMINAL_CURSOR_BLINK_RATE, true); } diff --git a/termux-shared/src/main/java/com/termux/shared/termux/shell/TermuxShellUtils.java b/termux-shared/src/main/java/com/termux/shared/termux/shell/TermuxShellUtils.java index 10583977..f3771f43 100644 --- a/termux-shared/src/main/java/com/termux/shared/termux/shell/TermuxShellUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/termux/shell/TermuxShellUtils.java @@ -5,11 +5,15 @@ import android.content.Context; import androidx.annotation.NonNull; import com.termux.shared.errors.Error; +import com.termux.shared.file.filesystem.FileTypes; import com.termux.shared.termux.TermuxConstants; import com.termux.shared.file.FileUtils; import com.termux.shared.logger.Logger; import com.termux.shared.android.PackageUtils; import com.termux.shared.termux.TermuxUtils; +import com.termux.shared.termux.settings.properties.TermuxAppSharedProperties; + +import org.apache.commons.io.filefilter.TrueFileFilter; import java.io.File; import java.io.FileInputStream; @@ -27,6 +31,8 @@ public class TermuxShellUtils { public static String TERMUX_API_VERSION_NAME; + private static final String LOG_TAG = "TermuxShellUtils"; + public static String getDefaultWorkingDirectoryPath() { return TermuxConstants.TERMUX_HOME_DIR_PATH; } @@ -159,9 +165,29 @@ public class TermuxShellUtils { return; Error error; - error = FileUtils.clearDirectory("$TMPDIR", FileUtils.getCanonicalPath(TermuxConstants.TERMUX_TMP_PREFIX_DIR_PATH, null)); - if (error != null) { - Logger.logErrorExtended(error.toString()); + + TermuxAppSharedProperties properties = TermuxAppSharedProperties.getProperties(); + int days = properties.getDeleteTMPDIRFilesOlderThanXDaysOnExit(); + + // Disable currently until FileUtils.deleteFilesOlderThanXDays() is fixed. + if (days > 0) + days = 0; + + if (days < 0) { + Logger.logInfo(LOG_TAG, "Not clearing termux $TMPDIR"); + } else if (days == 0) { + error = FileUtils.clearDirectory("$TMPDIR", + FileUtils.getCanonicalPath(TermuxConstants.TERMUX_TMP_PREFIX_DIR_PATH, null)); + if (error != null) { + Logger.logErrorExtended(LOG_TAG, "Failed to clear termux $TMPDIR\n" + error); + } + } else { + error = FileUtils.deleteFilesOlderThanXDays("$TMPDIR", + FileUtils.getCanonicalPath(TermuxConstants.TERMUX_TMP_PREFIX_DIR_PATH, null), + TrueFileFilter.INSTANCE, days, true, FileTypes.FILE_TYPE_ANY_FLAGS); + if (error != null) { + Logger.logErrorExtended(LOG_TAG, "Failed to delete files from termux $TMPDIR older than " + days + " days\n" + error); + } } }