summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2011-02-28 10:43:36 -0600
committerDan McGee <dan@archlinux.org>2011-02-28 10:43:36 -0600
commit5ea4706f57e15de23a5fd36eff3bc4619aeac224 (patch)
treee2183dab0dc35c7acfbb0a519655fa1a308b24d3
parent1eccae3d9349a4366f511910f64870027a7fd2d7 (diff)
downloadpacman-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>
-rw-r--r--lib/libalpm/trans.c55
-rw-r--r--lib/libalpm/util.c40
-rw-r--r--lib/libalpm/util.h2
3 files changed, 48 insertions, 49 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",
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index 149a7f80..9281f0e6 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -28,7 +28,6 @@
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
-#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <dirent.h>
@@ -210,45 +209,6 @@ char *_alpm_strtrim(char *str)
return(str);
}
-/* Create a lock file */
-FILE *_alpm_lckmk(void)
-{
- int fd;
- char *dir, *ptr;
- const char *file = alpm_option_get_lockfile();
-
- /* create the dir of the lockfile first */
- dir = strdup(file);
- ptr = strrchr(dir, '/');
- if(ptr) {
- *ptr = '\0';
- }
- _alpm_makepath(dir);
- FREE(dir);
-
- do {
- fd = open(file, 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);
- return(f);
- }
- return(NULL);
-}
-
-/* Remove a lock file */
-int _alpm_lckrm(void)
-{
- const char *file = alpm_option_get_lockfile();
- if(unlink(file) == -1 && errno != ENOENT) {
- return(-1);
- }
- return(0);
-}
-
/* Compression functions */
/**
diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h
index 930503b7..d65f7734 100644
--- a/lib/libalpm/util.h
+++ b/lib/libalpm/util.h
@@ -80,8 +80,6 @@ int _alpm_makepath(const char *path);
int _alpm_makepath_mode(const char *path, mode_t mode);
int _alpm_copyfile(const char *src, const char *dest);
char *_alpm_strtrim(char *str);
-FILE *_alpm_lckmk(void);
-int _alpm_lckrm(void);
int _alpm_unpack_single(const char *archive, const char *prefix, const char *fn);
int _alpm_unpack(const char *archive, const char *prefix, alpm_list_t *list, int breakfirst);
int _alpm_rmrf(const char *path);