summaryrefslogtreecommitdiff
path: root/rolling-directory
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2018-04-18 14:28:15 +0200
committerGregor Kleen <gkleen@yggdrasil.li>2018-04-18 14:28:15 +0200
commit1490b9cb5334b1b1704005884a6a4880ed51afc9 (patch)
treebb56adec63d07919d1d4baab2ca02beb27e3cbfe /rolling-directory
parentd3900cc71c7a5b06a932d56ce25f694ce31397b0 (diff)
downloadutils-1490b9cb5334b1b1704005884a6a4880ed51afc9.tar
utils-1490b9cb5334b1b1704005884a6a4880ed51afc9.tar.gz
utils-1490b9cb5334b1b1704005884a6a4880ed51afc9.tar.bz2
utils-1490b9cb5334b1b1704005884a6a4880ed51afc9.tar.xz
utils-1490b9cb5334b1b1704005884a6a4880ed51afc9.zip
rolling-directory
Diffstat (limited to 'rolling-directory')
-rw-r--r--rolling-directory58
1 files changed, 58 insertions, 0 deletions
diff --git a/rolling-directory b/rolling-directory
new file mode 100644
index 0000000..d85f8b7
--- /dev/null
+++ b/rolling-directory
@@ -0,0 +1,58 @@
1#!@zsh@/bin/zsh
2
3set -e
4
5PATH=@coreutils@/bin:@findutils@/bin:@gawk@/bin:@inotify@/bin:$PATH
6
7dir=${1:A}
8maxSize=$(numfmt --from=auto --to=none -- $2)
9shift 2
10
11[[ -d "${dir}" ]] || exit 2
12[[ "${maxSize}" = <-> ]] || exit 2
13
14typeset -a findExtra
15findExtra=(-not -path ${dir}/CACHEDIR.TAG)
16
17resize() {
18 echo "Resizing ${dir} to below $(numfmt --to=iec-i --suffix=B -- ${maxSize})..." >&2
19
20 while [[ $(du -xbs ${dir} | awk '{ print $1; }') -gt $maxSize ]]; do
21 find ${dir} -xdev -type f \( ${findExtra} \) -print0 | \
22 xargs -r0 -- stat --printf '%W %Y %Z %X %n\0' | \
23 sort -t '\0' -z | \
24 awk -F '\0' '{ gsub("^\\S+\\s+\\S+\\s+\\S+\\s+\\S+\\s+", "", $1); printf "%s\0", $1; }' | \
25 xargs -r0 -- rm -v
26 done
27
28 find ${dir} -xdev -xtype l -print0 | xargs -r0 -- rm -v
29 find ${dir} -xdev -type d \( -not -path ${dir} \) -empty -print0 | xargs -r0 -- rmdir -v
30}
31
32case "${1}" in
33 monitor)
34 shift
35
36 typeset -a inotifyExtra
37 inotifyExtra=()
38
39 if [[ "${1}" = <-> ]]; then
40 inotifyExtra=(-t "${1}")
41 shift
42 fi
43
44 while
45 resize
46 inotifywait -qq -r ${dir} ${inotifyExtra} \
47 -e modify \
48 -e close_write \
49 -e moved_to \
50 -e move_self \
51 -e create
52 true
53 do :; done
54 ;;
55 *)
56 resize
57 ;;
58esac