From: Alexander Barton Date: Wed, 21 Sep 2022 17:38:06 +0000 (+0200) Subject: 30_env.ax-io: Try to be even more clever when setting TMPDIR X-Git-Url: https://arthur.ath.cx/gitweb/?a=commitdiff_plain;h=7ecd2c9ecea3a49ed77d68ef4d9ce363577ccdbb;p=ax-zsh.git 30_env.ax-io: Try to be even more clever when setting TMPDIR Silently use the "runtime directory" of the user, when this is set already. And if not, make sure to not use "/" but fallback to "/tmp" when the mktemp(1) command failed for example. --- diff --git a/core/30_env/30_env.ax-io b/core/30_env/30_env.ax-io index 84bd1c2..6b101e3 100644 --- a/core/30_env/30_env.ax-io +++ b/core/30_env/30_env.ax-io @@ -8,18 +8,27 @@ fi # Validate temporary directory if [[ -z "$TMPDIR" ]]; then - TMPDIR="$(dirname $(mktemp -ut tmp.XXXXXXXXXX))/" - user_tmpdir="$TMPDIR$UID" - mkdir -p "$user_tmpdir" >/dev/null 2>&1 - if [[ -w "$user_tmpdir" ]]; then - TMPDIR="$user_tmpdir/" - chmod 0700 "$TMPDIR" + if [[ -n "$XDG_RUNTIME_DIR" && -w "$XDG_RUNTIME_DIR" ]]; then + # The "runtime directory" is set for this user, good, so use + # it silently as the "temporary directory", too: + TMPDIR="$XDG_RUNTIME_DIR" + else + # Try to find a sane "temporary directory", but warn the user + # that this is a best guess only! + TMPDIR="$(dirname $(mktemp -ut tmp.XXXXXXXXXX))/" + [[ -z "$TMPDIR" || "$TMPDIR" = "/" ]] && TMPDIR="/tmp" + user_tmpdir="$TMPDIR$UID" + mkdir -p "$user_tmpdir" >/dev/null 2>&1 + if [[ -w "$user_tmpdir" ]]; then + TMPDIR="$user_tmpdir/" + chmod 0700 "$TMPDIR" + fi + echo "Note: \"TMPDIR\" was not set, using \"$TMPDIR\"." >&2 + unset user_tmpdir fi - echo "WARNING: \"TMPDIR\" is not set, using \"$TMPDIR\" as default!" >&2 - unset user_tmpdir fi if [[ ! -w "$TMPDIR" ]]; then - echo "WARNING: Temporary directory \"$TMPDIR\" is not writable!" >&2 + echo "Warning: Temporary directory \"$TMPDIR\" is not writable!" >&2 fi # Setup XDG cache directory