#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>
#include "handle.h"
#include "alpm_list.h"
#include "util.h"
#include "log.h"
#include "delta.h"
#include "trans.h"
#include "alpm.h"
alpm_handle_t *_alpm_handle_new(void)
{
alpm_handle_t *handle;
CALLOC(handle, 1, sizeof(alpm_handle_t), return NULL);
handle->deltaratio = 0.0;
return handle;
}
void _alpm_handle_free(alpm_handle_t *handle)
{
if(handle == NULL) {
return;
}
if(handle->logstream) {
fclose(handle->logstream);
handle->logstream = NULL;
}
if(handle->usesyslog) {
handle->usesyslog = 0;
closelog();
}
#ifdef HAVE_LIBCURL
curl_easy_cleanup(handle->curl);
#endif
regfree(&handle->delta_regex);
_alpm_trans_free(handle->trans);
FREE(handle->root);
FREE(handle->dbpath);
FREELIST(handle->cachedirs);
FREE(handle->logfile);
FREE(handle->lockfile);
FREE(handle->arch);
FREE(handle->gpgdir);
FREELIST(handle->dbs_sync);
FREELIST(handle->noupgrade);
FREELIST(handle->noextract);
FREELIST(handle->ignorepkg);
FREELIST(handle->ignoregroup);
FREE(handle);
}
int _alpm_handle_lock(alpm_handle_t *handle)
{
int fd;
char *dir, *ptr;
ASSERT(handle->lockfile != NULL, return -1);
ASSERT(handle->lckstream == NULL, return 0);
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;
}
int _alpm_handle_unlock(alpm_handle_t *handle)
{
ASSERT(handle->lockfile != NULL, return -1);
ASSERT(handle->lckstream != NULL, return 0);
fclose(handle->lckstream);
handle->lckstream = NULL;
if
|