]> arthur.barton.de Git - ax-zsh.git/blobdiff - core/30_env/30_env.ax-io
P10k: Read config after enabling instant prompt
[ax-zsh.git] / core / 30_env / 30_env.ax-io
index 33a165b579015de01f45c6859a673ee757a86f22..2a2c83982917c53821f50b53b8059a9c28af2ebd 100644 (file)
@@ -6,6 +6,45 @@ if [[ -z "$TZ" && -r "/etc/timezone" ]]; then
        TZ=$(<"/etc/timezone") export TZ
 fi
 
+# Validate temporary directory
+if [[ -z "$TMPDIR" || "$TMPDIR" = "/" ]]; then
+       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
+fi
+# Make sure TMPDIR ends in a slash (like on macOS by default): this makes its
+# usage a bit safer ...
+case "$TMPDIR" in
+       */) ;;
+       *)  TMPDIR="$TMPDIR/"
+esac
+if [[ ! -w "$TMPDIR" ]]; then
+       echo "Warning: Temporary directory \"$TMPDIR\" is not writable!" >&2
+fi
+export TMPDIR
+
+# TMPDIR is the only one required to be set, but make sure that TMP, TEMP
+# and TEMPDIR are set to the same sane path name when already present in the
+# environment:
+[[ -n "$TMP" ]] && export TMP="$TMPDIR"
+[[ -n "$TEMP" ]] && export TEMP="$TMPDIR"
+[[ -n "$TEMPDIR" ]] && export TEMPDIR="$TMPDIR"
+
 # Setup XDG cache directory
 if [[ -z "$XDG_CACHE_HOME" ]]; then
        XDG_CACHE_HOME="$LOCAL_HOME/.cache"
@@ -18,7 +57,7 @@ export XDG_CACHE_HOME
 
 # Setup XDG runtime directory
 if [[ -z "$XDG_RUNTIME_DIR" ]]; then
-       XDG_RUNTIME_DIR="${TMPDIR:-/tmp/${UID}-runtime-dir}"
+       XDG_RUNTIME_DIR="$TMPDIR/runtime-dir.$UID"
 fi
 if [[ ! -d "$XDG_RUNTIME_DIR" ]]; then
        mkdir -p "$XDG_RUNTIME_DIR"