lib/common.sh: Introduce locking helper functions

Reduces code duplication.

With makechrootpkg not calling mkarchroot anymore,
the lock handover protocol is unneeded.

arch-nspawn does not do any locking, so add protection to archbuild.
This commit is contained in:
Jan Alexander Steffens (heftig) 2013-05-02 07:03:12 +02:00
parent 0e98bd8c48
commit a7a05deb37
4 changed files with 29 additions and 37 deletions

View File

@ -51,12 +51,7 @@ if ${clean_first} || [[ ! -d "${chroots}/${repo}-${arch}" ]]; then
[[ -d $copy ]] || continue
msg2 "Deleting chroot copy '$(basename "${copy}")'..."
exec 9>"$copydir.lock"
if ! flock -n 9; then
stat_busy "Locking chroot copy '$copy'"
flock 9
stat_done
fi
lock 9 "$copydir.lock" "Locking chroot copy '$copy'"
if [[ "$(stat -f -c %T "${copy}")" == btrfs ]]; then
{ type -P btrfs && btrfs subvolume delete "${copy}"; } &>/dev/null
@ -73,6 +68,7 @@ if ${clean_first} || [[ ! -d "${chroots}/${repo}-${arch}" ]]; then
"${chroots}/${repo}-${arch}/root" \
"${base_packages[@]}" || abort
else
lock 9 "${chroots}/${repo}-${arch}/root.lock" "Locking clean chroot"
setarch ${arch} arch-nspawn \
-C "@pkgdatadir@/pacman-${repo}.conf" \
-M "@pkgdatadir@/makepkg-${arch}.conf" \

View File

@ -130,3 +130,27 @@ get_full_version() {
fi
fi
}
##
# usage : lock( $fd, $file, $message )
##
lock() {
eval "exec $1>"'"$2"'
if ! flock -n $1; then
stat_busy "$3"
flock $1
stat_done
fi
}
##
# usage : slock( $fd, $file, $message )
##
slock() {
eval "exec $1>"'"$2"'
if ! flock -sn $1; then
stat_busy "$3"
flock -s $1
stat_done
fi
}

View File

@ -120,24 +120,12 @@ umask 0022
chroottype=$(stat -f -c %T "$chrootdir")
# Lock the chroot we want to use. We'll keep this lock until we exit.
# Note this is the same FD number as in mkarchroot
exec 9>"$copydir.lock"
if ! flock -n 9; then
stat_busy "Locking chroot copy [$copy]"
flock 9
stat_done
fi
lock 9 "$copydir.lock" "Locking chroot copy [$copy]"
if [[ ! -d $copydir ]] || $clean_first; then
# Get a read lock on the root chroot to make
# sure we don't clone a half-updated chroot
exec 8>"$chrootdir/root.lock"
if ! flock -sn 8; then
stat_busy "Locking clean chroot"
flock -s 8
stat_done
fi
slock 8 "$chrootdir/root.lock" "Locking clean chroot"
stat_busy "Creating clean working copy [$copy]"
if [[ "$chroottype" == btrfs ]]; then

View File

@ -49,29 +49,13 @@ else
cache_dirs=(${cache_dir})
fi
# {{{ functions
chroot_lock () {
# Only reopen the FD if it wasn't handed to us
if [[ $(readlink -f /dev/fd/9) != "${working_dir}.lock" ]]; then
exec 9>"${working_dir}.lock"
fi
# Lock the chroot. Take note of the FD number.
if ! flock -n 9; then
stat_busy "Locking chroot"
flock 9
stat_done
fi
}
# }}}
umask 0022
[[ -e $working_dir ]] && die "Working directory '$working_dir' already exists"
mkdir -p "$working_dir"
chroot_lock
lock 9 "${working_dir}.lock" "Locking chroot"
if [[ $(stat -f -c %T "$working_dir") == btrfs ]]; then
rmdir "$working_dir"