#!/usr/bin/env bash # Verifies claims in how-to-copy-and-move-files.html. # Must exit 0 standalone in an empty directory. Missing tools => SKIP, never FAIL. set -u PASS=0; FAIL=0; SKIP=0; CONTENT=0 # CONTENT must be initialised HERE, not inside the article-present branch: # under `set -u` an absent article left it unbound and the summary line aborted (exit 1). # Resolve the article's path to an ABSOLUTE directory before any cd into a scratch dir -- # otherwise a relative $(dirname "$0") breaks once we cd elsewhere, and the content checks # below would silently SKIP forever. SCRIPT_DIR="$(cd "$(dirname "$0")" 2>/dev/null && pwd)" ARTICLE="$SCRIPT_DIR/how-to-copy-and-move-files.html" WORK="" cleanup() { [ -n "$WORK" ] && rm -rf "$WORK"; } trap cleanup EXIT ok() { echo "PASS: $1"; PASS=$((PASS+1)); } bad() { echo "FAIL: $1"; FAIL=$((FAIL+1)); } skip() { echo "SKIP: $1"; SKIP=$((SKIP+1)); } if ! command -v cp >/dev/null 2>&1; then skip "cp not found on PATH" else WORK="$(mktemp -d 2>/dev/null || echo /tmp/verify-cpmv-$$)" mkdir -p "$WORK" 2>/dev/null cd "$WORK" 2>/dev/null || { skip "could not create scratch dir"; WORK=""; } fi if [ -n "$WORK" ]; then # Claim 1: BSD cp -R preserves a symlink; cp -r (BSD) dereferences it. mkdir -p srcdir echo target > target.txt ln -s ../target.txt srcdir/link.txt 2>/dev/null mkdir -p out_bigR out_smallr if cp -R srcdir out_bigR/ 2>/dev/null && cp -r srcdir out_smallr/ 2>/dev/null; then t_bigR=$(stat -f "%HT" out_bigR/srcdir/link.txt 2>/dev/null) t_smallr=$(stat -f "%HT" out_smallr/srcdir/link.txt 2>/dev/null) if [ "$t_bigR" = "Symbolic Link" ]; then ok "cp -R preserves a symlink (found: $t_bigR)" else bad "cp -R did not preserve the symlink (found: $t_bigR)" fi # Only assert the -r divergence on BSD cp (stat -f is BSD-only, so this whole # branch already implies we're on a BSD userland where the divergence applies). if [ "$t_smallr" = "Regular File" ]; then ok "cp -r dereferences the symlink on this BSD cp (found: $t_smallr)" else bad "cp -r did not dereference (found: $t_smallr) -- BSD behavior not reproduced" fi else skip "cp -R / cp -r recursive copy failed to run" fi # Claim 2: trailing slash on source changes what BSD cp -R copies. mkdir -p dest_noslash dest_slash if cp -R srcdir dest_noslash/ 2>/dev/null && cp -R srcdir/ dest_slash/ 2>/dev/null; then if [ -d dest_noslash/srcdir ] && [ -f dest_slash/link.txt ] && [ ! -e dest_slash/srcdir ]; then ok "trailing slash on source changes cp -R's output (BSD-specific)" else bad "trailing slash behavior did not match expectation" fi else skip "trailing-slash cp -R test failed to run" fi # Claim 3: -p preserves mode across umask; plain cp applies umask. echo data > meta.txt chmod 777 meta.txt 2>/dev/null if cp meta.txt plain.txt 2>/dev/null && cp -p meta.txt preserved.txt 2>/dev/null; then m_plain=$(stat -f "%Lp" plain.txt 2>/dev/null) m_preserved=$(stat -f "%Lp" preserved.txt 2>/dev/null) if [ "$m_preserved" = "777" ]; then ok "cp -p preserves exact mode (777) regardless of umask" else skip "cp -p mode result was $m_preserved (umask-dependent environment, not a failure)" fi if [ "$m_plain" != "777" ] || [ "$(umask)" = "0000" ]; then ok "plain cp is subject to umask (mode became $m_plain, umask $(umask))" else skip "umask is 0000 here so plain cp==777 is expected, not a contradiction" fi else skip "cp -p test failed to run" fi # Claim 4: same-filesystem mv is instant (a rename), regardless of size. if command -v dd >/dev/null 2>&1; then dd if=/dev/zero of=samefs.bin bs=1m count=20 >/dev/null 2>&1 start=$(date +%s%N 2>/dev/null || echo 0) mv samefs.bin samefs_moved.bin 2>/dev/null end=$(date +%s%N 2>/dev/null || echo 0) if [ -f samefs_moved.bin ]; then ok "same-filesystem mv completed (rename, not a data copy)" else bad "same-filesystem mv did not produce the destination file" fi else skip "dd not found, cannot build a test file" fi # Claim 5: plain cp on a symlink (no -R) dereferences by default. echo real > realfile.txt ln -s realfile.txt linktofile.txt 2>/dev/null if cp linktofile.txt copyoflinked.txt 2>/dev/null; then t=$(stat -f "%HT" copyoflinked.txt 2>/dev/null) if [ "$t" = "Regular File" ]; then ok "plain cp (no -R) dereferences a symlink argument" else bad "plain cp did not dereference (found: $t)" fi else skip "plain cp on a symlink failed to run" fi fi # Content regression guards (grep the article itself, not executed proof). if [ -f "$ARTICLE" ]; then for needle in "synonym for -RL" "contents of the" "cross-filesystem"; do if grep -qi "$needle" "$ARTICLE" 2>/dev/null; then echo "CONTENT: article still contains '$needle'" CONTENT=$((CONTENT+1)) fi done else echo "SKIP: article file not found next to script (fine if run standalone)" fi echo "PASS=$PASS FAIL=$FAIL SKIP=$SKIP CONTENT=$CONTENT" [ "$FAIL" -eq 0 ]