@@ -90,6 +90,44 @@ function error() {
9090 printf " ${RED} ==> $( gettext " ERROR:" ) ${ALL_OFF}${BOLD} ${mesg}${ALL_OFF} \n" " $@ " >&2
9191}
9292
93+ # #
94+ # usage : lock( $fd, $file, $message, [ $message_arguments... ] )
95+ # #
96+ lock () {
97+ # Only reopen the FD if it wasn't handed to us
98+ if ! [[ " /dev/fd/$1 " -ef " $2 " ]]; then
99+ mkdir -p -- " $( dirname -- " $2 " ) "
100+ eval " exec $1 >" ' "$2"'
101+ fi
102+
103+ flock -n " $1 "
104+ }
105+
106+ # #
107+ # usage : slock( $fd, $file, $message, [ $message_arguments... ] )
108+ # #
109+ slock () {
110+ # Only reopen the FD if it wasn't handed to us
111+ if ! [[ " /dev/fd/$1 " -ef " $2 " ]]; then
112+ mkdir -p -- " $( dirname -- " $2 " ) "
113+ eval " exec $1 >" ' "$2"'
114+ fi
115+
116+ if ! flock -sn " $1 " ; then
117+ flock -s " $1 "
118+ fi
119+ }
120+
121+ # #
122+ # usage : lock_close( $fd )
123+ # #
124+ lock_close () {
125+ local fd=$1
126+ # https://github.com/koalaman/shellcheck/issues/862
127+ # shellcheck disable=2034
128+ exec {fd}>& -
129+ }
130+
93131# Desc: Executes an command inside a given nspawn container
94132# 1: Container name
95133# 2: Command to execute
@@ -167,7 +205,7 @@ function init_chroot(){
167205
168206 # Prepare root chroot
169207 if [ ! -d " $BUILDDIRECTORY " /root ]; then
170-
208+ lock 9 " $BUILDDIRECTORY " /root.lock
171209 msg " Preparing chroot"
172210 trap ' { cleanup_root_volume; exit 1; }' ERR
173211 trap ' { cleanup_root_volume; trap - INT; kill -INT $$; }' INT
@@ -194,11 +232,17 @@ function init_chroot(){
194232 exec_nspawn root useradd -m -G wheel -s /bin/bash -d /build builduser
195233 echo " keyserver-options auto-key-retrieve" | install -Dm644 /dev/stdin " $BUILDDIRECTORY /root" /build/.gnupg/gpg.conf
196234 exec_nspawn root chown -R builduser /build/.gnupg
235+ lock_close 9
197236 else
237+
238+ if lock 9 " $BUILDDIRECTORY " /root.lock; then
198239 printf ' Server = %s\n' " $HOSTMIRROR " > " $BUILDDIRECTORY " /root/etc/pacman.d/mirrorlist
199240 exec_nspawn root pacman -Syu --noconfirm
241+ lock_close 9
242+ else
243+ msg " Couldn't acquire lock on root chroot, didn't update."
244+ fi
200245 fi
201-
202246 trap - ERR INT
203247}
204248
@@ -215,9 +259,6 @@ function cmd_check(){
215259
216260 trap - ERR INT
217261
218- msg " Starting build..."
219- create_snapshot " build" 0
220- build=" build"
221262
222263 declare -A buildinfo
223264 while IFS=$' =' read -r key value; do
@@ -235,6 +276,17 @@ function cmd_check(){
235276 pkgbuild_sha256sum= " ${buildinfo[pkgbuild_sha256sum]} "
236277 SOURCE_DATE_EPOCH= " ${buildinfo[builddate]} "
237278
279+
280+ msg2 " Preparing packages"
281+ mkdir -p " ${cachedir} "
282+ mapfile -t packages < < (buildinfo -d " ${cachedir} " " ${pkg} " )
283+ msg2 " Finished preparing packages"
284+
285+ msg " Starting build..."
286+ slock 9 " $BUILDDIRECTORY " /root.lock
287+ create_snapshot " $build " 0
288+
289+ # Setup environment
238290 {
239291 printf ' MAKEFLAGS="%s"\n' " ${MAKEFLAGS:- -j$(nproc)} "
240292 printf ' PKGDEST=/pkgdest\n'
@@ -246,12 +298,11 @@ function cmd_check(){
246298 printf ' COMPRESSZST=(zstd -c -T0 --ultra -20 -)\n'
247299 printf ' PKGEXT=".pkg.tar%s"\n' " ${pkg##* tar} "
248300 } >> " $BUILDDIRECTORY /$build /etc/makepkg.conf"
249-
250301 # We do the signature checking with pacman -Udd
251302 sed -i " s/LocalFileSigLevel.*//g" " $BUILDDIRECTORY /$build /etc/pacman.conf"
252303
253304 # Father I have sinned
254- exec_nspawn " build" \
305+ exec_nspawn " $ build" \
255306 bash << -__END__
256307shopt -s globstar
257308install -d -o builduser -g builduser /startdir
@@ -271,26 +322,23 @@ done
271322exit 1
272323__END__
273324
274- msg2 " Preparing packages"
275- mkdir -p " ${cachedir} "
276- mapfile -t packages < < (buildinfo -d " ${cachedir} " " ${pkg} " )
277- msg2 " Finished preparing packages"
278325 msg " Installing packages"
279326 # shellcheck disable=SC2086
280- exec_nspawn build --bind= " $( readlink -e ${cachedir} ) :/cache" bash -c \
327+ exec_nspawn " $ build" --bind= " $( readlink -e ${cachedir} ) :/cache" bash -c \
281328 ' yes y | pacman -Udd --overwrite "*" -- "$@"' -b ash " ${packages[@]} "
282329
283330 read -r -a buildinfo_packages <<< " $( buildinfo -f installed " ${pkg} " ) "
284331 uninstall= $( comm -13 \
285332 <( printf ' %s\n' " ${buildinfo_packages[@]} " | rev | cut -d- -f4- | rev | sort) \
286- <( exec_nspawn build --bind=" $( readlink -e ${cachedir} ) :/cache" pacman -Qq | sort) )
333+ <( exec_nspawn " $ build" --bind=" $( readlink -e ${cachedir} ) :/cache" pacman -Qq | sort) )
287334
288335 if [ -n " $uninstall " ]; then
289- exec_nspawn build pacman -Rdd --noconfirm -- $uninstall
336+ exec_nspawn " $ build" pacman -Rdd --noconfirm -- $uninstall
290337 fi
291338
292- build_package " build" " $builddir "
293- remove_snapshot " build"
339+ build_package " $build " " $builddir "
340+ remove_snapshot " $build "
341+ lock_close 9
294342
295343 msg " Comparing hashes..."
296344 if diff -q -- " $pkg " ./build/" $( basename " $pkg " ) " > /dev/null ; then
0 commit comments