1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-12-22 15:58:50 -05:00

Add helper methods for setting directory options

This keeps duplicate code to a minimum. This will come in more handy as
we refactor some of these option setters away.

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2011-06-03 16:42:41 -05:00
parent cc25576f8b
commit 991b3ff7e6
5 changed files with 63 additions and 62 deletions

View File

@ -31,6 +31,7 @@
#include "alpm.h" #include "alpm.h"
#include "alpm_list.h" #include "alpm_list.h"
#include "handle.h" #include "handle.h"
#include "log.h"
#include "util.h" #include "util.h"
/* Globals */ /* Globals */

View File

@ -35,6 +35,7 @@
#include "package.h" #include "package.h"
#include "db.h" #include "db.h"
#include "handle.h" #include "handle.h"
#include "trans.h"
/* global handle variable */ /* global handle variable */
extern pmhandle_t *handle; extern pmhandle_t *handle;

View File

@ -298,72 +298,76 @@ int SYMEXPORT alpm_option_set_totaldlcb(alpm_cb_totaldl cb)
return 0; return 0;
} }
int SYMEXPORT alpm_option_set_root(const char *root) static char *canonicalize_path(const char *path) {
char *new_path;
size_t len;
/* verify path ends in a '/' */
len = strlen(path);
if(path[len - 1] != '/') {
len += 1;
}
new_path = calloc(len + 1, sizeof(char));
strncpy(new_path, path, len);
new_path[len - 1] = '/';
return new_path;
}
int _alpm_set_directory_option(const char *value,
char **storage, int must_exist)
{ {
struct stat st; struct stat st;
char *realroot; char *real = NULL;
size_t rootlen; const char *path;
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1)); path = value;
if(!path) {
if(!root) {
pm_errno = PM_ERR_WRONG_ARGS; pm_errno = PM_ERR_WRONG_ARGS;
return -1; return -1;
} }
if(stat(root, &st) == -1 || !S_ISDIR(st.st_mode)) { if(must_exist) {
if(stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
pm_errno = PM_ERR_NOT_A_DIR; pm_errno = PM_ERR_NOT_A_DIR;
return -1; return -1;
} }
real = calloc(PATH_MAX + 1, sizeof(char));
realroot = calloc(PATH_MAX+1, sizeof(char)); if(!realpath(path, real)) {
if(!realpath(root, realroot)) { free(real);
FREE(realroot);
pm_errno = PM_ERR_NOT_A_DIR; pm_errno = PM_ERR_NOT_A_DIR;
return -1; return -1;
} }
path = real;
}
/* verify root ends in a '/' */ if(*storage) {
rootlen = strlen(realroot); FREE(*storage);
if(realroot[rootlen-1] != '/') {
rootlen += 1;
} }
if(handle->root) { *storage = canonicalize_path(path);
FREE(handle->root); free(real);
return 0;
}
int SYMEXPORT alpm_option_set_root(const char *root)
{
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
if(_alpm_set_directory_option(root, &(handle->root), 1)) {
return -1;
} }
handle->root = calloc(rootlen + 1, sizeof(char));
strncpy(handle->root, realroot, rootlen);
handle->root[rootlen-1] = '/';
FREE(realroot);
_alpm_log(PM_LOG_DEBUG, "option 'root' = %s\n", handle->root); _alpm_log(PM_LOG_DEBUG, "option 'root' = %s\n", handle->root);
return 0; return 0;
} }
int SYMEXPORT alpm_option_set_dbpath(const char *dbpath) int SYMEXPORT alpm_option_set_dbpath(const char *dbpath)
{ {
struct stat st;
size_t dbpathlen, lockfilelen;
const char *lf = "db.lck"; const char *lf = "db.lck";
size_t lockfilelen;
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1)); ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
if(!dbpath) {
pm_errno = PM_ERR_WRONG_ARGS; if(_alpm_set_directory_option(dbpath, &(handle->dbpath), 1)) {
return -1; return -1;
} }
if(stat(dbpath, &st) == -1 || !S_ISDIR(st.st_mode)) {
pm_errno = PM_ERR_NOT_A_DIR;
return -1;
}
/* verify dbpath ends in a '/' */
dbpathlen = strlen(dbpath);
if(dbpath[dbpathlen-1] != '/') {
dbpathlen += 1;
}
if(handle->dbpath) {
FREE(handle->dbpath);
}
handle->dbpath = calloc(dbpathlen+1, sizeof(char));
strncpy(handle->dbpath, dbpath, dbpathlen);
handle->dbpath[dbpathlen-1] = '/';
_alpm_log(PM_LOG_DEBUG, "option 'dbpath' = %s\n", handle->dbpath); _alpm_log(PM_LOG_DEBUG, "option 'dbpath' = %s\n", handle->dbpath);
if(handle->lockfile) { if(handle->lockfile) {
@ -379,7 +383,6 @@ int SYMEXPORT alpm_option_set_dbpath(const char *dbpath)
int SYMEXPORT alpm_option_add_cachedir(const char *cachedir) int SYMEXPORT alpm_option_add_cachedir(const char *cachedir)
{ {
char *newcachedir; char *newcachedir;
size_t cachedirlen;
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1)); ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
if(!cachedir) { if(!cachedir) {
@ -389,16 +392,9 @@ int SYMEXPORT alpm_option_add_cachedir(const char *cachedir)
/* don't stat the cachedir yet, as it may not even be needed. we can /* don't stat the cachedir yet, as it may not even be needed. we can
* fail later if it is needed and the path is invalid. */ * fail later if it is needed and the path is invalid. */
/* verify cachedir ends in a '/' */ newcachedir = canonicalize_path(cachedir);
cachedirlen = strlen(cachedir);
if(cachedir[cachedirlen-1] != '/') {
cachedirlen += 1;
}
newcachedir = calloc(cachedirlen + 1, sizeof(char));
strncpy(newcachedir, cachedir, cachedirlen);
newcachedir[cachedirlen-1] = '/';
handle->cachedirs = alpm_list_add(handle->cachedirs, newcachedir); handle->cachedirs = alpm_list_add(handle->cachedirs, newcachedir);
_alpm_log(PM_LOG_DEBUG, "option 'cachedir' = %s\n", newcachedir); _alpm_log(PM_LOG_DEBUG, "backend option 'cachedir' = %s\n", newcachedir);
return 0; return 0;
} }

View File

@ -24,10 +24,7 @@
#include <sys/types.h> #include <sys/types.h>
#include "alpm_list.h" #include "alpm_list.h"
#include "db.h"
#include "log.h"
#include "alpm.h" #include "alpm.h"
#include "trans.h"
#ifdef HAVE_LIBCURL #ifdef HAVE_LIBCURL
#include <curl/curl.h> #include <curl/curl.h>
@ -78,6 +75,8 @@ struct __pmhandle_t {
pmhandle_t *_alpm_handle_new(void); pmhandle_t *_alpm_handle_new(void);
void _alpm_handle_free(pmhandle_t *handle); void _alpm_handle_free(pmhandle_t *handle);
int _alpm_set_directory_option(const char *value, char **storage, int must_exist);
#endif /* _ALPM_HANDLE_H */ #endif /* _ALPM_HANDLE_H */
/* vim: set ts=2 sw=2 noet: */ /* vim: set ts=2 sw=2 noet: */

View File

@ -56,6 +56,7 @@
#include "alpm.h" #include "alpm.h"
#include "alpm_list.h" #include "alpm_list.h"
#include "handle.h" #include "handle.h"
#include "trans.h"
#ifndef HAVE_STRSEP #ifndef HAVE_STRSEP
/* This is a replacement for strsep which is not portable (missing on Solaris). /* This is a replacement for strsep which is not portable (missing on Solaris).
@ -606,15 +607,18 @@ const char *_alpm_filecache_setup(void)
} else if(S_ISDIR(buf.st_mode) && (buf.st_mode & S_IWUSR)) { } else if(S_ISDIR(buf.st_mode) && (buf.st_mode & S_IWUSR)) {
_alpm_log(PM_LOG_DEBUG, "using cachedir: %s\n", cachedir); _alpm_log(PM_LOG_DEBUG, "using cachedir: %s\n", cachedir);
return cachedir; return cachedir;
} else {
_alpm_log(PM_LOG_DEBUG, "skipping cachedir: %s\n", cachedir);
} }
} }
/* we didn't find a valid cache directory. use /tmp. */ /* we didn't find a valid cache directory. use /tmp. */
tmp = alpm_list_add(NULL, strdup("/tmp/")); tmp = alpm_list_add(NULL, "/tmp/");
alpm_option_set_cachedirs(tmp); alpm_option_set_cachedirs(tmp);
_alpm_log(PM_LOG_DEBUG, "using cachedir: %s", "/tmp/\n"); alpm_list_free(tmp);
_alpm_log(PM_LOG_DEBUG, "using cachedir: %s\n", "/tmp/");
_alpm_log(PM_LOG_WARNING, _("couldn't create package cache, using /tmp instead\n")); _alpm_log(PM_LOG_WARNING, _("couldn't create package cache, using /tmp instead\n"));
return alpm_list_getdata(tmp); return "/tmp/";
} }
/** lstat wrapper that treats /path/dirsymlink/ the same as /path/dirsymlink. /** lstat wrapper that treats /path/dirsymlink/ the same as /path/dirsymlink.