-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb-functions
More file actions
902 lines (743 loc) · 20 KB
/
db-functions
File metadata and controls
902 lines (743 loc) · 20 KB
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
#!/bin/bash
shopt -s extglob
# Some PKGBUILDs need CARCH to be set
CARCH="x86_64"
# Useful functions
UMASK=""
set_umask () {
[ "$UMASK" == "" ] && UMASK="$(umask)"
export UMASK
umask 002
}
restore_umask () {
umask $UMASK >/dev/null
}
# 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"
}
if [[ $USER != $DBSCRIPTS_USER ]]; then
exec sudo -H -u $DBSCRIPTS_USER "$0" "$@"
else
cd "$TMPDIR"
fi
# set up general environment
WORKDIR=$(mktemp -d "${TMPDIR}/${0##*/}.XXXXXXXXXX")
LOCKS=()
REPO_MODIFIED=0
declare -a HISTORY_add_files=()
declare -a HISTORY_remove_files=()
# check if messages are to be printed using color
unset ALL_OFF BOLD BLUE CYAN GREEN RED YELLOW
if [[ -t 2 ]]; then
ALL_OFF="$(tput sgr0)"
BOLD="$(tput bold)"
BLUE="${BOLD}$(tput setaf 4)"
CYAN="${BOLD}$(tput setaf 6)"
GREEN="${BOLD}$(tput setaf 2)"
RED="${BOLD}$(tput setaf 1)"
YELLOW="${BOLD}$(tput setaf 3)"
fi
readonly ALL_OFF BOLD BLUE CYAN GREEN RED YELLOW
plain() {
local mesg=$1; shift
printf "${BOLD} ${mesg}${ALL_OFF}\n" "$@"
}
msg() {
local mesg=$1; shift
printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@"
}
msg2() {
local mesg=$1; shift
printf "${BLUE} ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@"
}
warning() {
local mesg=$1; shift
printf "${YELLOW}==> WARNING:${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
}
error() {
local mesg=$1; shift
printf "${RED}==> ERROR:${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
}
debug() {
local mesg=$1; shift
[[ ${DBSCRIPTS_DEBUG} -eq 1 ]] && printf "${CYAN}==> DEBUG:${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
}
stat_busy() {
local mesg=$1; shift
printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}...${ALL_OFF}" >&2
}
stat_done() {
printf "${BOLD}done${ALL_OFF}\n" >&2
}
##
# usage : in_array( $needle, $haystack )
# return : 0 - found
# 1 - not found
##
in_array() {
local needle=$1; shift
[[ -z $1 ]] && return 1 # Not Found
local item
for item in "$@"; do
[[ $item = $needle ]] && return 0 # Found
done
return 1 # Not Found
}
##
# usage : uniq_array( $item... ). items cannot have spaces
# return : items that appear only once
# remarks : can be used to diff arrays
##
uniq_array() {
echo $@ | tr ' ' '\n' | sort | uniq -u
}
##
# usage : dedup_array( $item... ). items cannot have spaces
# return : array with duplicates removed
##
dedup_array() {
echo $@ | tr ' ' '\n' | sort -u
}
##
# usage : get_full_version( [$pkgname] )
# return : full version spec, including epoch (if necessary), pkgver, pkgrel
##
get_full_version() {
# set defaults if they weren't specified in buildfile
pkgbase=${pkgbase:-${pkgname[0]}}
epoch=${epoch:-0}
if [[ -z $1 ]]; then
if (( ! epoch )); then
echo $pkgver-$pkgrel
else
echo $epoch:$pkgver-$pkgrel
fi
else
for i in pkgver pkgrel epoch; do
local indirect="${i}_override"
eval $(declare -f package_$1 | sed -n "s/\(^[[:space:]]*$i=\)/${i}_override=/p")
[[ -z ${!indirect} ]] && eval ${indirect}=\"${!i}\"
done
if (( ! $epoch_override )); then
echo $pkgver_override-$pkgrel_override
else
echo $epoch_override:$pkgver_override-$pkgrel_override
fi
fi
}
##
# usage : get_arch( [$pkgname] )
# return : return arch from buildfile, including package override if defined
##
get_arch() {
if [[ -z $1 ]]; then
echo ${arch[@]}
else
eval $(declare -f package_$1 | sed -n "s/\(^[[:space:]]*arch=\)/arch_override=/p")
[[ -z ${arch_override} ]] && eval arch_override=\"${arch[@]}\"
echo ${arch_override[@]}
fi
}
##
# usage: pkgver_equal( $pkgver1, $pkgver2 )
##
pkgver_equal() {
if [[ $1 = *-* && $2 = *-* ]]; then
# if both versions have a pkgrel, then they must be an exact match
[[ $1 = "$2" ]]
else
# otherwise, trim any pkgrel and compare the bare version.
[[ ${1%%-*} = "${2%%-*}" ]]
fi
}
##
# usage: find_cached_package( $pkgname, $pkgver, $arch, $search_dirs... )
#
# $pkgver can be supplied with or without a pkgrel appended.
# If not supplied, any pkgrel will be matched.
##
find_cached_package() {
local targetname=$1 targetver=$2 targetarch=$3
local searchdirs=("$PKGDEST" "${@:4}") results=()
local dir pkg pkgbasename pkgparts name ver rel arch size r results
for dir in "${searchdirs[@]}"; do
[[ -d $dir ]] || continue
for pkg in "$dir"/*.pkg.tar?(.?z); do
[[ -f $pkg ]] || continue
# avoid adding duplicates of the same inode
for r in "${results[@]}"; do
[[ $r -ef $pkg ]] && continue 2
done
# split apart package filename into parts
pkgbasename=${pkg##*/}
pkgbasename=${pkgbasename%.pkg.tar?(.?z)}
arch=${pkgbasename##*-}
pkgbasename=${pkgbasename%-"$arch"}
rel=${pkgbasename##*-}
pkgbasename=${pkgbasename%-"$rel"}
ver=${pkgbasename##*-}
name=${pkgbasename%-"$ver"}
if [[ $targetname = "$name" && $targetarch = "$arch" ]] &&
pkgver_equal "$targetver" "$ver-$rel"; then
results+=("$pkg")
fi
done
done
case ${#results[*]} in
0)
return 1
;;
1)
printf '%s\n' "$results"
return 0
;;
*)
error 'Multiple packages found:'
printf '\t%s\n' "${results[@]}" >&2
return 1
esac
}
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 ${0##*/} is already locked by $_owner."
exit 1
else
set_umask
return 0
fi
}
script_unlock() {
local LOCKDIR="$TMPDIR/.scriptlock.${0##*/}"
if [ ! -d "$LOCKDIR" ]; then
warning "Script ${0##*/} was not locked!"
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 [${repo}] (${arch})"
repo_unlock $repo $arch
fi
done
if [ -d "$TMPDIR/.scriptlock.${0##*/}" ]; then
msg "Removing left over lock from ${0##*/}"
script_unlock
fi
rm -rf "$WORKDIR"
if (( REPO_MODIFIED )); then
date +%s > "${FTP_BASE}/lastupdate"
fi
[ "$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 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
debug "Locking repo [${1}] (${2})"
# This is the lock file used by repo-add and repo-remove
if [ -f "${DBLOCKFILE}" ]; then
error "Repo [${1}] (${2}) is already locked by repo-{add,remove} process $(cat $DBLOCKFILE)"
return 1
fi
if [ $# -eq 2 ]; then
_lockblock=true
_trial=0
elif [ $# -eq 3 ]; then
_lockblock=false
_timeout=$3
let _trial=$_timeout/$LOCK_DELAY
fi
_count=0
while [ $_count -le $_trial ] || $_lockblock ; do
if ! mkdir "$LOCKDIR" >/dev/null 2>&1 ; then
_owner="$(/usr/bin/stat -c %U $LOCKDIR)"
warning "Repo [${1}] (${2}) is already locked by $_owner. "
msg2 "Retrying in $LOCK_DELAY seconds..."
else
LOCKS+=("$1.$2")
set_umask
return 0
fi
sleep $LOCK_DELAY
let _count=$_count+1
done
error "Repo [${1}] (${2}) is already locked by $_owner. Giving up!"
return 1
}
repo_unlock () { #repo_unlock <repo-name> <arch>
debug "Unlocking repo [${1}] (${2})"
local LOCKDIR="$TMPDIR/.repolock.$1.$2"
if [ ! -d "$LOCKDIR" ]; then
warning "Repo lock [${1}] (${2}) was not locked!"
restore_umask
return 1
else
rmdir "$LOCKDIR"
restore_umask
return 0
fi
}
# usage: _grep_pkginfo pkgfile pattern
_grep_pkginfo() {
local _ret
_ret="$(/usr/bin/bsdtar -xOqf "$1" .PKGINFO | grep -m 1 "^${2} = ")"
echo "${_ret#${2} = }"
}
# usage: _grep_buildinfo pkgfile pattern
_grep_buildinfo() {
local _ret
_ret="$(/usr/bin/bsdtar -xOqf "$1" .BUILDINFO | grep -m 1 "^${2} = ")"
echo "${_ret#${2} = }"
}
# 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
}
issplitpkg() {
local _base
_base="$(_grep_pkginfo "$1" "pkgbase")"
if [ -z "$_base" ]; then
return 1
else
return 0
fi
}
# Get the package name
getpkgname() {
local _name
_name="$(_grep_pkginfo "$1" "pkgname")"
if [ -z "$_name" ]; then
error "Package '$1' has no pkgname in the PKGINFO. Fail!"
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 '$1' has no pkgver in the PKGINFO. Fail!"
exit 1
fi
echo "$_ver"
}
getpkgarch() {
local _ver
_ver="$(_grep_pkginfo "$1" "arch")"
if [ -z "$_ver" ]; then
error "Package '$1' has no arch in the PKGINFO. Fail!"
exit 1
fi
echo "$_ver"
}
check_packager() {
local _packager
_packager=$(_grep_pkginfo "$1" "packager")
[[ $_packager && $_packager != 'Unknown Packager' ]]
}
check_buildinfo() {
/usr/bin/bsdtar -tf "$1" .BUILDINFO >/dev/null 2>&1
}
check_builddir() {
local _builddir
_builddir=$(_grep_buildinfo "$1" "builddir")
[[ $_builddir && $_builddir = '/build' ]]
}
getpkgfile() {
if [[ ${#} -ne 1 ]]; then
error 'No canonical package found!'
exit 1
elif [ ! -f "${1}" ]; then
error "Package ${1} not found!"
exit 1
elif ${REQUIRE_SIGNATURE} && [ ! -f "${1}.sig" ]; then
error "Package signature ${1}.sig not found!"
exit 1
fi
echo ${1}
}
getpkgfiles() {
local f
if [ ! -z "$(echo ${@%\.*} | sed "s/ /\n/g" | sort | uniq -D)" ]; then
error 'Duplicate packages found!'
exit 1
fi
for f in ${@}; do
if [ ! -f "${f}" ]; then
error "Package ${f} not found!"
exit 1
elif ${REQUIRE_SIGNATURE} && [ ! -f "${f}.sig" ]; then
error "Package signature ${f}.sig not found!"
exit 1
fi
done
echo ${@}
}
check_pkgfile() {
local pkgfile=$1
local pkgname="$(getpkgname ${pkgfile})"
[ $? -ge 1 ] && return 1
local pkgver="$(getpkgver ${pkgfile})"
[ $? -ge 1 ] && return 1
local pkgarch="$(getpkgarch ${pkgfile})"
[ $? -ge 1 ] && return 1
in_array "${pkgarch}" ${ARCHES[@]} 'any' || return 1
if echo "${pkgfile##*/}" | grep -q "${pkgname}-${pkgver}-${pkgarch}"; then
return 0
else
return 1
fi
}
check_pkgrepos() {
local pkgfile=$1
local pkgname="$(getpkgname ${pkgfile})"
[ $? -ge 1 ] && return 1
local pkgver="$(getpkgver ${pkgfile})"
[ $? -ge 1 ] && return 1
local pkgarch="$(getpkgarch ${pkgfile})"
[ $? -ge 1 ] && return 1
[ -f "${FTP_BASE}/${PKGPOOL}/${pkgname}-${pkgver}-${pkgarch}"${PKGEXT} ] && return 1
[ -f "${FTP_BASE}/${PKGPOOL}/${pkgname}-${pkgver}-${pkgarch}"${PKGEXT}.sig ] && return 1
[ -f "${FTP_BASE}/${PKGPOOL}/${pkgfile##*/}" ] && return 1
[ -f "${FTP_BASE}/${PKGPOOL}/${pkgfile##*/}.sig" ] && return 1
return 0
}
#usage: chk_license ${license[@]}"
chk_license() {
local l
for l in ${@}; do
in_array ${l} ${ALLOWED_LICENSES[@]} && return 0
done
return 1
}
check_repo_permission() {
local repo=$1
[ ${#PKGREPOS[@]} -eq 0 ] && return 1
[ -z "${PKGPOOL}" ] && return 1
in_array "${repo}" ${PKGREPOS[@]} ${PKGREPOS[@]/%/-${DEBUGSUFFIX}} || 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} -a ! -w "${dir}"${repo}${DBEXT} ] && return 1
[ -f "${dir}"${repo}${FILESEXT} -a ! -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 ${dbfile} to $group"
chgrp $group "${filesfile}" || error "Could not change group of ${filesfile} to $group"
chmod g+w "${dbfile}" || error "Could not set write permission for group $group to ${dbfile}"
chmod g+w "${filesfile}" || error "Could not set write permission for group $group to ${filesfile}"
else
error "You don't have permission to change ${dbfile}"
fi
}
arch_repo_add() {
local repo=$1
local arch=$2
local pkgs=(${@:3})
# package files might be relative to repo dir
pushd "${FTP_BASE}/${repo}/os/${arch}" >/dev/null
/usr/bin/repo-add -q "${repo}${DBEXT}" ${pkgs[@]} \
|| error "repo-add ${repo}${DBEXT} ${pkgs[@]}"
popd >/dev/null
set_repo_permission "${repo}" "${arch}"
REPO_MODIFIED=1
}
arch_repo_remove() {
local repo=$1
local arch=$2
local pkgs=(${@:3})
local dbfile="${FTP_BASE}/${repo}/os/${arch}/${repo}${DBEXT}"
if [ ! -f "${dbfile}" ]; then
error "No database found at '${dbfile}'"
return 1
fi
/usr/bin/repo-remove -q "${dbfile}" ${pkgs[@]} \
|| error "repo-remove ${dbfile} ${pkgs[@]}"
set_repo_permission "${repo}" "${arch}"
REPO_MODIFIED=1
}
check_pkgfile_pkgbuild() {
local pkgfile="${1}"
local _pkgbase="$(getpkgbase ${pkgfile})"
[ $? -ge 1 ] && return 1
local _pkgname="$(getpkgname ${pkgfile})"
[ $? -ge 1 ] && return 1
local _pkgver="$(getpkgver ${pkgfile})"
[ $? -ge 1 ] && return 1
local _pkgarch="$(getpkgarch ${pkgfile})"
[ $? -ge 1 ] && return 1
get_pkgbuild ${repo} ${_pkgbase} ${_pkgver} || return 1
local pkgbuild_ver="$(. "${WORKDIR}/pkgbuilds/${repo}/${_pkgbase}-${_pkgver}"; get_full_version ${_pkgname})"
[ "${pkgbuild_ver}" == "${_pkgver}" ] || return 1
local pkgbuild_names=($(. "${WORKDIR}/pkgbuilds/${repo}/${_pkgbase}-${_pkgver}"; echo ${pkgname[@]}))
in_array "${_pkgname}" ${pkgbuild_names[@]} ${pkgbuild_names[@]/%/-${DEBUGSUFFIX}} || return 1
return 0
}
arch_add_to_pool() {
local pkgfile="$1"
if [ -f "${pkgfile}" ]; then
if ! check_pkgfile_pkgbuild "$pkgfile"; then
warning "package $pkgfile failed check against PKGBUILD. skipping"
return 0
fi
mv "${pkgfile}" "$FTP_BASE/${PKGPOOL}"
fi
# also move signatures
if [ -f "${pkgfile}.sig" ]; then
mv "${pkgfile}.sig" "$FTP_BASE/${PKGPOOL}"
fi
}
# add a package file (from the pool) to the database
# call arch_add_to_pool first
arch_db_add() {
local repo="$1"
local tarch="$2"
local pkgfiles=("${@:3}")
pkgfiles=("${pkgfiles[@]##*/}")
local dstdir="$FTP_BASE/$repo/os/$tarch"
local pkgfile
for pkgfile in "${pkgfiles[@]}"; do
local srcfile="$FTP_BASE/${PKGPOOL}/${pkgfile}"
if [[ ! -f "${srcfile}" ]]; then
warning "Package file ${pkgfile} not found in ${FTP_BASE}/${PKGPOOL}. skipping"
return 0
fi
ln -sr "$srcfile" "$dstdir/"
if [ -f "${srcfile}.sig" ]; then
ln -sr "${srcfile}.sig" "$dstdir/"
fi
done
arch_repo_add "${repo}" "${tarch}" "${pkgfiles[@]}"
for pkgfile in "${pkgfiles[@]}"; do
arch_history_add "$repo" "$dstdir/$pkgfile" "$tarch"
done
}
arch_db_remove() {
local repo=$1
local arch=$2
local pkgs=(${@:3})
local pkgname
arch_repo_remove "$@"
for pkgname in "${pkgs[@]}"; do
arch_history_remove "$repo" "$pkgname" "$arch"
done
}
arch_db_move() {
local repo_from=$1
local repo_to=$2
local arch=$3
local pkgnames=(${@:4})
local pkgname
local ftppath_from="${FTP_BASE}/${repo_from}/os"
for pkgname in "${pkgnames[@]}"; do
pkgentry=$(pkgentry_from_db "$repo_from" "$arch" "$pkgname")
pkgs=($(getpkgfiles "$ftppath_from/$arch/$pkgentry"*${PKGEXT}))
if [[ -z $pkgentry ]]; then
warning "Failed to detect pkgentry for $pkgname"
return 1
fi
for pkg in "${pkgs[@]}"; do
pkgname=$(getpkgname "$pkg")
arch_db_add "${repo_to}" "$arch" "${pkg##*/}"
arch_db_remove "${repo_from}" "$arch" "$pkgname"
done
done
}
find_removed_split_packages() {
local repo_from=$1
local repo_to=$2
local arch=$3
local pkgname=$4
local ftppath_from="${FTP_BASE}/${repo_from}/os"
pkgver_from=$(pkgver_from_db "${repo_from}" "${arch}" "${pkgname}")
pkgver_to=$(pkgver_from_db "${repo_to}" "${arch}" "${pkgname}")
# package isn't in target repo
[[ -z ${pkgver_to} ]] && return
pkg=$(getpkgfile "${ftppath_from}/${arch}/${pkgname}-${pkgver_from}"*${PKGEXT})
pkgbase=$(getpkgbase "${pkg}")
get_pkgbuild ${repo_from} ${pkgbase} ${pkgver_from} || die "PKGBUILD not found for ${pkgbase}-${pkgver_from}"
get_pkgbuild ${repo_to} ${pkgbase} ${pkgver_to} || die "PKGBUILD not found for ${pkgbase}-${pkgver_to}"
pkgnames_from=($(. "${WORKDIR}/pkgbuilds/${repo_from}/${pkgbase}-${pkgver_from}"; echo ${pkgname[@]}))
pkgnames_to=($(. "${WORKDIR}/pkgbuilds/${repo_to}/${pkgbase}-${pkgver_to}"; echo ${pkgname[@]}))
# list pkgnames_from twice to make them non-unique
uniq_array ${pkgnames_from[@]} ${pkgnames_from[@]} ${pkgnames_to[@]}
}
find_repo_for_package() {
local pkgname=$1
local pkgarch=$2
local repo
for repo in ${STABLE_REPOS[@]}; do
path="${HISTORYREPO}/${repo}/${pkgarch}/${pkgname}"
if [ -e "${path}" ]; then
echo "${repo}"
return
fi
done
return 1
}
arch_history_add() {
local repo="$1"
local pkgfile="$2"
local tarch="$3"
local pkgname=$(getpkgname "$pkgfile")
local pkgbase=$(getpkgbase "$pkgfile")
local pkgver=$(getpkgver "$pkgfile")
local staging_dir="$WORKDIR/history"
local history_file="$staging_dir/$repo/$tarch/$pkgname"
mkdir -p "${history_file%/*}"
echo "$pkgbase $pkgver" > "$history_file"
debug "History: Adding $pkgname $pkgver [$repo] ($tarch)"
HISTORY_add_files=("${HISTORY_add_files[@]}" "${history_file/$staging_dir/$HISTORYREPO}")
}
arch_history_remove() {
local repo="$1"
local pkgname="$2"
local tarch="$3"
local history_file="$HISTORYREPO/$repo/$tarch/$pkgname"
[[ -f "$history_file" ]] || return 1
debug "History: Removing $pkgname [$repo] ($tarch)"
HISTORY_remove_files=("${HISTORY_remove_files[@]}" "$history_file")
}
arch_history_commit() {
flock -e 9
pushd "$HISTORYREPO" >/dev/null
did_work=0
if ((${#HISTORY_add_files[@]} > 0)); then
cp -r -- "$WORKDIR/history/"* .
git add -- "${HISTORY_add_files[@]}"
did_work=1
fi
if ((${#HISTORY_remove_files[@]} > 0)); then
git rm -q -- "${HISTORY_remove_files[@]}"
did_work=1
fi
if ((did_work)); then
debug "History: Commiting \"$1\""
git commit -q -m "$1"
fi
popd >/dev/null
} 9> $HISTORYREPO/.git/dbscripts.gitlock
pkgentry_from_db() {
local repo="$1"
local arch="$2"
local pkgname="$3"
local db="$FTP_BASE/$repo/os/$arch/$repo$DBEXT"
local line
if [[ ! -e $db ]]; then
debug "Database not found at \"$db\""
return 1
fi
for line in $(tar tf "$db" | sed -n 's#^\([^/]*\)/$#\1#p'); do
local name=${line##*/}
if [[ ${line%-*-*} = $pkgname ]]; then
echo $name
return 0
fi
done
return 1
}
pkgver_from_db() {
local pkgentry
local pkgver
pkgentry="$(pkgentry_from_db $1 $2 $3)" || return 1
pkgver="${pkgentry##*-}"
pkgentry=${pkgentry%-*}
pkgver="${pkgentry##*-}-$pkgver"
echo $pkgver
}
get_pkgbuild() {
repo=$1
pkgbase=$2
pkgver=$3
[ -f "$WORKDIR/pkgbuilds/$repo/$pkgbase-$pkgver" ] && return
mkdir -p "$WORKDIR/pkgbuilds/$repo"
get_file_from_pkgrepo "$pkgbase" "$pkgver" PKGBUILD \
> "$WORKDIR/pkgbuilds/$repo/$pkgbase-$pkgver"
}
# get_file_from_pkgrepo pkgbase tag filename
#
# Get a file from the package's git/svn/whatever repo.
# tag is the full package version (epoch:pkgver-pkgrel) or depending on how
# the VCS is used any other recognized tag.
#
# Implementation note: This function should be the only one directly
# interacting with the given VCS and should automatically detect
# where a pkgbase is stored if multiple VCSs are used.
get_file_from_pkgrepo() {
local pkgbase="$1"
local tag="$2"
local filename="$3"
# TODO: implement svn/git extraction
# for git: cd $whereever; git show $tag:$filename
#curl -s https://projects.archlinux.org/svntogit/community.git/plain/trunk/$filename?h=packages/$pkgbase
# TODO: this requires us to restructure the repos
arch_svn cat "${SVNREPO}/${pkgbase}/tags/$tag/$filename"
#arch_svn cat "${SVNREPO}/${pkgbase}/trunk/$filename"
}
get_dir_from_pkgrepo() {
local pkgbase="$1"
local tag="$2"
local src="$3"
local dest="$4"
# TODO: implement svn/git extraction
# for git: use git archive?
arch_svn export -q "${SVNREPO}/${pkgbase}/tags/${tag}/${src}" "${dest}"
}
arch_svn() {
if [ -z "${SVNUSER}" ]; then
/usr/bin/svn "${@}"
else
sudo -u "${SVNUSER}" -- /usr/bin/svn --username "${SUDO_USER}" "${@}"
fi
}