summaryrefslogtreecommitdiffstats
path: root/src/pacman/query.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pacman/query.c')
-rw-r--r--src/pacman/query.c229
1 files changed, 111 insertions, 118 deletions
diff --git a/src/pacman/query.c b/src/pacman/query.c
index 6c63774f..163c3319 100644
--- a/src/pacman/query.c
+++ b/src/pacman/query.c
@@ -26,7 +26,6 @@
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
-#include <unistd.h>
#include <alpm.h>
#include <alpm_list.h>
@@ -41,17 +40,17 @@ static char *resolve_path(const char *file)
{
char *str = NULL;
- str = calloc(PATH_MAX + 1, sizeof(char));
+ str = calloc(PATH_MAX, sizeof(char));
if(!str) {
- return(NULL);
+ return NULL;
}
if(!realpath(file, str)) {
free(str);
- return(NULL);
+ return NULL;
}
- return(str);
+ return str;
}
/* check if filename exists in PATH */
@@ -61,10 +60,10 @@ static int search_path(char **filename, struct stat *bufptr)
size_t flen;
if((envpath = getenv("PATH")) == NULL) {
- return(-1);
+ return -1;
}
if((envpath = envpathsplit = strdup(envpath)) == NULL) {
- return(-1);
+ return -1;
}
flen = strlen(*filename);
@@ -80,7 +79,7 @@ static int search_path(char **filename, struct stat *bufptr)
fullname = malloc(plen + flen + 2);
if(!fullname) {
free(envpath);
- return(-1);
+ return -1;
}
sprintf(fullname, "%s/%s", path, *filename);
@@ -88,15 +87,15 @@ static int search_path(char **filename, struct stat *bufptr)
free(*filename);
*filename = fullname;
free(envpath);
- return(0);
+ return 0;
}
free(fullname);
}
free(envpath);
- return(-1);
+ return -1;
}
-static void print_query_fileowner(const char *filename, pmpkg_t *info)
+static void print_query_fileowner(const char *filename, alpm_pkg_t *info)
{
if(!config->quiet) {
printf(_("%s is owned by %s %s\n"), filename,
@@ -111,26 +110,29 @@ static int query_fileowner(alpm_list_t *targets)
int ret = 0;
char path[PATH_MAX];
const char *root;
- char *append;
- size_t max_length;
+ size_t rootlen;
alpm_list_t *t;
- pmdb_t *db_local;
+ alpm_db_t *db_local;
/* This code is here for safety only */
if(targets == NULL) {
- pm_fprintf(stderr, PM_LOG_ERROR, _("no file was specified for --owns\n"));
- return(1);
+ pm_fprintf(stderr, ALPM_LOG_ERROR, _("no file was specified for --owns\n"));
+ return 1;
}
/* Set up our root path buffer. We only need to copy the location of root in
* once, then we can just overwrite whatever file was there on the previous
* iteration. */
- root = alpm_option_get_root();
- strncpy(path, root, PATH_MAX - 1);
- append = path + strlen(path);
- max_length = PATH_MAX - (append - path) - 1;
+ root = alpm_option_get_root(config->handle);
+ rootlen = strlen(root);
+ if(rootlen + 1 > PATH_MAX) {
+ /* we are in trouble here */
+ pm_fprintf(stderr, ALPM_LOG_ERROR, _("path too long: %s%s\n"), root, "");
+ return 1;
+ }
+ strcpy(path, root);
- db_local = alpm_option_get_localdb();
+ db_local = alpm_option_get_localdb(config->handle);
for(t = targets; t; t = alpm_list_next(t)) {
char *filename, *dname, *rpath;
@@ -145,14 +147,14 @@ static int query_fileowner(alpm_list_t *targets)
/* if it is not a path but a program name, then check in PATH */
if(strchr(filename, '/') == NULL) {
if(search_path(&filename, &buf) == -1) {
- pm_fprintf(stderr, PM_LOG_ERROR, _("failed to find '%s' in PATH: %s\n"),
+ pm_fprintf(stderr, ALPM_LOG_ERROR, _("failed to find '%s' in PATH: %s\n"),
filename, strerror(errno));
ret++;
free(filename);
continue;
}
} else {
- pm_fprintf(stderr, PM_LOG_ERROR, _("failed to read file '%s': %s\n"),
+ pm_fprintf(stderr, ALPM_LOG_ERROR, _("failed to read file '%s': %s\n"),
filename, strerror(errno));
ret++;
free(filename);
@@ -161,7 +163,7 @@ static int query_fileowner(alpm_list_t *targets)
}
if(S_ISDIR(buf.st_mode)) {
- pm_fprintf(stderr, PM_LOG_ERROR,
+ pm_fprintf(stderr, ALPM_LOG_ERROR,
_("cannot determine ownership of directory '%s'\n"), filename);
ret++;
free(filename);
@@ -171,13 +173,13 @@ static int query_fileowner(alpm_list_t *targets)
bname = mbasename(filename);
dname = mdirname(filename);
/* for files in '/', there is no directory name to match */
- if (strcmp(dname, "") == 0) {
+ if(strcmp(dname, "") == 0) {
rpath = NULL;
} else {
rpath = resolve_path(dname);
if(!rpath) {
- pm_fprintf(stderr, PM_LOG_ERROR, _("cannot determine real path for '%s': %s\n"),
+ pm_fprintf(stderr, ALPM_LOG_ERROR, _("cannot determine real path for '%s': %s\n"),
filename, strerror(errno));
free(filename);
free(dname);
@@ -189,12 +191,14 @@ static int query_fileowner(alpm_list_t *targets)
free(dname);
for(i = alpm_db_get_pkgcache(db_local); i && !found; i = alpm_list_next(i)) {
- alpm_list_t *j;
- pmpkg_t *info = alpm_list_getdata(i);
+ alpm_pkg_t *info = alpm_list_getdata(i);
+ alpm_filelist_t *filelist = alpm_pkg_get_files(info);
+ size_t i;
- for(j = alpm_pkg_get_files(info); j && !found; j = alpm_list_next(j)) {
+ for(i = 0; i < filelist->count; i++) {
+ const alpm_file_t *file = filelist->files + i;
char *ppath, *pdname;
- const char *pkgfile = alpm_list_getdata(j);
+ const char *pkgfile = file->name;
/* avoid the costly resolve_path usage if the basenames don't match */
if(strcmp(mbasename(pkgfile), bname) != 0) {
@@ -208,11 +212,11 @@ static int query_fileowner(alpm_list_t *targets)
continue;
}
- if(strlen(pkgfile) > max_length) {
- pm_fprintf(stderr, PM_LOG_ERROR, _("path too long: %s%s\n"), root, pkgfile);
+ if(rootlen + 1 + strlen(pkgfile) > PATH_MAX) {
+ pm_fprintf(stderr, ALPM_LOG_ERROR, _("path too long: %s%s\n"), root, pkgfile);
}
/* concatenate our file and the root path */
- strcpy(append, pkgfile);
+ strcpy(path + rootlen, pkgfile);
pdname = mdirname(path);
ppath = resolve_path(pdname);
@@ -226,7 +230,7 @@ static int query_fileowner(alpm_list_t *targets)
}
}
if(!found) {
- pm_fprintf(stderr, PM_LOG_ERROR, _("No package owns %s\n"), filename);
+ pm_fprintf(stderr, ALPM_LOG_ERROR, _("No package owns %s\n"), filename);
ret++;
}
free(filename);
@@ -241,7 +245,7 @@ static int query_search(alpm_list_t *targets)
{
alpm_list_t *i, *searchlist;
int freelist;
- pmdb_t *db_local = alpm_option_get_localdb();
+ alpm_db_t *db_local = alpm_option_get_localdb(config->handle);
/* if we have a targets list, search for packages matching it */
if(targets) {
@@ -252,29 +256,21 @@ static int query_search(alpm_list_t *targets)
freelist = 0;
}
if(searchlist == NULL) {
- return(1);
+ return 1;
}
for(i = searchlist; i; i = alpm_list_next(i)) {
alpm_list_t *grp;
- pmpkg_t *pkg = alpm_list_getdata(i);
+ alpm_pkg_t *pkg = alpm_list_getdata(i);
- if (!config->quiet) {
+ if(!config->quiet) {
printf("local/%s %s", alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg));
} else {
printf("%s", alpm_pkg_get_name(pkg));
}
- /* print the package size with the output if ShowSize option set */
- if(!config->quiet && config->showsize) {
- /* Convert byte size to MB */
- double mbsize = (double)alpm_pkg_get_size(pkg) / (1024.0 * 1024.0);
-
- printf(" [%.2f MB]", mbsize);
- }
-
- if (!config->quiet) {
+ if(!config->quiet) {
if((grp = alpm_pkg_get_groups(pkg)) != NULL) {
alpm_list_t *k;
printf(" (");
@@ -300,7 +296,7 @@ static int query_search(alpm_list_t *targets)
if(freelist) {
alpm_list_free(searchlist);
}
- return(0);
+ return 0;
}
static int query_group(alpm_list_t *targets)
@@ -308,29 +304,26 @@ static int query_group(alpm_list_t *targets)
alpm_list_t *i, *j;
char *grpname = NULL;
int ret = 0;
- pmdb_t *db_local = alpm_option_get_localdb();
+ alpm_db_t *db_local = alpm_option_get_localdb(config->handle);
if(targets == NULL) {
- for(j = alpm_db_get_grpcache(db_local); j; j = alpm_list_next(j)) {
- pmgrp_t *grp = alpm_list_getdata(j);
- const alpm_list_t *p, *packages;
- const char *grpname;
-
- grpname = alpm_grp_get_name(grp);
- packages = alpm_grp_get_pkgs(grp);
+ for(j = alpm_db_get_groupcache(db_local); j; j = alpm_list_next(j)) {
+ alpm_group_t *grp = alpm_list_getdata(j);
+ const alpm_list_t *p;
- for(p = packages; p; p = alpm_list_next(p)) {
- printf("%s %s\n", grpname, alpm_pkg_get_name(alpm_list_getdata(p)));
+ for(p = grp->packages; p; p = alpm_list_next(p)) {
+ alpm_pkg_t *pkg = alpm_list_getdata(p);
+ printf("%s %s\n", grp->name, alpm_pkg_get_name(pkg));
}
}
} else {
for(i = targets; i; i = alpm_list_next(i)) {
- pmgrp_t *grp;
+ alpm_group_t *grp;
grpname = alpm_list_getdata(i);
- grp = alpm_db_readgrp(db_local, grpname);
+ grp = alpm_db_readgroup(db_local, grpname);
if(grp) {
- const alpm_list_t *p, *packages = alpm_grp_get_pkgs(grp);
- for(p = packages; p; p = alpm_list_next(p)) {
+ const alpm_list_t *p;
+ for(p = grp->packages; p; p = alpm_list_next(p)) {
if(!config->quiet) {
printf("%s %s\n", grpname,
alpm_pkg_get_name(alpm_list_getdata(p)));
@@ -339,7 +332,7 @@ static int query_group(alpm_list_t *targets)
}
}
} else {
- pm_fprintf(stderr, PM_LOG_ERROR, _("group \"%s\" was not found\n"), grpname);
+ pm_fprintf(stderr, ALPM_LOG_ERROR, _("group \"%s\" was not found\n"), grpname);
ret++;
}
}
@@ -347,90 +340,94 @@ static int query_group(alpm_list_t *targets)
return ret;
}
-static int is_foreign(pmpkg_t *pkg)
+static int is_foreign(alpm_pkg_t *pkg)
{
const char *pkgname = alpm_pkg_get_name(pkg);
alpm_list_t *j;
- alpm_list_t *sync_dbs = alpm_option_get_syncdbs();
+ alpm_list_t *sync_dbs = alpm_option_get_syncdbs(config->handle);
int match = 0;
for(j = sync_dbs; j; j = alpm_list_next(j)) {
- pmdb_t *db = alpm_list_getdata(j);
- pmpkg_t *findpkg = alpm_db_get_pkg(db, pkgname);
+ alpm_db_t *db = alpm_list_getdata(j);
+ alpm_pkg_t *findpkg = alpm_db_get_pkg(db, pkgname);
if(findpkg) {
match = 1;
break;
}
}
if(match == 0) {
- return(1);
+ return 1;
}
- return(0);
+ return 0;
}
-static int is_unrequired(pmpkg_t *pkg)
+static int is_unrequired(alpm_pkg_t *pkg)
{
alpm_list_t *requiredby = alpm_pkg_compute_requiredby(pkg);
if(requiredby == NULL) {
- return(1);
+ return 1;
}
FREELIST(requiredby);
- return(0);
+ return 0;
}
-static int filter(pmpkg_t *pkg)
+static int filter(alpm_pkg_t *pkg)
{
/* check if this package was explicitly installed */
if(config->op_q_explicit &&
- alpm_pkg_get_reason(pkg) != PM_PKG_REASON_EXPLICIT) {
- return(0);
+ alpm_pkg_get_reason(pkg) != ALPM_PKG_REASON_EXPLICIT) {
+ return 0;
}
/* check if this package was installed as a dependency */
if(config->op_q_deps &&
- alpm_pkg_get_reason(pkg) != PM_PKG_REASON_DEPEND) {
- return(0);
+ alpm_pkg_get_reason(pkg) != ALPM_PKG_REASON_DEPEND) {
+ return 0;
}
/* check if this pkg isn't in a sync DB */
if(config->op_q_foreign && !is_foreign(pkg)) {
- return(0);
+ return 0;
}
/* check if this pkg is unrequired */
if(config->op_q_unrequired && !is_unrequired(pkg)) {
- return(0);
+ return 0;
}
/* check if this pkg is outdated */
- if(config->op_q_upgrade && (alpm_sync_newversion(pkg, alpm_option_get_syncdbs()) == NULL)) {
- return(0);
+ if(config->op_q_upgrade && (alpm_sync_newversion(pkg,
+ alpm_option_get_syncdbs(config->handle)) == NULL)) {
+ return 0;
}
- return(1);
+ return 1;
}
/* Loop through the packages. For each package,
* loop through files to check if they exist. */
-static int check(pmpkg_t *pkg)
+static int check(alpm_pkg_t *pkg)
{
- alpm_list_t *i;
- const char *root;
+ const char *root, *pkgname;
int allfiles = 0, errors = 0;
size_t rootlen;
char f[PATH_MAX];
+ alpm_filelist_t *filelist;
+ size_t i;
- root = alpm_option_get_root();
+ root = alpm_option_get_root(config->handle);
rootlen = strlen(root);
if(rootlen + 1 > PATH_MAX) {
/* we are in trouble here */
- pm_fprintf(stderr, PM_LOG_ERROR, _("path too long: %s%s\n"), root, "");
- return(1);
+ pm_fprintf(stderr, ALPM_LOG_ERROR, _("path too long: %s%s\n"), root, "");
+ return 1;
}
strcpy(f, root);
- const char *pkgname = alpm_pkg_get_name(pkg);
- for(i = alpm_pkg_get_files(pkg); i; i = alpm_list_next(i)) {
+ pkgname = alpm_pkg_get_name(pkg);
+ filelist = alpm_pkg_get_files(pkg);
+ for(i = 0; i < filelist->count; i++) {
+ const alpm_file_t *file = filelist->files + i;
struct stat st;
- const char *path = alpm_list_getdata(i);
+ const char *path = file->name;
if(rootlen + 1 + strlen(path) > PATH_MAX) {
- pm_fprintf(stderr, PM_LOG_WARNING, _("path too long: %s%s\n"), root, path);
+ pm_fprintf(stderr, ALPM_LOG_WARNING, _("path too long: %s%s\n"), root, path);
continue;
}
strcpy(f + rootlen, path);
@@ -440,7 +437,7 @@ static int check(pmpkg_t *pkg)
if(config->quiet) {
printf("%s %s\n", pkgname, f);
} else {
- pm_printf(PM_LOG_WARNING, "%s: %s (%s)\n",
+ pm_printf(ALPM_LOG_WARNING, "%s: %s (%s)\n",
pkgname, f, strerror(errno));
}
errors++;
@@ -454,19 +451,18 @@ static int check(pmpkg_t *pkg)
(unsigned long)errors), errors);
}
- return(errors != 0 ? 1 : 0);
+ return (errors != 0 ? 1 : 0);
}
-static int display(pmpkg_t *pkg)
+static int display(alpm_pkg_t *pkg)
{
int ret = 0;
if(config->op_q_info) {
if(config->op_q_isfile) {
- /* omit info that isn't applicable for a file package */
- dump_pkg_full(pkg, 0);
+ dump_pkg_full(pkg, PKG_FROM_FILE, 0);
} else {
- dump_pkg_full(pkg, config->op_q_info);
+ dump_pkg_full(pkg, PKG_FROM_LOCALDB, config->op_q_info > 1);
}
}
if(config->op_q_list) {
@@ -480,13 +476,13 @@ static int display(pmpkg_t *pkg)
}
if(!config->op_q_info && !config->op_q_list
&& !config->op_q_changelog && !config->op_q_check) {
- if (!config->quiet) {
+ if(!config->quiet) {
printf("%s %s\n", alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg));
} else {
printf("%s\n", alpm_pkg_get_name(pkg));
}
}
- return(ret);
+ return ret;
}
int pacman_query(alpm_list_t *targets)
@@ -494,41 +490,38 @@ int pacman_query(alpm_list_t *targets)
int ret = 0;
int match = 0;
alpm_list_t *i;
- pmpkg_t *pkg = NULL;
- pmdb_t *db_local;
+ alpm_pkg_t *pkg = NULL;
+ alpm_db_t *db_local;
/* First: operations that do not require targets */
/* search for a package */
if(config->op_q_search) {
ret = query_search(targets);
- return(ret);
+ return ret;
}
/* looking for groups */
if(config->group) {
ret = query_group(targets);
- return(ret);
+ return ret;
}
- if(config->op_q_foreign) {
- /* ensure we have at least one valid sync db set up */
- alpm_list_t *sync_dbs = alpm_option_get_syncdbs();
- if(sync_dbs == NULL || alpm_list_count(sync_dbs) == 0) {
- pm_printf(PM_LOG_ERROR, _("no usable package repositories configured.\n"));
- return(1);
+ if(config->op_q_foreign || config->op_q_upgrade) {
+ if(check_syncdbs(1, 1)) {
+ return 1;
}
}
- db_local = alpm_option_get_localdb();
+ db_local = alpm_option_get_localdb(config->handle);
/* operations on all packages in the local DB
* valid: no-op (plain -Q), list, info, check
* invalid: isfile, owns */
if(targets == NULL) {
if(config->op_q_isfile || config->op_q_owns) {
- pm_printf(PM_LOG_ERROR, _("no targets specified (use -h for help)\n"));
- return(1);
+ pm_printf(ALPM_LOG_ERROR, _("no targets specified (use -h for help)\n"));
+ return 1;
}
for(i = alpm_db_get_pkgcache(db_local); i; i = alpm_list_next(i)) {
@@ -544,7 +537,7 @@ int pacman_query(alpm_list_t *targets)
if(!match) {
ret = 1;
}
- return(ret);
+ return ret;
}
/* Second: operations that require target(s) */
@@ -552,7 +545,7 @@ int pacman_query(alpm_list_t *targets)
/* determine the owner of a file */
if(config->op_q_owns) {
ret = query_fileowner(targets);
- return(ret);
+ return ret;
}
/* operations on named packages in the local DB
@@ -561,13 +554,13 @@ int pacman_query(alpm_list_t *targets)
char *strname = alpm_list_getdata(i);
if(config->op_q_isfile) {
- alpm_pkg_load(strname, 1, &pkg);
+ alpm_pkg_load(config->handle, strname, 1, 0, &pkg);
} else {
pkg = alpm_db_get_pkg(db_local, strname);
}
if(pkg == NULL) {
- pm_fprintf(stderr, PM_LOG_ERROR, _("package \"%s\" not found\n"), strname);
+ pm_fprintf(stderr, ALPM_LOG_ERROR, _("package \"%s\" not found\n"), strname);
ret = 1;
continue;
}
@@ -590,7 +583,7 @@ int pacman_query(alpm_list_t *targets)
ret = 1;
}
- return(ret);
+ return ret;
}
/* vim: set ts=2 sw=2 noet: */