Repository navigation
Expand file tree
/
Copy pathwrite-settings.sh
More file actions
executable file
·45 lines (39 loc) · 1.52 KB
/
Copy pathwrite-settings.sh
File metadata and controls
executable file
·45 lines (39 loc) · 1.52 KB
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
41
42
43
44
45
#!/usr/bin/env bash
# write-settings.sh <state-dir>
#
# Commit settings.json atomically. The JSON arrives on STDIN as ONE line:
# - not as an argument, because a webhook URL in argv is world-readable in
# /proc/<pid>/cmdline, and webhook URLs can carry access tokens;
# - one line, not until EOF, because Quickshell keeps a Process's stdin open:
# `cat` would block forever, while `read` returns at the newline.
#
# The temp file is created with mktemp in the target directory (a planted
# symlink at a fixed name cannot redirect the write). `mv -T` replaces the exact
# destination: without -T, `mv` moves the temp file *into* a directory named
# settings.json and still reports success.
set -uo pipefail
state_dir="${1:?usage: write-settings.sh <state-dir>}"
[[ -n "$state_dir" ]] || { echo "write-settings: empty state dir" >&2; exit 2; }
# Bounded read: one line, no EOF needed.
IFS= read -r json || { echo "write-settings: no payload on stdin" >&2; exit 2; }
if ! mkdir -p "$state_dir" 2>/dev/null; then
echo "write-settings: cannot create $state_dir" >&2
exit 1
fi
tmp=$(mktemp "$state_dir/settings.json.XXXXXX" 2>/dev/null) || {
echo "write-settings: cannot create a temp file in $state_dir" >&2
exit 1
}
cleanup() { rm -f "$tmp" 2>/dev/null || true; }
trap cleanup EXIT
umask 077
if ! printf '%s' "$json" > "$tmp"; then
echo "write-settings: cannot write $tmp" >&2
exit 1
fi
if ! mv -fT "$tmp" "$state_dir/settings.json"; then
echo "write-settings: cannot replace $state_dir/settings.json" >&2
exit 1
fi
trap - EXIT
exit 0