From 7b8f8f69f14dac2bbcd7e96fc548aa084be7cd8e Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Fri, 24 Jun 2011 04:02:58 -0500 Subject: Move locking functions to handle These operate on the handle, and the state is stored on the handle, so move them where they belong. Up until now only the transaction stuff calls them, but this will soon change and alpm_db_update() will handle locking all on its own. Signed-off-by: Dan McGee --- lib/libalpm/trans.c | 52 ++-------------------------------------------------- 1 file changed, 2 insertions(+), 50 deletions(-) (limited to 'lib/libalpm/trans.c') diff --git a/lib/libalpm/trans.c b/lib/libalpm/trans.c index 11a28e5c..507ea027 100644 --- a/lib/libalpm/trans.c +++ b/lib/libalpm/trans.c @@ -30,7 +30,6 @@ #include #include #include -#include /* libalpm */ #include "trans.h" @@ -48,53 +47,6 @@ * @{ */ -/* Create a lock file */ -static int make_lock(pmhandle_t *handle) -{ - int fd; - char *dir, *ptr; - - ASSERT(handle->lockfile != NULL, return -1); - - /* create the dir of the lockfile first */ - dir = strdup(handle->lockfile); - ptr = strrchr(dir, '/'); - if(ptr) { - *ptr = '\0'; - } - if(_alpm_makepath(dir)) { - FREE(dir); - return -1; - } - FREE(dir); - - do { - fd = open(handle->lockfile, O_WRONLY | O_CREAT | O_EXCL, 0000); - } while(fd == -1 && errno == EINTR); - if(fd > 0) { - FILE *f = fdopen(fd, "w"); - fprintf(f, "%ld\n", (long)getpid()); - fflush(f); - fsync(fd); - handle->lckstream = f; - return 0; - } - return -1; -} - -/* Remove a lock file */ -static int remove_lock(pmhandle_t *handle) -{ - if(handle->lckstream != NULL) { - fclose(handle->lckstream); - handle->lckstream = NULL; - } - if(unlink(handle->lockfile) == -1 && errno != ENOENT) { - return -1; - } - return 0; -} - /** Initialize the transaction. */ int SYMEXPORT alpm_trans_init(pmhandle_t *handle, pmtransflag_t flags, alpm_trans_cb_event event, alpm_trans_cb_conv conv, @@ -116,7 +68,7 @@ int SYMEXPORT alpm_trans_init(pmhandle_t *handle, pmtransflag_t flags, /* lock db */ if(!(flags & PM_TRANS_FLAG_NOLOCK)) { - if(make_lock(handle)) { + if(_alpm_handle_lock(handle)) { RET_ERR(handle, PM_ERR_HANDLE_LOCK, -1); } } @@ -278,7 +230,7 @@ int SYMEXPORT alpm_trans_release(pmhandle_t *handle) /* unlock db */ if(!nolock_flag) { - if(remove_lock(handle)) { + if(_alpm_handle_unlock(handle)) { _alpm_log(handle, PM_LOG_WARNING, _("could not remove lock file %s\n"), alpm_option_get_lockfile(handle)); alpm_logaction(handle, "warning: could not remove lock file %s\n", -- cgit v1.2.3-24-g4f1b