Commit a7a05deb authored by Jan Alexander Steffens (heftig)'s avatar Jan Alexander Steffens (heftig)
Browse files

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.
parent 0e98bd8c
Loading
Loading
Loading
Loading
+2 −6
Original line number Diff line number Diff line
@@ -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" \
+24 −0
Original line number Diff line number Diff line
@@ -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
}
+2 −14
Original line number Diff line number Diff line
@@ -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
+1 −17
Original line number Diff line number Diff line
@@ -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"