diff options
author | Dan McGee <dan@archlinux.org> | 2011-02-28 10:43:36 -0600 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2011-02-28 10:43:36 -0600 |
commit | 5ea4706f57e15de23a5fd36eff3bc4619aeac224 (patch) | |
tree | e2183dab0dc35c7acfbb0a519655fa1a308b24d3 /lib/libalpm/trans.c | |
parent | 1eccae3d9349a4366f511910f64870027a7fd2d7 (diff) | |
download | pacman-5ea4706f57e15de23a5fd36eff3bc4619aeac224.tar.xz |
Move locking functions to where they are needed
We only call these from the transaction init and teardown, so move them
to that file, mark them static, and push more of the logic of handle
manipulation into these functions.
Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'lib/libalpm/trans.c')
-rw-r--r-- | lib/libalpm/trans.c | 55 |
1 files changed, 48 insertions, 7 deletions
diff --git a/lib/libalpm/trans.c b/lib/libalpm/trans.c index c1710e04..8e47f377 100644 --- a/lib/libalpm/trans.c +++ b/lib/libalpm/trans.c @@ -32,6 +32,7 @@ #include <sys/statvfs.h> #include <errno.h> #include <limits.h> +#include <fcntl.h> /* libalpm */ #include "trans.h" @@ -51,6 +52,51 @@ * @{ */ +/* Create a lock file */ +static int make_lock(pmhandle_t *handle) +{ + int fd; + char *dir, *ptr; + + /* 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. * @param flags flags of the transaction (like nodeps, etc) * @param event event callback function pointer @@ -73,8 +119,7 @@ int SYMEXPORT alpm_trans_init(pmtransflag_t flags, /* lock db */ if(!(flags & PM_TRANS_FLAG_NOLOCK)) { - handle->lckstream = _alpm_lckmk(); - if(handle->lckstream == NULL) { + if(make_lock(handle)) { RET_ERR(PM_ERR_HANDLE_LOCK, -1); } } @@ -260,11 +305,7 @@ int SYMEXPORT alpm_trans_release(void) /* unlock db */ if(!nolock_flag) { - if(handle->lckstream != NULL) { - fclose(handle->lckstream); - handle->lckstream = NULL; - } - if(_alpm_lckrm()) { + if(remove_lock(handle)) { _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", |