summaryrefslogtreecommitdiffhomepage
path: root/bin/srcdist.rawball
blob: 4906e23cd8806625d09720261faa50f1e64bf4b4 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/sh

# srcdist.rawball: extract and repackage a subset of unmodified source files
# from a known upstream source distribution tarball, along with a manifest.

# this file is covered by COPYING.SRCDIST.

set -eu

mb_ruledir=
mb_rawball=
mb_tarball=
mb_tarball_sha256=

mb_script="$0"
mb_status=1
mb_manifest=MANIFEST.rawball

export LC_ALL=C

rawball_usage()
{
	printf 'usage: %s\n' "$mb_script" >&2
	printf '\t%s\n' \
		'--ruledir=/path/to/rule/dir'           \
		'--tarball=/path/to/upstream.tar.xz'    \
		'--tarball-sha256=upstream-signature'   \
		'--rawball=output-tarball-name'         \
		>&2

	printf '\nThis will extract and repackage a subset of unmodified source\n' >&2
	printf '  files from a known upstream source distribution tarball,\n' >&2
	printf '  along with a manifest.\n\n' >&2

	printf '\nThe --ruledir argument should point to a directory containing the following files:\n' >&2
	printf '  upstream.sha256.known\n' >&2
	printf '  rawball.include.patterns  (appropriate for pax)\n' >&2
	printf '  rawball.exclude.patterns  (appropriate for grep -v -f)\n\n' >&2

	exit ${mb_status}
}

for arg ; do
	case "$arg" in
		--ruledir=*)
			mb_ruledir=${arg#*=}
			;;

		--rawball=*)
			mb_rawball=${arg#*=}
			;;

		--tarball=*)
			mb_tarball=${arg#*=}
			;;

		--tarball-sha256=*)
			mb_tarball_sha256=${arg#*=}
			;;

		*)
			printf '\n*** %s: unsupported argument!' "$mb_script" >&2
			printf '\n*** %s\n' "${arg#}" >&2
			exit 2
	esac
done

rawball_ruledir_error()
{
	mb_status=2

	printf '\n*** %s: a required file in %s is missing!\n\n' "$mb_script" "$mb_ruledir" >&2

	rawball_usage
}

rawball_init_vars()
{
	if [ -z ${mb_ruledir:-} ]; then
		rawball_usage
	fi


	if [ -z ${mb_rawball:-} ] ||  [ -z ${mb_tarball:-} ] ||  [ -z ${mb_tarball_sha256:-} ]; then
		rawball_usage
	fi


	eval mb_ruledir=$(printf '%s' "$mb_ruledir")
	eval mb_tarball=$(printf '%s' "$mb_tarball")


	stat "$mb_ruledir/upstream.sha256.known"    > /dev/null || rawball_ruledir_error
	stat "$mb_ruledir/rawball.include.patterns" > /dev/null || rawball_ruledir_error
	stat "$mb_ruledir/rawball.exclude.patterns" > /dev/null || rawball_ruledir_error


	mb_known_sha256=$(grep "$mb_tarball_sha256" "${mb_ruledir}/upstream.sha256.known" || true)
	mb_known_sha256="${mb_known_sha256%% *}"

	if [ "$mb_known_sha256" != "$mb_tarball_sha256" ]; then
		printf '\n*** %s: %s is not a known sha-256 signature!\n\n' "$mb_script" "$mb_tarball_sha256" >&2
		exit 2
	fi
}

rawball_verify_sha256_signature()
{
	mb_tarball_sha256_test=$(sha256sum "$mb_tarball")
	mb_tarball_sha256_test="${mb_tarball_sha256_test%% *}"

	if [ "$mb_tarball_sha256_test" != "$mb_tarball_sha256" ]; then
		printf '\n*** %s: sha-256 signature does not match!\n\n' "$mb_script" >&2
		exit 2
	fi

}

rawball_extract_upstream_tarball()
{
	mb_rawball_files=$(xz -d -c -f "$mb_tarball"                \
		| pax $(cat "$mb_ruledir/rawball.include.patterns")  \
		| grep -v -f "${mb_ruledir}/rawball.exclude.patterns" \
		| sort)

	mb_topdir=$(printf '%s\n' ${mb_rawball_files} | sed -n 1p)
	mb_topdir=${mb_topdir%%/*}
	mb_rawdir=${mb_topdir}.raw

	if [ -d ${mb_rawdir} ]; then
		printf '\n*** %s: the directory `%s already exists!\n\n' "$mb_script" "${mb_rawdir}'" >&2
		exit 2
	fi

	mkdir ${mb_rawdir}

	xz -d -c -f "$mb_tarball" | pax -d -r -s "/^${mb_topdir}/${mb_rawdir}/" ${mb_rawball_files}

	mb_packed=$(find ${mb_rawdir} -type f | sort)
	mb_refobj=$(find ${mb_rawdir} -type f -printf "%T@ %p\n" | sort -n | cut -d' ' -f 2- | tail -n 1)
	mb_status=$(cd -- ${mb_rawdir}; sha256sum $(find . -type f | sort) > ${mb_manifest})

	touch -a -c -m -r ${mb_refobj} $(find ${mb_rawdir} -type d)
	touch -a -c -m -r ${mb_refobj} ${mb_rawdir}/${mb_manifest}
	touch -a -c -m -r ${mb_refobj} ${mb_rawdir}
}

rawball_create_downstream_rawball()
{
	pax -w -x tar ${mb_packed} ${mb_rawdir}/${mb_manifest} | xz -c > "$mb_rawball"
}

# init variables and verify arguments
rawball_init_vars

# validate input
rawball_verify_sha256_signature

# extract tarball (top-level directory must not exist)
rawball_extract_upstream_tarball

# create downstream rawball of unmodified source files
rawball_create_downstream_rawball

# all done
exit 0