[pacman-dev] [PATCH 1/2] Introduce PM_TRANS_FLAG_NOLOCK

Nagy Gabor ngaba at bibl.u-szeged.hu
Sat May 16 12:24:51 EDT 2009


From afa07db77af2896b407b24c6ef69bb19f6b80417 Mon Sep 17 00:00:00 2001
From: Nagy Gabor <ngaba at bibl.u-szeged.hu>
Date: Sat, 16 May 2009 16:54:21 +0200
Subject: [PATCH 1/2] Introduce PM_TRANS_FLAG_NOLOCK

This flag indicates that the front-end will not call alpm_trans_commit(),
so the database needn't be locked. This is the first step toward fixing
FS#8905.

If this flag is set, alpm_trans_commit() does nothing and returns with
an error (that has new code: PM_ERR_TRANS_NOT_LOCKED).

Signed-off-by: Nagy Gabor <ngaba at bibl.u-szeged.hu>
---
 lib/libalpm/alpm.h  |    4 +++-
 lib/libalpm/error.c |    2 ++
 lib/libalpm/trans.c |   32 ++++++++++++++++++++------------
 3 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h
index 2a6c2e0..1e22a59 100644
--- a/lib/libalpm/alpm.h
+++ b/lib/libalpm/alpm.h
@@ -276,7 +276,8 @@ typedef enum _pmtransflag_t {
 	PM_TRANS_FLAG_NEEDED = 0x2000,
 	PM_TRANS_FLAG_ALLEXPLICIT = 0x4000,
 	PM_TRANS_FLAG_UNNEEDED = 0x8000,
-	PM_TRANS_FLAG_RECURSEALL = 0x10000
+	PM_TRANS_FLAG_RECURSEALL = 0x10000,
+	PM_TRANS_FLAG_NOLOCK = 0x20000
 } pmtransflag_t;
 
 /**
@@ -487,6 +488,7 @@ enum _pmerrno_t {
 	PM_ERR_TRANS_NOT_PREPARED,
 	PM_ERR_TRANS_ABORT,
 	PM_ERR_TRANS_TYPE,
+	PM_ERR_TRANS_NOT_LOCKED,
 	/* Packages */
 	PM_ERR_PKG_NOT_FOUND,
 	PM_ERR_PKG_IGNORED,
diff --git a/lib/libalpm/error.c b/lib/libalpm/error.c
index 47f0750..fdff9ed 100644
--- a/lib/libalpm/error.c
+++ b/lib/libalpm/error.c
@@ -99,6 +99,8 @@ const char SYMEXPORT *alpm_strerror(int err)
 			return _("transaction aborted");
 		case PM_ERR_TRANS_TYPE:
 			return _("operation not compatible with the transaction type");
+		case PM_ERR_TRANS_NOT_LOCKED:
+			return _("transaction commit attempt when database is not locked");
 		/* Packages */
 		case PM_ERR_PKG_NOT_FOUND:
 			return _("could not find or read package");
diff --git a/lib/libalpm/trans.c b/lib/libalpm/trans.c
index b144b2a..6e74f3f 100644
--- a/lib/libalpm/trans.c
+++ b/lib/libalpm/trans.c
@@ -71,9 +71,11 @@ int SYMEXPORT alpm_trans_init(pmtranstype_t type, pmtransflag_t flags,
 	ASSERT(handle->trans == NULL, RET_ERR(PM_ERR_TRANS_NOT_NULL, -1));
 
 	/* lock db */
-	handle->lckfd = _alpm_lckmk();
-	if(handle->lckfd == -1) {
-		RET_ERR(PM_ERR_HANDLE_LOCK, -1);
+	if(!(flags & PM_TRANS_FLAG_NOLOCK)) {
+		handle->lckfd = _alpm_lckmk();
+		if(handle->lckfd == -1) {
+			RET_ERR(PM_ERR_HANDLE_LOCK, -1);
+		}
 	}
 
 	handle->trans = _alpm_trans_new();
@@ -158,6 +160,8 @@ int SYMEXPORT alpm_trans_commit(alpm_list_t **data)
 	ASSERT(handle->trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
 	ASSERT(handle->trans->state == STATE_PREPARED, RET_ERR(PM_ERR_TRANS_NOT_PREPARED, -1));
 
+	ASSERT(!(handle->trans->flags & PM_TRANS_FLAG_NOLOCK), RET_ERR(PM_ERR_TRANS_NOT_LOCKED, -1));
+
 	return(_alpm_trans_commit(handle->trans, data));
 }
 
@@ -199,19 +203,23 @@ int SYMEXPORT alpm_trans_release()
 	ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
 	ASSERT(trans->state != STATE_IDLE, RET_ERR(PM_ERR_TRANS_NULL, -1));
 
+	int nolock_flag = trans->flags & PM_TRANS_FLAG_NOLOCK;
+
 	_alpm_trans_free(trans);
 	handle->trans = NULL;
 
 	/* unlock db */
-	if(handle->lckfd != -1) {
-		while(close(handle->lckfd) == -1 && errno == EINTR);
-		handle->lckfd = -1;
-	}
-	if(_alpm_lckrm()) {
-		_alpm_log(PM_LOG_WARNING, _("could not remove lock file %s\n"),
-				alpm_option_get_lockfile());
-		alpm_logaction("warning: could not remove lock file %s\n",
-				alpm_option_get_lockfile());
+	if(!nolock_flag) {
+		if(handle->lckfd != -1) {
+			while(close(handle->lckfd) == -1 && errno == EINTR);
+			handle->lckfd = -1;
+		}
+		if(_alpm_lckrm()) {
+			_alpm_log(PM_LOG_WARNING, _("could not remove lock file %s\n"),
+					alpm_option_get_lockfile());
+			alpm_logaction("warning: could not remove lock file %s\n",
+					alpm_option_get_lockfile());
+		}
 	}
 
 	return(0);
-- 
1.6.3.1



More information about the pacman-dev mailing list