Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 60 lines (45 sloc) 1.31 KB
#!/usr/bin/env bash
set -efuo pipefail
log() {
echo "$@"
}
size_of() {
du -s "$1" | awk '{print $1}'
}
target=$(readlink --canonicalize "$1")
yesterday="$(date --date=yesterday +'%s')"
# 1GB
limit_size=1000000
bin_dir="$(dirname "$0")"
# shellcheck source=/dev/null
. "$bin_dir/garbage-conf"
cur_size=$(size_of "$target")
if [[ "$cur_size" -gt "$limit_size" ]]
then
log "Cleaning $target..."
find "$target" -not -type l -not -user "$USER" -exec chown "$USER" {} +
find "$target" -not -type l -not -writable -exec chmod +w {} +
while read -r -d $'\0' garbage
do
timestamp="$(echo "$garbage" | cut -d ' ' -f 1)"
path="$(echo "$garbage" | cut -d ' ' -f 2-)"
garbage_size=$(size_of "$path")
if [[ "$(bc <<< "$timestamp > $yesterday")" -eq 1 ]]
then
log "File $path is newer than 24 hours. Stopping."
break
fi
"rm" -f "$path"
cur_size=$((cur_size-garbage_size))
if [[ "$cur_size" -lt "$limit_size" ]]
then
log "Size of target is now $cur_size < $limit_size."
break
fi
done < <(
find "$target" -type f -printf '%C@ %p\0' \
| sort -z -k 1 -n)
else
log "Size of $target is $cur_size < $limit_size. Not cleaning."
fi
find "$target" -type d -empty -delete