Skip to content

Commit

Permalink
create: Use $XDG_CACHE_HOME, keep cache files in $HOME/.cache/distrob…
Browse files Browse the repository at this point in the history
…ox 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.
  • Loading branch information
bjornfor authored Feb 1, 2024
1 parent 202f13f commit be3249d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
12 changes: 7 additions & 5 deletions distrobox-create
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -228,9 +230,9 @@ show_compatibility() {
cut -d '|' -f 4 |
sed 's|<br>|\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
Expand Down
16 changes: 9 additions & 7 deletions distrobox-enter
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit be3249d

Please sign in to comment.