4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include <alpm_list.h>
39 static char *resolve_path(const char *file
)
43 str
= calloc(PATH_MAX
+ 1, sizeof(char));
48 if(!realpath(file
, str
)) {
56 /* check if filename exists in PATH */
57 static int search_path(char **filename
, struct stat
*bufptr
)
59 char *envpath
, *envpathsplit
, *path
, *fullname
;
62 if((envpath
= getenv("PATH")) == NULL
) {
65 if((envpath
= envpathsplit
= strdup(envpath
)) == NULL
) {
69 flen
= strlen(*filename
);
71 while((path
= strsep(&envpathsplit
, ":")) != NULL
) {
72 size_t plen
= strlen(path
);
74 /* strip the trailing slash if one exists */
75 while(path
[plen
- 1] == '/') {
79 fullname
= malloc(plen
+ flen
+ 2);
80 sprintf(fullname
, "%s/%s", path
, *filename
);
82 if(lstat(fullname
, bufptr
) == 0) {
94 static void print_query_fileowner(const char *filename
, pmpkg_t
*info
)
97 printf(_("%s is owned by %s %s\n"), filename
,
98 alpm_pkg_get_name(info
), alpm_pkg_get_version(info
));
100 printf("%s\n", alpm_pkg_get_name(info
));
104 static int query_fileowner(alpm_list_t
*targets
)
114 /* This code is here for safety only */
115 if(targets
== NULL
) {
116 pm_fprintf(stderr
, PM_LOG_ERROR
, _("no file was specified for --owns\n"));
120 /* Set up our root path buffer. We only need to copy the location of root in
121 * once, then we can just overwrite whatever file was there on the previous
123 root
= alpm_option_get_root();
124 strncpy(path
, root
, PATH_MAX
- 1);
125 append
= path
+ strlen(path
);
126 max_length
= PATH_MAX
- (append
- path
) - 1;
128 db_local
= alpm_option_get_localdb();
130 for(t
= targets
; t
; t
= alpm_list_next(t
)) {
131 char *filename
, *dname
, *rpath
;
137 filename
= strdup(alpm_list_getdata(t
));
139 if(lstat(filename
, &buf
) == -1) {
140 /* if it is not a path but a program name, then check in PATH */
141 if(strchr(filename
, '/') == NULL
) {
142 if(search_path(&filename
, &buf
) == -1) {
143 pm_fprintf(stderr
, PM_LOG_ERROR
, _("failed to find '%s' in PATH: %s\n"),
144 filename
, strerror(errno
));
150 pm_fprintf(stderr
, PM_LOG_ERROR
, _("failed to read file '%s': %s\n"),
151 filename
, strerror(errno
));
158 if(S_ISDIR(buf
.st_mode
)) {
159 pm_fprintf(stderr
, PM_LOG_ERROR
,
160 _("cannot determine ownership of directory '%s'\n"), filename
);
166 bname
= mbasename(filename
);
167 dname
= mdirname(filename
);
168 /* for files in '/', there is no directory name to match */
169 if(strcmp(dname
, "") == 0) {
172 rpath
= resolve_path(dname
);
175 pm_fprintf(stderr
, PM_LOG_ERROR
, _("cannot determine real path for '%s': %s\n"),
176 filename
, strerror(errno
));
186 for(i
= alpm_db_get_pkgcache(db_local
); i
&& !found
; i
= alpm_list_next(i
)) {
188 pmpkg_t
*info
= alpm_list_getdata(i
);
190 for(j
= alpm_pkg_get_files(info
); j
&& !found
; j
= alpm_list_next(j
)) {
191 char *ppath
, *pdname
;
192 const char *pkgfile
= alpm_list_getdata(j
);
194 /* avoid the costly resolve_path usage if the basenames don't match */
195 if(strcmp(mbasename(pkgfile
), bname
) != 0) {
199 /* for files in '/', there is no directory name to match */
201 print_query_fileowner(filename
, info
);
206 if(strlen(pkgfile
) > max_length
) {
207 pm_fprintf(stderr
, PM_LOG_ERROR
, _("path too long: %s%s\n"), root
, pkgfile
);
209 /* concatenate our file and the root path */
210 strcpy(append
, pkgfile
);
212 pdname
= mdirname(path
);
213 ppath
= resolve_path(pdname
);
216 if(ppath
&& strcmp(ppath
, rpath
) == 0) {
217 print_query_fileowner(filename
, info
);
224 pm_fprintf(stderr
, PM_LOG_ERROR
, _("No package owns %s\n"), filename
);
234 /* search the local database for a matching package */
235 static int query_search(alpm_list_t
*targets
)
237 alpm_list_t
*i
, *searchlist
;
239 pmdb_t
*db_local
= alpm_option_get_localdb();
241 /* if we have a targets list, search for packages matching it */
243 searchlist
= alpm_db_search(db_local
, targets
);
246 searchlist
= alpm_db_get_pkgcache(db_local
);
249 if(searchlist
== NULL
) {
253 for(i
= searchlist
; i
; i
= alpm_list_next(i
)) {
255 pmpkg_t
*pkg
= alpm_list_getdata(i
);
258 printf("local/%s %s", alpm_pkg_get_name(pkg
), alpm_pkg_get_version(pkg
));
260 printf("%s", alpm_pkg_get_name(pkg
));
265 if((grp
= alpm_pkg_get_groups(pkg
)) != NULL
) {
268 for(k
= grp
; k
; k
= alpm_list_next(k
)) {
269 const char *group
= alpm_list_getdata(k
);
271 if(alpm_list_next(k
)) {
272 /* only print a spacer if there are more groups */
279 /* we need a newline and initial indent first */
281 indentprint(alpm_pkg_get_desc(pkg
), 4);
286 /* we only want to free if the list was a search list */
288 alpm_list_free(searchlist
);
293 static int query_group(alpm_list_t
*targets
)
296 char *grpname
= NULL
;
298 pmdb_t
*db_local
= alpm_option_get_localdb();
300 if(targets
== NULL
) {
301 for(j
= alpm_db_get_grpcache(db_local
); j
; j
= alpm_list_next(j
)) {
302 pmgrp_t
*grp
= alpm_list_getdata(j
);
303 const alpm_list_t
*p
, *packages
;
306 grpname
= alpm_grp_get_name(grp
);
307 packages
= alpm_grp_get_pkgs(grp
);
309 for(p
= packages
; p
; p
= alpm_list_next(p
)) {
310 printf("%s %s\n", grpname
, alpm_pkg_get_name(alpm_list_getdata(p
)));
314 for(i
= targets
; i
; i
= alpm_list_next(i
)) {
316 grpname
= alpm_list_getdata(i
);
317 grp
= alpm_db_readgrp(db_local
, grpname
);
319 const alpm_list_t
*p
, *packages
= alpm_grp_get_pkgs(grp
);
320 for(p
= packages
; p
; p
= alpm_list_next(p
)) {
322 printf("%s %s\n", grpname
,
323 alpm_pkg_get_name(alpm_list_getdata(p
)));
325 printf("%s\n", alpm_pkg_get_name(alpm_list_getdata(p
)));
329 pm_fprintf(stderr
, PM_LOG_ERROR
, _("group \"%s\" was not found\n"), grpname
);
337 static int is_foreign(pmpkg_t
*pkg
)
339 const char *pkgname
= alpm_pkg_get_name(pkg
);
341 alpm_list_t
*sync_dbs
= alpm_option_get_syncdbs();
344 for(j
= sync_dbs
; j
; j
= alpm_list_next(j
)) {
345 pmdb_t
*db
= alpm_list_getdata(j
);
346 pmpkg_t
*findpkg
= alpm_db_get_pkg(db
, pkgname
);
358 static int is_unrequired(pmpkg_t
*pkg
)
360 alpm_list_t
*requiredby
= alpm_pkg_compute_requiredby(pkg
);
361 if(requiredby
== NULL
) {
364 FREELIST(requiredby
);
368 static int filter(pmpkg_t
*pkg
)
370 /* check if this package was explicitly installed */
371 if(config
->op_q_explicit
&&
372 alpm_pkg_get_reason(pkg
) != PM_PKG_REASON_EXPLICIT
) {
375 /* check if this package was installed as a dependency */
376 if(config
->op_q_deps
&&
377 alpm_pkg_get_reason(pkg
) != PM_PKG_REASON_DEPEND
) {
380 /* check if this pkg isn't in a sync DB */
381 if(config
->op_q_foreign
&& !is_foreign(pkg
)) {
384 /* check if this pkg is unrequired */
385 if(config
->op_q_unrequired
&& !is_unrequired(pkg
)) {
388 /* check if this pkg is outdated */
389 if(config
->op_q_upgrade
&& (alpm_sync_newversion(pkg
, alpm_option_get_syncdbs()) == NULL
)) {
395 /* Loop through the packages. For each package,
396 * loop through files to check if they exist. */
397 static int check(pmpkg_t
*pkg
)
401 int allfiles
= 0, errors
= 0;
405 root
= alpm_option_get_root();
406 rootlen
= strlen(root
);
407 if(rootlen
+ 1 > PATH_MAX
) {
408 /* we are in trouble here */
409 pm_fprintf(stderr
, PM_LOG_ERROR
, _("path too long: %s%s\n"), root
, "");
414 const char *pkgname
= alpm_pkg_get_name(pkg
);
415 for(i
= alpm_pkg_get_files(pkg
); i
; i
= alpm_list_next(i
)) {
417 const char *path
= alpm_list_getdata(i
);
419 if(rootlen
+ 1 + strlen(path
) > PATH_MAX
) {
420 pm_fprintf(stderr
, PM_LOG_WARNING
, _("path too long: %s%s\n"), root
, path
);
423 strcpy(f
+ rootlen
, path
);
425 /* use lstat to prevent errors from symlinks */
426 if(lstat(f
, &st
) != 0) {
428 printf("%s %s\n", pkgname
, f
);
430 pm_printf(PM_LOG_WARNING
, "%s: %s (%s)\n",
431 pkgname
, f
, strerror(errno
));
438 printf(_n("%s: %d total file, ", "%s: %d total files, ",
439 (unsigned long)allfiles
), pkgname
, allfiles
);
440 printf(_n("%d missing file\n", "%d missing files\n",
441 (unsigned long)errors
), errors
);
444 return (errors
!= 0 ? 1 : 0);
447 static int display(pmpkg_t
*pkg
)
451 if(config
->op_q_info
) {
452 if(config
->op_q_isfile
) {
453 dump_pkg_full(pkg
, PKG_FROM_FILE
, 0);
455 dump_pkg_full(pkg
, PKG_FROM_LOCALDB
, config
->op_q_info
> 1);
458 if(config
->op_q_list
) {
459 dump_pkg_files(pkg
, config
->quiet
);
461 if(config
->op_q_changelog
) {
462 dump_pkg_changelog(pkg
);
464 if(config
->op_q_check
) {
467 if(!config
->op_q_info
&& !config
->op_q_list
468 && !config
->op_q_changelog
&& !config
->op_q_check
) {
470 printf("%s %s\n", alpm_pkg_get_name(pkg
), alpm_pkg_get_version(pkg
));
472 printf("%s\n", alpm_pkg_get_name(pkg
));
478 int pacman_query(alpm_list_t
*targets
)
486 /* First: operations that do not require targets */
488 /* search for a package */
489 if(config
->op_q_search
) {
490 ret
= query_search(targets
);
494 /* looking for groups */
496 ret
= query_group(targets
);
500 if(config
->op_q_foreign
) {
501 /* ensure we have at least one valid sync db set up */
502 alpm_list_t
*sync_dbs
= alpm_option_get_syncdbs();
503 if(sync_dbs
== NULL
|| alpm_list_count(sync_dbs
) == 0) {
504 pm_printf(PM_LOG_ERROR
, _("no usable package repositories configured.\n"));
509 db_local
= alpm_option_get_localdb();
511 /* operations on all packages in the local DB
512 * valid: no-op (plain -Q), list, info, check
513 * invalid: isfile, owns */
514 if(targets
== NULL
) {
515 if(config
->op_q_isfile
|| config
->op_q_owns
) {
516 pm_printf(PM_LOG_ERROR
, _("no targets specified (use -h for help)\n"));
520 for(i
= alpm_db_get_pkgcache(db_local
); i
; i
= alpm_list_next(i
)) {
521 pkg
= alpm_list_getdata(i
);
523 int value
= display(pkg
);
536 /* Second: operations that require target(s) */
538 /* determine the owner of a file */
539 if(config
->op_q_owns
) {
540 ret
= query_fileowner(targets
);
544 /* operations on named packages in the local DB
545 * valid: no-op (plain -Q), list, info, check */
546 for(i
= targets
; i
; i
= alpm_list_next(i
)) {
547 char *strname
= alpm_list_getdata(i
);
549 if(config
->op_q_isfile
) {
550 alpm_pkg_load(strname
, 1, PM_PGP_VERIFY_OPTIONAL
, &pkg
);
552 pkg
= alpm_db_get_pkg(db_local
, strname
);
556 pm_fprintf(stderr
, PM_LOG_ERROR
, _("package \"%s\" not found\n"), strname
);
562 int value
= display(pkg
);
569 if(config
->op_q_isfile
) {
582 /* vim: set ts=2 sw=2 noet: */