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:
parent
1eccae3d93
commit
5ea4706f57
3 changed files with 48 additions and 49 deletions
|
@ -32,6 +32,7 @@
|
||||||
#include <sys/statvfs.h>
|
#include <sys/statvfs.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
/* libalpm */
|
/* libalpm */
|
||||||
#include "trans.h"
|
#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.
|
/** Initialize the transaction.
|
||||||
* @param flags flags of the transaction (like nodeps, etc)
|
* @param flags flags of the transaction (like nodeps, etc)
|
||||||
* @param event event callback function pointer
|
* @param event event callback function pointer
|
||||||
|
@ -73,8 +119,7 @@ int SYMEXPORT alpm_trans_init(pmtransflag_t flags,
|
||||||
|
|
||||||
/* lock db */
|
/* lock db */
|
||||||
if(!(flags & PM_TRANS_FLAG_NOLOCK)) {
|
if(!(flags & PM_TRANS_FLAG_NOLOCK)) {
|
||||||
handle->lckstream = _alpm_lckmk();
|
if(make_lock(handle)) {
|
||||||
if(handle->lckstream == NULL) {
|
|
||||||
RET_ERR(PM_ERR_HANDLE_LOCK, -1);
|
RET_ERR(PM_ERR_HANDLE_LOCK, -1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -260,11 +305,7 @@ int SYMEXPORT alpm_trans_release(void)
|
||||||
|
|
||||||
/* unlock db */
|
/* unlock db */
|
||||||
if(!nolock_flag) {
|
if(!nolock_flag) {
|
||||||
if(handle->lckstream != NULL) {
|
if(remove_lock(handle)) {
|
||||||
fclose(handle->lckstream);
|
|
||||||
handle->lckstream = NULL;
|
|
||||||
}
|
|
||||||
if(_alpm_lckrm()) {
|
|
||||||
_alpm_log(PM_LOG_WARNING, _("could not remove lock file %s\n"),
|
_alpm_log(PM_LOG_WARNING, _("could not remove lock file %s\n"),
|
||||||
alpm_option_get_lockfile());
|
alpm_option_get_lockfile());
|
||||||
alpm_logaction("warning: could not remove lock file %s\n",
|
alpm_logaction("warning: could not remove lock file %s\n",
|
||||||
|
|
|
@ -28,7 +28,6 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <fcntl.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
@ -210,45 +209,6 @@ char *_alpm_strtrim(char *str)
|
||||||
return(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 */
|
/* Compression functions */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -80,8 +80,6 @@ int _alpm_makepath(const char *path);
|
||||||
int _alpm_makepath_mode(const char *path, mode_t mode);
|
int _alpm_makepath_mode(const char *path, mode_t mode);
|
||||||
int _alpm_copyfile(const char *src, const char *dest);
|
int _alpm_copyfile(const char *src, const char *dest);
|
||||||
char *_alpm_strtrim(char *str);
|
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_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_unpack(const char *archive, const char *prefix, alpm_list_t *list, int breakfirst);
|
||||||
int _alpm_rmrf(const char *path);
|
int _alpm_rmrf(const char *path);
|
||||||
|
|
Loading…
Add table
Reference in a new issue