2005-03-14 20:51:43 -05:00
|
|
|
/*
|
|
|
|
* query.c
|
2007-06-02 12:34:01 -04:00
|
|
|
*
|
|
|
|
* Copyright (c) 2002-2007 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
|
2007-06-02 12:34:01 -04:00
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
2005-03-14 20:51:43 -05:00
|
|
|
* 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)
|
|
|
|
{
|
2007-05-18 02:20:49 -04:00
|
|
|
char *str = NULL;
|
2007-03-22 03:32:54 -04:00
|
|
|
|
2007-05-18 02:20:49 -04:00
|
|
|
str = calloc(PATH_MAX+1, sizeof(char));
|
|
|
|
if(!str) {
|
|
|
|
/* null hmmm.... */
|
2007-03-22 03:32:54 -04:00
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
2007-05-20 02:20:07 -04:00
|
|
|
if(!realpath(file, str)) {
|
2007-05-18 02:20:49 -04:00
|
|
|
return(NULL);
|
2007-03-22 03:32:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-02 12:34:01 -04:00
|
|
|
static int query_fileowner(alpm_list_t *targets)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2007-06-02 12:34:01 -04:00
|
|
|
int ret = 0;
|
|
|
|
alpm_list_t *t;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-06-12 23:55:14 -04:00
|
|
|
/* This code is here for safety only */
|
2007-06-02 12:34:01 -04:00
|
|
|
if(targets == NULL) {
|
2007-04-26 16:23:59 -04:00
|
|
|
fprintf(stderr, _("error: no file was specified for --owns\n"));
|
2007-06-02 12:34:01 -04:00
|
|
|
return(1);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2007-06-02 12:34:01 -04:00
|
|
|
for(t = targets; t; t = alpm_list_next(t)) {
|
|
|
|
int found = 0;
|
|
|
|
char *filename = alpm_list_getdata(t);
|
|
|
|
char *rpath;
|
|
|
|
struct stat buf;
|
|
|
|
alpm_list_t *i, *j;
|
|
|
|
|
|
|
|
if(stat(filename, &buf) == -1) {
|
|
|
|
fprintf(stderr, _("error: failed to read file '%s': %s\n"),
|
|
|
|
filename, strerror(errno));
|
|
|
|
ret++;
|
|
|
|
continue;
|
|
|
|
}
|
2007-03-20 23:08:19 -04:00
|
|
|
|
2007-06-02 12:34:01 -04:00
|
|
|
if(S_ISDIR(buf.st_mode)) {
|
|
|
|
fprintf(stderr, _("error: cannot determine ownership of a directory\n"));
|
|
|
|
ret++;
|
|
|
|
continue;
|
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-06-02 12:34:01 -04:00
|
|
|
if(!(rpath = resolve_path(filename))) {
|
|
|
|
fprintf(stderr, _("error: cannot determine real path for '%s': %s\n"),
|
|
|
|
filename, strerror(errno));
|
|
|
|
ret++;
|
|
|
|
continue;
|
|
|
|
}
|
2005-03-18 13:41:27 -05:00
|
|
|
|
2007-06-02 12:34:01 -04:00
|
|
|
for(i = alpm_db_getpkgcache(db_local); i && !found; i = alpm_list_next(i)) {
|
|
|
|
pmpkg_t *info = alpm_list_getdata(i);
|
2007-03-22 03:32:54 -04:00
|
|
|
|
2007-06-02 12:34:01 -04:00
|
|
|
for(j = alpm_pkg_get_files(info); j && !found; j = alpm_list_next(j)) {
|
|
|
|
char path[PATH_MAX], *ppath;
|
|
|
|
snprintf(path, PATH_MAX, "%s%s",
|
|
|
|
alpm_option_get_root(), (const char *)alpm_list_getdata(j));
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-06-02 12:34:01 -04:00
|
|
|
ppath = resolve_path(path);
|
2007-03-22 03:32:54 -04:00
|
|
|
|
2007-06-02 12:34:01 -04:00
|
|
|
if(ppath && strcmp(ppath, rpath) == 0) {
|
|
|
|
printf(_("%s is owned by %s %s\n"), rpath,
|
|
|
|
alpm_pkg_get_name(info), alpm_pkg_get_version(info));
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
free(ppath);
|
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2007-06-02 12:34:01 -04:00
|
|
|
if(!found) {
|
|
|
|
fprintf(stderr, _("error: No package owns %s\n"), filename);
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
free(rpath);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2007-03-22 03:32:54 -04:00
|
|
|
|
2007-06-02 12:34:01 -04:00
|
|
|
return ret;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2007-06-02 12:34:01 -04:00
|
|
|
/* search the local database for a matching package */
|
|
|
|
static int query_search(alpm_list_t *targets)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2007-06-02 12:34:01 -04:00
|
|
|
alpm_list_t *i, *searchlist;
|
|
|
|
int freelist;
|
|
|
|
|
|
|
|
/* if we have a targets list, search for packages matching it */
|
|
|
|
if(targets) {
|
|
|
|
searchlist = alpm_db_search(db_local, targets);
|
|
|
|
freelist = 1;
|
|
|
|
} else {
|
|
|
|
searchlist = alpm_db_getpkgcache(db_local);
|
|
|
|
freelist = 0;
|
|
|
|
}
|
|
|
|
if(searchlist == NULL) {
|
|
|
|
return(1);
|
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-06-02 12:34:01 -04:00
|
|
|
for(i = searchlist; i; i = alpm_list_next(i)) {
|
|
|
|
char *group = NULL;
|
|
|
|
alpm_list_t *grp;
|
|
|
|
pmpkg_t *pkg = alpm_list_getdata(i);
|
|
|
|
|
|
|
|
printf("local/%s %s", alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg));
|
|
|
|
|
|
|
|
/* print the package size with the output if ShowSize option set */
|
2007-06-04 12:51:23 -04:00
|
|
|
if(config->showsize) {
|
2007-06-02 12:34:01 -04:00
|
|
|
/* Convert byte size to MB */
|
|
|
|
double mbsize = alpm_pkg_get_size(pkg) / (1024.0 * 1024.0);
|
|
|
|
|
|
|
|
printf(" [%.2f MB]", mbsize);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2006-10-15 15:34:52 -04:00
|
|
|
|
2007-06-02 12:34:01 -04:00
|
|
|
if((grp = alpm_pkg_get_groups(pkg)) != NULL) {
|
|
|
|
group = alpm_list_getdata(grp);
|
|
|
|
printf(" (%s)", (char *)alpm_list_getdata(grp));
|
|
|
|
}
|
2007-06-01 11:00:39 -04:00
|
|
|
|
2007-06-02 12:34:01 -04:00
|
|
|
/* we need a newline and initial indent first */
|
|
|
|
printf("\n ");
|
|
|
|
indentprint(alpm_pkg_get_desc(pkg), 4);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
/* we only want to free if the list was a search list */
|
|
|
|
if(freelist) {
|
|
|
|
alpm_list_free(searchlist);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
2007-05-14 01:21:42 -04:00
|
|
|
|
2007-06-02 12:34:01 -04:00
|
|
|
static int query_group(alpm_list_t *targets)
|
|
|
|
{
|
|
|
|
alpm_list_t *i, *j;
|
|
|
|
char *package = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
if(targets == NULL) {
|
|
|
|
for(j = alpm_db_getgrpcache(db_local); j; j = alpm_list_next(j)) {
|
|
|
|
pmgrp_t *grp = alpm_list_getdata(j);
|
2007-06-05 17:34:33 -04:00
|
|
|
const alpm_list_t *p, *pkgnames;
|
2007-06-02 12:34:01 -04:00
|
|
|
const char *grpname;
|
|
|
|
|
|
|
|
grpname = alpm_grp_get_name(grp);
|
|
|
|
pkgnames = alpm_grp_get_pkgs(grp);
|
2007-01-24 03:51:50 -05:00
|
|
|
|
2007-06-02 12:34:01 -04:00
|
|
|
for(p = pkgnames; p; p = alpm_list_next(p)) {
|
|
|
|
printf("%s %s\n", grpname, (char *)alpm_list_getdata(p));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(i = targets; i; i = alpm_list_next(i)) {
|
|
|
|
package = alpm_list_getdata(i);
|
|
|
|
pmgrp_t *grp = alpm_db_readgrp(db_local, package);
|
|
|
|
if(grp) {
|
2007-06-05 17:34:33 -04:00
|
|
|
const alpm_list_t *p, *pkgnames = alpm_grp_get_pkgs(grp);
|
2007-06-02 12:34:01 -04:00
|
|
|
for(p = pkgnames; p; p = alpm_list_next(p)) {
|
|
|
|
printf("%s %s\n", package, (char *)alpm_list_getdata(p));
|
|
|
|
}
|
2007-01-24 03:51:50 -05:00
|
|
|
} else {
|
2007-06-02 12:34:01 -04:00
|
|
|
fprintf(stderr, _("error: group \"%s\" was not found\n"), package);
|
|
|
|
ret++;
|
2007-01-24 03:51:50 -05:00
|
|
|
}
|
2007-06-02 12:34:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2007-01-24 03:51:50 -05:00
|
|
|
|
2007-06-12 23:55:14 -04:00
|
|
|
static int query_test(void)
|
2007-06-02 12:34:01 -04:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2007-06-12 23:55:14 -04:00
|
|
|
alpm_list_t *testlist;
|
2007-06-02 12:34:01 -04:00
|
|
|
|
2007-07-12 15:12:08 -04:00
|
|
|
printf(_("Checking database for consistency... "));
|
2007-06-12 23:55:14 -04:00
|
|
|
testlist = alpm_db_test(db_local);
|
|
|
|
if(testlist == NULL) {
|
|
|
|
printf(_("check complete.\n"));
|
|
|
|
return(0);
|
|
|
|
} else {
|
|
|
|
/* on failure, increment the ret val by 1 for each failure */
|
|
|
|
alpm_list_t *i;
|
|
|
|
printf(_("check failed!\n"));
|
|
|
|
fflush(stdout);
|
|
|
|
for(i = testlist; i; i = alpm_list_next(i)) {
|
|
|
|
fprintf(stderr, "%s\n", (char*)alpm_list_getdata(i));
|
|
|
|
ret++;
|
|
|
|
}
|
2007-06-02 12:34:01 -04:00
|
|
|
return(ret);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2007-06-12 23:55:14 -04:00
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-06-12 23:55:14 -04:00
|
|
|
static int query_upgrades(void)
|
|
|
|
{
|
2007-07-12 15:12:08 -04:00
|
|
|
printf(_("Checking for package upgrades... \n"));
|
2007-06-12 23:55:14 -04:00
|
|
|
alpm_list_t *syncpkgs;
|
|
|
|
|
|
|
|
if((syncpkgs = alpm_db_get_upgrades()) != NULL) {
|
|
|
|
display_targets(syncpkgs);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2007-07-12 15:12:08 -04:00
|
|
|
printf(_("no upgrades found.\n"));
|
2007-06-12 23:55:14 -04:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
2007-07-14 14:45:30 -04:00
|
|
|
static int is_foreign(pmpkg_t *pkg)
|
2007-06-12 23:55:14 -04:00
|
|
|
{
|
2007-07-14 14:45:30 -04:00
|
|
|
const char *pkgname = alpm_pkg_get_name(pkg);
|
|
|
|
alpm_list_t *j;
|
|
|
|
alpm_list_t *sync_dbs = alpm_option_get_syncdbs();
|
|
|
|
|
|
|
|
int match = 0;
|
|
|
|
for(j = sync_dbs; j; j = alpm_list_next(j)) {
|
|
|
|
pmdb_t *db = alpm_list_getdata(j);
|
|
|
|
pmpkg_t *pkg = alpm_db_get_pkg(db, pkgname);
|
|
|
|
if(pkg) {
|
|
|
|
match = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(match == 0) {
|
2007-06-12 23:55:14 -04:00
|
|
|
return(1);
|
|
|
|
}
|
2007-07-14 14:45:30 -04:00
|
|
|
return(0);
|
|
|
|
}
|
2007-06-12 23:55:14 -04:00
|
|
|
|
2007-07-14 14:45:30 -04:00
|
|
|
static int is_orphan(pmpkg_t *pkg)
|
|
|
|
{
|
|
|
|
if(alpm_pkg_get_requiredby(pkg) == NULL) {
|
|
|
|
return(1);
|
2007-05-31 15:48:16 -04:00
|
|
|
}
|
2007-06-12 23:55:14 -04:00
|
|
|
return(0);
|
|
|
|
}
|
2007-05-31 15:48:16 -04:00
|
|
|
|
2007-07-14 14:45:30 -04:00
|
|
|
static int filter(pmpkg_t *pkg)
|
2007-06-12 23:55:14 -04:00
|
|
|
{
|
2007-07-14 14:45:30 -04:00
|
|
|
/* check if this pkg isn't in a sync DB */
|
|
|
|
if(config->op_q_foreign && !is_foreign(pkg)) {
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
/* check if this pkg is orphaned */
|
|
|
|
if(config->op_q_orphans && !is_orphan(pkg)) {
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
return(1);
|
|
|
|
}
|
2007-01-19 04:28:44 -05:00
|
|
|
|
2007-07-14 14:45:30 -04:00
|
|
|
static void display(pmpkg_t *pkg)
|
|
|
|
{
|
|
|
|
if(config->op_q_info) {
|
|
|
|
dump_pkg_full(pkg, config->op_q_info);
|
|
|
|
}
|
|
|
|
if(config->op_q_list) {
|
|
|
|
dump_pkg_files(pkg);
|
|
|
|
}
|
|
|
|
if(config->op_q_changelog) {
|
|
|
|
char changelog[PATH_MAX];
|
|
|
|
/* TODO should be done in the backend- no raw DB stuff up front */
|
|
|
|
snprintf(changelog, PATH_MAX, "%s/%s/%s-%s/changelog",
|
|
|
|
alpm_option_get_dbpath(),
|
|
|
|
alpm_db_get_name(db_local),
|
|
|
|
alpm_pkg_get_name(pkg),
|
|
|
|
alpm_pkg_get_version(pkg));
|
|
|
|
dump_pkg_changelog(changelog, alpm_pkg_get_name(pkg));
|
|
|
|
}
|
|
|
|
if(!config->op_q_info && !config->op_q_list && !config->op_q_changelog) {
|
|
|
|
printf("%s %s\n", alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg));
|
2005-12-13 21:23:14 -05:00
|
|
|
}
|
2007-06-12 23:55:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int pacman_query(alpm_list_t *targets)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
alpm_list_t *i;
|
2005-12-13 21:23:14 -05:00
|
|
|
|
2007-06-12 23:55:14 -04:00
|
|
|
/* First: operations that do not require targets */
|
|
|
|
|
|
|
|
/* search for a package */
|
|
|
|
if(config->op_q_search) {
|
|
|
|
ret = query_search(targets);
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check DB consistancy */
|
|
|
|
if(config->op_q_test) {
|
|
|
|
ret = query_test();
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for package upgrades */
|
2007-02-04 03:26:52 -05:00
|
|
|
if(config->op_q_upgrade) {
|
2007-06-12 23:55:14 -04:00
|
|
|
ret = query_upgrades();
|
|
|
|
return(ret);
|
2007-02-04 03:26:52 -05:00
|
|
|
}
|
|
|
|
|
2007-06-02 12:34:01 -04:00
|
|
|
/* looking for groups */
|
|
|
|
if(config->group) {
|
|
|
|
ret = query_group(targets);
|
|
|
|
return(ret);
|
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-06-12 23:55:14 -04:00
|
|
|
if(config->op_q_foreign) {
|
2007-07-14 14:45:30 -04:00
|
|
|
/* 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);
|
|
|
|
}
|
2007-06-02 12:34:01 -04:00
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-06-12 23:55:14 -04:00
|
|
|
/* operations on all packages in the local DB
|
|
|
|
* valid: no-op (plain -Q), list, info
|
|
|
|
* invalid: isfile, owns */
|
2007-07-14 14:45:30 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-06-02 12:34:01 -04:00
|
|
|
for(i = alpm_db_getpkgcache(db_local); i; i = alpm_list_next(i)) {
|
2007-06-12 23:55:14 -04:00
|
|
|
pmpkg_t *pkg = alpm_list_getdata(i);
|
2007-07-14 14:45:30 -04:00
|
|
|
if(filter(pkg)) {
|
|
|
|
display(pkg);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
2007-07-02 05:29:05 -04:00
|
|
|
return(0);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2007-06-12 23:55:14 -04:00
|
|
|
/* Second: operations that require target(s) */
|
|
|
|
|
|
|
|
/* determine the owner of a file */
|
|
|
|
if(config->op_q_owns) {
|
|
|
|
ret = query_fileowner(targets);
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* operations on named packages in the local DB
|
|
|
|
* valid: no-op (plain -Q), list, info */
|
|
|
|
for(i = targets; i; i = alpm_list_next(i)) {
|
|
|
|
char *strname = alpm_list_getdata(i);
|
2007-07-14 14:45:30 -04:00
|
|
|
pmpkg_t *pkg = NULL;
|
|
|
|
|
|
|
|
if(config->op_q_isfile) {
|
|
|
|
alpm_pkg_load(strname, &pkg);
|
|
|
|
} else {
|
|
|
|
pkg = alpm_db_get_pkg(db_local, strname);
|
|
|
|
}
|
|
|
|
|
2007-06-12 23:55:14 -04:00
|
|
|
if(pkg == NULL) {
|
|
|
|
fprintf(stderr, _("error: package \"%s\" not found\n"), strname);
|
|
|
|
ret++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-07-14 14:45:30 -04:00
|
|
|
if(filter(pkg)) {
|
|
|
|
display(pkg);
|
2007-06-12 23:55:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-31 01:12:17 -04:00
|
|
|
return(ret);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set ts=2 sw=2 noet: */
|