2 * dpkg-query - program for query the dpkg database
3 * querycmd.c - status enquiry and listing options
5 * Copyright © 1995,1996 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2000,2001 Wichert Akkerman <wakkerma@debian.org>
7 * Copyright © 2006-2015 Guillem Jover <guillem@debian.org>
8 * Copyright © 2011 Linaro Limited
9 * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
11 * This is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
28 #include <sys/types.h>
44 #include <dpkg/i18n.h>
45 #include <dpkg/dpkg.h>
46 #include <dpkg/dpkg-db.h>
47 #include <dpkg/debug.h>
48 #include <dpkg/pkg-array.h>
49 #include <dpkg/pkg-spec.h>
50 #include <dpkg/pkg-format.h>
51 #include <dpkg/pkg-show.h>
52 #include <dpkg/string.h>
53 #include <dpkg/path.h>
54 #include <dpkg/file.h>
55 #include <dpkg/pager.h>
56 #include <dpkg/options.h>
57 #include <dpkg/db-ctrl.h>
58 #include <dpkg/db-fsys.h>
62 static const char *opt_showformat
= "${binary:Package}\t${Version}\n";
64 static int opt_loadavail
= 0;
67 pkg_array_match_patterns(struct pkg_array
*array
,
68 pkg_array_visitor_func
*pkg_visitor
, void *pkg_data
,
69 const char *const *argv
)
71 int argc
, i
, ip
, *found
;
75 for (argc
= 0; argv
[argc
]; argc
++);
76 found
= m_calloc(argc
, sizeof(int));
78 ps
= m_malloc(sizeof(*ps
) * argc
);
79 for (ip
= 0; ip
< argc
; ip
++) {
80 pkg_spec_init(&ps
[ip
], PKG_SPEC_PATTERNS
| PKG_SPEC_ARCH_WILDCARD
);
81 pkg_spec_parse(&ps
[ip
], argv
[ip
]);
84 for (i
= 0; i
< array
->n_pkgs
; i
++) {
86 bool pkg_found
= false;
89 for (ip
= 0; ip
< argc
; ip
++) {
90 if (pkg_spec_match_pkg(&ps
[ip
], pkg
, &pkg
->installed
)) {
96 array
->pkgs
[i
] = NULL
;
99 pkg_array_foreach(array
, pkg_visitor
, pkg_data
);
101 for (ip
= 0; ip
< argc
; ip
++) {
103 notice(_("no packages found matching %s"), argv
[ip
]);
106 pkg_spec_destroy(&ps
[ip
]);
124 list_format_init(struct list_format
*fmt
, struct pkg_array
*array
)
136 for (i
= 0; i
< array
->n_pkgs
; i
++) {
137 int plen
, vlen
, alen
, dlen
;
139 if (array
->pkgs
[i
] == NULL
)
142 plen
= str_width(pkg_name(array
->pkgs
[i
], pnaw_nonambig
));
143 vlen
= str_width(versiondescribe(&array
->pkgs
[i
]->installed
.version
,
145 alen
= str_width(dpkg_arch_describe(array
->pkgs
[i
]->installed
.arch
));
146 pkg_synopsis(array
->pkgs
[i
], &dlen
);
160 list_format_print(struct list_format
*fmt
,
161 int c_want
, int c_status
, int c_eflag
,
162 const char *name
, const char *version
, const char *arch
,
163 const char *desc
, int desc_len
)
165 struct str_crop_info ns
, vs
, as
, ds
;
167 str_gen_crop(name
, fmt
->nw
, &ns
);
168 str_gen_crop(version
, fmt
->vw
, &vs
);
169 str_gen_crop(arch
, fmt
->aw
, &as
);
170 str_gen_crop(desc
, desc_len
, &ds
);
172 printf("%c%c%c %-*.*s %-*.*s %-*.*s %.*s\n", c_want
, c_status
, c_eflag
,
173 ns
.max_bytes
, ns
.str_bytes
, name
,
174 vs
.max_bytes
, vs
.str_bytes
, version
,
175 as
.max_bytes
, as
.str_bytes
, arch
,
180 list_format_print_header(struct list_format
*fmt
)
187 /* TRANSLATORS: This is the header that appears on 'dpkg-query -l'. The
188 * string should remain under 80 characters. The uppercase letters in
189 * the state values denote the abbreviated letter that will appear on
190 * the first three columns, which should ideally match the English one
191 * (e.g. Remove → supRimeix), see dpkg-query(1) for further details. The
192 * translated message can use additional lines if needed. */
194 Desired=Unknown/Install/Remove/Purge/Hold\n\
195 | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend\n\
196 |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)\n"), stdout
);
197 list_format_print(fmt
, '|', '|', '/', _("Name"), _("Version"),
198 _("Architecture"), _("Description"), fmt
->dw
);
204 for (l
= 0; l
< fmt
->nw
; l
++)
209 for (l
= 0; l
< fmt
->vw
; l
++)
214 for (l
= 0; l
< fmt
->aw
; l
++)
219 for (l
= 0; l
< fmt
->dw
; l
++)
227 pkg_array_list_item(struct pkg_array
*array
, struct pkginfo
*pkg
, void *pkg_data
)
229 struct list_format
*fmt
= pkg_data
;
233 list_format_init(fmt
, array
);
234 list_format_print_header(fmt
);
236 pdesc
= pkg_synopsis(pkg
, &l
);
239 list_format_print(fmt
,
240 pkg_abbrev_want(pkg
),
241 pkg_abbrev_status(pkg
),
242 pkg_abbrev_eflag(pkg
),
243 pkg_name(pkg
, pnaw_nonambig
),
244 versiondescribe(&pkg
->installed
.version
, vdew_nonambig
),
245 dpkg_arch_describe(pkg
->installed
.arch
),
250 listpackages(const char *const *argv
)
252 struct pkg_array array
;
254 struct list_format fmt
;
258 modstatdb_open(msdbrw_readonly
);
260 modstatdb_open(msdbrw_readonly
| msdbrw_available_readonly
);
262 pkg_array_init_from_hash(&array
);
263 pkg_array_sort(&array
, pkg_sorter_by_nonambig_name_arch
);
265 memset(&fmt
, 0, sizeof(fmt
));
267 pager
= pager_spawn(_("showing package list on pager"));
272 for (i
= 0; i
< array
.n_pkgs
; i
++) {
276 if (pkg
->status
== PKG_STAT_NOTINSTALLED
)
277 array
.pkgs
[i
] = NULL
;
280 pkg_array_foreach(&array
, pkg_array_list_item
, &fmt
);
282 rc
= pkg_array_match_patterns(&array
, pkg_array_list_item
, &fmt
, argv
);
285 m_output(stdout
, _("<standard output>"));
286 m_output(stderr
, _("<standard error>"));
290 pkg_array_destroy(&array
);
291 modstatdb_shutdown();
297 searchoutput(struct fsys_namenode
*namenode
)
299 struct fsys_node_pkgs_iter
*iter
;
300 struct pkginfo
*pkg_owner
;
303 if (namenode
->divert
) {
304 const char *name_from
= namenode
->divert
->camefrom
?
305 namenode
->divert
->camefrom
->name
: namenode
->name
;
306 const char *name_to
= namenode
->divert
->useinstead
?
307 namenode
->divert
->useinstead
->name
: namenode
->name
;
309 if (namenode
->divert
->pkgset
) {
310 printf(_("diversion by %s from: %s\n"),
311 namenode
->divert
->pkgset
->name
, name_from
);
312 printf(_("diversion by %s to: %s\n"),
313 namenode
->divert
->pkgset
->name
, name_to
);
315 printf(_("local diversion from: %s\n"), name_from
);
316 printf(_("local diversion to: %s\n"), name_to
);
321 iter
= fsys_node_pkgs_iter_new(namenode
);
322 while ((pkg_owner
= fsys_node_pkgs_iter_next(iter
))) {
325 fputs(pkg_name(pkg_owner
, pnaw_nonambig
), stdout
);
328 fsys_node_pkgs_iter_free(iter
);
330 if (found
) printf(": %s\n",namenode
->name
);
331 return found
+ (namenode
->divert
? 1 : 0);
335 searchfiles(const char *const *argv
)
339 struct varbuf path
= VARBUF_INIT
;
340 static struct varbuf vb
;
343 badusage(_("--search needs at least one file name pattern argument"));
345 modstatdb_open(msdbrw_readonly
);
346 ensure_allinstfiles_available_quiet();
349 while ((thisarg
= *argv
++) != NULL
) {
350 struct fsys_namenode
*namenode
;
353 if (!strchr("*[?/",*thisarg
)) {
355 varbuf_add_char(&vb
, '*');
356 varbuf_add_str(&vb
, thisarg
);
357 varbuf_add_char(&vb
, '*');
361 if (!strpbrk(thisarg
, "*[?\\")) {
362 /* Trim trailing ‘/’ and ‘/.’ from the argument if it is not
363 * a pattern, just a pathname. */
365 varbuf_add_str(&path
, thisarg
);
366 varbuf_end_str(&path
);
367 varbuf_trunc(&path
, path_trim_slash_slashdot(path
.buf
));
369 namenode
= fsys_hash_find_node(path
.buf
, 0);
370 found
+= searchoutput(namenode
);
372 struct fsys_hash_iter
*iter
;
374 iter
= fsys_hash_iter_new();
375 while ((namenode
= fsys_hash_iter_next(iter
)) != NULL
) {
376 if (fnmatch(thisarg
,namenode
->name
,0)) continue;
377 found
+= searchoutput(namenode
);
379 fsys_hash_iter_free(iter
);
382 notice(_("no path found matching pattern %s"), thisarg
);
384 m_output(stderr
, _("<standard error>"));
386 m_output(stdout
, _("<standard output>"));
389 modstatdb_shutdown();
391 varbuf_destroy(&path
);
397 print_status(const char *const *argv
)
401 modstatdb_open(msdbrw_readonly
);
404 writedb_stanzas(stdout
, _("<standard output>"), 0);
408 while ((thisarg
= *argv
++) != NULL
) {
411 pkg
= dpkg_options_parse_pkgname(cipaction
, thisarg
);
413 if (pkg
->status
== PKG_STAT_NOTINSTALLED
&&
414 pkg
->priority
== PKG_PRIO_UNKNOWN
&&
415 str_is_unset(pkg
->section
) &&
417 pkg
->want
== PKG_WANT_UNKNOWN
&&
418 !pkg_is_informative(pkg
, &pkg
->installed
)) {
419 notice(_("package '%s' is not installed and no information is available"),
420 pkg_name(pkg
, pnaw_nonambig
));
423 write_stanza(stdout
, _("<standard output>"), pkg
, &pkg
->installed
);
431 m_output(stdout
, _("<standard output>"));
433 fputs(_("Use dpkg --info (= dpkg-deb --info) to examine archive files.\n"),
435 m_output(stderr
, _("<standard error>"));
438 modstatdb_shutdown();
444 print_avail(const char *const *argv
)
448 modstatdb_open(msdbrw_readonly
| msdbrw_available_readonly
);
451 writedb_stanzas(stdout
, _("<standard output>"), wdb_dump_available
);
455 while ((thisarg
= *argv
++) != NULL
) {
458 pkg
= dpkg_options_parse_pkgname(cipaction
, thisarg
);
460 if (!pkg_is_informative(pkg
, &pkg
->available
)) {
461 notice(_("package '%s' is not available"),
462 pkgbin_name(pkg
, &pkg
->available
, pnaw_nonambig
));
465 write_stanza(stdout
, _("<standard output>"), pkg
, &pkg
->available
);
473 m_output(stdout
, _("<standard output>"));
475 m_output(stderr
, _("<standard error>"));
477 modstatdb_shutdown();
483 list_files(const char *const *argv
)
486 struct fsys_namenode_list
*file
;
488 struct fsys_namenode
*namenode
;
492 badusage(_("--%s needs at least one package name argument"), cipaction
->olong
);
494 modstatdb_open(msdbrw_readonly
);
496 while ((thisarg
= *argv
++) != NULL
) {
497 pkg
= dpkg_options_parse_pkgname(cipaction
, thisarg
);
499 switch (pkg
->status
) {
500 case PKG_STAT_NOTINSTALLED
:
501 notice(_("package '%s' is not installed"),
502 pkg_name(pkg
, pnaw_nonambig
));
506 ensure_packagefiles_available(pkg
);
510 printf(_("Package '%s' does not contain any files (!)\n"),
511 pkg_name(pkg
, pnaw_nonambig
));
514 namenode
= file
->namenode
;
515 puts(namenode
->name
);
516 if (namenode
->divert
&& !namenode
->divert
->camefrom
) {
517 if (!namenode
->divert
->pkgset
)
518 printf(_("locally diverted to: %s\n"),
519 namenode
->divert
->useinstead
->name
);
520 else if (pkg
->set
== namenode
->divert
->pkgset
)
521 printf(_("package diverts others to: %s\n"),
522 namenode
->divert
->useinstead
->name
);
524 printf(_("diverted by %s to: %s\n"),
525 namenode
->divert
->pkgset
->name
,
526 namenode
->divert
->useinstead
->name
);
538 m_output(stdout
, _("<standard output>"));
540 fputs(_("Use dpkg --contents (= dpkg-deb --contents) to list archive files contents.\n"),
542 m_output(stderr
, _("<standard error>"));
545 modstatdb_shutdown();
551 pkg_array_load_db_fsys(struct pkg_array
*array
, struct pkginfo
*pkg
, void *pkg_data
)
553 ensure_packagefiles_available(pkg
);
557 pkg_array_show_item(struct pkg_array
*array
, struct pkginfo
*pkg
, void *pkg_data
)
559 struct pkg_format_node
*fmt
= pkg_data
;
561 pkg_format_show(fmt
, pkg
, &pkg
->installed
);
565 showpackages(const char *const *argv
)
567 struct dpkg_error err
;
568 struct pkg_array array
;
569 struct pkg_format_node
*fmt
;
570 bool fmt_needs_db_fsys
;
573 fmt
= pkg_format_parse(opt_showformat
, &err
);
575 notice(_("error in show format: %s"), err
.str
);
576 dpkg_error_destroy(&err
);
581 fmt_needs_db_fsys
= pkg_format_needs_db_fsys(fmt
);
584 modstatdb_open(msdbrw_readonly
);
586 modstatdb_open(msdbrw_readonly
| msdbrw_available_readonly
);
588 pkg_array_init_from_hash(&array
);
589 pkg_array_sort(&array
, pkg_sorter_by_nonambig_name_arch
);
594 if (fmt_needs_db_fsys
)
595 ensure_allinstfiles_available_quiet();
596 for (i
= 0; i
< array
.n_pkgs
; i
++) {
600 if (pkg
->status
== PKG_STAT_NOTINSTALLED
)
602 pkg_format_show(fmt
, pkg
, &pkg
->installed
);
605 if (fmt_needs_db_fsys
)
606 pkg_array_foreach(&array
, pkg_array_load_db_fsys
, NULL
);
607 rc
= pkg_array_match_patterns(&array
, pkg_array_show_item
, fmt
, argv
);
610 m_output(stdout
, _("<standard output>"));
611 m_output(stderr
, _("<standard error>"));
613 pkg_array_destroy(&array
);
614 pkg_format_free(fmt
);
615 modstatdb_shutdown();
621 pkg_infodb_is_internal(const char *filetype
)
623 /* Do not expose internal database files. */
624 if (strcmp(filetype
, LISTFILE
) == 0 ||
625 strcmp(filetype
, CONFFILESFILE
) == 0)
628 if (strlen(filetype
) > MAXCONTROLFILENAME
)
635 pkg_infodb_check_filetype(const char *filetype
)
639 /* Validate control file name for sanity. */
640 for (c
= "/."; *c
; c
++)
641 if (strchr(filetype
, *c
))
642 badusage(_("control file contains %c"), *c
);
646 pkg_infodb_print_filename(const char *filename
, const char *filetype
)
648 if (pkg_infodb_is_internal(filetype
))
651 printf("%s\n", filename
);
655 pkg_infodb_print_filetype(const char *filename
, const char *filetype
)
657 if (pkg_infodb_is_internal(filetype
))
660 printf("%s\n", filetype
);
664 control_path_file(struct pkginfo
*pkg
, const char *control_file
)
666 const char *control_pathname
;
669 control_pathname
= pkg_infodb_get_file(pkg
, &pkg
->installed
, control_file
);
670 if (stat(control_pathname
, &st
) < 0)
672 if (!S_ISREG(st
.st_mode
))
675 pkg_infodb_print_filename(control_pathname
, control_file
);
679 control_path(const char *const *argv
)
683 const char *control_file
;
687 badusage(_("--%s needs at least one package name argument"),
690 control_file
= *argv
++;
691 if (control_file
&& *argv
)
692 badusage(_("--%s takes at most two arguments"), cipaction
->olong
);
695 pkg_infodb_check_filetype(control_file
);
697 modstatdb_open(msdbrw_readonly
);
699 pkg
= dpkg_options_parse_pkgname(cipaction
, pkgname
);
700 if (pkg
->status
== PKG_STAT_NOTINSTALLED
)
701 ohshit(_("package '%s' is not installed"),
702 pkg_name(pkg
, pnaw_nonambig
));
705 control_path_file(pkg
, control_file
);
707 pkg_infodb_foreach(pkg
, &pkg
->installed
, pkg_infodb_print_filename
);
709 modstatdb_shutdown();
715 control_list(const char *const *argv
)
721 if (!pkgname
|| *argv
)
722 badusage(_("--%s takes one package name argument"), cipaction
->olong
);
724 modstatdb_open(msdbrw_readonly
);
726 pkg
= dpkg_options_parse_pkgname(cipaction
, pkgname
);
727 if (pkg
->status
== PKG_STAT_NOTINSTALLED
)
728 ohshit(_("package '%s' is not installed"), pkg_name(pkg
, pnaw_nonambig
));
730 pkg_infodb_foreach(pkg
, &pkg
->installed
, pkg_infodb_print_filetype
);
732 modstatdb_shutdown();
738 control_show(const char *const *argv
)
742 const char *filename
;
743 const char *control_file
;
746 if (!pkgname
|| !*argv
)
747 badusage(_("--%s takes exactly two arguments"),
750 control_file
= *argv
++;
751 if (!control_file
|| *argv
)
752 badusage(_("--%s takes exactly two arguments"), cipaction
->olong
);
754 pkg_infodb_check_filetype(control_file
);
756 modstatdb_open(msdbrw_readonly
);
758 pkg
= dpkg_options_parse_pkgname(cipaction
, pkgname
);
759 if (pkg
->status
== PKG_STAT_NOTINSTALLED
)
760 ohshit(_("package '%s' is not installed"), pkg_name(pkg
, pnaw_nonambig
));
762 if (pkg_infodb_has_file(pkg
, &pkg
->installed
, control_file
))
763 filename
= pkg_infodb_get_file(pkg
, &pkg
->installed
, control_file
);
765 ohshit(_("control file '%s' does not exist"), control_file
);
767 modstatdb_shutdown();
775 set_no_pager(const struct cmdinfo
*ci
, const char *value
)
781 printversion(const char *const *argv
)
783 printf(_("Debian %s package management program query tool version %s.\n"),
784 DPKGQUERY
, PACKAGE_RELEASE
);
786 "This is free software; see the GNU General Public License version 2 or\n"
787 "later for copying conditions. There is NO warranty.\n"));
789 m_output(stdout
, _("<standard output>"));
795 usage(const char *const *argv
)
798 "Usage: %s [<option>...] <command>\n"
803 " -s, --status [<package>...] Display package status details.\n"
804 " -p, --print-avail [<package>...] Display available version details.\n"
805 " -L, --listfiles <package>... List files 'owned' by package(s).\n"
806 " -l, --list [<pattern>...] List packages concisely.\n"
807 " -W, --show [<pattern>...] Show information on package(s).\n"
808 " -S, --search <pattern>... Find package(s) owning file(s).\n"
809 " --control-list <package> Print the package control file list.\n"
810 " --control-show <package> <file>\n"
811 " Show the package control file.\n"
812 " -c, --control-path <package> [<file>]\n"
813 " Print path for package control file.\n"
817 " -?, --help Show this help message.\n"
818 " --version Show the version.\n"
823 " --admindir=<directory> Use <directory> instead of %s.\n"
824 " --root=<directory> Use <directory> instead of %s.\n"
825 " --load-avail Use available file on --show and --list.\n"
826 " --no-pager Disables the use of any pager.\n"
827 " -f|--showformat=<format> Use alternative format for --show.\n"
828 "\n"), ADMINDIR
, "/");
832 " A format is a string that will be output for each package. The format\n"
833 " can include the standard escape sequences \\n (newline), \\r (carriage\n"
834 " return) or \\\\ (plain backslash). Package information can be included\n"
835 " by inserting variable references to package fields using the ${var[;width]}\n"
836 " syntax. Fields will be right-aligned unless the width is negative in which\n"
837 " case left alignment will be used.\n"));
839 m_output(stdout
, _("<standard output>"));
844 static const char printforhelp
[] = N_(
845 "Use --help for help about querying packages.");
847 /* This table has both the action entries in it and the normal options.
848 * The action entries are made with the ACTION macro, as they all
849 * have a very similar structure. */
850 static const struct cmdinfo cmdinfos
[]= {
851 ACTION( "listfiles", 'L', act_listfiles
, list_files
),
852 ACTION( "status", 's', act_status
, print_status
),
853 ACTION( "print-avail", 'p', act_printavail
, print_avail
),
854 ACTION( "list", 'l', act_listpackages
, listpackages
),
855 ACTION( "search", 'S', act_searchfiles
, searchfiles
),
856 ACTION( "show", 'W', act_listpackages
, showpackages
),
857 ACTION( "control-path", 'c', act_controlpath
, control_path
),
858 ACTION( "control-list", 0, act_controllist
, control_list
),
859 ACTION( "control-show", 0, act_controlshow
, control_show
),
860 ACTION( "help", '?', act_help
, usage
),
861 ACTION( "version", 0, act_version
, printversion
),
863 { "admindir", 0, 1, NULL
, NULL
, set_admindir
, 0 },
864 { "root", 0, 1, NULL
, NULL
, set_root
, 0 },
865 { "load-avail", 0, 0, &opt_loadavail
, NULL
, NULL
, 1 },
866 { "showformat", 'f', 1, NULL
, &opt_showformat
, NULL
},
867 { "no-pager", 0, 0, NULL
, NULL
, set_no_pager
},
868 { NULL
, 0, 0, NULL
, NULL
, NULL
}
871 int main(int argc
, const char *const *argv
) {
874 dpkg_set_report_piped_mode(_IOFBF
);
875 dpkg_locales_init(PACKAGE
);
876 dpkg_program_init("dpkg-query");
877 dpkg_options_parse(&argv
, cmdinfos
, printforhelp
);
879 debug(dbg_general
, "root=%s admindir=%s", dpkg_fsys_get_dir(), dpkg_db_get_dir());
881 if (!cipaction
) badusage(_("need an action option"));
883 ret
= cipaction
->action(argv
);