[pacman-dev] [PATCH 2/2] pacman-optimize: fix database locking race conditions

Andres P aepd87 at gmail.com
Mon Jun 21 12:55:38 EDT 2010


Instead of making the lock file with touch, use mkdir since it's the only
portable atomic transaction available to shell scripts.

Signed-off-by: Andres P <aepd87 at gmail.com>
---
 scripts/pacman-optimize.sh.in |   12 +++++-------
 1 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/scripts/pacman-optimize.sh.in b/scripts/pacman-optimize.sh.in
index 78b2345..f95f827 100644
--- a/scripts/pacman-optimize.sh.in
+++ b/scripts/pacman-optimize.sh.in
@@ -67,7 +67,7 @@ die() {
 }
 
 die_r() {
-	rm -f "$lockfile"
+	rm -rf "$lock"
 	die "$@"
 }
 
@@ -109,15 +109,13 @@ fi
 
 # strip any trailing slash from our dbroot
 dbroot="${dbroot%/}"
-# form the path to our lockfile location
-lockfile="${dbroot}/db.lck"
+# form the path to our db lock
+lock="${dbroot}/db.lck"
 
 # make sure pacman isn't running
-if [[ -f $lockfile ]]; then
+if ! mkdir "$lock"; then
 	die "$(gettext "Pacman lock file was found. Cannot run while pacman is running.")"
 fi
-# do not let pacman run while we do this
-touch "$lockfile"
 
 workdir=$(mktemp -d /tmp/pacman-optimize.XXXXXXXXXX) ||
 	die_r "$(gettext "ERROR: Can not create temp directory for database building.")\n" >&2
@@ -174,7 +172,7 @@ fi
 rm -rf "$dbroot.old"
 
 # remove the lock file and our working directory with sums and tarfile
-rm -f "$lockfile"
+rm -rf "$lock"
 rm -rf "$workdir"
 
 echo
-- 
1.7.1



More information about the pacman-dev mailing list