-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect-chunks.sh
executable file
·68 lines (60 loc) · 1.7 KB
/
collect-chunks.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
# Collects the chunks from an rN phase by hardlinking files from the
# incoming/ subdirectory to the output/ subdirectory. Files are named
# like "chunk-r0-0000-output.bin".
#
# Writes the file SHA256 checksums to standard output (in the same format
# as sha256sum) which should be redirected to a file (or /dev/null).
#
# Example:
#
# ./collect-chunk-output.sh 5 > chunk-r5-output.sha25sum
set -e -E -o pipefail
shopt -s nullglob
phase=$1
if [ -z "$phase" ]; then
echo 'Missing phase'
exit 1
fi
if [ ! -d incoming ]; then
echo 'Missing incoming directory.'
exit 1
fi
if [ ! -d output ]; then
echo 'Missing output directory.'
exit 1
fi
sqlite3 chunks.db "SELECT phase,chunk,hex(sha256sum) FROM WorkQueue WHERE phase=$phase AND completed IS NOT NULL" | {
IFS='|'
while read phase chunk sha256sum
do
sha256sum=$(echo "$sha256sum" | tr A-Z a-z)
filename=$(printf 'chunk-r%d-%04d-output.bin' "$phase" "$chunk")
echo "$sha256sum $filename"
dst=output/$filename
if [ -f "$dst" ]; then
echo "$dst already exists. Skipping." >&2
else
src=incoming/$sha256sum
if [ -f "$src" ]; then
# Link newly uploaded chunk.
if ! ln "$src" "$dst"; then
echo "Could not link $src to $dst!" >&2
exit 1
fi
else
compressed_srcs=(archive/*/$sha256sum.zst)
if [ ${#compressed_srcs[@]} -gt 0 ]; then
compressed_src=${compressed_srcs[0]}
if ! zstdcat "$compressed_src" > "$dst"; then
echo "Could not decompress $compressed_src to $dst!" >&2
exit 1
fi
else
echo "$src missing! Chunk: $chunk" >&2
exit 1
fi
fi
fi
done
}