enquiry.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * dpkg - main program for package management
  3. * enquiry.c - status enquiry and listing options
  4. *
  5. * Copyright © 1995,1996 Ian Jackson <ian@chiark.greenend.org.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * 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.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. /* FIXME: per-package audit */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <dpkg/i18n.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <fnmatch.h>
  29. #include <assert.h>
  30. #include <unistd.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <sys/ioctl.h>
  34. #include <sys/termios.h>
  35. #include <fcntl.h>
  36. #include <dpkg/dpkg.h>
  37. #include <dpkg/dpkg-db.h>
  38. #include <dpkg/myopt.h>
  39. #include "filesdb.h"
  40. #include "main.h"
  41. struct badstatinfo {
  42. int (*yesno)(struct pkginfo*, const struct badstatinfo *bsi);
  43. int val;
  44. const char *explanation;
  45. };
  46. static int bsyn_reinstreq(struct pkginfo *pkg, const struct badstatinfo *bsi) {
  47. return pkg->eflag &= eflag_reinstreq;
  48. }
  49. static int bsyn_status(struct pkginfo *pkg, const struct badstatinfo *bsi) {
  50. if (pkg->eflag &= eflag_reinstreq)
  51. return 0;
  52. return (int)pkg->status == bsi->val;
  53. }
  54. static const struct badstatinfo badstatinfos[]= {
  55. {
  56. bsyn_reinstreq, 0,
  57. N_("The following packages are in a mess due to serious problems during\n"
  58. "installation. They must be reinstalled for them (and any packages\n"
  59. "that depend on them) to function properly:\n")
  60. }, {
  61. bsyn_status, stat_unpacked,
  62. N_("The following packages have been unpacked but not yet configured.\n"
  63. "They must be configured using dpkg --configure or the configure\n"
  64. "menu option in dselect for them to work:\n")
  65. }, {
  66. bsyn_status, stat_halfconfigured,
  67. N_("The following packages are only half configured, probably due to problems\n"
  68. "configuring them the first time. The configuration should be retried using\n"
  69. "dpkg --configure <package> or the configure menu option in dselect:\n")
  70. }, {
  71. bsyn_status, stat_halfinstalled,
  72. N_("The following packages are only half installed, due to problems during\n"
  73. "installation. The installation can probably be completed by retrying it;\n"
  74. "the packages can be removed using dselect or dpkg --remove:\n")
  75. }, {
  76. bsyn_status, stat_triggersawaited,
  77. N_("The following packages are awaiting processing of triggers that they\n"
  78. "have activated in other packages. This processing can be requested using\n"
  79. "dselect or dpkg --configure --pending (or dpkg --triggers-only):\n")
  80. }, {
  81. bsyn_status, stat_triggerspending,
  82. N_("The following packages have been triggered, but the trigger processing\n"
  83. "has not yet been done. Trigger processing can be requested using\n"
  84. "dselect or dpkg --configure --pending (or dpkg --triggers-only):\n")
  85. }, {
  86. NULL
  87. }
  88. };
  89. static void describebriefly(struct pkginfo *pkg) {
  90. int maxl, l;
  91. const char *pdesc;
  92. maxl= 57;
  93. l= strlen(pkg->name);
  94. if (l>20) maxl -= (l-20);
  95. limiteddescription(pkg,maxl,&pdesc,&l);
  96. printf(" %-20s %.*s\n",pkg->name,l,pdesc);
  97. }
  98. void audit(const char *const *argv) {
  99. struct pkgiterator *it;
  100. struct pkginfo *pkg;
  101. const struct badstatinfo *bsi;
  102. int head;
  103. if (*argv)
  104. badusage(_("--%s takes no arguments"), cipaction->olong);
  105. modstatdb_init(admindir,msdbrw_readonly);
  106. for (bsi= badstatinfos; bsi->yesno; bsi++) {
  107. head= 0;
  108. it= iterpkgstart();
  109. while ((pkg= iterpkgnext(it))) {
  110. if (!bsi->yesno(pkg,bsi)) continue;
  111. if (!head) {
  112. fputs(gettext(bsi->explanation),stdout);
  113. head= 1;
  114. }
  115. describebriefly(pkg);
  116. }
  117. iterpkgend(it);
  118. if (head) putchar('\n');
  119. }
  120. m_output(stdout, _("<standard output>"));
  121. }
  122. struct sectionentry {
  123. struct sectionentry *next;
  124. const char *name;
  125. int count;
  126. };
  127. static int yettobeunpacked(struct pkginfo *pkg, const char **thissect) {
  128. if (pkg->want != want_install) return 0;
  129. switch (pkg->status) {
  130. case stat_unpacked: case stat_installed: case stat_halfconfigured:
  131. case stat_triggerspending:
  132. case stat_triggersawaited:
  133. return 0;
  134. case stat_notinstalled: case stat_halfinstalled: case stat_configfiles:
  135. if (thissect)
  136. *thissect= pkg->section && *pkg->section ? pkg->section : _("<unknown>");
  137. return 1;
  138. default:
  139. internerr("unknown package status '%d'", pkg->status);
  140. }
  141. return 0;
  142. }
  143. void unpackchk(const char *const *argv) {
  144. int totalcount, sects;
  145. struct sectionentry *sectionentries, *se, **sep;
  146. struct pkgiterator *it;
  147. struct pkginfo *pkg;
  148. const char *thissect;
  149. char buf[20];
  150. int width;
  151. if (*argv)
  152. badusage(_("--%s takes no arguments"), cipaction->olong);
  153. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  154. totalcount= 0;
  155. sectionentries = NULL;
  156. sects= 0;
  157. it= iterpkgstart();
  158. while ((pkg= iterpkgnext(it))) {
  159. if (!yettobeunpacked(pkg, &thissect)) continue;
  160. for (se= sectionentries; se && strcasecmp(thissect,se->name); se= se->next);
  161. if (!se) {
  162. se= nfmalloc(sizeof(struct sectionentry));
  163. for (sep= &sectionentries;
  164. *sep && strcasecmp(thissect,(*sep)->name) > 0;
  165. sep= &(*sep)->next);
  166. se->name= thissect;
  167. se->count= 0;
  168. se->next= *sep;
  169. *sep= se;
  170. sects++;
  171. }
  172. se->count++; totalcount++;
  173. }
  174. iterpkgend(it);
  175. if (totalcount == 0) exit(0);
  176. if (totalcount <= 12) {
  177. it= iterpkgstart();
  178. while ((pkg= iterpkgnext(it))) {
  179. if (!yettobeunpacked(pkg, NULL))
  180. continue;
  181. describebriefly(pkg);
  182. }
  183. iterpkgend(it);
  184. } else if (sects <= 12) {
  185. for (se= sectionentries; se; se= se->next) {
  186. sprintf(buf,"%d",se->count);
  187. printf(_(" %d in %s: "),se->count,se->name);
  188. width= 70-strlen(se->name)-strlen(buf);
  189. while (width > 59) { putchar(' '); width--; }
  190. it= iterpkgstart();
  191. while ((pkg= iterpkgnext(it))) {
  192. if (!yettobeunpacked(pkg,&thissect)) continue;
  193. if (strcasecmp(thissect,se->name)) continue;
  194. width -= strlen(pkg->name); width--;
  195. if (width < 4) { printf(" ..."); break; }
  196. printf(" %s",pkg->name);
  197. }
  198. iterpkgend(it);
  199. putchar('\n');
  200. }
  201. } else {
  202. printf(_(" %d packages, from the following sections:"),totalcount);
  203. width= 0;
  204. for (se= sectionentries; se; se= se->next) {
  205. sprintf(buf,"%d",se->count);
  206. width -= (6 + strlen(se->name) + strlen(buf));
  207. if (width < 0) { putchar('\n'); width= 73 - strlen(se->name) - strlen(buf); }
  208. printf(" %s (%d)",se->name,se->count);
  209. }
  210. putchar('\n');
  211. }
  212. m_output(stdout, _("<standard output>"));
  213. }
  214. static void
  215. assert_version_support(const char *const *argv,
  216. struct versionrevision *version,
  217. const char *feature_name)
  218. {
  219. struct pkginfo *pkg;
  220. if (*argv)
  221. badusage(_("--%s takes no arguments"), cipaction->olong);
  222. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  223. pkg= findpackage("dpkg");
  224. switch (pkg->status) {
  225. case stat_installed:
  226. case stat_triggerspending:
  227. break;
  228. case stat_unpacked: case stat_halfconfigured: case stat_halfinstalled:
  229. case stat_triggersawaited:
  230. if (versionsatisfied3(&pkg->configversion, version, dvr_laterequal))
  231. break;
  232. printf(_("Version of dpkg with working %s support not yet configured.\n"
  233. " Please use 'dpkg --configure dpkg', and then try again.\n"),
  234. feature_name);
  235. exit(1);
  236. default:
  237. printf(_("dpkg not recorded as installed, cannot check for %s support!\n"),
  238. feature_name);
  239. exit(1);
  240. }
  241. }
  242. void assertpredep(const char *const *argv) {
  243. struct versionrevision version = { 0, "1.1.0", NULL };
  244. assert_version_support(argv, &version, _("Pre-Depends field"));
  245. }
  246. void assertepoch(const char *const *argv) {
  247. struct versionrevision version = { 0, "1.4.0.7", NULL };
  248. assert_version_support(argv, &version, _("epoch"));
  249. }
  250. void assertlongfilenames(const char *const *argv) {
  251. struct versionrevision version = { 0, "1.4.1.17", NULL };
  252. assert_version_support(argv, &version, _("long filenames"));
  253. }
  254. void assertmulticonrep(const char *const *argv) {
  255. struct versionrevision version = { 0, "1.4.1.19", NULL };
  256. assert_version_support(argv, &version, _("multiple Conflicts and Replaces"));
  257. }
  258. void predeppackage(const char *const *argv) {
  259. /* Print a single package which:
  260. * (a) is the target of one or more relevant predependencies.
  261. * (b) has itself no unsatisfied pre-dependencies.
  262. * If such a package is present output is the Packages file entry,
  263. * which can be massaged as appropriate.
  264. * Exit status:
  265. * 0 = a package printed, OK
  266. * 1 = no suitable package available
  267. * 2 = error
  268. */
  269. static struct varbuf vb;
  270. struct pkgiterator *it;
  271. struct pkginfo *pkg = NULL, *startpkg, *trypkg;
  272. struct dependency *dep;
  273. struct deppossi *possi, *provider;
  274. if (*argv)
  275. badusage(_("--%s takes no arguments"), cipaction->olong);
  276. modstatdb_init(admindir,msdbrw_readonly);
  277. clear_istobes(); /* We use clientdata->istobe to detect loops */
  278. for (it = iterpkgstart(), dep = NULL;
  279. !dep && (pkg=iterpkgnext(it));
  280. ) {
  281. if (pkg->want != want_install) continue; /* Ignore packages user doesn't want */
  282. if (!pkg->files) continue; /* Ignore packages not available */
  283. pkg->clientdata->istobe= itb_preinstall;
  284. for (dep= pkg->available.depends; dep; dep= dep->next) {
  285. if (dep->type != dep_predepends) continue;
  286. if (depisok(dep, &vb, NULL, 1))
  287. continue;
  288. break; /* This will leave dep non-NULL, and so exit the loop. */
  289. }
  290. pkg->clientdata->istobe= itb_normal;
  291. /* If dep is NULL we go and get the next package. */
  292. }
  293. if (!dep) exit(1); /* Not found */
  294. assert(pkg);
  295. startpkg= pkg;
  296. pkg->clientdata->istobe= itb_preinstall;
  297. /* OK, we have found an unsatisfied predependency.
  298. * Now go and find the first thing we need to install, as a first step
  299. * towards satisfying it.
  300. */
  301. do {
  302. /* We search for a package which would satisfy dep, and put it in pkg */
  303. for (possi = dep->list, pkg = NULL;
  304. !pkg && possi;
  305. possi=possi->next) {
  306. trypkg= possi->ed;
  307. if (!trypkg->available.valid) continue;
  308. if (trypkg->files && versionsatisfied(&trypkg->available,possi)) {
  309. if (trypkg->clientdata->istobe == itb_normal) { pkg= trypkg; break; }
  310. }
  311. if (possi->verrel != dvr_none) continue;
  312. for (provider=possi->ed->available.depended;
  313. !pkg && provider;
  314. provider=provider->next) {
  315. if (provider->up->type != dep_provides) continue;
  316. trypkg= provider->up->up;
  317. if (!trypkg->available.valid || !trypkg->files) continue;
  318. if (trypkg->clientdata->istobe == itb_normal) { pkg= trypkg; break; }
  319. }
  320. }
  321. if (!pkg) {
  322. varbufreset(&vb);
  323. describedepcon(&vb,dep);
  324. varbufaddc(&vb,0);
  325. fprintf(stderr, _("dpkg: cannot see how to satisfy pre-dependency:\n %s\n"),vb.buf);
  326. ohshit(_("cannot satisfy pre-dependencies for %.250s (wanted due to %.250s)"),
  327. dep->up->name,startpkg->name);
  328. }
  329. pkg->clientdata->istobe= itb_preinstall;
  330. for (dep= pkg->available.depends; dep; dep= dep->next) {
  331. if (dep->type != dep_predepends) continue;
  332. if (depisok(dep, &vb, NULL, 1))
  333. continue;
  334. break; /* This will leave dep non-NULL, and so exit the loop. */
  335. }
  336. } while (dep);
  337. /* OK, we've found it - pkg has no unsatisfied pre-dependencies ! */
  338. writerecord(stdout, _("<standard output>"), pkg, &pkg->available);
  339. m_output(stdout, _("<standard output>"));
  340. }
  341. void printarch(const char *const *argv) {
  342. if (*argv)
  343. badusage(_("--%s takes no arguments"), cipaction->olong);
  344. printf("%s\n", architecture);
  345. m_output(stdout, _("<standard output>"));
  346. }
  347. void
  348. printinstarch(const char *const *argv)
  349. {
  350. warning(_("obsolete option '--%s', please use '--%s' instead."),
  351. "print-installation-architecture", "print-architecture");
  352. printarch(argv);
  353. }
  354. void cmpversions(const char *const *argv) {
  355. struct relationinfo {
  356. const char *string;
  357. /* These values are exit status codes, so 0=true, 1=false */
  358. int if_lesser, if_equal, if_greater;
  359. int if_none_a, if_none_both, if_none_b;
  360. };
  361. static const struct relationinfo relationinfos[]= {
  362. /* < = > !a!2!b */
  363. { "le", 0,0,1, 0,0,1 },
  364. { "lt", 0,1,1, 0,1,1 },
  365. { "eq", 1,0,1, 1,0,1 },
  366. { "ne", 0,1,0, 0,1,0 },
  367. { "ge", 1,0,0, 1,0,0 },
  368. { "gt", 1,1,0, 1,1,0 },
  369. { "le-nl", 0,0,1, 1,0,0 }, /* Here none */
  370. { "lt-nl", 0,1,1, 1,1,0 }, /* is counted */
  371. { "ge-nl", 1,0,0, 0,0,1 }, /* than any version.*/
  372. { "gt-nl", 1,1,0, 0,1,1 }, /* */
  373. { "<", 0,0,1, 0,0,1 }, /* For compatibility*/
  374. { "<=", 0,0,1, 0,0,1 }, /* with dpkg */
  375. { "<<", 0,1,1, 0,1,1 }, /* control file */
  376. { "=", 1,0,1, 1,0,1 }, /* syntax */
  377. { ">", 1,0,0, 1,0,0 }, /* */
  378. { ">=", 1,0,0, 1,0,0 },
  379. { ">>", 1,1,0, 1,1,0 },
  380. { NULL }
  381. };
  382. const struct relationinfo *rip;
  383. const char *emsg;
  384. struct versionrevision a, b;
  385. int r;
  386. if (!argv[0] || !argv[1] || !argv[2] || argv[3])
  387. badusage(_("--compare-versions takes three arguments:"
  388. " <version> <relation> <version>"));
  389. for (rip=relationinfos; rip->string && strcmp(rip->string,argv[1]); rip++);
  390. if (!rip->string) badusage(_("--compare-versions bad relation"));
  391. if (*argv[0] && strcmp(argv[0],"<unknown>")) {
  392. emsg= parseversion(&a,argv[0]);
  393. if (emsg)
  394. ohshit(_("version '%s' has bad syntax: %s"), argv[0], emsg);
  395. } else {
  396. blankversion(&a);
  397. }
  398. if (*argv[2] && strcmp(argv[2],"<unknown>")) {
  399. emsg= parseversion(&b,argv[2]);
  400. if (emsg)
  401. ohshit(_("version '%s' has bad syntax: %s"), argv[2], emsg);
  402. } else {
  403. blankversion(&b);
  404. }
  405. if (!informativeversion(&a)) {
  406. exit(informativeversion(&b) ? rip->if_none_a : rip->if_none_both);
  407. } else if (!informativeversion(&b)) {
  408. exit(rip->if_none_b);
  409. }
  410. r= versioncompare(&a,&b);
  411. debug(dbg_general,"cmpversions a=`%s' b=`%s' r=%d",
  412. versiondescribe(&a,vdew_always),
  413. versiondescribe(&b,vdew_always),
  414. r);
  415. if (r>0) exit(rip->if_greater);
  416. else if (r<0) exit(rip->if_lesser);
  417. else exit(rip->if_equal);
  418. }