diff options
author | Jan Alexander Steffens (heftig) <jan.steffens@gmail.com> | 2013-05-02 07:03:12 +0200 |
---|---|---|
committer | Jan Alexander Steffens (heftig) <jan.steffens@gmail.com> | 2013-05-03 04:34:29 +0200 |
commit | a7a05deb37b3db6aa3606f488467f621c40ce32d (patch) | |
tree | 3091e3cdfc4abb0c73c9199d5c54b6fdfbe5f1d4 | |
parent | 0e98bd8c485b2c55c3971823f99a13c41b1ed696 (diff) |
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.
-rw-r--r-- | archbuild.in | 8 | ||||
-rw-r--r-- | lib/common.sh | 24 | ||||
-rw-r--r-- | makechrootpkg.in | 16 | ||||
-rw-r--r-- | mkarchroot.in | 18 |
4 files changed, 29 insertions, 37 deletions
diff --git a/archbuild.in b/archbuild.in index 7872cd4..9f1b98a 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 b39bbbc..9446ff5 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 bf4034e..c234b72 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 68db64c..970bbb9 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" |