1
0
mirror of https://github.com/moparisthebest/pacman synced 2025-03-01 01:41:52 -05:00

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>
This commit is contained in:
Dan McGee 2011-02-28 10:43:36 -06:00
parent 1eccae3d93
commit 5ea4706f57
3 changed files with 48 additions and 49 deletions

View File

@ -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",

View File

@ -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 */
/**

View File

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