1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-12-22 07:48:50 -05:00

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>
This commit is contained in:
Dan McGee 2011-06-24 04:02:58 -05:00
parent 79e98316ea
commit 7b8f8f69f1
3 changed files with 59 additions and 50 deletions

View File

@ -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);

View File

@ -78,6 +78,9 @@ struct __pmhandle_t {
pmhandle_t *_alpm_handle_new(void);
void _alpm_handle_free(pmhandle_t *handle);
int _alpm_handle_lock(pmhandle_t *handle);
int _alpm_handle_unlock(pmhandle_t *handle);
enum _pmerrno_t _alpm_set_directory_option(const char *value,
char **storage, int must_exist);

View File

@ -30,7 +30,6 @@
#include <sys/types.h>
#include <errno.h>
#include <limits.h>
#include <fcntl.h>
/* 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",