enquiry.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*
  2. * dpkg - main program for package management
  3. * enquiry.c - status enquiry and listing options
  4. *
  5. * Copyright © 1995,1996 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2006, 2008-2016 Guillem Jover <guillem@debian.org>
  7. * Copyright © 2011 Linaro Limited
  8. * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
  9. *
  10. * This is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. #include <config.h>
  24. #include <compat.h>
  25. #include <sys/types.h>
  26. #include <assert.h>
  27. #include <string.h>
  28. #include <fcntl.h>
  29. #include <unistd.h>
  30. #include <stdbool.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <dpkg/i18n.h>
  34. #include <dpkg/dpkg.h>
  35. #include <dpkg/dpkg-db.h>
  36. #include <dpkg/arch.h>
  37. #include <dpkg/pkg-array.h>
  38. #include <dpkg/pkg-show.h>
  39. #include <dpkg/triglib.h>
  40. #include <dpkg/string.h>
  41. #include <dpkg/options.h>
  42. #include "filesdb.h"
  43. #include "infodb.h"
  44. #include "main.h"
  45. struct audit_problem {
  46. bool (*check)(struct pkginfo *, const struct audit_problem *problem);
  47. union {
  48. int number;
  49. const char *string;
  50. } value;
  51. const char *explanation;
  52. };
  53. static bool
  54. audit_reinstreq(struct pkginfo *pkg, const struct audit_problem *problem)
  55. {
  56. return pkg->eflag & PKG_EFLAG_REINSTREQ;
  57. }
  58. static bool
  59. audit_status(struct pkginfo *pkg, const struct audit_problem *problem)
  60. {
  61. if (pkg->eflag & PKG_EFLAG_REINSTREQ)
  62. return false;
  63. return (int)pkg->status == problem->value.number;
  64. }
  65. static bool
  66. audit_infofile(struct pkginfo *pkg, const struct audit_problem *problem)
  67. {
  68. if (pkg->status < PKG_STAT_HALFINSTALLED)
  69. return false;
  70. return !pkg_infodb_has_file(pkg, &pkg->installed, problem->value.string);
  71. }
  72. static bool
  73. audit_arch(struct pkginfo *pkg, const struct audit_problem *problem)
  74. {
  75. if (pkg->status < PKG_STAT_HALFINSTALLED)
  76. return false;
  77. return pkg->installed.arch->type == (enum dpkg_arch_type)problem->value.number;
  78. }
  79. static const struct audit_problem audit_problems[] = {
  80. {
  81. .check = audit_reinstreq,
  82. .value.number = 0,
  83. .explanation = N_(
  84. "The following packages are in a mess due to serious problems during\n"
  85. "installation. They must be reinstalled for them (and any packages\n"
  86. "that depend on them) to function properly:\n")
  87. }, {
  88. .check = audit_status,
  89. .value.number = PKG_STAT_UNPACKED,
  90. .explanation = N_(
  91. "The following packages have been unpacked but not yet configured.\n"
  92. "They must be configured using dpkg --configure or the configure\n"
  93. "menu option in dselect for them to work:\n")
  94. }, {
  95. .check = audit_status,
  96. .value.number = PKG_STAT_HALFCONFIGURED,
  97. .explanation = N_(
  98. "The following packages are only half configured, probably due to problems\n"
  99. "configuring them the first time. The configuration should be retried using\n"
  100. "dpkg --configure <package> or the configure menu option in dselect:\n")
  101. }, {
  102. .check = audit_status,
  103. .value.number = PKG_STAT_HALFINSTALLED,
  104. .explanation = N_(
  105. "The following packages are only half installed, due to problems during\n"
  106. "installation. The installation can probably be completed by retrying it;\n"
  107. "the packages can be removed using dselect or dpkg --remove:\n")
  108. }, {
  109. .check = audit_status,
  110. .value.number = PKG_STAT_TRIGGERSAWAITED,
  111. .explanation = N_(
  112. "The following packages are awaiting processing of triggers that they\n"
  113. "have activated in other packages. This processing can be requested using\n"
  114. "dselect or dpkg --configure --pending (or dpkg --triggers-only):\n")
  115. }, {
  116. .check = audit_status,
  117. .value.number = PKG_STAT_TRIGGERSPENDING,
  118. .explanation = N_(
  119. "The following packages have been triggered, but the trigger processing\n"
  120. "has not yet been done. Trigger processing can be requested using\n"
  121. "dselect or dpkg --configure --pending (or dpkg --triggers-only):\n")
  122. }, {
  123. .check = audit_infofile,
  124. .value.string = LISTFILE,
  125. .explanation = N_(
  126. "The following packages are missing the list control file in the\n"
  127. "database, they need to be reinstalled:\n")
  128. }, {
  129. .check = audit_infofile,
  130. .value.string = HASHFILE,
  131. .explanation = N_(
  132. "The following packages are missing the md5sums control file in the\n"
  133. "database, they need to be reinstalled:\n")
  134. }, {
  135. .check = audit_arch,
  136. .value.number = DPKG_ARCH_EMPTY,
  137. .explanation = N_("The following packages do not have an architecture:\n")
  138. }, {
  139. .check = audit_arch,
  140. .value.number = DPKG_ARCH_ILLEGAL,
  141. .explanation = N_("The following packages have an illegal architecture:\n")
  142. }, {
  143. .check = audit_arch,
  144. .value.number = DPKG_ARCH_UNKNOWN,
  145. .explanation = N_(
  146. "The following packages have an unknown foreign architecture, which will\n"
  147. "cause dependency issues on front-ends. This can be fixed by registering\n"
  148. "the foreign architecture with dpkg --add-architecture:\n")
  149. }, {
  150. .check = NULL
  151. }
  152. };
  153. static void describebriefly(struct pkginfo *pkg) {
  154. int maxl, l;
  155. const char *pdesc;
  156. maxl= 57;
  157. l= strlen(pkg->set->name);
  158. if (l>20) maxl -= (l-20);
  159. pdesc = pkgbin_summary(pkg, &pkg->installed, &l);
  160. l = min(l, maxl);
  161. printf(" %-20s %.*s\n", pkg_name(pkg, pnaw_nonambig), l, pdesc);
  162. }
  163. static struct pkginfo *
  164. pkg_array_mapper(const char *name)
  165. {
  166. struct pkginfo *pkg;
  167. pkg = dpkg_options_parse_pkgname(cipaction, name);
  168. if (pkg->status == PKG_STAT_NOTINSTALLED)
  169. notice(_("package '%s' is not installed"), pkg_name(pkg, pnaw_nonambig));
  170. return pkg;
  171. }
  172. int
  173. audit(const char *const *argv)
  174. {
  175. const struct audit_problem *problem;
  176. struct pkg_array array;
  177. bool head_running = false;
  178. int i;
  179. modstatdb_open(msdbrw_readonly);
  180. if (!*argv)
  181. pkg_array_init_from_db(&array);
  182. else
  183. pkg_array_init_from_names(&array, pkg_array_mapper, (const char **)argv);
  184. pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
  185. for (problem = audit_problems; problem->check; problem++) {
  186. bool head = false;
  187. for (i = 0; i < array.n_pkgs; i++) {
  188. struct pkginfo *pkg = array.pkgs[i];
  189. if (!problem->check(pkg, problem))
  190. continue;
  191. if (!head_running) {
  192. if (modstatdb_is_locked())
  193. puts(_(
  194. "Another process has locked the database for writing, and might currently be\n"
  195. "modifying it, some of the following problems might just be due to that.\n"));
  196. head_running = true;
  197. }
  198. if (!head) {
  199. fputs(gettext(problem->explanation), stdout);
  200. head = true;
  201. }
  202. describebriefly(pkg);
  203. }
  204. if (head) putchar('\n');
  205. }
  206. pkg_array_destroy(&array);
  207. m_output(stdout, _("<standard output>"));
  208. return 0;
  209. }
  210. struct sectionentry {
  211. struct sectionentry *next;
  212. const char *name;
  213. int count;
  214. };
  215. static bool
  216. yettobeunpacked(struct pkginfo *pkg, const char **thissect)
  217. {
  218. if (pkg->want != PKG_WANT_INSTALL)
  219. return false;
  220. switch (pkg->status) {
  221. case PKG_STAT_UNPACKED:
  222. case PKG_STAT_INSTALLED:
  223. case PKG_STAT_HALFCONFIGURED:
  224. case PKG_STAT_TRIGGERSPENDING:
  225. case PKG_STAT_TRIGGERSAWAITED:
  226. return false;
  227. case PKG_STAT_NOTINSTALLED:
  228. case PKG_STAT_HALFINSTALLED:
  229. case PKG_STAT_CONFIGFILES:
  230. if (thissect)
  231. *thissect = str_is_set(pkg->section) ? pkg->section :
  232. C_("section", "<unknown>");
  233. return true;
  234. default:
  235. internerr("unknown package status '%d'", pkg->status);
  236. }
  237. return false;
  238. }
  239. int
  240. unpackchk(const char *const *argv)
  241. {
  242. int totalcount, sects;
  243. struct sectionentry *sectionentries, *se, **sep;
  244. struct pkgiterator *iter;
  245. struct pkginfo *pkg;
  246. const char *thissect;
  247. char buf[20];
  248. int width;
  249. if (*argv)
  250. badusage(_("--%s takes no arguments"), cipaction->olong);
  251. modstatdb_open(msdbrw_readonly);
  252. totalcount= 0;
  253. sectionentries = NULL;
  254. sects= 0;
  255. iter = pkg_db_iter_new();
  256. while ((pkg = pkg_db_iter_next_pkg(iter))) {
  257. if (!yettobeunpacked(pkg, &thissect)) continue;
  258. for (se= sectionentries; se && strcasecmp(thissect,se->name); se= se->next);
  259. if (!se) {
  260. se= nfmalloc(sizeof(struct sectionentry));
  261. for (sep= &sectionentries;
  262. *sep && strcasecmp(thissect,(*sep)->name) > 0;
  263. sep= &(*sep)->next);
  264. se->name= thissect;
  265. se->count= 0;
  266. se->next= *sep;
  267. *sep= se;
  268. sects++;
  269. }
  270. se->count++; totalcount++;
  271. }
  272. pkg_db_iter_free(iter);
  273. if (totalcount == 0)
  274. return 0;
  275. if (totalcount <= 12) {
  276. iter = pkg_db_iter_new();
  277. while ((pkg = pkg_db_iter_next_pkg(iter))) {
  278. if (!yettobeunpacked(pkg, NULL))
  279. continue;
  280. describebriefly(pkg);
  281. }
  282. pkg_db_iter_free(iter);
  283. } else if (sects <= 12) {
  284. for (se= sectionentries; se; se= se->next) {
  285. sprintf(buf,"%d",se->count);
  286. printf(_(" %d in %s: "),se->count,se->name);
  287. width= 70-strlen(se->name)-strlen(buf);
  288. while (width > 59) { putchar(' '); width--; }
  289. iter = pkg_db_iter_new();
  290. while ((pkg = pkg_db_iter_next_pkg(iter))) {
  291. const char *pkgname;
  292. if (!yettobeunpacked(pkg,&thissect)) continue;
  293. if (strcasecmp(thissect,se->name)) continue;
  294. pkgname = pkg_name(pkg, pnaw_nonambig);
  295. width -= strlen(pkgname);
  296. width--;
  297. if (width < 4) { printf(" ..."); break; }
  298. printf(" %s", pkgname);
  299. }
  300. pkg_db_iter_free(iter);
  301. putchar('\n');
  302. }
  303. } else {
  304. printf(P_(" %d package, from the following section:",
  305. " %d packages, from the following sections:", totalcount),
  306. totalcount);
  307. width= 0;
  308. for (se= sectionentries; se; se= se->next) {
  309. sprintf(buf,"%d",se->count);
  310. width -= (6 + strlen(se->name) + strlen(buf));
  311. if (width < 0) { putchar('\n'); width= 73 - strlen(se->name) - strlen(buf); }
  312. printf(" %s (%d)",se->name,se->count);
  313. }
  314. putchar('\n');
  315. }
  316. m_output(stdout, _("<standard output>"));
  317. return 0;
  318. }
  319. static int
  320. assert_version_support(const char *const *argv,
  321. struct dpkg_version *version,
  322. const char *feature_name)
  323. {
  324. struct pkginfo *pkg;
  325. if (*argv)
  326. badusage(_("--%s takes no arguments"), cipaction->olong);
  327. modstatdb_open(msdbrw_readonly);
  328. pkg = pkg_db_find_singleton("dpkg");
  329. switch (pkg->status) {
  330. case PKG_STAT_INSTALLED:
  331. case PKG_STAT_TRIGGERSPENDING:
  332. return 0;
  333. case PKG_STAT_UNPACKED:
  334. case PKG_STAT_HALFCONFIGURED:
  335. case PKG_STAT_HALFINSTALLED:
  336. case PKG_STAT_TRIGGERSAWAITED:
  337. if (dpkg_version_relate(&pkg->configversion, DPKG_RELATION_GE, version))
  338. return 0;
  339. printf(_("Version of dpkg with working %s support not yet configured.\n"
  340. " Please use 'dpkg --configure dpkg', and then try again.\n"),
  341. feature_name);
  342. return 1;
  343. default:
  344. printf(_("dpkg not recorded as installed, cannot check for %s support!\n"),
  345. feature_name);
  346. return 1;
  347. }
  348. }
  349. int
  350. assertpredep(const char *const *argv)
  351. {
  352. struct dpkg_version version = { 0, "1.1.0", NULL };
  353. return assert_version_support(argv, &version, _("Pre-Depends field"));
  354. }
  355. int
  356. assertepoch(const char *const *argv)
  357. {
  358. struct dpkg_version version = { 0, "1.4.0.7", NULL };
  359. return assert_version_support(argv, &version, _("epoch"));
  360. }
  361. int
  362. assertlongfilenames(const char *const *argv)
  363. {
  364. struct dpkg_version version = { 0, "1.4.1.17", NULL };
  365. return assert_version_support(argv, &version, _("long filenames"));
  366. }
  367. int
  368. assertmulticonrep(const char *const *argv)
  369. {
  370. struct dpkg_version version = { 0, "1.4.1.19", NULL };
  371. return assert_version_support(argv, &version,
  372. _("multiple Conflicts and Replaces"));
  373. }
  374. int
  375. assertmultiarch(const char *const *argv)
  376. {
  377. struct dpkg_version version = { 0, "1.16.2", NULL };
  378. return assert_version_support(argv, &version, _("multi-arch"));
  379. }
  380. int
  381. assertverprovides(const char *const *argv)
  382. {
  383. struct dpkg_version version = { 0, "1.17.11", NULL };
  384. return assert_version_support(argv, &version, _("versioned Provides"));
  385. }
  386. /**
  387. * Print a single package which:
  388. * (a) is the target of one or more relevant predependencies.
  389. * (b) has itself no unsatisfied pre-dependencies.
  390. *
  391. * If such a package is present output is the Packages file entry,
  392. * which can be massaged as appropriate.
  393. *
  394. * Exit status:
  395. * 0 = a package printed, OK
  396. * 1 = no suitable package available
  397. * 2 = error
  398. */
  399. int
  400. predeppackage(const char *const *argv)
  401. {
  402. static struct varbuf vb;
  403. struct pkgiterator *iter;
  404. struct pkginfo *pkg = NULL, *startpkg, *trypkg;
  405. struct dependency *dep;
  406. struct deppossi *possi, *provider;
  407. if (*argv)
  408. badusage(_("--%s takes no arguments"), cipaction->olong);
  409. modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
  410. /* We use clientdata->istobe to detect loops. */
  411. clear_istobes();
  412. dep = NULL;
  413. iter = pkg_db_iter_new();
  414. while (!dep && (pkg = pkg_db_iter_next_pkg(iter))) {
  415. /* Ignore packages user doesn't want. */
  416. if (pkg->want != PKG_WANT_INSTALL)
  417. continue;
  418. /* Ignore packages not available. */
  419. if (!pkg->files)
  420. continue;
  421. pkg->clientdata->istobe = PKG_ISTOBE_PREINSTALL;
  422. for (dep= pkg->available.depends; dep; dep= dep->next) {
  423. if (dep->type != dep_predepends) continue;
  424. if (depisok(dep, &vb, NULL, NULL, true))
  425. continue;
  426. /* This will leave dep non-NULL, and so exit the loop. */
  427. break;
  428. }
  429. pkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
  430. /* If dep is NULL we go and get the next package. */
  431. }
  432. pkg_db_iter_free(iter);
  433. if (!dep)
  434. return 1; /* Not found. */
  435. assert(pkg);
  436. startpkg= pkg;
  437. pkg->clientdata->istobe = PKG_ISTOBE_PREINSTALL;
  438. /* OK, we have found an unsatisfied predependency.
  439. * Now go and find the first thing we need to install, as a first step
  440. * towards satisfying it. */
  441. do {
  442. /* We search for a package which would satisfy dep, and put it in pkg. */
  443. for (possi = dep->list, pkg = NULL;
  444. !pkg && possi;
  445. possi=possi->next) {
  446. struct deppossi_pkg_iterator *possi_iter;
  447. possi_iter = deppossi_pkg_iter_new(possi, wpb_available);
  448. while (!pkg && (trypkg = deppossi_pkg_iter_next(possi_iter))) {
  449. if (trypkg->files &&
  450. trypkg->clientdata->istobe == PKG_ISTOBE_NORMAL &&
  451. versionsatisfied(&trypkg->available, possi)) {
  452. pkg = trypkg;
  453. break;
  454. }
  455. for (provider = possi->ed->depended.available;
  456. !pkg && provider;
  457. provider = provider->next) {
  458. if (provider->up->type != dep_provides)
  459. continue;
  460. if (!pkg_virtual_deppossi_satisfied(possi, provider))
  461. continue;
  462. trypkg = provider->up->up;
  463. if (!trypkg->files)
  464. continue;
  465. if (trypkg->clientdata->istobe == PKG_ISTOBE_NORMAL) {
  466. pkg = trypkg;
  467. break;
  468. }
  469. }
  470. }
  471. deppossi_pkg_iter_free(possi_iter);
  472. }
  473. if (!pkg) {
  474. varbuf_reset(&vb);
  475. describedepcon(&vb,dep);
  476. varbuf_end_str(&vb);
  477. notice(_("cannot see how to satisfy pre-dependency:\n %s"), vb.buf);
  478. ohshit(_("cannot satisfy pre-dependencies for %.250s (wanted due to %.250s)"),
  479. pkgbin_name(dep->up, &dep->up->available, pnaw_nonambig),
  480. pkgbin_name(startpkg, &startpkg->available, pnaw_nonambig));
  481. }
  482. pkg->clientdata->istobe = PKG_ISTOBE_PREINSTALL;
  483. for (dep= pkg->available.depends; dep; dep= dep->next) {
  484. if (dep->type != dep_predepends) continue;
  485. if (depisok(dep, &vb, NULL, NULL, true))
  486. continue;
  487. /* This will leave dep non-NULL, and so exit the loop. */
  488. break;
  489. }
  490. } while (dep);
  491. /* OK, we've found it - pkg has no unsatisfied pre-dependencies! */
  492. writerecord(stdout, _("<standard output>"), pkg, &pkg->available);
  493. m_output(stdout, _("<standard output>"));
  494. return 0;
  495. }
  496. int
  497. printarch(const char *const *argv)
  498. {
  499. if (*argv)
  500. badusage(_("--%s takes no arguments"), cipaction->olong);
  501. printf("%s\n", dpkg_arch_get(DPKG_ARCH_NATIVE)->name);
  502. m_output(stdout, _("<standard output>"));
  503. return 0;
  504. }
  505. int
  506. print_foreign_arches(const char *const *argv)
  507. {
  508. struct dpkg_arch *arch;
  509. if (*argv)
  510. badusage(_("--%s takes no arguments"), cipaction->olong);
  511. dpkg_arch_load_list();
  512. for (arch = dpkg_arch_get_list(); arch; arch = arch->next) {
  513. if (arch->type != DPKG_ARCH_FOREIGN)
  514. continue;
  515. printf("%s\n", arch->name);
  516. }
  517. m_output(stdout, _("<standard output>"));
  518. return 0;
  519. }
  520. int
  521. validate_pkgname(const char *const *argv)
  522. {
  523. const char *emsg;
  524. if (!argv[0] || argv[1])
  525. badusage(_("--%s takes one <pkgname> argument"), cipaction->olong);
  526. emsg = pkg_name_is_illegal(argv[0]);
  527. if (emsg)
  528. ohshit(_("package name '%s' is invalid: %s"), argv[0], emsg);
  529. return 0;
  530. }
  531. int
  532. validate_trigname(const char *const *argv)
  533. {
  534. const char *emsg;
  535. if (!argv[0] || argv[1])
  536. badusage(_("--%s takes one <trigname> argument"), cipaction->olong);
  537. emsg = trig_name_is_illegal(argv[0]);
  538. if (emsg)
  539. ohshit(_("trigger name '%s' is invalid: %s"), argv[0], emsg);
  540. return 0;
  541. }
  542. int
  543. validate_archname(const char *const *argv)
  544. {
  545. const char *emsg;
  546. if (!argv[0] || argv[1])
  547. badusage(_("--%s takes one <archname> argument"), cipaction->olong);
  548. emsg = dpkg_arch_name_is_illegal(argv[0]);
  549. if (emsg)
  550. ohshit(_("architecture name '%s' is invalid: %s"), argv[0], emsg);
  551. return 0;
  552. }
  553. int
  554. validate_version(const char *const *argv)
  555. {
  556. struct dpkg_version version;
  557. struct dpkg_error err;
  558. if (!argv[0] || argv[1])
  559. badusage(_("--%s takes one <version> argument"), cipaction->olong);
  560. if (parseversion(&version, argv[0], &err) < 0) {
  561. dpkg_error_print(&err, _("version '%s' has bad syntax"), argv[0]);
  562. dpkg_error_destroy(&err);
  563. return 1;
  564. }
  565. return 0;
  566. }
  567. int
  568. cmpversions(const char *const *argv)
  569. {
  570. struct relationinfo {
  571. const char *string;
  572. /* These values are exit status codes, so 0 = true, 1 = false. */
  573. int if_lesser, if_equal, if_greater;
  574. int if_none_a, if_none_both, if_none_b;
  575. bool obsolete;
  576. };
  577. static const struct relationinfo relationinfos[]= {
  578. /* < = > !a!2!b */
  579. { "le", 0,0,1, 0,0,1 },
  580. { "lt", 0,1,1, 0,1,1 },
  581. { "eq", 1,0,1, 1,0,1 },
  582. { "ne", 0,1,0, 0,1,0 },
  583. { "ge", 1,0,0, 1,0,0 },
  584. { "gt", 1,1,0, 1,1,0 },
  585. /* These treat an empty version as later than any version. */
  586. { "le-nl", 0,0,1, 1,0,0 },
  587. { "lt-nl", 0,1,1, 1,1,0 },
  588. { "ge-nl", 1,0,0, 0,0,1 },
  589. { "gt-nl", 1,1,0, 0,1,1 },
  590. /* For compatibility with dpkg control file syntax. */
  591. { "<", 0,0,1, 0,0,1, .obsolete = true },
  592. { "<=", 0,0,1, 0,0,1 },
  593. { "<<", 0,1,1, 0,1,1 },
  594. { "=", 1,0,1, 1,0,1 },
  595. { ">", 1,0,0, 1,0,0, .obsolete = true },
  596. { ">=", 1,0,0, 1,0,0 },
  597. { ">>", 1,1,0, 1,1,0 },
  598. { NULL }
  599. };
  600. const struct relationinfo *rip;
  601. struct dpkg_version a, b;
  602. struct dpkg_error err;
  603. int rc;
  604. if (!argv[0] || !argv[1] || !argv[2] || argv[3])
  605. badusage(_("--compare-versions takes three arguments:"
  606. " <version> <relation> <version>"));
  607. for (rip=relationinfos; rip->string && strcmp(rip->string,argv[1]); rip++);
  608. if (!rip->string) badusage(_("--compare-versions bad relation"));
  609. if (rip->obsolete)
  610. warning(_("--%s used with obsolete relation operator '%s'"),
  611. cipaction->olong, rip->string);
  612. if (*argv[0] && strcmp(argv[0],"<unknown>")) {
  613. if (parseversion(&a, argv[0], &err) < 0) {
  614. dpkg_error_print(&err, _("version '%s' has bad syntax"), argv[0]);
  615. dpkg_error_destroy(&err);
  616. }
  617. } else {
  618. dpkg_version_blank(&a);
  619. }
  620. if (*argv[2] && strcmp(argv[2],"<unknown>")) {
  621. if (parseversion(&b, argv[2], &err) < 0) {
  622. dpkg_error_print(&err, _("version '%s' has bad syntax"), argv[2]);
  623. dpkg_error_destroy(&err);
  624. }
  625. } else {
  626. dpkg_version_blank(&b);
  627. }
  628. if (!dpkg_version_is_informative(&a)) {
  629. if (dpkg_version_is_informative(&b))
  630. return rip->if_none_a;
  631. else
  632. return rip->if_none_both;
  633. } else if (!dpkg_version_is_informative(&b)) {
  634. return rip->if_none_b;
  635. }
  636. rc = dpkg_version_compare(&a, &b);
  637. debug(dbg_general, "cmpversions a='%s' b='%s' r=%d",
  638. versiondescribe(&a,vdew_always),
  639. versiondescribe(&b,vdew_always),
  640. rc);
  641. if (rc > 0)
  642. return rip->if_greater;
  643. else if (rc < 0)
  644. return rip->if_lesser;
  645. else
  646. return rip->if_equal;
  647. }