From be3249d10122fdf7532221b0611e6d9e9aca9014 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Forsman?= Date: Thu, 1 Feb 2024 22:22:45 +0100 Subject: [PATCH] create: Use $XDG_CACHE_HOME, keep cache files in $HOME/.cache/distrobox by default (#1082) * Read $XDG_CACHE_HOME, fall back to $HOME/.cache This allows configuring where the cache dir is located, according to XDG Base Directory Specification (https://specifications.freedesktop.org/basedir-spec/latest/ar01s03.html). * Change default cache dir from ~/.cache to ~/.cache/distrobox I think it's a good idea to keep the cache files grouped under a directory, indicating ownership, and this is the most common thing to do in GNU/Linux distros. --- distrobox-create | 12 +++++++----- distrobox-enter | 16 +++++++++------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/distrobox-create b/distrobox-create index 4087e768d7..51304ddc9d 100755 --- a/distrobox-create +++ b/distrobox-create @@ -94,6 +94,8 @@ distrobox_hostexec_path="$(cd "$(dirname "${0}")" && pwd)/distrobox-host-exec" verbose=0 version="1.6.0.1" +app_cache_dir=${XDG_CACHE_HOME:-"${HOME}/.cache"}/distrobox + # Source configuration files, this is done in an hierarchy so local files have # priority over system defaults # leave priority to environment variables. @@ -210,9 +212,9 @@ EOF # Outputs: # print usage with examples. show_compatibility() { - if [ ! -e "${HOME}/.cache/distrobox-compatibility-${version}" ] || - [ ! -s "${HOME}/.cache/distrobox-compatibility-${version}" ]; then - mkdir -p "${HOME}/.cache" + if [ ! -e "${app_cache_dir}/distrobox-compatibility-${version}" ] || + [ ! -s "${app_cache_dir}/distrobox-compatibility-${version}" ]; then + mkdir -p "${app_cache_dir}" # If we don't have a cache file, we need connectivity. Ensure we have # one and return error if not. @@ -228,9 +230,9 @@ show_compatibility() { cut -d '|' -f 4 | sed 's|
|\n|g' | tr -d ' ' | - sort -u > "${HOME}/.cache/distrobox-compatibility-${version}" + sort -u > "${app_cache_dir}/distrobox-compatibility-${version}" fi - cat "${HOME}/.cache/distrobox-compatibility-${version}" + cat "${app_cache_dir}/distrobox-compatibility-${version}" } # Parse arguments diff --git a/distrobox-enter b/distrobox-enter index 96a97a4dee..7df2b0a9f8 100755 --- a/distrobox-enter +++ b/distrobox-enter @@ -28,10 +28,12 @@ # DBX_SKIP_WORKDIR # DBX_SUDO_PROGRAM +app_cache_dir=${XDG_CACHE_HOME:-"${HOME}/.cache"}/distrobox + trap cleanup TERM INT HUP EXIT cleanup() { - rm -f "${HOME}/.cache/.${container_name}.fifo" + rm -f "${app_cache_dir}/.${container_name}.fifo" if [ -n "${logs_pid:-}" ]; then kill "${logs_pid:-}" 2> /dev/null || : fi @@ -527,14 +529,14 @@ if [ "${container_status}" != "running" ]; then fi printf >&2 "%-40s\t" "Starting container..." - mkdir -p "${HOME}/.cache/" - rm -f "${HOME}/.cache/.${container_name}.fifo" - mkfifo "${HOME}/.cache/.${container_name}.fifo" + mkdir -p "${app_cache_dir}" + rm -f "${app_cache_dir}/.${container_name}.fifo" + mkfifo "${app_cache_dir}/.${container_name}.fifo" while true; do # save starting loop timestamp in temp variable, we'll use it # after to let logs command minimize possible holes ${container_manager} logs -f "${container_name}" 2> /dev/null \ - > "${HOME}/.cache/.${container_name}.fifo" & + > "${app_cache_dir}/.${container_name}.fifo" & logs_pid="$!" # read logs from log_timestamp to now, line by line @@ -559,10 +561,10 @@ if [ "${container_status}" != "running" ]; then ;; *) ;; esac - done < "${HOME}/.cache/.${container_name}.fifo" + done < "${app_cache_dir}/.${container_name}.fifo" done # cleanup fifo - rm -f "${HOME}/.cache/.${container_name}.fifo" + rm -f "${app_cache_dir}/.${container_name}.fifo" printf >&2 "\nContainer Setup Complete!\n" fi