diff --git a/archbuild.in b/archbuild.in index 7872cd406574cdabc0e2b269795927a9356af173..9f1b98a32fa2b9635540271001557c39610fb825 100644 --- a/archbuild.in +++ b/archbuild.in @@ -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" \ diff --git a/lib/common.sh b/lib/common.sh index b39bbbcb3f9e9f70aeb854e65ba78fa3ab193156..9446ff56731073b1a77b48c0689812f06d6a1460 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -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 +} diff --git a/makechrootpkg.in b/makechrootpkg.in index bf4034e87e0b3ec45052c3064ecb4ceb67b00471..c234b72ff4f4150440b5b0d85d5c0d6daaef5d17 100644 --- a/makechrootpkg.in +++ b/makechrootpkg.in @@ -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 diff --git a/mkarchroot.in b/mkarchroot.in index 68db64c90b4029610e071489c043d2c3246702a3..970bbb9078eb861b187ca6655f66e0540bc01983 100644 --- a/mkarchroot.in +++ b/mkarchroot.in @@ -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"