From c22e381a8b86412b6c181446128affe32ab1d71e Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Tue, 21 Aug 2007 21:28:05 -0400 Subject: Post trial install changes, round one A bunch of changes related to my first "real" install of pacman-git into /usr/local and trying to use it. * Shift some uses of free -> FREE in libalpm. * Move stat and sanity checks of config paths into libalpm from the config and argument parsing in pacman.c. * Fix issue where dbpath still was not defined early enough due to its requirement for being used in alpm_db_register. This should be rewritten so it doesn't have this dependency, but this will work for now. Signed-off-by: Dan McGee --- lib/libalpm/alpm.h | 13 ++-- lib/libalpm/conflict.c | 2 +- lib/libalpm/db.c | 13 ++-- lib/libalpm/deps.c | 8 +-- lib/libalpm/error.c | 2 + lib/libalpm/handle.c | 182 +++++++++++++++++++++++++++++++++---------------- lib/libalpm/package.c | 2 +- lib/libalpm/server.c | 2 +- lib/libalpm/sync.c | 8 +-- lib/libalpm/trans.c | 2 +- lib/libalpm/util.c | 2 +- 11 files changed, 151 insertions(+), 85 deletions(-) (limited to 'lib') diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h index beb06c01..844d9bf2 100644 --- a/lib/libalpm/alpm.h +++ b/lib/libalpm/alpm.h @@ -93,17 +93,17 @@ alpm_cb_download alpm_option_get_dlcb(); void alpm_option_set_dlcb(alpm_cb_download cb); const char *alpm_option_get_root(); -void alpm_option_set_root(const char *root); +int alpm_option_set_root(const char *root); const char *alpm_option_get_dbpath(); -void alpm_option_set_dbpath(const char *dbpath); +int alpm_option_set_dbpath(const char *dbpath); alpm_list_t *alpm_option_get_cachedirs(); -void alpm_option_add_cachedir(const char *cachedir); +int alpm_option_add_cachedir(const char *cachedir); void alpm_option_set_cachedirs(alpm_list_t *cachedirs); const char *alpm_option_get_logfile(); -void alpm_option_set_logfile(const char *logfile); +int alpm_option_set_logfile(const char *logfile); const char *alpm_option_get_lockfile(); /* no set_lockfile, path is determined from dbpath */ @@ -145,7 +145,7 @@ alpm_list_t *alpm_option_get_syncdbs(); pmdb_t *alpm_db_register(const char *treename); int alpm_db_unregister(pmdb_t *db); -int alpm_db_unregister_all(); +int alpm_db_unregister_all(void); const char *alpm_db_get_name(const pmdb_t *db); const char *alpm_db_get_url(const pmdb_t *db); @@ -163,7 +163,7 @@ alpm_list_t *alpm_db_getgrpcache(pmdb_t *db); alpm_list_t *alpm_db_test(pmdb_t *db); alpm_list_t *alpm_db_search(pmdb_t *db, const alpm_list_t* needles); -alpm_list_t *alpm_db_get_upgrades(); +alpm_list_t *alpm_db_get_upgrades(void); /* * Packages @@ -389,6 +389,7 @@ enum _pmerrno_t { PM_ERR_SYSTEM, PM_ERR_BADPERMS, PM_ERR_NOT_A_FILE, + PM_ERR_NOT_A_DIR, PM_ERR_WRONG_ARGS, /* Interface */ PM_ERR_HANDLE_NULL, diff --git a/lib/libalpm/conflict.c b/lib/libalpm/conflict.c index c7e5eeb4..832db7cc 100644 --- a/lib/libalpm/conflict.c +++ b/lib/libalpm/conflict.c @@ -62,7 +62,7 @@ static int does_conflict(pmpkg_t *pkg1, const char *conflict, pmpkg_t *pkg2) _alpm_log(PM_LOG_DEBUG, "package %s conflicts with %s (by %s)", pkg1name, pkg2name, conflict); } - free(conf); + FREE(conf); return(match); } diff --git a/lib/libalpm/db.c b/lib/libalpm/db.c index aa9f5b30..32ac48bc 100644 --- a/lib/libalpm/db.c +++ b/lib/libalpm/db.c @@ -73,7 +73,7 @@ pmdb_t SYMEXPORT *alpm_db_register(const char *treename) /** Unregister all package databases * @return 0 on success, -1 on error (pm_errno is set accordingly) */ -int SYMEXPORT alpm_db_unregister_all() +int SYMEXPORT alpm_db_unregister_all(void) { alpm_list_t *i; @@ -431,7 +431,7 @@ alpm_list_t SYMEXPORT *alpm_db_search(pmdb_t *db, const alpm_list_t* needles) /** Get a list of upgradable packages on the current system * @return a pmsyncpkg_t list of packages that are out of date */ -alpm_list_t SYMEXPORT *alpm_db_get_upgrades() +alpm_list_t SYMEXPORT *alpm_db_get_upgrades(void) { alpm_list_t *syncpkgs = NULL; const alpm_list_t *i, *j, *k, *m; @@ -592,15 +592,13 @@ pmdb_t *_alpm_db_new(const char *dbpath, const char *treename) db = calloc(1, sizeof(pmdb_t)); if(db == NULL) { - _alpm_log(PM_LOG_ERROR, _("malloc failed: could not allocate %d bytes"), - sizeof(pmdb_t)); + _alpm_log(PM_LOG_ERROR, "calloc : %s\n", strerror(errno)); RET_ERR(PM_ERR_MEMORY, NULL); } db->path = calloc(1, pathsize); if(db->path == NULL) { - _alpm_log(PM_LOG_ERROR, _("malloc failed: could not allocate %d bytes"), - pathsize); + _alpm_log(PM_LOG_ERROR, "calloc : %s\n", strerror(errno)); FREE(db); RET_ERR(PM_ERR_MEMORY, NULL); } @@ -723,6 +721,7 @@ pmdb_t *_alpm_db_register(const char *treename) RET_ERR(PM_ERR_DB_OPEN, NULL); } snprintf(path, PATH_MAX, "%s%s", dbpath, treename); + /* TODO this is rediculous, we try to do this even if we can't */ if(stat(path, &buf) != 0 || !S_ISDIR(buf.st_mode)) { _alpm_log(PM_LOG_DEBUG, "database dir '%s' does not exist, creating it", path); @@ -731,7 +730,7 @@ pmdb_t *_alpm_db_register(const char *treename) } } - db = _alpm_db_new(handle->dbpath, treename); + db = _alpm_db_new(dbpath, treename); if(db == NULL) { RET_ERR(PM_ERR_DB_CREATE, NULL); } diff --git a/lib/libalpm/deps.c b/lib/libalpm/deps.c index 67cfb0f9..8b2c32cf 100644 --- a/lib/libalpm/deps.c +++ b/lib/libalpm/deps.c @@ -330,7 +330,7 @@ alpm_list_t *_alpm_checkdeps(pmdb_t *db, pmtranstype_t op, } } } - free(depend); + FREE(depend); } } } @@ -378,7 +378,7 @@ alpm_list_t *_alpm_checkdeps(pmdb_t *db, pmtranstype_t op, FREE(miss); } } - free(depend); + FREE(depend); } } } else if(op == PM_TRANS_TYPE_REMOVE) { @@ -434,7 +434,7 @@ alpm_list_t *_alpm_checkdeps(pmdb_t *db, pmtranstype_t op, } } } - free(depend); + FREE(depend); } } } @@ -632,7 +632,7 @@ void _alpm_recursedeps(pmdb_t *db, alpm_list_t **targs, int include_explicit) ready = 0; } } - free(depend); + FREE(depend); } } } diff --git a/lib/libalpm/error.c b/lib/libalpm/error.c index 29ee61ad..b5e301aa 100644 --- a/lib/libalpm/error.c +++ b/lib/libalpm/error.c @@ -42,6 +42,8 @@ const char SYMEXPORT *alpm_strerror(int err) return _("insufficient privileges"); case PM_ERR_NOT_A_FILE: return _("could not find or read file"); + case PM_ERR_NOT_A_DIR: + return _("could not find or read directory"); case PM_ERR_WRONG_ARGS: return _("wrong or NULL argument passed"); /* Interface */ diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c index e45be016..62ce378e 100644 --- a/lib/libalpm/handle.c +++ b/lib/libalpm/handle.c @@ -30,6 +30,8 @@ #include #include #include +#include +#include /* libalpm */ #include "handle.h" @@ -126,79 +128,112 @@ void SYMEXPORT alpm_option_set_logcb(alpm_cb_log cb) { handle->logcb = cb; } void SYMEXPORT alpm_option_set_dlcb(alpm_cb_download cb) { handle->dlcb = cb; } -void SYMEXPORT alpm_option_set_root(const char *root) +int SYMEXPORT alpm_option_set_root(const char *root) { + struct stat st; + char *realroot; + size_t rootlen; + ALPM_LOG_FUNC; - if(handle->root) FREE(handle->root); + if(!root) { + pm_errno = PM_ERR_WRONG_ARGS; + return(-1); + } + if(stat(root, &st) == -1 || !S_ISDIR(st.st_mode)) { + pm_errno = PM_ERR_NOT_A_DIR; + return(-1); + } /* According to the man page, realpath is safe to use IFF the second arg is * NULL. */ - char *realroot = realpath(root, NULL); - if(realroot) { - root = realroot; - } else { - _alpm_log(PM_LOG_ERROR, _("cannot canonicalize specified root path '%s'"), root); + realroot = realpath(root, NULL); + if(!realroot) { + pm_errno = PM_ERR_NOT_A_DIR; + return(-1); } - if(root) { - /* verify root ends in a '/' */ - int rootlen = strlen(realroot); - if(realroot[rootlen-1] != '/') { - rootlen += 1; - } - handle->root = calloc(rootlen+1, sizeof(char)); - strncpy(handle->root, realroot, rootlen); - handle->root[rootlen-1] = '/'; + /* verify root ends in a '/' */ + rootlen = strlen(realroot); + if(realroot[rootlen-1] != '/') { + rootlen += 1; } - if(realroot) { - free(realroot); + if(handle->root) { + FREE(handle->root); } + 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", handle->root); + return(0); } -void 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"; + ALPM_LOG_FUNC; - if(handle->dbpath) FREE(handle->dbpath); - if(handle->lockfile) FREE(handle->lockfile); - if(dbpath) { - /* verify dbpath ends in a '/' */ - int dbpathlen = strlen(dbpath); - if(dbpath[dbpathlen-1] != '/') { - dbpathlen += 1; - } - handle->dbpath = calloc(dbpathlen+1, sizeof(char)); - strncpy(handle->dbpath, dbpath, dbpathlen); - handle->dbpath[dbpathlen-1] = '/'; - _alpm_log(PM_LOG_DEBUG, "option 'dbpath' = %s", handle->dbpath); - - const char *lf = "db.lck"; - int lockfilelen = strlen(handle->dbpath) + strlen(lf) + 1; - handle->lockfile = calloc(lockfilelen, sizeof(char)); - snprintf(handle->lockfile, lockfilelen, "%s%s", handle->dbpath, lf); - _alpm_log(PM_LOG_DEBUG, "option 'lockfile' = %s", handle->lockfile); + if(!dbpath) { + pm_errno = PM_ERR_WRONG_ARGS; + 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", handle->dbpath); + if(handle->lockfile) { + FREE(handle->lockfile); + } + lockfilelen = strlen(handle->dbpath) + strlen(lf) + 1; + handle->lockfile = calloc(lockfilelen, sizeof(char)); + snprintf(handle->lockfile, lockfilelen, "%s%s", handle->dbpath, lf); + _alpm_log(PM_LOG_DEBUG, "option 'lockfile' = %s", handle->lockfile); + return(0); } -void SYMEXPORT alpm_option_add_cachedir(const char *cachedir) +int SYMEXPORT alpm_option_add_cachedir(const char *cachedir) { + struct stat st; + char *newcachedir; + size_t cachedirlen; + ALPM_LOG_FUNC; - if(cachedir) { - char *newcachedir; - /* verify cachedir ends in a '/' */ - int 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); - _alpm_log(PM_LOG_DEBUG, "option 'cachedir' = %s", newcachedir); + if(!cachedir) { + pm_errno = PM_ERR_WRONG_ARGS; + return(-1); } + if(stat(cachedir, &st) == -1 || !S_ISDIR(st.st_mode)) { + pm_errno = PM_ERR_NOT_A_DIR; + return(-1); + } + /* verify cachedir ends in a '/' */ + 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); + _alpm_log(PM_LOG_DEBUG, "option 'cachedir' = %s", newcachedir); + return(0); } void SYMEXPORT alpm_option_set_cachedirs(alpm_list_t *cachedirs) @@ -207,21 +242,48 @@ void SYMEXPORT alpm_option_set_cachedirs(alpm_list_t *cachedirs) if(cachedirs) handle->cachedirs = cachedirs; } -void SYMEXPORT alpm_option_set_logfile(const char *logfile) +int SYMEXPORT alpm_option_set_logfile(const char *logfile) { + char *oldlogfile = handle->logfile; + FILE *oldlogstream = handle->logstream; + ALPM_LOG_FUNC; - if(handle->logfile) { - FREE(handle->logfile); - if(handle->logstream) { - fclose(handle->logstream); - handle->logstream = NULL; + if(!logfile) { + pm_errno = PM_ERR_WRONG_ARGS; + return(-1); + } + + handle->logfile = strdup(logfile); + handle->logstream = fopen(logfile, "a"); + if(handle->logstream == NULL) { + /* TODO we probably want to do this at some point, but right now + * it just blows up when a user calls pacman without privilages */ + _alpm_log(PM_LOG_DEBUG, "couldn't open logfile for writing, ignoring"); + /* + if(errno == EACCES) { + pm_errno = PM_ERR_BADPERMS; + } else if(errno == ENOENT) { + pm_errno = PM_ERR_NOT_A_DIR; + } else { + pm_errno = PM_ERR_SYSTEM; } + * reset logfile to its previous value * + FREE(handle->logfile); + handle->logfile = oldlogfile; + handle->logstream = oldlogstream; + return(-1); + */ + } + + if(oldlogfile) { + FREE(oldlogfile); } - if(logfile) { - handle->logfile = strdup(logfile); - handle->logstream = fopen(logfile, "a"); + if(oldlogstream) { + fclose(oldlogstream); } + _alpm_log(PM_LOG_DEBUG, "option 'logfile' = %s", handle->logfile); + return(0); } void SYMEXPORT alpm_option_set_usesyslog(unsigned short usesyslog) @@ -255,6 +317,7 @@ void SYMEXPORT alpm_option_add_ignorepkg(const char *pkg) { handle->ignorepkg = alpm_list_add(handle->ignorepkg, strdup(pkg)); } + void alpm_option_set_ignorepkgs(alpm_list_t *ignorepkgs) { if(handle->ignorepkg) FREELIST(handle->ignorepkg); @@ -265,6 +328,7 @@ void SYMEXPORT alpm_option_add_holdpkg(const char *pkg) { handle->holdpkg = alpm_list_add(handle->holdpkg, strdup(pkg)); } + void SYMEXPORT alpm_option_set_holdpkgs(alpm_list_t *holdpkgs) { if(handle->holdpkg) FREELIST(handle->holdpkg); diff --git a/lib/libalpm/package.c b/lib/libalpm/package.c index 135a3510..86c19d33 100644 --- a/lib/libalpm/package.c +++ b/lib/libalpm/package.c @@ -1137,7 +1137,7 @@ void _alpm_pkg_update_requiredby(pmpkg_t *pkg) } satisfies = alpm_depcmp(pkg, dep); - free(dep); + FREE(dep); if(satisfies) { alpm_list_t *reqs = alpm_pkg_get_requiredby(pkg); _alpm_log(PM_LOG_DEBUG, "adding '%s' in requiredby field for '%s'", diff --git a/lib/libalpm/server.c b/lib/libalpm/server.c index 1ab89843..a1456a0a 100644 --- a/lib/libalpm/server.c +++ b/lib/libalpm/server.c @@ -133,7 +133,7 @@ static struct url *url_for_file(pmserver_t *server, const char *filename) doc, server->s_url->user, server->s_url->pwd); - free(doc); + FREE(doc); return(ret); } diff --git a/lib/libalpm/sync.c b/lib/libalpm/sync.c index 336e3ff9..8aabcdb3 100644 --- a/lib/libalpm/sync.c +++ b/lib/libalpm/sync.c @@ -734,7 +734,7 @@ int _alpm_sync_commit(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t **data) /* file is not in the cache dir, so add it to the list */ files = alpm_list_add(files, strdup(fname)); } - free(fpath); + FREE(fpath); } } } @@ -805,7 +805,7 @@ int _alpm_sync_commit(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t **data) *data = alpm_list_add(*data, ptr); retval = 1; } - free(filepath); + FREE(filepath); FREE(md5sum2); } if(retval) { @@ -885,10 +885,10 @@ int _alpm_sync_commit(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t **data) fpath = _alpm_filecache_find(fname); if(_alpm_trans_addtarget(tr, fpath) == -1) { - free(fpath); + FREE(fpath); goto error; } - free(fpath); + FREE(fpath); /* using alpm_list_last() is ok because addtarget() adds the new target at the * end of the tr->packages list */ diff --git a/lib/libalpm/trans.c b/lib/libalpm/trans.c index 8c35fa36..53820146 100644 --- a/lib/libalpm/trans.c +++ b/lib/libalpm/trans.c @@ -488,7 +488,7 @@ int _alpm_trans_update_depends(pmtrans_t *trans, pmpkg_t *pkg) } } } - free(dep); + FREE(dep); } return(0); } diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index 72bc9673..9986b49c 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -364,7 +364,7 @@ int _alpm_lckmk() } } - free(dir); + FREE(dir); return(fd > 0 ? fd : -1); } -- cgit v1.2.3-24-g4f1b