summaryrefslogtreecommitdiff
path: root/lib/libalpm/handle.c
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2011-06-24 04:02:58 -0500
committerDan McGee <dan@archlinux.org>2011-06-24 04:02:58 -0500
commit7b8f8f69f14dac2bbcd7e96fc548aa084be7cd8e (patch)
tree7d59d25daa8ffef5afce17627de0caba1902a0c6 /lib/libalpm/handle.c
parent79e98316ea89486d107466858543e965bcfbb0a9 (diff)
downloadpacman-7b8f8f69f14dac2bbcd7e96fc548aa084be7cd8e.tar.xz
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 <dan@archlinux.org>
Diffstat (limited to 'lib/libalpm/handle.c')
-rw-r--r--lib/libalpm/handle.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c
index b535e0f3..acd35409 100644
--- a/lib/libalpm/handle.c
+++ b/lib/libalpm/handle.c
@@ -22,12 +22,14 @@
#include "config.h"
+#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/types.h>
#include <syslog.h>
#include <sys/stat.h>
+#include <fcntl.h>
/* libalpm */
#include "handle.h"
@@ -86,6 +88,58 @@ void _alpm_handle_free(pmhandle_t *handle)
FREE(handle);
}
+/** Lock the database */
+int _alpm_handle_lock(pmhandle_t *handle)
+{
+ int fd;
+ char *dir, *ptr;
+
+ ASSERT(handle->lockfile != NULL, return -1);
+ ASSERT(handle->lckstream == NULL, return 0);
+
+ /* 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 */
+int _alpm_handle_unlock(pmhandle_t *handle)
+{
+ ASSERT(handle->lockfile != NULL, return -1);
+ ASSERT(handle->lckstream != NULL, return 0);
+
+ if(handle->lckstream != NULL) {
+ fclose(handle->lckstream);
+ handle->lckstream = NULL;
+ }
+ if(unlink(handle->lockfile) && errno != ENOENT) {
+ return -1;
+ }
+ return 0;
+}
+
+
alpm_cb_log SYMEXPORT alpm_option_get_logcb(pmhandle_t *handle)
{
CHECK_HANDLE(handle, return NULL);