interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / research / policy.sh
| 1 | #!/bin/sh |
| 2 | # Cooperative REMOVE replacement only: bounded stdin and namespace-atomic rename under the |
| 3 | # stable REMOVE.lock also held by sync.sh from final policy read through activation. |
| 4 | # No fsync/power-loss durability claim. Raw manual editor writes are outside this protocol. |
| 5 | set -eu |
| 6 | umask 077 |
| 7 | export LC_ALL=C |
| 8 | [ "$#" = 1 ] || { echo 'usage: policy.sh REMOVE < replacement-policy' >&2; exit 2; } |
| 9 | remove=$1 |
| 10 | parent=$(CDPATH= cd -- "$(dirname -- "$remove")" && pwd -P) |
| 11 | remove=$parent/$(basename -- "$remove") |
| 12 | work=$(mktemp -d "$parent/.research-policy.XXXXXXXX") |
| 13 | trap 'rm -rf "$work"' EXIT |
| 14 | trap 'exit 1' HUP INT TERM |
| 15 | # Read at most one byte beyond the policy boundary; overflow never replaces the old file. |
| 16 | head -c 65536 > "$work/remove" |
| 17 | [ "$(wc -c < "$work/remove")" -le 65535 ] || { echo 'research policy: input exceeds 65535 bytes' >&2; exit 1; } |
| 18 | version=$(sha256sum < "$work/remove" | cut -d ' ' -f1) |
| 19 | # The lock's inode is stable: never remove or rename REMOVE.lock. |
| 20 | exec 6>>"$remove.lock" |
| 21 | flock 6 |
| 22 | [ ! -e "$remove" ] || [ -f "$remove" ] || { echo 'research policy: target is not a regular file' >&2; exit 1; } |
| 23 | [ ! -L "$remove" ] || { echo 'research policy: target is a symlink' >&2; exit 1; } |
| 24 | mv -Tf "$work/remove" "$remove" |
| 25 | # Receipt is emitted before releasing the lock. If sync already owns the lock this acknowledgement |
| 26 | # waits for that activation; it then belongs to the next pass. A prior acknowledgement is included. |
| 27 | printf 'REMOVE_REPLACED sha256=%s\n' "$version" |