reworked lock handling (patch from VMiklos <vmiklos@frugalware.org>)

This commit is contained in:
Aurelien Foret 2006-01-26 22:16:57 +00:00
parent 18986b1805
commit 831ff882ae
2 changed files with 27 additions and 21 deletions

View file

@ -50,8 +50,6 @@
#include "handle.h" #include "handle.h"
#include "alpm.h" #include "alpm.h"
#define PM_LOCK "/tmp/pacman.lck"
/* Globals */ /* Globals */
pmhandle_t *handle = NULL; pmhandle_t *handle = NULL;
enum __pmerrno_t pm_errno; enum __pmerrno_t pm_errno;
@ -76,15 +74,6 @@ int alpm_initialize(char *root)
RET_ERR(PM_ERR_MEMORY, -1); RET_ERR(PM_ERR_MEMORY, -1);
} }
/* lock db */
if(handle->access == PM_ACCESS_RW) {
handle->lckfd = _alpm_lckmk(PM_LOCK);
if(handle->lckfd == -1) {
FREE(handle);
RET_ERR(PM_ERR_HANDLE_LOCK, -1);
}
}
STRNCPY(str, (root) ? root : PM_ROOT, PATH_MAX); STRNCPY(str, (root) ? root : PM_ROOT, PATH_MAX);
/* add a trailing '/' if there isn't one */ /* add a trailing '/' if there isn't one */
if(str[strlen(str)-1] != '/') { if(str[strlen(str)-1] != '/') {
@ -105,16 +94,14 @@ int alpm_release()
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1)); ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
/* unlock db */ /* unlock db */
if(handle->access == PM_ACCESS_RW) {
if(handle->lckfd != -1) { if(handle->lckfd != -1) {
close(handle->lckfd); close(handle->lckfd);
handle->lckfd = -1; handle->lckfd = -1;
} }
if(_alpm_lckrm(PM_LOCK)) { if(_alpm_lckrm(PM_LOCK) == -1) {
_alpm_log(PM_LOG_WARNING, "could not remove lock file %s", PM_LOCK); _alpm_log(PM_LOG_WARNING, "could not remove lock file %s", PM_LOCK);
alpm_logaction("warning: could not remove lock file %s", PM_LOCK); alpm_logaction("warning: could not remove lock file %s", PM_LOCK);
} }
}
/* close local database */ /* close local database */
if(handle->db_local) { if(handle->db_local) {
@ -664,6 +651,12 @@ int alpm_trans_init(unsigned char type, unsigned char flags, alpm_trans_cb_event
* perform sanity checks on type and flags: * perform sanity checks on type and flags:
* for instance, we can't set UPGRADE and FRESHEN at the same time */ * for instance, we can't set UPGRADE and FRESHEN at the same time */
/* lock db */
handle->lckfd = _alpm_lckmk(PM_LOCK);
if(handle->lckfd == -1) {
RET_ERR(PM_ERR_HANDLE_LOCK, -1);
}
handle->trans = trans_new(); handle->trans = trans_new();
if(handle->trans == NULL) { if(handle->trans == NULL) {
RET_ERR(PM_ERR_MEMORY, -1); RET_ERR(PM_ERR_MEMORY, -1);
@ -765,6 +758,16 @@ int alpm_trans_release()
FREETRANS(handle->trans); FREETRANS(handle->trans);
/* unlock db */
if(handle->lckfd != -1) {
close(handle->lckfd);
handle->lckfd = -1;
}
if(_alpm_lckrm(PM_LOCK) == -1) {
_alpm_log(PM_LOG_WARNING, "could not remove lock file %s", PM_LOCK);
alpm_logaction("warning: could not remove lock file %s", PM_LOCK);
}
return(0); return(0);
} }
/** @} */ /** @} */

View file

@ -225,7 +225,10 @@ int _alpm_lckmk(char *file)
*/ */
int _alpm_lckrm(char *file) int _alpm_lckrm(char *file)
{ {
return(unlink(file) == -1); if(unlink(file) == -1 && errno != ENOENT) {
return(-1);
}
return(0);
} }
int _alpm_unpack(char *archive, const char *prefix, const char *fn) int _alpm_unpack(char *archive, const char *prefix, const char *fn)