enquiry.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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.h>
  37. #include <dpkg-db.h>
  38. #include <dpkg-priv.h>
  39. #include <myopt.h>
  40. #include "filesdb.h"
  41. #include "main.h"
  42. struct badstatinfo {
  43. int (*yesno)(struct pkginfo*, const struct badstatinfo *bsi);
  44. int val;
  45. const char *explanation;
  46. };
  47. static int bsyn_reinstreq(struct pkginfo *pkg, const struct badstatinfo *bsi) {
  48. return pkg->eflag &= eflagf_reinstreq;
  49. }
  50. static int bsyn_status(struct pkginfo *pkg, const struct badstatinfo *bsi) {
  51. if (pkg->eflag &= eflagf_reinstreq) 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. if (ferror(stderr)) werr("stderr");
  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. fflush(stdout);
  213. if (ferror(stdout)) werr("stdout");
  214. }
  215. static void
  216. assert_version_support(const char *const *argv,
  217. struct versionrevision *version,
  218. const char *feature_name)
  219. {
  220. struct pkginfo *pkg;
  221. if (*argv)
  222. badusage(_("--%s takes no arguments"), cipaction->olong);
  223. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  224. pkg= findpackage("dpkg");
  225. switch (pkg->status) {
  226. case stat_installed:
  227. case stat_triggerspending:
  228. break;
  229. case stat_unpacked: case stat_halfconfigured: case stat_halfinstalled:
  230. case stat_triggersawaited:
  231. if (versionsatisfied3(&pkg->configversion, version, dvr_laterequal))
  232. break;
  233. printf(_("Version of dpkg with working %s support not yet configured.\n"
  234. " Please use 'dpkg --configure dpkg', and then try again.\n"),
  235. feature_name);
  236. exit(1);
  237. default:
  238. printf(_("dpkg not recorded as installed, cannot check for %s support!\n"),
  239. feature_name);
  240. exit(1);
  241. }
  242. }
  243. void assertpredep(const char *const *argv) {
  244. struct versionrevision version = { 0, "1.1.0", NULL };
  245. assert_version_support(argv, &version, _("Pre-Depends field"));
  246. }
  247. void assertepoch(const char *const *argv) {
  248. struct versionrevision version = { 0, "1.4.0.7", NULL };
  249. assert_version_support(argv, &version, _("epoch"));
  250. }
  251. void assertlongfilenames(const char *const *argv) {
  252. struct versionrevision version = { 0, "1.4.1.17", NULL };
  253. assert_version_support(argv, &version, _("long filenames"));
  254. }
  255. void assertmulticonrep(const char *const *argv) {
  256. struct versionrevision version = { 0, "1.4.1.19", NULL };
  257. assert_version_support(argv, &version, _("multiple Conflicts and Replaces"));
  258. }
  259. void predeppackage(const char *const *argv) {
  260. /* Print a single package which:
  261. * (a) is the target of one or more relevant predependencies.
  262. * (b) has itself no unsatisfied pre-dependencies.
  263. * If such a package is present output is the Packages file entry,
  264. * which can be massaged as appropriate.
  265. * Exit status:
  266. * 0 = a package printed, OK
  267. * 1 = no suitable package available
  268. * 2 = error
  269. */
  270. static struct varbuf vb;
  271. struct pkgiterator *it;
  272. struct pkginfo *pkg = NULL, *startpkg, *trypkg;
  273. struct dependency *dep;
  274. struct deppossi *possi, *provider;
  275. if (*argv)
  276. badusage(_("--%s takes no arguments"), cipaction->olong);
  277. modstatdb_init(admindir,msdbrw_readonly);
  278. clear_istobes(); /* We use clientdata->istobe to detect loops */
  279. for (it = iterpkgstart(), dep = NULL;
  280. !dep && (pkg=iterpkgnext(it));
  281. ) {
  282. if (pkg->want != want_install) continue; /* Ignore packages user doesn't want */
  283. if (!pkg->files) continue; /* Ignore packages not available */
  284. pkg->clientdata->istobe= itb_preinstall;
  285. for (dep= pkg->available.depends; dep; dep= dep->next) {
  286. if (dep->type != dep_predepends) continue;
  287. if (depisok(dep, &vb, NULL, 1))
  288. continue;
  289. break; /* This will leave dep non-NULL, and so exit the loop. */
  290. }
  291. pkg->clientdata->istobe= itb_normal;
  292. /* If dep is NULL we go and get the next package. */
  293. }
  294. if (!dep) exit(1); /* Not found */
  295. assert(pkg);
  296. startpkg= pkg;
  297. pkg->clientdata->istobe= itb_preinstall;
  298. /* OK, we have found an unsatisfied predependency.
  299. * Now go and find the first thing we need to install, as a first step
  300. * towards satisfying it.
  301. */
  302. do {
  303. /* We search for a package which would satisfy dep, and put it in pkg */
  304. for (possi = dep->list, pkg = NULL;
  305. !pkg && possi;
  306. possi=possi->next) {
  307. trypkg= possi->ed;
  308. if (!trypkg->available.valid) continue;
  309. if (trypkg->files && versionsatisfied(&trypkg->available,possi)) {
  310. if (trypkg->clientdata->istobe == itb_normal) { pkg= trypkg; break; }
  311. }
  312. if (possi->verrel != dvr_none) continue;
  313. for (provider=possi->ed->available.depended;
  314. !pkg && provider;
  315. provider=provider->next) {
  316. if (provider->up->type != dep_provides) continue;
  317. trypkg= provider->up->up;
  318. if (!trypkg->available.valid || !trypkg->files) continue;
  319. if (trypkg->clientdata->istobe == itb_normal) { pkg= trypkg; break; }
  320. }
  321. }
  322. if (!pkg) {
  323. varbufreset(&vb);
  324. describedepcon(&vb,dep);
  325. varbufaddc(&vb,0);
  326. fprintf(stderr, _("dpkg: cannot see how to satisfy pre-dependency:\n %s\n"),vb.buf);
  327. ohshit(_("cannot satisfy pre-dependencies for %.250s (wanted due to %.250s)"),
  328. dep->up->name,startpkg->name);
  329. }
  330. pkg->clientdata->istobe= itb_preinstall;
  331. for (dep= pkg->available.depends; dep; dep= dep->next) {
  332. if (dep->type != dep_predepends) continue;
  333. if (depisok(dep, &vb, NULL, 1))
  334. continue;
  335. break; /* This will leave dep non-NULL, and so exit the loop. */
  336. }
  337. } while (dep);
  338. /* OK, we've found it - pkg has no unsatisfied pre-dependencies ! */
  339. writerecord(stdout,"<stdout>",pkg,&pkg->available);
  340. if (fflush(stdout)) werr("stdout");
  341. }
  342. void printarch(const char *const *argv) {
  343. if (*argv)
  344. badusage(_("--%s takes no arguments"), cipaction->olong);
  345. if (printf("%s\n",architecture) == EOF) werr("stdout");
  346. if (fflush(stdout)) werr("stdout");
  347. }
  348. void cmpversions(const char *const *argv) {
  349. struct relationinfo {
  350. const char *string;
  351. /* These values are exit status codes, so 0=true, 1=false */
  352. int if_lesser, if_equal, if_greater;
  353. int if_none_a, if_none_both, if_none_b;
  354. };
  355. static const struct relationinfo relationinfos[]= {
  356. /* < = > !a!2!b */
  357. { "le", 0,0,1, 0,0,1 },
  358. { "lt", 0,1,1, 0,1,1 },
  359. { "eq", 1,0,1, 1,0,1 },
  360. { "ne", 0,1,0, 0,1,0 },
  361. { "ge", 1,0,0, 1,0,0 },
  362. { "gt", 1,1,0, 1,1,0 },
  363. { "le-nl", 0,0,1, 1,0,0 }, /* Here none */
  364. { "lt-nl", 0,1,1, 1,1,0 }, /* is counted */
  365. { "ge-nl", 1,0,0, 0,0,1 }, /* than any version.*/
  366. { "gt-nl", 1,1,0, 0,1,1 }, /* */
  367. { "<", 0,0,1, 0,0,1 }, /* For compatibility*/
  368. { "<=", 0,0,1, 0,0,1 }, /* with dpkg */
  369. { "<<", 0,1,1, 0,1,1 }, /* control file */
  370. { "=", 1,0,1, 1,0,1 }, /* syntax */
  371. { ">", 1,0,0, 1,0,0 }, /* */
  372. { ">=", 1,0,0, 1,0,0 },
  373. { ">>", 1,1,0, 1,1,0 },
  374. { NULL }
  375. };
  376. const struct relationinfo *rip;
  377. const char *emsg;
  378. struct versionrevision a, b;
  379. int r;
  380. if (!argv[0] || !argv[1] || !argv[2] || argv[3])
  381. badusage(_("--compare-versions takes three arguments:"
  382. " <version> <relation> <version>"));
  383. for (rip=relationinfos; rip->string && strcmp(rip->string,argv[1]); rip++);
  384. if (!rip->string) badusage(_("--compare-versions bad relation"));
  385. if (*argv[0] && strcmp(argv[0],"<unknown>")) {
  386. emsg= parseversion(&a,argv[0]);
  387. if (emsg) {
  388. if (printf(_("dpkg: version '%s' has bad syntax: %s\n"), argv[0], emsg) == EOF)
  389. werr("stdout");
  390. if (fflush(stdout)) werr("stdout");
  391. exit(1);
  392. }
  393. } else {
  394. blankversion(&a);
  395. }
  396. if (*argv[2] && strcmp(argv[2],"<unknown>")) {
  397. emsg= parseversion(&b,argv[2]);
  398. if (emsg) {
  399. if (printf(_("dpkg: version '%s' has bad syntax: %s\n"), argv[2], emsg) == EOF)
  400. werr("stdout");
  401. if (fflush(stdout)) werr("stdout");
  402. exit(1);
  403. }
  404. } else {
  405. blankversion(&b);
  406. }
  407. if (!informativeversion(&a)) {
  408. exit(informativeversion(&b) ? rip->if_none_a : rip->if_none_both);
  409. } else if (!informativeversion(&b)) {
  410. exit(rip->if_none_b);
  411. }
  412. r= versioncompare(&a,&b);
  413. debug(dbg_general,"cmpversions a=`%s' b=`%s' r=%d",
  414. versiondescribe(&a,vdew_always),
  415. versiondescribe(&b,vdew_always),
  416. r);
  417. if (r>0) exit(rip->if_greater);
  418. else if (r<0) exit(rip->if_lesser);
  419. else exit(rip->if_equal);
  420. }