#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"
#include "deps.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;
handle->lockfd = -1;
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
#ifdef HAVE_LIBGPGME
FREELIST(handle->known_keys);
#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->noupgrade);
FREELIST(handle->noextract);
FREELIST(handle->ignorepkg);
FREELIST(handle->ignoregroup);
alpm_list_free_inner(handle->assumeinstalled, (alpm_list_fn_free)alpm_dep_free);
alpm_list_free(handle->assumeinstalled);
FREE(handle);
}
int _alpm_handle_lock(alpm_handle_t *handle)
{
char *dir, *ptr;
ASSERT(handle->lockfile != NULL, return -1);
ASSERT(handle->lockfd < 0, return 0);
dir = strdup(handle->lockfile);
ptr = strrchr(dir, '/');
if(ptr) {
*ptr = '\0';
}
if(_alpm_makepath(dir)) {
FREE(dir);
return -1;
}
FREE(dir);
do {
handle->lockfd = open(handle->lockfile, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0000);
} while(handle->lockfd == -1 && errno == EINTR);
return (handle->lockfd >= 0 ? 0 : -1);
}
int _alpm_handle_unlock(alpm_handle_t *handle)
{
ASSERT(handle->lockfile != NULL, return -1);
ASSERT(handle->lockfd >= 0, return 0);
close(handle->lockfd);
handle->lockfd = -1;
if(unlink(handle->lockfile) != 0) {
if(errno == ENOENT) {
_alpm_log(handle, ALPM_LOG_WARNING,
_("lock file missing %s\n"), handle->lockfile);
alpm_logaction(handle, ALPM_CALLER_PREFIX,
"warning: lock file missing %s\n", handle->lockfile);
return 0;
} else {
_alpm_log(handle, ALPM_LOG_WARNING,
_("could not remove lock file %s\n"), handle->lockfile);
alpm_logaction(handle, ALPM_CALLER_PREFIX,
"warning: could not remove lock file %s\n", handle->lockfile);
return -1;
}
}
return 0;
}
alpm_cb_log SYMEXPORT alpm_option_get_logcb(alpm_handle_t *handle)
{
CHECK_HANDLE(handle, return NULL);
return handle->logcb;
}
alpm_cb_download SYMEXPORT alpm_option_get_dlcb(alpm_handle_t *handle)
{
CHECK_HANDLE(handle, return NULL);
|