2005-03-14 20:51:43 -05:00
|
|
|
/*
|
|
|
|
* query.c
|
|
|
|
*
|
2006-01-02 14:55:35 -05:00
|
|
|
* Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
|
2005-03-14 20:51:43 -05:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
|
|
|
* USA.
|
|
|
|
*/
|
|
|
|
|
2007-03-05 17:13:33 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2007-03-20 23:08:19 -04:00
|
|
|
#include <errno.h>
|
2007-03-22 03:32:54 -04:00
|
|
|
#include <unistd.h>
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
#include <alpm.h>
|
2007-01-19 04:28:44 -05:00
|
|
|
#include <alpm_list.h>
|
2007-03-05 17:13:33 -05:00
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* pacman */
|
2007-04-25 02:21:12 -04:00
|
|
|
#include "pacman.h"
|
2007-03-05 17:13:33 -05:00
|
|
|
#include "package.h"
|
2005-10-28 08:20:40 -04:00
|
|
|
#include "conf.h"
|
2005-12-13 21:23:14 -05:00
|
|
|
#include "util.h"
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2005-10-29 03:00:56 -04:00
|
|
|
extern config_t *config;
|
2006-11-20 04:10:23 -05:00
|
|
|
extern pmdb_t *db_local;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-03-22 03:32:54 -04:00
|
|
|
static char *resolve_path(const char* file)
|
|
|
|
{
|
|
|
|
char *copy, *p, *str = NULL;
|
|
|
|
|
|
|
|
if(!(copy = strdup(file))) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if((p = strrchr(copy, '/')) == NULL) {
|
|
|
|
return(copy);
|
|
|
|
} else {
|
|
|
|
*p = '\0'; ++p;
|
|
|
|
|
|
|
|
str = calloc(PATH_MAX+1, sizeof(char));
|
|
|
|
if(!str) {
|
|
|
|
/* null hmmm.... */
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!realpath(copy, str)) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
str[strlen(str)] = '/';
|
|
|
|
strcat(str, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(copy);
|
|
|
|
return(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-31 03:00:50 -05:00
|
|
|
static void query_fileowner(pmdb_t *db, char *filename)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
|
|
|
struct stat buf;
|
2007-05-07 14:21:44 -04:00
|
|
|
int found = 0;
|
2007-03-22 03:32:54 -04:00
|
|
|
char *rpath;
|
2007-01-19 04:28:44 -05:00
|
|
|
alpm_list_t *i, *j;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
if(db == NULL) {
|
2007-01-31 03:00:50 -05:00
|
|
|
return;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
if(filename == NULL || strlen(filename) == 0) {
|
2007-04-26 16:23:59 -04:00
|
|
|
fprintf(stderr, _("error: no file was specified for --owns\n"));
|
2007-01-31 03:00:50 -05:00
|
|
|
return;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2007-03-20 23:08:19 -04:00
|
|
|
if(stat(filename, &buf) == -1) {
|
2007-05-07 14:21:44 -04:00
|
|
|
fprintf(stderr, _("error: failed to read file '%s': %s\n"),
|
2007-04-26 16:23:59 -04:00
|
|
|
filename, strerror(errno));
|
2007-03-20 23:08:19 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(S_ISDIR(buf.st_mode)) {
|
2007-05-07 14:21:44 -04:00
|
|
|
fprintf(stderr, _("error: cannot determine ownership of a directory\n"));
|
2007-03-20 23:08:19 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-22 03:32:54 -04:00
|
|
|
if(!(rpath = resolve_path(filename))) {
|
2007-05-07 14:21:44 -04:00
|
|
|
fprintf(stderr, _("error: cannot determine real path for '%s': %s\n"),
|
2007-04-26 16:23:59 -04:00
|
|
|
filename, strerror(errno));
|
2007-01-31 03:00:50 -05:00
|
|
|
return;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2007-05-07 14:21:44 -04:00
|
|
|
for(i = alpm_db_getpkgcache(db); i && !found; i = alpm_list_next(i)) {
|
2007-01-19 04:28:44 -05:00
|
|
|
pmpkg_t *info = alpm_list_getdata(i);
|
2005-03-18 13:41:27 -05:00
|
|
|
|
2007-05-07 14:21:44 -04:00
|
|
|
for(j = alpm_pkg_get_files(info); j && !found; j = alpm_list_next(j)) {
|
2007-03-22 03:32:54 -04:00
|
|
|
char path[PATH_MAX], *ppath;
|
|
|
|
snprintf(path, PATH_MAX, "%s%s", alpm_option_get_root(), (const char *)alpm_list_getdata(j));
|
|
|
|
|
|
|
|
ppath = resolve_path(path);
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-03-22 03:32:54 -04:00
|
|
|
if(ppath && strcmp(ppath, rpath) == 0) {
|
|
|
|
printf(_("%s is owned by %s %s\n"), filename, alpm_pkg_get_name(info), alpm_pkg_get_version(info));
|
2007-05-07 14:21:44 -04:00
|
|
|
found = 1;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2007-03-22 03:32:54 -04:00
|
|
|
|
|
|
|
free(ppath);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
2007-05-07 14:21:44 -04:00
|
|
|
if(!found) {
|
2007-04-26 16:23:59 -04:00
|
|
|
fprintf(stderr, _("error: No package owns %s\n"), filename);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2007-03-22 03:32:54 -04:00
|
|
|
|
|
|
|
free(rpath);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2007-01-19 04:28:44 -05:00
|
|
|
int pacman_query(alpm_list_t *targets)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2007-02-07 00:25:45 -05:00
|
|
|
alpm_list_t *sync_dbs = NULL, *i, *j, *k;
|
2006-11-20 04:10:23 -05:00
|
|
|
pmpkg_t *info = NULL;
|
2005-03-14 20:51:43 -05:00
|
|
|
char *package = NULL;
|
|
|
|
int done = 0;
|
|
|
|
|
2005-10-28 08:20:40 -04:00
|
|
|
if(config->op_q_search) {
|
2007-02-11 23:45:21 -05:00
|
|
|
alpm_list_t *ret = alpm_db_search(db_local, targets);
|
2006-10-15 15:34:52 -04:00
|
|
|
if(ret == NULL) {
|
2007-03-06 17:06:28 -05:00
|
|
|
return(0);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2007-01-19 04:28:44 -05:00
|
|
|
for(i = ret; i; i = alpm_list_next(i)) {
|
2007-01-24 03:51:50 -05:00
|
|
|
char *group = NULL;
|
|
|
|
alpm_list_t *grp;
|
2007-01-19 04:28:44 -05:00
|
|
|
pmpkg_t *pkg = alpm_list_getdata(i);
|
2006-10-15 15:34:52 -04:00
|
|
|
|
2007-05-14 01:21:42 -04:00
|
|
|
/* print the package size with the output if -z option passed */
|
|
|
|
if(config->showsize) {
|
|
|
|
/* Convert byte size to MB */
|
|
|
|
double mbsize = alpm_pkg_get_size(pkg) / (1024.0 * 1024.0);
|
|
|
|
|
|
|
|
printf("local/%s %s [%.2f MB]", alpm_pkg_get_name(pkg),
|
|
|
|
alpm_pkg_get_version(pkg), mbsize);
|
|
|
|
} else {
|
|
|
|
printf("local/%s %s", alpm_pkg_get_name(pkg),
|
|
|
|
alpm_pkg_get_version(pkg));
|
|
|
|
}
|
2007-01-24 03:51:50 -05:00
|
|
|
|
|
|
|
if((grp = alpm_pkg_get_groups(pkg)) != NULL) {
|
|
|
|
group = alpm_list_getdata(grp);
|
|
|
|
printf(" (%s)\n ", (char *)alpm_list_getdata(grp));
|
|
|
|
} else {
|
|
|
|
printf("\n ");
|
|
|
|
}
|
|
|
|
|
2006-11-20 04:10:23 -05:00
|
|
|
indentprint(alpm_pkg_get_desc(pkg), 4);
|
2006-10-15 15:34:52 -04:00
|
|
|
printf("\n");
|
|
|
|
}
|
2007-01-24 03:51:50 -05:00
|
|
|
alpm_list_free(ret);
|
2005-03-14 20:51:43 -05:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2005-12-13 21:23:14 -05:00
|
|
|
if(config->op_q_foreign) {
|
2007-01-19 04:28:44 -05:00
|
|
|
sync_dbs = alpm_option_get_syncdbs();
|
|
|
|
|
|
|
|
if(sync_dbs == NULL || alpm_list_count(sync_dbs) == 0) {
|
2007-04-26 16:23:59 -04:00
|
|
|
fprintf(stderr, _("error: no usable package repositories configured.\n"));
|
2005-12-13 21:23:14 -05:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-04 03:26:52 -05:00
|
|
|
if(config->op_q_upgrade) {
|
2007-04-26 15:28:54 -04:00
|
|
|
printf(_("Checking for package upgrades..."));
|
2007-02-04 03:26:52 -05:00
|
|
|
alpm_list_t *syncpkgs;
|
|
|
|
|
|
|
|
if((syncpkgs = alpm_get_upgrades()) != NULL) {
|
|
|
|
display_targets(syncpkgs);
|
|
|
|
return(0);
|
|
|
|
} else {
|
2007-04-26 15:28:54 -04:00
|
|
|
printf(_("no upgrades found"));
|
2007-02-04 03:26:52 -05:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-19 04:28:44 -05:00
|
|
|
for(i = targets; !done; i = (i ? alpm_list_next(i) : NULL)) {
|
2005-03-14 20:51:43 -05:00
|
|
|
if(targets == NULL) {
|
|
|
|
done = 1;
|
|
|
|
} else {
|
2007-01-19 04:28:44 -05:00
|
|
|
if(alpm_list_next(i) == NULL) {
|
2005-03-14 20:51:43 -05:00
|
|
|
done = 1;
|
|
|
|
}
|
2007-01-19 04:28:44 -05:00
|
|
|
package = alpm_list_getdata(i);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* looking for groups */
|
2005-10-28 08:20:40 -04:00
|
|
|
if(config->group) {
|
2005-03-14 20:51:43 -05:00
|
|
|
if(targets == NULL) {
|
2007-01-19 04:28:44 -05:00
|
|
|
for(j = alpm_db_getgrpcache(db_local); j; j = alpm_list_next(j)) {
|
|
|
|
pmgrp_t *grp = alpm_list_getdata(j);
|
|
|
|
alpm_list_t *p, *pkgnames;
|
2006-11-20 04:10:23 -05:00
|
|
|
const char *grpname;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2006-11-20 04:10:23 -05:00
|
|
|
grpname = alpm_grp_get_name(grp);
|
2007-02-26 03:43:02 -05:00
|
|
|
pkgnames = alpm_grp_get_pkgs(grp);
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-01-19 04:28:44 -05:00
|
|
|
for(p = pkgnames; p; p = alpm_list_next(p)) {
|
2007-04-26 15:28:54 -04:00
|
|
|
printf("%s %s\n", grpname, (char *)alpm_list_getdata(p));
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2006-11-20 04:10:23 -05:00
|
|
|
pmgrp_t *grp = alpm_db_readgrp(db_local, package);
|
2005-03-14 20:51:43 -05:00
|
|
|
if(grp) {
|
2007-02-26 03:43:02 -05:00
|
|
|
alpm_list_t *p, *pkgnames = alpm_grp_get_pkgs(grp);
|
2007-01-19 04:28:44 -05:00
|
|
|
for(p = pkgnames; p; p = alpm_list_next(p)) {
|
2007-04-26 15:28:54 -04:00
|
|
|
printf("%s %s\n", package, (char *)alpm_list_getdata(p));
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
} else {
|
2007-04-26 16:23:59 -04:00
|
|
|
fprintf(stderr, _("error: group \"%s\" was not found\n"), package);
|
2006-11-22 04:03:41 -05:00
|
|
|
/* do not return on query operations - let's just carry on */
|
|
|
|
/*return(2);*/
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output info for a .tar.gz package */
|
2005-10-28 08:20:40 -04:00
|
|
|
if(config->op_q_isfile) {
|
2005-03-14 20:51:43 -05:00
|
|
|
if(package == NULL) {
|
2007-04-26 16:23:59 -04:00
|
|
|
fprintf(stderr, _("error: no package file was specified for --file\n"));
|
2005-03-14 20:51:43 -05:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
if(alpm_pkg_load(package, &info) == -1) {
|
2007-04-26 16:23:59 -04:00
|
|
|
fprintf(stderr, _("error: failed to load package '%s' (%s)\n"),
|
|
|
|
package, alpm_strerror(pm_errno));
|
2005-03-14 20:51:43 -05:00
|
|
|
return(1);
|
|
|
|
}
|
2005-10-28 08:20:40 -04:00
|
|
|
if(config->op_q_info) {
|
2007-03-11 21:25:40 -04:00
|
|
|
dump_pkg_full(info, config->op_q_info);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2005-10-28 08:20:40 -04:00
|
|
|
if(config->op_q_list) {
|
2005-03-14 20:51:43 -05:00
|
|
|
dump_pkg_files(info);
|
|
|
|
}
|
2005-10-28 08:20:40 -04:00
|
|
|
if(!config->op_q_info && !config->op_q_list) {
|
2007-04-26 15:28:54 -04:00
|
|
|
printf("%s %s\n", alpm_pkg_get_name(info),
|
|
|
|
alpm_pkg_get_version(info));
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2007-04-25 17:24:23 -04:00
|
|
|
alpm_pkg_free(info);
|
|
|
|
info = NULL;
|
2005-03-14 20:51:43 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* determine the owner of a file */
|
2005-10-28 08:20:40 -04:00
|
|
|
if(config->op_q_owns) {
|
2007-01-31 03:00:50 -05:00
|
|
|
query_fileowner(db_local, package);
|
|
|
|
continue;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* find packages in the db */
|
|
|
|
if(package == NULL) {
|
|
|
|
/* no target */
|
2007-01-19 04:28:44 -05:00
|
|
|
for(i = alpm_db_getpkgcache(db_local); i; i = alpm_list_next(i)) {
|
|
|
|
pmpkg_t *tmpp = alpm_list_getdata(i);
|
2006-11-20 04:10:23 -05:00
|
|
|
const char *pkgname, *pkgver;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2006-11-20 04:10:23 -05:00
|
|
|
pkgname = alpm_pkg_get_name(tmpp);
|
|
|
|
pkgver = alpm_pkg_get_version(tmpp);
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2005-12-13 21:23:14 -05:00
|
|
|
if(config->op_q_list || config->op_q_orphans || config->op_q_foreign) {
|
2007-02-26 03:43:02 -05:00
|
|
|
info = alpm_db_get_pkg(db_local, (char *)pkgname);
|
2005-03-14 20:51:43 -05:00
|
|
|
if(info == NULL) {
|
|
|
|
/* something weird happened */
|
2007-04-26 16:23:59 -04:00
|
|
|
fprintf(stderr, _("error: package \"%s\" not found\n"), pkgname);
|
2006-11-22 04:03:41 -05:00
|
|
|
continue;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2007-02-07 00:25:45 -05:00
|
|
|
}
|
|
|
|
if(config->op_q_foreign) {
|
|
|
|
int match = 0;
|
|
|
|
for(j = sync_dbs; j; j = alpm_list_next(j)) {
|
|
|
|
pmdb_t *db = (pmdb_t *)alpm_list_getdata(j);
|
|
|
|
for(k = alpm_db_getpkgcache(db); k; k = alpm_list_next(k)) {
|
|
|
|
pmpkg_t *pkg = alpm_list_getdata(k);
|
|
|
|
if(strcmp(alpm_pkg_get_name(pkg), alpm_pkg_get_name(info)) == 0) {
|
|
|
|
match = 1;
|
2005-12-13 21:23:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-02-07 00:25:45 -05:00
|
|
|
if(match==0) {
|
2007-04-26 15:28:54 -04:00
|
|
|
printf("%s %s\n", pkgname, pkgver);
|
2007-02-07 00:25:45 -05:00
|
|
|
}
|
|
|
|
} else if(config->op_q_list) {
|
|
|
|
dump_pkg_files(info);
|
|
|
|
} else if(config->op_q_orphans) {
|
|
|
|
if(alpm_pkg_get_requiredby(info) == NULL
|
|
|
|
&& (long)alpm_pkg_get_reason(info) == PM_PKG_REASON_DEPEND) {
|
2007-04-26 15:28:54 -04:00
|
|
|
printf("%s %s\n", pkgname, pkgver);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
} else {
|
2007-04-26 15:28:54 -04:00
|
|
|
printf("%s %s\n", pkgname, pkgver);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2007-02-26 03:43:02 -05:00
|
|
|
info = alpm_db_get_pkg(db_local, package);
|
2005-03-14 20:51:43 -05:00
|
|
|
if(info == NULL) {
|
2007-04-26 16:23:59 -04:00
|
|
|
fprintf(stderr, _("error: package \"%s\" not found\n"), package);
|
2006-11-22 04:03:41 -05:00
|
|
|
continue;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* find a target */
|
2007-03-11 21:25:40 -04:00
|
|
|
if(config->op_q_info) {
|
|
|
|
dump_pkg_full(info, config->op_q_info);
|
|
|
|
}
|
|
|
|
if(config->op_q_list) {
|
|
|
|
dump_pkg_files(info);
|
|
|
|
}
|
|
|
|
if(!config->op_q_info && !config->op_q_list) {
|
2007-04-26 15:28:54 -04:00
|
|
|
printf("%s %s\n", alpm_pkg_get_name(info),
|
|
|
|
alpm_pkg_get_version(info));
|
2007-03-11 21:25:40 -04:00
|
|
|
}
|
2007-02-07 00:25:45 -05:00
|
|
|
if(config->op_q_changelog) {
|
|
|
|
char changelog[PATH_MAX];
|
|
|
|
snprintf(changelog, PATH_MAX, "%s%s/%s/%s-%s/changelog",
|
|
|
|
alpm_option_get_root(), alpm_option_get_dbpath(),
|
|
|
|
alpm_db_get_name(db_local),
|
|
|
|
alpm_pkg_get_name(info),
|
|
|
|
alpm_pkg_get_version(info));
|
|
|
|
dump_pkg_changelog(changelog, alpm_pkg_get_name(info));
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set ts=2 sw=2 noet: */
|