From 08c89714984ca64b9ddfe53c99254c70a784e81c Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 11 Apr 2011 12:40:13 +0000 Subject: [PATCH] * Lock the database during updates. --- scripts/NixManifest.pm.in | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/scripts/NixManifest.pm.in b/scripts/NixManifest.pm.in index 4dda69710d..20749acd7e 100644 --- a/scripts/NixManifest.pm.in +++ b/scripts/NixManifest.pm.in @@ -3,6 +3,7 @@ use DBI; use Cwd; use File::stat; use File::Path; +use Fcntl ':flock'; sub addPatch { @@ -259,7 +260,11 @@ EOF $dbh->do("create index if not exists Patches_storePath on Patches(storePath)"); - # !!! locking? + # Acquire an exclusive lock to ensure that only one process + # updates the DB at the same time. This isn't really necessary, + # but it prevents work duplication and lock contention in SQLite. + open MAINLOCK, ">>$manifestDir/cache.lock" or die; + flock(MAINLOCK, LOCK_EX) or die; # Read each manifest in $manifestDir and add it to the database, # unless we've already done so on a previous run. @@ -326,6 +331,8 @@ EOF $dbh->commit; + close MAINLOCK; + return $dbh; }