aboutsummaryrefslogtreecommitdiffstats
path: root/bin/disks-sample.sh
blob: 5291bcff3c3be66be05c0d9d1a87e8113db2feb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/bash
# Sample filesystem usage into a cache file, for widgets/disks.lua.
#
# This exists so the draw hook never calls statfs on an NFS path. An
# unreachable server blocks that call, and blocking the Cairo draw freezes the
# whole dashboard; here it costs a stale cache and nothing else.
#
# One NFS mount per server: Library and Slackware are the same export on one
# server, shared and backup_danix the same on another, so showing all four
# printed every number twice.

set -u

CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/udt"
CACHE="$CACHE_DIR/disks.txt"

MOUNTS=(/ /home /data /mnt/nfs/Library /mnt/nfs/shared)

umask 077
mkdir -p "$CACHE_DIR"

TMP="$CACHE.tmp.$$"
trap 'rm -f "$TMP"' EXIT

# /usr/bin/df by absolute path with -P -B1, deliberately:
#   - the user's shell aliases df to `df -h`, and an alias or a function would
#     hand the parser '1.6G' where it expects an integer
#   - -P is the POSIX format, one line per filesystem, mountpoint last
#   - -B1 is bytes, so the widget does the formatting and the parser never
#     has to interpret a suffix
if ! /usr/bin/df -P -B1 "${MOUNTS[@]}" > "$TMP" 2>/dev/null; then
    # A single unreachable mount must not discard the others: df still reports
    # the ones it could stat, so keep the output if it has any data rows.
    if [ "$(wc -l < "$TMP")" -lt 2 ]; then
        echo "disks-sample: df produced nothing usable" >&2
        exit 1
    fi
fi

mv -f "$TMP" "$CACHE"