enquiry.c 15 KB

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