scripts/ensure-tmp-symlink
#!/usr/bin/env bash
ensure-tmp-symlink
#
Guarantees that the project's tmp/ directory exists as a symbolic link into
a project-specific subdirectory of the system tmpfs at /tmp/, so that all
ephemeral output the project produces lives in RAM rather than on disk.
#
Idempotent: safe to call repeatedly from any script that is about to write
into tmp/. The first call materialises the tmpfs target and the symlink; later
calls verify them and exit cleanly. If the path already exists as a real
directory or as a symlink pointing somewhere else, the helper fails loudly
rather than silently rewriting it — silently moving someone's data into a
tmpfs would be worse than refusing and asking a human to investigate.
#
Usage:
scripts/ensure-tmp-symlink # uses hard-coded DIR
scripts/ensure-tmp-symlink /path/to/project/root # overrides DIR
source scripts/ensure-tmp-symlink # bash callers can source
set -eu
{{{ DIR resolution
Hard-coded project root with first-argument override, per the project-wide
convention for scripts.
DIR="${1:-/mnt/mtwo/programming/ai-stuff/neocities-modernization}"
}}}
{{{ TARGET resolution
The tmpfs-backed target lives under /tmp/ with a name that uniquely
identifies this project, so multiple projects on the same host do not
collide.
TMPFS_TARGET="/tmp/neocities-modernization"
SYMLINK_PATH="${DIR}/tmp"
}}}
{{{ ensure_tmpfs_target_exists
ensure_tmpfs_target_exists() {
# If the target subdirectory of /tmp/ does not exist yet, create it. Empty
# tmpfs directories cost nothing and are wiped on reboot, so the only
# consequence of a stale target is that it might have leftover bytes from a
# previous boot — harmless, since this directory is for ephemera anyway.
if [ ! -d "${TMPFS_TARGET}" ]; then
mkdir -p "${TMPFS_TARGET}"
fi
}
}}}
{{{ ensure_symlink_correct
ensure_symlink_correct() {
# Case 1: the symlink path does not exist at all. Create the symlink.
# This is the fresh-clone case.
if [ ! -e "${SYMLINK_PATH}" ] && [ ! -L "${SYMLINK_PATH}" ]; then
ln -s "${TMPFS_TARGET}" "${SYMLINK_PATH}"
return 0
fi
# Case 2: the path is a symlink and points to the right place. Nothing to
# do. This is the steady-state happy path that most calls hit.
if [ -L "${SYMLINK_PATH}" ]; then
local current_target
current_target=$(readlink "${SYMLINK_PATH}")
if [ "${current_target}" = "${TMPFS_TARGET}" ]; then
return 0
fi
# Case 3: the path is a symlink, but points somewhere else. Fail loud.
# The user may have configured an override they care about; we will not
# silently break it.
echo "ERROR: ${SYMLINK_PATH} is a symlink to '${current_target}'," >&2
echo " expected target '${TMPFS_TARGET}'. Investigate before" >&2
echo " re-running. To accept the expected target:" >&2
echo " rm '${SYMLINK_PATH}'" >&2
echo " scripts/ensure-tmp-symlink" >&2
exit 1
fi
# Case 4: the path exists but is not a symlink. Real directory or file.
# Fail loud — this is the violation case that drove this whole issue.
# Silent migration would risk losing whatever bytes the user has placed
# there.
echo "ERROR: ${SYMLINK_PATH} exists but is not a symbolic link." >&2
echo " Move its contents elsewhere, then remove it, then re-run." >&2
echo " Suggested:" >&2
echo " mv '${SYMLINK_PATH}' '${SYMLINK_PATH}.preserved'" >&2
echo " scripts/ensure-tmp-symlink" >&2
exit 1
}
}}}
ensure_tmpfs_target_exists
ensure_symlink_correct