-
Notifications
You must be signed in to change notification settings - Fork 15
/
db-functions
549 lines (444 loc) · 12.7 KB
/
db-functions
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#!/hint/bash
. /usr/share/makepkg/util.sh
# global shell options for enhanced bash scripting
shopt -s extglob globstar nullglob
# Some PKGBUILDs need CARCH to be set
CARCH="x86_64"
# Useful functions
UMASK=""
set_umask () {
export UMASK="${UMASK:-$(umask)}"
umask 002
}
restore_umask () {
umask "$UMASK" >/dev/null
}
# Proxy function to check if a file exists. Using [[ -f ... ]] directly is not
# always wanted because we might want to expand bash globs first. This way we
# can pass unquoted globs to is_globfile() and have them expanded as function
# arguments before being checked.
is_globfile() {
[[ -f $1 ]]
}
# just like mv -f, but we touch the file and then copy the content so
# default ACLs in the target dir will be applied
mv_acl() {
rm -f "$2"
touch "$2"
cat "$1" >"$2" || return 1
rm -f "$1"
}
# set up general environment
WORKDIR=$(mktemp -dt "${0##*/}.XXXXXXXXXX")
LOCKS=()
REPO_MODIFIED=0
script_lock() {
local LOCKDIR="$TMPDIR/.scriptlock.${0##*/}"
if ! mkdir "$LOCKDIR" >/dev/null 2>&1 ; then
local _owner="$(/usr/bin/stat -c %U "$LOCKDIR")"
error "Script %s is already locked by %s." "${0##*/}" "$_owner"
exit 1
else
set_umask
return 0
fi
}
script_unlock() {
local LOCKDIR="$TMPDIR/.scriptlock.${0##*/}"
if [[ ! -d $LOCKDIR ]]; then
warning "Script %s was not locked!" "${0##*/}"
restore_umask
return 1
else
rmdir "$LOCKDIR"
restore_umask
return 0
fi
}
cleanup() {
local l
local repo
local arch
trap - EXIT INT QUIT TERM
for l in "${LOCKS[@]}"; do
repo=${l%.*}
arch=${l#*.}
if [[ -d $TMPDIR/.repolock.$repo.$arch ]]; then
msg "Removing left over lock from [%s] (%s)" "$repo" "$arch"
repo_unlock "$repo" "$arch"
fi
done
if [[ -d $TMPDIR/.scriptlock.${0##*/} ]]; then
msg "Removing left over lock from %s" "${0##*/}"
script_unlock
fi
rm -rf "$WORKDIR"
if (( REPO_MODIFIED )); then
date +%s > "${FTP_BASE}/lastupdate"
fi
[[ -n $1 ]] && exit "$1"
}
abort() {
msg 'Aborting...'
cleanup 0
}
die() {
error "$@"
cleanup 1
}
trap abort INT QUIT TERM HUP
trap cleanup EXIT
#repo_lock <repo-name> <arch> [timeout]
repo_lock() {
local repo base=${1}; shift
for repo in ${base} ${base}-debug; do
_repo_lock ${repo} "${@}" || exit 1
done
}
_repo_lock () {
local LOCKDIR="$TMPDIR/.repolock.$1.$2"
local DBLOCKFILE="${FTP_BASE}/${1}/os/${2}/${1}${DBEXT}.lck"
local _count
local _trial
local _timeout
local _lockblock
local _owner
# This is the lock file used by repo-add and repo-remove
if [[ -f ${DBLOCKFILE} ]]; then
error "Repo [%s] (%s) is already locked by repo-{add,remove} process %s" "$1" "$2" "$(cat "$DBLOCKFILE")"
return 1
fi
if (( $# == 2 )); then
_lockblock=true
_trial=0
elif (( $# == 3 )); then
_lockblock=false
_timeout=$3
let _trial=$_timeout/$LOCK_DELAY
fi
_count=0
while (( _count <= _trial )) || [[ $_lockblock = true ]]; do
if ! mkdir "$LOCKDIR" >/dev/null 2>&1 ; then
_owner="$(/usr/bin/stat -c %U "$LOCKDIR")"
warning "Repo [%s] (%s) is already locked by %s." "$1" "$2" "$_owner"
msg2 "Retrying in %s seconds..." "$LOCK_DELAY"
else
LOCKS+=("$1.$2")
set_umask
return 0
fi
sleep "$LOCK_DELAY"
let _count=$_count+1
done
error "Repo [%s] (%s) is already locked by %s. Giving up!" "$1" "$2" "$_owner"
return 1
}
#repo_unlock <repo-name> <arch>
repo_unlock () {
local repo base=${1}; shift
for repo in ${base} ${base}-debug; do
_repo_unlock ${repo} "${@}"
done
}
_repo_unlock () {
local LOCKDIR="$TMPDIR/.repolock.$1.$2"
if [[ ! -d $LOCKDIR ]]; then
warning "Repo lock [%s] (%s) was not locked!" "$1" "$2"
restore_umask
return 1
else
rmdir "$LOCKDIR"
restore_umask
return 0
fi
}
# usage: _grep_all_info pkgfile infofile key
_grep_all_info() {
local _ret=()
mapfile -t _ret < <(/usr/bin/bsdtar -xOqf "$1" "${2}" | grep "^${3} = ")
printf '%s\n' "${_ret[@]#${3} = }"
}
# usage: _grep_pkginfo pkgfile pattern
_grep_pkginfo() {
_grep_all_info "${1}" .PKGINFO "${2}" | tail -1
}
# usage: _grep_buildinfo pkgfile pattern
_grep_buildinfo() {
_grep_all_info "${1}" .BUILDINFO "${2}" | tail -1
}
# Get the package base or name as fallback
getpkgbase() {
local _base
_base="$(_grep_pkginfo "$1" "pkgbase")"
if [[ -z $_base ]]; then
getpkgname "$1"
else
echo "$_base"
fi
}
# Get the package name
getpkgname() {
local _name
_name="$(_grep_pkginfo "$1" "pkgname")"
if [[ -z $_name ]]; then
error "Package '%s' has no pkgname in the PKGINFO. Fail!" "$1"
exit 1
fi
echo "$_name"
}
# Get the pkgver-pkgrel of this package
getpkgver() {
local _ver
_ver="$(_grep_pkginfo "$1" "pkgver")"
if [[ -z $_ver ]]; then
error "Package '%s' has no pkgver in the PKGINFO. Fail!" "$1"
exit 1
fi
echo "$_ver"
}
getpkgarch() {
local _ver
_ver="$(_grep_pkginfo "$1" "arch")"
if [[ -z $_ver ]]; then
error "Package '%s' has no arch in the PKGINFO. Fail!" "$1"
exit 1
fi
echo "$_ver"
}
getpkgdesc() {
local _desc
_desc="$(_grep_pkginfo "$1" "pkgdesc")"
if [[ -z $_desc ]]; then
error "Package '%s' has no pkgdesc in the PKGINFO. Fail!" "$1"
exit 1
fi
echo "$_desc"
}
# TODO: We need to not depend on pkgdesc
# here be dragons
is_debug_package() {
local pkgfile=${1}
local pkgbase="$(getpkgbase "${pkgfile}")"
local pkgname="$(getpkgname "${pkgfile}")"
local pkgdesc="$(getpkgdesc "${pkgfile}")"
[[ ${pkgdesc} == "Detached debugging symbols for "* && ${pkgbase}-debug = ${pkgname} ]]
}
check_packager() {
local _packager
_packager=$(_grep_pkginfo "$1" "packager")
[[ -n $_packager && $_packager != 'Unknown Packager' ]]
}
check_buildinfo() {
/usr/bin/bsdtar -tqf "$1" .BUILDINFO >/dev/null 2>&1
}
check_builddir() {
local _builddir
_builddir=$(_grep_buildinfo "$1" "builddir")
[[ -n $_builddir && $_builddir = '/build' ]]
}
# Non fatal getpkgfile expanding globs
maybe_getpkgfile() {
if (( $# != 1 )); then
exit 1
elif [[ ! -f ${1} ]]; then
exit 1
elif [[ ! -f ${1}.sig ]]; then
error "Package signature %s not found!" "$1.sig"
exit 1
fi
echo "${1}"
}
getpkgfile() {
if (( $# != 1 )); then
error 'No canonical package found!'
exit 1
elif [[ ! -f ${1} ]]; then
error "Package %s not found!" "$1"
exit 1
elif [[ ! -f ${1}.sig ]]; then
error "Package signature %s not found!" "$1.sig"
exit 1
fi
echo "${1}"
}
getpkgfiles() {
local f files
if ! printf '%s\n' "${@%\.*}" | awk 'a[$0]++{exit 1}'; then
error 'Duplicate packages found!'
exit 1
fi
for f in "$@"; do
files+=("$(getpkgfile "$f")") || exit 1
done
echo "${files[@]}"
}
check_pkgfile() {
local pkgfile=$1
local pkgname="$(getpkgname "${pkgfile}")" || return 1
local pkgver="$(getpkgver "${pkgfile}")" || return 1
local pkgarch="$(getpkgarch "${pkgfile}")" || return 1
in_array "${pkgarch}" "${ARCHES[@]}" 'any' || return 1
[[ ${pkgfile##*/} = "${pkgname}-${pkgver}-${pkgarch}"* ]]
}
# Check that the package file is consistent with the PKGBUILD in version control
check_pkgvcs() {
local pkgfile="${1}"
local repo="${2}"
local _pkgbase="$(getpkgbase "${pkgfile}")" || return 1
local _pkgname="$(getpkgname "${pkgfile}")" || return 1
local _pkgver="$(getpkgver "${pkgfile}")" || return 1
local _pkgarch="$(getpkgarch "${pkgfile}")" || return 1
in_array "${repo}" "${PKGREPOS[@]}" || return 1
local vcsver vcsnames=()
read -rd'\n' vcsver vcsnames < <(source_pkgbuild "${_pkgbase}" "repos/${repo}-${_pkgarch}"; \
get_full_version; echo "${pkgname[@]}")
read -ra vcsnames <<<"${vcsnames}"
[[ "${vcsver}" = "${_pkgver}" ]] || return 1
in_array "${_pkgname}" "${vcsnames[@]}" "${_pkgbase}-debug" || return 1
return 0
}
check_splitpkgs() {
local repo="${1}"
shift
local pkgfiles=("${@}")
local pkgfile
local pkgdir
local vcsname
mkdir -p "${WORKDIR}/check_splitpkgs/"
pushd "${WORKDIR}/check_splitpkgs" >/dev/null
for pkgfile in "${pkgfiles[@]}"; do
local _pkgbase="$(getpkgbase "${pkgfile}")"
local _pkgname="$(getpkgname "${pkgfile}")"
local _pkgarch="$(getpkgarch "${pkgfile}")"
local vcsnames=($(source_pkgbuild "${_pkgbase}" "repos/${repo}-${_pkgarch}"; echo "${pkgname[@]}"))
# not a split package
(( ${#vcsnames[@]} > 1 )) || continue
[[ ${_pkgbase}-debug = ${_pkgname} ]] && continue
mkdir -p "${repo}/${_pkgarch}/${_pkgbase}"
echo "${_pkgname}" >> "${repo}/${_pkgarch}/${_pkgbase}/staging"
printf '%s\n' "${vcsnames[@]}" >> "${repo}/${_pkgarch}/${_pkgbase}/vcs"
done
popd >/dev/null
for pkgdir in "${WORKDIR}/check_splitpkgs/${repo}"/*/*; do
[[ ! -d ${pkgdir} ]] && continue
sort -u "${pkgdir}/staging" -o "${pkgdir}/staging"
sort -u "${pkgdir}/vcs" -o "${pkgdir}/vcs"
if [[ ! -z "$(comm -13 "${pkgdir}/staging" "${pkgdir}/vcs")" ]]; then
return 1
fi
done
return 0
}
check_pkgrepos() {
local pkgfile=$1
local pkgname="$(getpkgname "${pkgfile}")" || return 1
local pkgver="$(getpkgver "${pkgfile}")" || return 1
local pkgarch="$(getpkgarch "${pkgfile}")" || return 1
is_globfile "${FTP_BASE}/${PKGPOOL}/${pkgname}-${pkgver}-${pkgarch}"${PKGEXTS} && return 1
is_globfile "${FTP_BASE}/${PKGPOOL}/${pkgname}-${pkgver}-${pkgarch}"${PKGEXTS}.sig && return 1
[[ -f ${FTP_BASE}/${PKGPOOL}/${pkgfile##*/} ]] && return 1
[[ -f ${FTP_BASE}/${PKGPOOL}/${pkgfile##*/}.sig ]] && return 1
return 0
}
check_stagingrepos() {
local pkgfile=${1}
local pkgrepo=${2}
local pkgbase=$(getpkgbase "${pkgfile}")
local pkgname=$(getpkgname "${pkgfile}")
local pkgarch=$(getpkgarch "${pkgfile}")
local candidate candidates=()
if in_array "${pkgrepo}" "${STABLE_REPOS[@]}"; then
candidates+=($(find_repo_for_package "${pkgbase}" "${pkgarch}" "${TESTING_REPOS[@]}"))
fi
if in_array "${pkgrepo}" "${STABLE_REPOS[@]}" "${TESTING_REPOS[@]}"; then
candidates+=($(find_repo_for_package "${pkgbase}" "${pkgarch}" "${STAGING_REPOS[@]}"))
fi
(( ${#candidates[@]} == 0 )) && return 0
printf '%s\n' "${candidates[@]%-*}"
for candidate in "${candidates[@]}"; do
for candidate in "${STAGING}/${candidate%-*}"/*${PKGEXTS}; do
[[ ${pkgname} = $(getpkgname "${candidate}" 2>/dev/null) ]] && return 0
done
done
return 1
}
#usage: check_license ${license[@]}"
check_license() {
local l
for l in "${@}"; do
in_array "${l}" "${ALLOWED_LICENSES[@]}" && return 0
done
return 1
}
check_repo_configured() {
local repo=$1
local count=$(printf '%s\n' "${PKGREPOS[@]}" | grep --count --line-regexp "$repo")
[[ $count -gt 0 ]] && return 0
return 1
}
check_repo_permission() {
local repo=$1
(( ${#PKGREPOS[@]} == 0 )) && return 1
[[ -z "${PKGPOOL}" ]] && return 1
in_array "${repo}" "${PKGREPOS[@]}" "${DEBUGREPOS[@]}" || return 1
[[ -w $FTP_BASE/${PKGPOOL} ]] || return 1
local arch
for arch in "${ARCHES[@]}"; do
local dir="${FTP_BASE}/${repo}/os/${arch}/"
[[ -w ${dir} ]] || return 1
[[ -f ${dir}${repo}${DBEXT} && ! -w ${dir}${repo}${DBEXT} ]] && return 1
[[ -f ${dir}${repo}${FILESEXT} && ! -w ${dir}${repo}${FILESEXT} ]] && return 1
done
return 0
}
set_repo_permission() {
local repo=$1
local arch=$2
local dbfile="${FTP_BASE}/${repo}/os/${arch}/${repo}${DBEXT}"
local filesfile="${FTP_BASE}/${repo}/os/${arch}/${repo}${FILESEXT}"
if [[ -w ${dbfile} ]]; then
local group=$(/usr/bin/stat --printf='%G' "$(dirname "${dbfile}")")
chgrp "$group" "${dbfile}" || error "Could not change group of %s to %s" "$dbfile" "$group"
chgrp "$group" "${filesfile}" || error "Could not change group of %s to %s" "$filesfile" "$group"
chmod g+w "${dbfile}" || error "Could not set write permission for group %s to %s" "$group" "$dbfile"
chmod g+w "${filesfile}" || error "Could not set write permission for group %s to %s" "$group" "$filesfile"
else
error "You don't have permission to change %s" "$dbfile"
fi
}
arch_repo_modify() {
local action=$1
local repo=$2
local arch=$3
local pkgs=("${@:4}")
local dbfile="${FTP_BASE}/${repo}/os/${arch}/${repo}${DBEXT}"
if [[ ${action} = remove && ! -f ${dbfile} ]]; then
error "No database found at '%s'" "$dbfile"
return 1
fi
# package files for repo-add might be relative to repo dir
pushd "${dbfile%/*}" >/dev/null
/usr/bin/"repo-${action}" -q "${dbfile}" "${pkgs[@]}" \
|| error '%s' "repo-${action} ${dbfile@Q} ${pkgs[*]@Q}"
set_repo_permission "${repo}" "${arch}"
popd >/dev/null
REPO_MODIFIED=1
}
# Verify the existence of dependent packages needed by a given pkgfile
# usage: check_reproducible pkgfile
check_reproducible() {
local pkg dir pkgs=() pkgfile pkgfiles=()
mapfile -t pkgs < <(_grep_all_info "${1}" .BUILDINFO installed)
for pkg in "${pkgs[@]}"; do
local pkgname=${pkg%-*-*-*}
for dir in "${FTP_BASE}"/pool/* "${ARCHIVE_BASE}/packages/${pkgname:0:1}/${pkgname}" "${STAGING}"/**/; do
if pkgfile="$(getpkgfile "${dir}/${pkg}"${PKGEXTS} 2>/dev/null)"; then
pkgfiles+=("${pkgfile}")
continue 2
fi
done
error "could not find existing or staged package for dependency %s" "${pkg}"
return 1
done
}
. "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")/db-functions-${VCS}"