Fossil SCM

fossil-scm / tools / powerloss / powerloss.sh
Blame History Raw 145 lines
1
#!/bin/sh
2
#
3
# Usage: powerloss.sh FOSSIL-BINARY WORKDIR ?REPO-MODE?
4
#
5
# Dan Shearer, November 2026
6
#
7
# A standalone driver for powerloss.so, and the worked example in
8
# README-POWERLOSS.md. test/powerloss.test in Fossil's test suite
9
# interrupts the same check-in with more checks, and other commands too.
10
#
11
# Runs a check-in once for each point where powerloss.so will stop it.
12
# After each stop, prints the integrity_check result of the repository and
13
# the check-out database, and whether the two agree on the latest check-in.
14
#
15
# FOSSIL-BINARY creates the repository and check-out, and reads both
16
# databases with its sqlite3 command. Give it as an absolute path, because
17
# the script runs it from other directories. WORKDIR is deleted and
18
# recreated. REPO-MODE is "wal" (the default) or "delete".
19
#
20
# PL_COMMIT is the shell command that makes the check-in. It defaults to
21
# "FOSSIL-BINARY commit -m two", and lets you test another check-in
22
# program against the same repository.
23
#
24
# With STOP_MODE=kill in the environment, powerloss.so also stops at each
25
# pwrite and write, and leaves the files as the program wrote them, as a
26
# killed process would.
27
#
28
# Exits with status 0, with 1 if a run did not stop at its stop point, or
29
# with 2 if the set-up failed.
30
31
usage(){ echo "usage: $0 FOSSIL-BINARY WORKDIR ?wal|delete?" >&2; exit 2; }
32
[ $# -ge 2 ] || usage
33
fossil=$1
34
work=$2
35
case ${3:-wal} in
36
wal) walopt=--wal ;;
37
delete) walopt= ;;
38
*) usage ;;
39
esac
40
shim=$(cd "$(dirname "$0")" && pwd)/powerloss.so
41
if [ ! -f "$shim" ]; then
42
echo "$0: build $shim from powerloss.c first" >&2
43
exit 2
44
fi
45
repo=$work/run/r.fossil
46
ckout=$work/run/ck/.fslckout
47
48
sql(){ "$fossil" sqlite3 --no-repository -list "$@" 2>&1; }
49
50
rm -rf "$work"
51
mkdir -p "$work/home" "$work/run/ck"
52
export FOSSIL_HOME="$work/home" FOSSIL_USER=tester
53
54
(
55
set -e
56
cd "$work/run"
57
"$fossil" new r.fossil
58
"$fossil" rebuild $walopt r.fossil
59
cd ck
60
"$fossil" open ../r.fossil
61
echo one >a.txt
62
echo bee >b.txt
63
echo sea >c.txt
64
"$fossil" add a.txt b.txt c.txt
65
"$fossil" commit -m one
66
echo "second version" >a.txt
67
echo new >d.txt
68
"$fossil" add d.txt
69
"$fossil" rm b.txt
70
"$fossil" mv --hard c.txt e.txt
71
) >/dev/null
72
[ $? -eq 0 ] || exit 2
73
echo "repository journal mode: $(sql "$repo" 'PRAGMA journal_mode;')"
74
echo "check-out journal mode: $(sql "$ckout" 'PRAGMA journal_mode;')"
75
cp -pR "$work/run" "$work/template"
76
77
# Prints the check-in that .fslckout records, the repository's latest
78
# check-in, the integrity_check result of each database, and a verdict:
79
# AHEAD when the repository does not have the check-in that .fslckout
80
# records, BEHIND when the repository has a later one, and otherwise
81
# consistent.
82
state(){
83
ckin=$(sql "$ckout" "SELECT value FROM vvar WHERE name='checkout';")
84
have=$(sql "$repo" "SELECT count(*) FROM blob WHERE rid=0+'$ckin';")
85
last=$(sql "$repo" "SELECT objid FROM event WHERE type='ci'
86
ORDER BY mtime DESC, objid DESC LIMIT 1;")
87
repook=$(sql "$repo" 'PRAGMA integrity_check;')
88
ckoutok=$(sql "$ckout" 'PRAGMA integrity_check;')
89
if [ "$have" != 1 ]; then verdict=AHEAD
90
elif [ "$ckin" != "$last" ]; then verdict=BEHIND
91
else verdict=consistent; fi
92
echo "checkout=$ckin repo-latest=$last integrity=$repook/$ckoutok $verdict"
93
}
94
95
# Makes the check-in from a fresh copy of the template. powerloss.so
96
# stops it at stop point $1, or lets it finish if $1 is 0. Returns the
97
# exit status of the check-in command.
98
run(){
99
rm -rf "$work/run" "$work/shadow" "$work/log"
100
cp -pR "$work/template" "$work/run"
101
( export STOP_DIR="$work" STOP_AT=$1 LD_PRELOAD="$shim"
102
cd "$work/run/ck" || exit 2
103
if [ -n "$PL_COMMIT" ]; then sh -c "$PL_COMMIT"
104
else "$fossil" commit -m two; fi
105
) >/dev/null 2>&1
106
}
107
108
run 0
109
rc=$?
110
if [ ! -s "$work/log" ]; then
111
echo "$0: powerloss.so logged no stop points (exit $rc)." \
112
"A statically linked fossil does not load it." >&2
113
exit 2
114
fi
115
n=$(($(wc -l <"$work/log")))
116
echo "commit without a stop: exit $rc, $n stop points"
117
118
# A run that does not stop at its stop point leaves the files of a
119
# finished command, and those files pass every check in state(). So the
120
# loop requires that the run logged the stop point, that in power mode it
121
# logged nothing after it, and that the exit status is not 0. In kill
122
# mode the other processes of the command continue, and can log more.
123
# The status is 77 when the check-in command itself stops, and some other
124
# status when a process that the command started stops and the command
125
# then fails. Each line of output has the call and the file that the
126
# stopped run logged at the stop point.
127
fail=0
128
i=0
129
while [ "$i" -lt "$n" ]; do
130
i=$((i+1))
131
run "$i"
132
rc=$?
133
logged=$(($(wc -l <"$work/log")))
134
last=$(sed -n "${i}p" "$work/log" | cut -d' ' -f2,3)
135
if [ "$rc" = 0 ] || [ -z "$last" ] ||
136
{ [ "$STOP_MODE" != kill ] && [ "$logged" != "$i" ]; }; then
137
echo "$i $last DID NOT STOP THERE: exit $rc," \
138
"$logged stop points logged"
139
fail=1
140
continue
141
fi
142
echo "$i $last $(state)"
143
done
144
exit $fail
145

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button