#!/usr/bin/env bash
# leak.sh <root> <ntasks>
#
# Mimic the agent's exec bookkeeping. For every task, for each of three
# containers, create <root>/task/<task>/<container>/<exec-id>/, put a FIFO
# in it, then remove only the <exec-id> leaf. The two parent directories
# are left behind, exactly like the bug.
#
# Runs are cumulative: a counter next to this script remembers how many
# tasks have been created so far, so you can call it repeatedly and watch
# df -i climb.
set -u
root=$1
n=$2
counter="$(dirname "$0")/$(basename "$root").counter"
start=$(cat "$counter" 2>/dev/null || echo 0)
end=$((start + n))

for t in $(seq $((start + 1)) "$end"); do
    task="$root/task/$(printf '%032x' "$t")"
    for c in app sidecar-a sidecar-b; do
        exec_dir="$task/$c/$(cat /proc/sys/kernel/random/uuid)"
        if ! mkdir -p "$exec_dir" || ! mkfifo "$exec_dir/stdout"; then
            echo "task=$t container=$c: failed"
            echo "$((t - 1))" >"$counter"
            exit 1
        fi
        rm -rf "$exec_dir"   # cleanup removes only the exec-id leaf
    done
done
echo "$end" >"$counter"
echo "tasks $((start + 1))..$end done; task dirs on fs: $(find "$root/task" -mindepth 1 -maxdepth 1 -type d | wc -l)"
