enquiry.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 : 0;
  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. 0
  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= 0;
  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,0)) continue;
  179. describebriefly(pkg);
  180. }
  181. iterpkgend(it);
  182. } else if (sects <= 12) {
  183. for (se= sectionentries; se; se= se->next) {
  184. sprintf(buf,"%d",se->count);
  185. printf(_(" %d in %s: "),se->count,se->name);
  186. width= 70-strlen(se->name)-strlen(buf);
  187. while (width > 59) { putchar(' '); width--; }
  188. it= iterpkgstart();
  189. while ((pkg= iterpkgnext(it))) {
  190. if (!yettobeunpacked(pkg,&thissect)) continue;
  191. if (strcasecmp(thissect,se->name)) continue;
  192. width -= strlen(pkg->name); width--;
  193. if (width < 4) { printf(" ..."); break; }
  194. printf(" %s",pkg->name);
  195. }
  196. iterpkgend(it);
  197. putchar('\n');
  198. }
  199. } else {
  200. printf(_(" %d packages, from the following sections:"),totalcount);
  201. width= 0;
  202. for (se= sectionentries; se; se= se->next) {
  203. sprintf(buf,"%d",se->count);
  204. width -= (6 + strlen(se->name) + strlen(buf));
  205. if (width < 0) { putchar('\n'); width= 73 - strlen(se->name) - strlen(buf); }
  206. printf(" %s (%d)",se->name,se->count);
  207. }
  208. putchar('\n');
  209. }
  210. fflush(stdout);
  211. if (ferror(stdout)) werr("stdout");
  212. }
  213. static void assertversion(const char *const *argv,
  214. struct versionrevision *verrev_buf,
  215. const char *reqversion) {
  216. struct pkginfo *pkg;
  217. if (*argv) badusage(_("--assert-* does not take any arguments"));
  218. modstatdb_init(admindir,msdbrw_readonly|msdbrw_noavail);
  219. if (verrev_buf->epoch == ~0UL) {
  220. verrev_buf->epoch= 0;
  221. verrev_buf->version= nfstrsave(reqversion);
  222. verrev_buf->revision= 0;
  223. }
  224. pkg= findpackage("dpkg");
  225. switch (pkg->status) {
  226. case stat_installed:
  227. break;
  228. case stat_unpacked: case stat_halfconfigured: case stat_halfinstalled:
  229. if (versionsatisfied3(&pkg->configversion,verrev_buf,dvr_laterequal))
  230. break;
  231. printf(_("Version of dpkg with working epoch support not yet configured.\n"
  232. " Please use `dpkg --configure dpkg', and then try again.\n"));
  233. exit(1);
  234. default:
  235. printf(_("dpkg not recorded as installed, cannot check for epoch support !\n"));
  236. exit(1);
  237. }
  238. }
  239. void assertpredep(const char *const *argv) {
  240. static struct versionrevision predepversion = {~0UL,0,0};
  241. assertversion(argv,&predepversion,"1.1.0");
  242. }
  243. void assertepoch(const char *const *argv) {
  244. static struct versionrevision epochversion = {~0UL,0,0};
  245. assertversion(argv,&epochversion,"1.4.0.7");
  246. }
  247. void assertlongfilenames(const char *const *argv) {
  248. static struct versionrevision epochversion = {~0UL,0,0};
  249. assertversion(argv,&epochversion,"1.4.1.17");
  250. }
  251. void assertmulticonrep(const char *const *argv) {
  252. static struct versionrevision epochversion = {~0UL,0,0};
  253. assertversion(argv,&epochversion,"1.4.1.19");
  254. }
  255. void predeppackage(const char *const *argv) {
  256. /* Print a single package which:
  257. * (a) is the target of one or more relevant predependencies.
  258. * (b) has itself no unsatisfied pre-dependencies.
  259. * If such a package is present output is the Packages file entry,
  260. * which can be massaged as appropriate.
  261. * Exit status:
  262. * 0 = a package printed, OK
  263. * 1 = no suitable package available
  264. * 2 = error
  265. */
  266. static struct varbuf vb;
  267. struct pkgiterator *it;
  268. struct pkginfo *pkg= 0, *startpkg, *trypkg;
  269. struct dependency *dep;
  270. struct deppossi *possi, *provider;
  271. if (*argv) badusage(_("--predep-package does not take any argument"));
  272. modstatdb_init(admindir,msdbrw_readonly);
  273. clear_istobes(); /* We use clientdata->istobe to detect loops */
  274. for (it=iterpkgstart(), dep=0;
  275. !dep && (pkg=iterpkgnext(it));
  276. ) {
  277. if (pkg->want != want_install) continue; /* Ignore packages user doesn't want */
  278. if (!pkg->files) continue; /* Ignore packages not available */
  279. pkg->clientdata->istobe= itb_preinstall;
  280. for (dep= pkg->available.depends; dep; dep= dep->next) {
  281. if (dep->type != dep_predepends) continue;
  282. if (depisok(dep,&vb,0,1)) continue;
  283. break; /* This will leave dep non-NULL, and so exit the loop. */
  284. }
  285. pkg->clientdata->istobe= itb_normal;
  286. /* If dep is NULL we go and get the next package. */
  287. }
  288. if (!dep) exit(1); /* Not found */
  289. assert(pkg);
  290. startpkg= pkg;
  291. pkg->clientdata->istobe= itb_preinstall;
  292. /* OK, we have found an unsatisfied predependency.
  293. * Now go and find the first thing we need to install, as a first step
  294. * towards satisfying it.
  295. */
  296. do {
  297. /* We search for a package which would satisfy dep, and put it in pkg */
  298. for (possi=dep->list, pkg=0;
  299. !pkg && possi;
  300. possi=possi->next) {
  301. trypkg= possi->ed;
  302. if (!trypkg->available.valid) continue;
  303. if (trypkg->files && versionsatisfied(&trypkg->available,possi)) {
  304. if (trypkg->clientdata->istobe == itb_normal) { pkg= trypkg; break; }
  305. }
  306. if (possi->verrel != dvr_none) continue;
  307. for (provider=possi->ed->available.depended;
  308. !pkg && provider;
  309. provider=provider->next) {
  310. if (provider->up->type != dep_provides) continue;
  311. trypkg= provider->up->up;
  312. if (!trypkg->available.valid || !trypkg->files) continue;
  313. if (trypkg->clientdata->istobe == itb_normal) { pkg= trypkg; break; }
  314. }
  315. }
  316. if (!pkg) {
  317. varbufreset(&vb);
  318. describedepcon(&vb,dep);
  319. varbufaddc(&vb,0);
  320. fprintf(stderr, _("dpkg: cannot see how to satisfy pre-dependency:\n %s\n"),vb.buf);
  321. ohshit(_("cannot satisfy pre-dependencies for %.250s (wanted due to %.250s)"),
  322. dep->up->name,startpkg->name);
  323. }
  324. pkg->clientdata->istobe= itb_preinstall;
  325. for (dep= pkg->available.depends; dep; dep= dep->next) {
  326. if (dep->type != dep_predepends) continue;
  327. if (depisok(dep,&vb,0,1)) continue;
  328. break; /* This will leave dep non-NULL, and so exit the loop. */
  329. }
  330. } while (dep);
  331. /* OK, we've found it - pkg has no unsatisfied pre-dependencies ! */
  332. writerecord(stdout,"<stdout>",pkg,&pkg->available);
  333. if (fflush(stdout)) werr("stdout");
  334. }
  335. void printarch(const char *const *argv) {
  336. if (*argv) badusage(_("--print-architecture does not take any argument"));
  337. if (printf("%s\n",architecture) == EOF) werr("stdout");
  338. if (fflush(stdout)) werr("stdout");
  339. }
  340. void cmpversions(const char *const *argv) {
  341. struct relationinfo {
  342. const char *string;
  343. /* These values are exit status codes, so 0=true, 1=false */
  344. int if_lesser, if_equal, if_greater;
  345. int if_none_a, if_none_both, if_none_b;
  346. };
  347. static const struct relationinfo relationinfos[]= {
  348. /* < = > !a!2!b */
  349. { "le", 0,0,1, 0,0,1 },
  350. { "lt", 0,1,1, 0,1,1 },
  351. { "eq", 1,0,1, 1,0,1 },
  352. { "ne", 0,1,0, 0,1,0 },
  353. { "ge", 1,0,0, 1,0,0 },
  354. { "gt", 1,1,0, 1,1,0 },
  355. { "le-nl", 0,0,1, 1,0,0 }, /* Here none */
  356. { "lt-nl", 0,1,1, 1,1,0 }, /* is counted */
  357. { "ge-nl", 1,0,0, 0,0,1 }, /* than any version.*/
  358. { "gt-nl", 1,1,0, 0,1,1 }, /* */
  359. { "<", 0,0,1, 0,0,1 }, /* For compatibility*/
  360. { "<=", 0,0,1, 0,0,1 }, /* with dpkg */
  361. { "<<", 0,1,1, 0,1,1 }, /* control file */
  362. { "=", 1,0,1, 1,0,1 }, /* syntax */
  363. { ">", 1,0,0, 1,0,0 }, /* */
  364. { ">=", 1,0,0, 1,0,0 },
  365. { ">>", 1,1,0, 1,1,0 },
  366. { 0 }
  367. };
  368. const struct relationinfo *rip;
  369. const char *emsg;
  370. struct versionrevision a, b;
  371. int r;
  372. if (!argv[0] || !argv[1] || !argv[2] || argv[3])
  373. badusage(_("--compare-versions takes three arguments:"
  374. " <version> <relation> <version>"));
  375. for (rip=relationinfos; rip->string && strcmp(rip->string,argv[1]); rip++);
  376. if (!rip->string) badusage(_("--compare-versions bad relation"));
  377. if (*argv[0] && strcmp(argv[0],"<unknown>")) {
  378. emsg= parseversion(&a,argv[0]);
  379. if (emsg) {
  380. if (printf(_("dpkg: version '%s' has bad syntax: %s\n"), argv[0], emsg) == EOF)
  381. werr("stdout");
  382. if (fflush(stdout)) werr("stdout");
  383. exit(1);
  384. }
  385. } else {
  386. blankversion(&a);
  387. }
  388. if (*argv[2] && strcmp(argv[2],"<unknown>")) {
  389. emsg= parseversion(&b,argv[2]);
  390. if (emsg) {
  391. if (printf(_("dpkg: version '%s' has bad syntax: %s\n"), argv[2], emsg) == EOF)
  392. werr("stdout");
  393. if (fflush(stdout)) werr("stdout");
  394. exit(1);
  395. }
  396. } else {
  397. blankversion(&b);
  398. }
  399. if (!informativeversion(&a)) {
  400. exit(informativeversion(&b) ? rip->if_none_a : rip->if_none_both);
  401. } else if (!informativeversion(&b)) {
  402. exit(rip->if_none_b);
  403. }
  404. r= versioncompare(&a,&b);
  405. debug(dbg_general,"cmpversions a=`%s' b=`%s' r=%d",
  406. versiondescribe(&a,vdew_always),
  407. versiondescribe(&b,vdew_always),
  408. r);
  409. if (r>0) exit(rip->if_greater);
  410. else if (r<0) exit(rip->if_lesser);
  411. else exit(rip->if_equal);
  412. }