parse.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * parse.c - database file parsing, main package/field loop
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2006,2008-2014 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #ifdef USE_MMAP
  26. #include <sys/mman.h>
  27. #endif
  28. #include <assert.h>
  29. #include <fcntl.h>
  30. #include <ctype.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <stdarg.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <dpkg/macros.h>
  37. #include <dpkg/i18n.h>
  38. #include <dpkg/dpkg.h>
  39. #include <dpkg/dpkg-db.h>
  40. #include <dpkg/string.h>
  41. #include <dpkg/pkg.h>
  42. #include <dpkg/parsedump.h>
  43. #include <dpkg/fdio.h>
  44. #include <dpkg/buffer.h>
  45. /**
  46. * Fields information.
  47. */
  48. const struct fieldinfo fieldinfos[]= {
  49. /* Note: Capitalization of field name strings is important. */
  50. { "Package", f_name, w_name },
  51. { "Essential", f_boolean, w_booleandefno, PKGIFPOFF(essential) },
  52. { "Status", f_status, w_status },
  53. { "Priority", f_priority, w_priority },
  54. { "Section", f_section, w_section },
  55. { "Installed-Size", f_charfield, w_charfield, PKGIFPOFF(installedsize) },
  56. { "Origin", f_charfield, w_charfield, PKGIFPOFF(origin) },
  57. { "Maintainer", f_charfield, w_charfield, PKGIFPOFF(maintainer) },
  58. { "Bugs", f_charfield, w_charfield, PKGIFPOFF(bugs) },
  59. { "Architecture", f_architecture, w_architecture },
  60. { "Multi-Arch", f_multiarch, w_multiarch, PKGIFPOFF(multiarch) },
  61. { "Source", f_charfield, w_charfield, PKGIFPOFF(source) },
  62. { "Version", f_version, w_version, PKGIFPOFF(version) },
  63. { "Revision", f_revision, w_null },
  64. { "Config-Version", f_configversion, w_configversion },
  65. { "Replaces", f_dependency, w_dependency, dep_replaces },
  66. { "Provides", f_dependency, w_dependency, dep_provides },
  67. { "Depends", f_dependency, w_dependency, dep_depends },
  68. { "Pre-Depends", f_dependency, w_dependency, dep_predepends },
  69. { "Recommends", f_dependency, w_dependency, dep_recommends },
  70. { "Suggests", f_dependency, w_dependency, dep_suggests },
  71. { "Breaks", f_dependency, w_dependency, dep_breaks },
  72. { "Conflicts", f_dependency, w_dependency, dep_conflicts },
  73. { "Enhances", f_dependency, w_dependency, dep_enhances },
  74. { "Conffiles", f_conffiles, w_conffiles },
  75. { "Filename", f_filecharf, w_filecharf, FILEFOFF(name) },
  76. { "Size", f_filecharf, w_filecharf, FILEFOFF(size) },
  77. { "MD5sum", f_filecharf, w_filecharf, FILEFOFF(md5sum) },
  78. { "MSDOS-Filename", f_filecharf, w_filecharf, FILEFOFF(msdosname) },
  79. { "Description", f_charfield, w_charfield, PKGIFPOFF(description) },
  80. { "Triggers-Pending", f_trigpend, w_trigpend },
  81. { "Triggers-Awaited", f_trigaw, w_trigaw },
  82. /* Note that aliases are added to the nicknames table. */
  83. { NULL }
  84. };
  85. static const struct nickname nicknames[] = {
  86. /* Note: Capitalization of these strings is important. */
  87. { .nick = "Recommended", .canon = "Recommends" },
  88. { .nick = "Optional", .canon = "Suggests" },
  89. { .nick = "Class", .canon = "Priority" },
  90. { .nick = "Package-Revision", .canon = "Revision" },
  91. { .nick = "Package_Revision", .canon = "Revision" },
  92. { .nick = NULL }
  93. };
  94. /**
  95. * Package object being parsed.
  96. *
  97. * Structure used to hold the parsed data for the package being constructed,
  98. * before it gets properly inserted into the package database.
  99. */
  100. struct pkg_parse_object {
  101. struct pkginfo *pkg;
  102. struct pkgbin *pkgbin;
  103. };
  104. /**
  105. * Parse the field and value into the package being constructed.
  106. */
  107. static void
  108. pkg_parse_field(struct parsedb_state *ps, struct field_state *fs,
  109. void *parse_obj)
  110. {
  111. struct pkg_parse_object *pkg_obj = parse_obj;
  112. const struct nickname *nick;
  113. const struct fieldinfo *fip;
  114. int *ip;
  115. for (nick = nicknames; nick->nick; nick++)
  116. if (strncasecmp(nick->nick, fs->fieldstart, fs->fieldlen) == 0 &&
  117. nick->nick[fs->fieldlen] == '\0')
  118. break;
  119. if (nick->nick) {
  120. fs->fieldstart = nick->canon;
  121. fs->fieldlen = strlen(fs->fieldstart);
  122. }
  123. for (fip = fieldinfos, ip = fs->fieldencountered; fip->name; fip++, ip++)
  124. if (strncasecmp(fip->name, fs->fieldstart, fs->fieldlen) == 0)
  125. break;
  126. if (fip->name) {
  127. if ((*ip)++)
  128. parse_error(ps,
  129. _("duplicate value for `%s' field"), fip->name);
  130. varbuf_reset(&fs->value);
  131. varbuf_add_buf(&fs->value, fs->valuestart, fs->valuelen);
  132. varbuf_end_str(&fs->value);
  133. fip->rcall(pkg_obj->pkg, pkg_obj->pkgbin, ps, fs->value.buf, fip);
  134. } else {
  135. struct arbitraryfield *arp, **larpp;
  136. if (fs->fieldlen < 2)
  137. parse_error(ps,
  138. _("user-defined field name `%.*s' too short"),
  139. fs->fieldlen, fs->fieldstart);
  140. larpp = &pkg_obj->pkgbin->arbs;
  141. while ((arp = *larpp) != NULL) {
  142. if (strncasecmp(arp->name, fs->fieldstart, fs->fieldlen) == 0)
  143. parse_error(ps,
  144. _("duplicate value for user-defined field `%.*s'"),
  145. fs->fieldlen, fs->fieldstart);
  146. larpp = &arp->next;
  147. }
  148. arp = nfmalloc(sizeof(struct arbitraryfield));
  149. arp->name = nfstrnsave(fs->fieldstart, fs->fieldlen);
  150. arp->value = nfstrnsave(fs->valuestart, fs->valuelen);
  151. arp->next = NULL;
  152. *larpp = arp;
  153. }
  154. }
  155. /**
  156. * Verify and fixup the package structure being constructed.
  157. */
  158. static void
  159. pkg_parse_verify(struct parsedb_state *ps,
  160. struct pkginfo *pkg, struct pkgbin *pkgbin)
  161. {
  162. struct dependency *dep;
  163. struct deppossi *dop;
  164. parse_must_have_field(ps, pkg->set->name, "package name");
  165. /* XXX: We need to check for status != stat_halfinstalled as while
  166. * unpacking an unselected package, it will not have yet all data in
  167. * place. But we cannot check for > stat_halfinstalled as stat_configfiles
  168. * always should have those fields. */
  169. if ((ps->flags & pdb_recordavailable) ||
  170. (pkg->status != stat_notinstalled &&
  171. pkg->status != stat_halfinstalled)) {
  172. parse_ensure_have_field(ps, &pkgbin->description, "description");
  173. parse_ensure_have_field(ps, &pkgbin->maintainer, "maintainer");
  174. parse_must_have_field(ps, pkgbin->version.version, "version");
  175. }
  176. /* XXX: Versions before dpkg 1.10.19 did not preserve the Architecture
  177. * field in the status file. So there's still live systems with packages
  178. * in stat_configfiles, ignore those too for now. */
  179. if ((ps->flags & pdb_recordavailable) ||
  180. pkg->status > stat_halfinstalled) {
  181. /* We always want usable architecture information (as long as the package
  182. * is in such a state that it make sense), so that it can be used safely
  183. * on string comparisons and the like. */
  184. if (pkgbin->arch->type == DPKG_ARCH_NONE)
  185. parse_warn(ps, _("missing %s"), "architecture");
  186. else if (pkgbin->arch->type == DPKG_ARCH_EMPTY)
  187. parse_warn(ps, _("empty value for %s"), "architecture");
  188. }
  189. /* Mark missing architectures as empty, to distinguish these from
  190. * unused slots in the db. */
  191. if (pkgbin->arch->type == DPKG_ARCH_NONE)
  192. pkgbin->arch = dpkg_arch_get(DPKG_ARCH_EMPTY);
  193. if (pkgbin->arch->type == DPKG_ARCH_EMPTY &&
  194. pkgbin->multiarch == PKG_MULTIARCH_SAME)
  195. parse_error(ps, _("package has field '%s' but is missing architecture"),
  196. "Multi-Arch: same");
  197. if (pkgbin->arch->type == DPKG_ARCH_ALL &&
  198. pkgbin->multiarch == PKG_MULTIARCH_SAME)
  199. parse_error(ps, _("package has field '%s' but is architecture all"),
  200. "Multi-Arch: same");
  201. /* Initialize deps to be arch-specific unless stated otherwise. */
  202. for (dep = pkgbin->depends; dep; dep = dep->next)
  203. for (dop = dep->list; dop; dop = dop->next)
  204. if (!dop->arch)
  205. dop->arch = pkgbin->arch;
  206. /* Check the Config-Version information:
  207. * If there is a Config-Version it is definitely to be used, but
  208. * there shouldn't be one if the package is ‘installed’ (in which case
  209. * the Version and/or Revision will be copied) or if the package is
  210. * ‘not-installed’ (in which case there is no Config-Version). */
  211. if (!(ps->flags & pdb_recordavailable)) {
  212. if (pkg->configversion.version) {
  213. if (pkg->status == stat_installed || pkg->status == stat_notinstalled)
  214. parse_error(ps,
  215. _("Config-Version for package with inappropriate Status"));
  216. } else {
  217. if (pkg->status == stat_installed)
  218. pkg->configversion = pkgbin->version;
  219. }
  220. }
  221. if (pkg->trigaw.head &&
  222. (pkg->status <= stat_configfiles ||
  223. pkg->status >= stat_triggerspending))
  224. parse_error(ps,
  225. _("package has status %s but triggers are awaited"),
  226. pkg_status_name(pkg));
  227. else if (pkg->status == stat_triggersawaited && !pkg->trigaw.head)
  228. parse_error(ps,
  229. _("package has status triggers-awaited but no triggers awaited"));
  230. if (pkg->trigpend_head &&
  231. !(pkg->status == stat_triggerspending ||
  232. pkg->status == stat_triggersawaited))
  233. parse_error(ps,
  234. _("package has status %s but triggers are pending"),
  235. pkg_status_name(pkg));
  236. else if (pkg->status == stat_triggerspending && !pkg->trigpend_head)
  237. parse_error(ps,
  238. _("package has status triggers-pending but no triggers "
  239. "pending"));
  240. /* FIXME: There was a bug that could make a not-installed package have
  241. * conffiles, so we check for them here and remove them (rather than
  242. * calling it an error, which will do at some point). */
  243. if (!(ps->flags & pdb_recordavailable) &&
  244. pkg->status == stat_notinstalled &&
  245. pkgbin->conffiles) {
  246. parse_warn(ps,
  247. _("Package which in state not-installed has conffiles, "
  248. "forgetting them"));
  249. pkgbin->conffiles = NULL;
  250. }
  251. /* XXX: Mark not-installed leftover packages for automatic removal on
  252. * next database dump. This code can be removed after dpkg 1.16.x, when
  253. * there's guarantee that no leftover is found on the status file on
  254. * major distributions. */
  255. if (!(ps->flags & pdb_recordavailable) &&
  256. pkg->status == stat_notinstalled &&
  257. pkg->eflag == eflag_ok &&
  258. (pkg->want == want_purge ||
  259. pkg->want == want_deinstall ||
  260. pkg->want == want_hold)) {
  261. pkg_set_want(pkg, want_unknown);
  262. }
  263. /* XXX: Mark not-installed non-arch-qualified selections for automatic
  264. * removal, as they do not make sense in a multiarch enabled world, and
  265. * might cause those selections to be unreferencable from command-line
  266. * interfaces when there's other more specific selections. */
  267. if (ps->type == pdb_file_status &&
  268. pkg->status == stat_notinstalled &&
  269. pkg->eflag == eflag_ok &&
  270. pkg->want == want_install &&
  271. pkgbin->arch->type == DPKG_ARCH_EMPTY)
  272. pkg->want = want_unknown;
  273. }
  274. struct pkgcount {
  275. int single;
  276. int multi;
  277. int total;
  278. };
  279. static void
  280. parse_count_pkg_instance(struct pkgcount *count,
  281. struct pkginfo *pkg, struct pkgbin *pkgbin)
  282. {
  283. if (pkg->status == stat_notinstalled)
  284. return;
  285. if (pkgbin->multiarch == PKG_MULTIARCH_SAME)
  286. count->multi++;
  287. else
  288. count->single++;
  289. count->total++;
  290. }
  291. /**
  292. * Lookup the package set slot for the parsed package.
  293. *
  294. * Perform various checks, to make sure the database is always in a sane
  295. * state, and to not allow breaking it.
  296. */
  297. static struct pkgset *
  298. parse_find_set_slot(struct parsedb_state *ps,
  299. struct pkginfo *new_pkg, struct pkgbin *new_pkgbin)
  300. {
  301. struct pkgcount count = { .single = 0, .multi = 0, .total = 0 };
  302. struct pkgset *set;
  303. struct pkginfo *pkg;
  304. set = pkg_db_find_set(new_pkg->set->name);
  305. /* Sanity checks: verify that the db is in a consistent state. */
  306. if (ps->type == pdb_file_status)
  307. parse_count_pkg_instance(&count, new_pkg, new_pkgbin);
  308. count.total = 0;
  309. for (pkg = &set->pkg; pkg; pkg = pkg->arch_next)
  310. parse_count_pkg_instance(&count, pkg, &pkg->installed);
  311. if (count.single > 1)
  312. parse_error(ps, _("multiple non-coinstallable package instances present; "
  313. "most probably due to an upgrade from an unofficial dpkg"));
  314. if (count.single > 0 && count.multi > 0)
  315. parse_error(ps, _("mixed non-coinstallable and coinstallable package "
  316. "instances present; most probably due to an upgrade "
  317. "from an unofficial dpkg"));
  318. if (pkgset_installed_instances(set) != count.total)
  319. internerr("in-core pkgset '%s' with inconsistent number of instances",
  320. set->name);
  321. return set;
  322. }
  323. /**
  324. * Lookup the package slot for the parsed package.
  325. *
  326. * Cross-grading (i.e. switching arch) is only possible when parsing an
  327. * update entry or when installing a new package.
  328. *
  329. * Most of the time each pkginfo in a pkgset has the same architecture for
  330. * both the installed and available pkgbin members. But when cross-grading
  331. * there's going to be a temporary discrepancy, because we reuse the single
  332. * instance and fill the available pkgbin with the candidate pkgbin, until
  333. * that is copied over the installed pkgbin.
  334. *
  335. * If there's 0 or > 1 package instances, then we match against the pkginfo
  336. * slot architecture, because cross-grading is just not possible.
  337. *
  338. * If there's 1 instance, we are cross-grading and both installed and
  339. * candidate are not PKG_MULTIARCH_SAME, we have to reuse the existing single
  340. * slot regardless of the arch differing between the two. If we are not
  341. * cross-grading, then we use the entry with the matching arch.
  342. */
  343. static struct pkginfo *
  344. parse_find_pkg_slot(struct parsedb_state *ps,
  345. struct pkginfo *new_pkg, struct pkgbin *new_pkgbin)
  346. {
  347. struct pkgset *db_set;
  348. struct pkginfo *db_pkg;
  349. db_set = parse_find_set_slot(ps, new_pkg, new_pkgbin);
  350. if (ps->type == pdb_file_available) {
  351. /* If there's a single package installed and the new package is not
  352. * “Multi-Arch: same”, then we preserve the previous behaviour of
  353. * possible architecture switch, for example from native to all. */
  354. if (pkgset_installed_instances(db_set) == 1 &&
  355. new_pkgbin->multiarch != PKG_MULTIARCH_SAME)
  356. return pkg_db_get_singleton(db_set);
  357. else
  358. return pkg_db_get_pkg(db_set, new_pkgbin->arch);
  359. } else {
  360. bool selection = false;
  361. /* If the package is part of the status file, and it's not installed
  362. * then this means it's just a selection. */
  363. if (ps->type == pdb_file_status && new_pkg->status == stat_notinstalled)
  364. selection = true;
  365. /* Verify we don't allow something that will mess up the db. */
  366. if (pkgset_installed_instances(db_set) > 1 &&
  367. !selection && new_pkgbin->multiarch != PKG_MULTIARCH_SAME)
  368. ohshit(_("%s %s (Multi-Arch: %s) is not co-installable with "
  369. "%s which has multiple installed instances"),
  370. pkgbin_name(new_pkg, new_pkgbin, pnaw_always),
  371. versiondescribe(&new_pkgbin->version, vdew_nonambig),
  372. multiarchinfos[new_pkgbin->multiarch].name, db_set->name);
  373. /* If we are parsing the status file, use a slot per arch. */
  374. if (ps->type == pdb_file_status)
  375. return pkg_db_get_pkg(db_set, new_pkgbin->arch);
  376. /* If we are doing an update, from the log or a new package, then
  377. * handle cross-grades. */
  378. if (pkgset_installed_instances(db_set) == 1) {
  379. db_pkg = pkg_db_get_singleton(db_set);
  380. if (db_pkg->installed.multiarch == PKG_MULTIARCH_SAME &&
  381. new_pkgbin->multiarch == PKG_MULTIARCH_SAME)
  382. return pkg_db_get_pkg(db_set, new_pkgbin->arch);
  383. else
  384. return db_pkg;
  385. } else {
  386. return pkg_db_get_pkg(db_set, new_pkgbin->arch);
  387. }
  388. }
  389. }
  390. /**
  391. * Copy into the in-core database the package being constructed.
  392. */
  393. static void
  394. pkg_parse_copy(struct parsedb_state *ps,
  395. struct pkginfo *dst_pkg, struct pkgbin *dst_pkgbin,
  396. struct pkginfo *src_pkg, struct pkgbin *src_pkgbin)
  397. {
  398. /* Copy the priority and section across, but don't overwrite existing
  399. * values if the pdb_weakclassification flag is set. */
  400. if (str_is_set(src_pkg->section) &&
  401. !((ps->flags & pdb_weakclassification) &&
  402. str_is_set(dst_pkg->section)))
  403. dst_pkg->section = src_pkg->section;
  404. if (src_pkg->priority != pri_unknown &&
  405. !((ps->flags & pdb_weakclassification) &&
  406. dst_pkg->priority != pri_unknown)) {
  407. dst_pkg->priority = src_pkg->priority;
  408. if (src_pkg->priority == pri_other)
  409. dst_pkg->otherpriority = src_pkg->otherpriority;
  410. }
  411. /* Sort out the dependency mess. */
  412. copy_dependency_links(dst_pkg, &dst_pkgbin->depends, src_pkgbin->depends,
  413. (ps->flags & pdb_recordavailable) ? true : false);
  414. /* Copy across data. */
  415. memcpy(dst_pkgbin, src_pkgbin, sizeof(struct pkgbin));
  416. if (!(ps->flags & pdb_recordavailable)) {
  417. struct trigaw *ta;
  418. pkg_set_want(dst_pkg, src_pkg->want);
  419. pkg_copy_eflags(dst_pkg, src_pkg);
  420. pkg_set_status(dst_pkg, src_pkg->status);
  421. dst_pkg->configversion = src_pkg->configversion;
  422. dst_pkg->files = NULL;
  423. dst_pkg->trigpend_head = src_pkg->trigpend_head;
  424. dst_pkg->trigaw = src_pkg->trigaw;
  425. for (ta = dst_pkg->trigaw.head; ta; ta = ta->sameaw.next) {
  426. assert(ta->aw == src_pkg);
  427. ta->aw = dst_pkg;
  428. /* ->othertrigaw_head is updated by trig_note_aw in *(pkg_db_find())
  429. * rather than in dst_pkg. */
  430. }
  431. } else if (!(ps->flags & pdb_ignorefiles)) {
  432. dst_pkg->files = src_pkg->files;
  433. }
  434. }
  435. /**
  436. * Return a descriptive parser type.
  437. */
  438. static enum parsedbtype
  439. parse_get_type(struct parsedb_state *ps, enum parsedbflags flags)
  440. {
  441. if (flags & pdb_recordavailable) {
  442. if (flags & pdb_single_stanza)
  443. return pdb_file_control;
  444. else
  445. return pdb_file_available;
  446. } else {
  447. if (flags & pdb_single_stanza)
  448. return pdb_file_update;
  449. else
  450. return pdb_file_status;
  451. }
  452. }
  453. /**
  454. * Create a new deb822 parser context.
  455. */
  456. struct parsedb_state *
  457. parsedb_new(const char *filename, int fd, enum parsedbflags flags)
  458. {
  459. struct parsedb_state *ps;
  460. ps = m_malloc(sizeof(*ps));
  461. ps->filename = filename;
  462. ps->type = parse_get_type(ps, flags);
  463. ps->flags = flags;
  464. ps->fd = fd;
  465. ps->lno = 0;
  466. ps->pkg = NULL;
  467. ps->pkgbin = NULL;
  468. return ps;
  469. }
  470. /**
  471. * Open a file for deb822 parsing.
  472. */
  473. struct parsedb_state *
  474. parsedb_open(const char *filename, enum parsedbflags flags)
  475. {
  476. struct parsedb_state *ps;
  477. int fd;
  478. /* Special case stdin handling. */
  479. if (flags & pdb_dash_is_stdin && strcmp(filename, "-") == 0)
  480. return parsedb_new(filename, STDIN_FILENO, flags);
  481. fd = open(filename, O_RDONLY);
  482. if (fd == -1)
  483. ohshite(_("failed to open package info file `%.255s' for reading"),
  484. filename);
  485. ps = parsedb_new(filename, fd, flags | pdb_close_fd);
  486. push_cleanup(cu_closefd, ~ehflag_normaltidy, NULL, 0, 1, &ps->fd);
  487. return ps;
  488. }
  489. /**
  490. * Load data for package deb822 style parsing.
  491. */
  492. void
  493. parsedb_load(struct parsedb_state *ps)
  494. {
  495. struct stat st;
  496. if (fstat(ps->fd, &st) == -1)
  497. ohshite(_("can't stat package info file `%.255s'"), ps->filename);
  498. if (S_ISFIFO(st.st_mode)) {
  499. struct varbuf buf = VARBUF_INIT;
  500. struct dpkg_error err;
  501. off_t size;
  502. size = fd_vbuf_copy(ps->fd, &buf, -1, &err);
  503. if (size < 0)
  504. ohshit(_("reading package info file '%s': %s"), ps->filename, err.str);
  505. varbuf_end_str(&buf);
  506. ps->dataptr = varbuf_detach(&buf);
  507. ps->endptr = ps->dataptr + size;
  508. } else if (st.st_size > 0) {
  509. #ifdef USE_MMAP
  510. ps->dataptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, ps->fd, 0);
  511. if (ps->dataptr == MAP_FAILED)
  512. ohshite(_("can't mmap package info file `%.255s'"), ps->filename);
  513. #else
  514. ps->dataptr = m_malloc(st.st_size);
  515. if (fd_read(ps->fd, ps->dataptr, st.st_size) < 0)
  516. ohshite(_("reading package info file '%.255s'"), ps->filename);
  517. #endif
  518. ps->endptr = ps->dataptr + st.st_size;
  519. } else {
  520. ps->dataptr = ps->endptr = NULL;
  521. }
  522. ps->data = ps->dataptr;
  523. }
  524. /**
  525. * Parse an RFC-822 style stanza.
  526. */
  527. bool
  528. parse_stanza(struct parsedb_state *ps, struct field_state *fs,
  529. parse_field_func *parse_field, void *parse_obj)
  530. {
  531. int c;
  532. /* Skip adjacent new lines. */
  533. while (!parse_EOF(ps)) {
  534. c = parse_getc(ps);
  535. if (c != '\n' && c != MSDOS_EOF_CHAR)
  536. break;
  537. ps->lno++;
  538. }
  539. /* Nothing relevant parsed, bail out. */
  540. if (parse_EOF(ps))
  541. return false;
  542. /* Loop per field. */
  543. for (;;) {
  544. bool blank_line;
  545. /* Scan field name. */
  546. fs->fieldstart = ps->dataptr - 1;
  547. while (!parse_EOF(ps) && !isspace(c) && c != ':' && c != MSDOS_EOF_CHAR)
  548. c = parse_getc(ps);
  549. fs->fieldlen = ps->dataptr - fs->fieldstart - 1;
  550. if (fs->fieldlen == 0)
  551. parse_error(ps, _("empty field name"));
  552. if (fs->fieldstart[0] == '-')
  553. parse_error(ps, _("field name '%.*s' cannot start with hyphen"),
  554. fs->fieldlen, fs->fieldstart);
  555. /* Skip spaces before ‘:’. */
  556. while (!parse_EOF(ps) && c != '\n' && isspace(c))
  557. c = parse_getc(ps);
  558. /* Validate ‘:’. */
  559. if (parse_EOF(ps))
  560. parse_error(ps,
  561. _("EOF after field name `%.*s'"), fs->fieldlen, fs->fieldstart);
  562. if (c == '\n')
  563. parse_error(ps,
  564. _("newline in field name `%.*s'"), fs->fieldlen, fs->fieldstart);
  565. if (c == MSDOS_EOF_CHAR)
  566. parse_error(ps,
  567. _("MSDOS EOF (^Z) in field name `%.*s'"),
  568. fs->fieldlen, fs->fieldstart);
  569. if (c != ':')
  570. parse_error(ps,
  571. _("field name `%.*s' must be followed by colon"),
  572. fs->fieldlen, fs->fieldstart);
  573. /* Skip space after ‘:’ but before value and EOL. */
  574. while (!parse_EOF(ps)) {
  575. c = parse_getc(ps);
  576. if (c == '\n' || !isspace(c))
  577. break;
  578. }
  579. if (parse_EOF(ps))
  580. parse_error(ps,
  581. _("EOF before value of field `%.*s' (missing final newline)"),
  582. fs->fieldlen, fs->fieldstart);
  583. if (c == MSDOS_EOF_CHAR)
  584. parse_error(ps,
  585. _("MSDOS EOF char in value of field `%.*s' (missing newline?)"),
  586. fs->fieldlen, fs->fieldstart);
  587. blank_line = false;
  588. /* Scan field value. */
  589. fs->valuestart = ps->dataptr - 1;
  590. for (;;) {
  591. if (c == '\n' || c == MSDOS_EOF_CHAR) {
  592. if (blank_line)
  593. parse_error(ps,
  594. _("blank line in value of field '%.*s'"),
  595. fs->fieldlen, fs->fieldstart);
  596. ps->lno++;
  597. if (parse_EOF(ps))
  598. break;
  599. c = parse_getc(ps);
  600. /* Found double EOL, or start of new field. */
  601. if (parse_EOF(ps) || c == '\n' || !isspace(c))
  602. break;
  603. parse_ungetc(c, ps);
  604. blank_line = true;
  605. } else if (blank_line && !isspace(c)) {
  606. blank_line = false;
  607. }
  608. if (parse_EOF(ps))
  609. parse_error(ps,
  610. _("EOF during value of field `%.*s' (missing final newline)"),
  611. fs->fieldlen, fs->fieldstart);
  612. c = parse_getc(ps);
  613. }
  614. fs->valuelen = ps->dataptr - fs->valuestart - 1;
  615. /* Trim ending space on value. */
  616. while (fs->valuelen && isspace(*(fs->valuestart + fs->valuelen - 1)))
  617. fs->valuelen--;
  618. parse_field(ps, fs, parse_obj);
  619. if (parse_EOF(ps) || c == '\n' || c == MSDOS_EOF_CHAR)
  620. break;
  621. } /* Loop per field. */
  622. if (c == '\n')
  623. ps->lno++;
  624. return true;
  625. }
  626. /**
  627. * Teardown a package deb822 parser context.
  628. */
  629. void
  630. parsedb_close(struct parsedb_state *ps)
  631. {
  632. if (ps->flags & pdb_close_fd) {
  633. pop_cleanup(ehflag_normaltidy);
  634. if (close(ps->fd))
  635. ohshite(_("failed to close after read: `%.255s'"), ps->filename);
  636. }
  637. if (ps->data != NULL) {
  638. #ifdef USE_MMAP
  639. munmap(ps->data, ps->endptr - ps->data);
  640. #else
  641. free(ps->data);
  642. #endif
  643. }
  644. free(ps);
  645. }
  646. /**
  647. * Parse deb822 style package data from a buffer.
  648. *
  649. * donep may be NULL.
  650. * If donep is not NULL only one package's information is expected.
  651. */
  652. int
  653. parsedb_parse(struct parsedb_state *ps, struct pkginfo **donep)
  654. {
  655. struct pkgset tmp_set;
  656. struct pkginfo *new_pkg, *db_pkg;
  657. struct pkgbin *new_pkgbin, *db_pkgbin;
  658. struct pkg_parse_object pkg_obj;
  659. int fieldencountered[array_count(fieldinfos)];
  660. int pdone;
  661. struct field_state fs;
  662. memset(&fs, 0, sizeof(fs));
  663. fs.fieldencountered = fieldencountered;
  664. new_pkg = &tmp_set.pkg;
  665. if (ps->flags & pdb_recordavailable)
  666. new_pkgbin = &new_pkg->available;
  667. else
  668. new_pkgbin = &new_pkg->installed;
  669. ps->pkg = new_pkg;
  670. ps->pkgbin = new_pkgbin;
  671. pkg_obj.pkg = new_pkg;
  672. pkg_obj.pkgbin = new_pkgbin;
  673. pdone= 0;
  674. /* Loop per package. */
  675. for (;;) {
  676. memset(fieldencountered, 0, sizeof(fieldencountered));
  677. pkgset_blank(&tmp_set);
  678. if (!parse_stanza(ps, &fs, pkg_parse_field, &pkg_obj))
  679. break;
  680. if (pdone && donep)
  681. parse_error(ps,
  682. _("several package info entries found, only one allowed"));
  683. pkg_parse_verify(ps, new_pkg, new_pkgbin);
  684. db_pkg = parse_find_pkg_slot(ps, new_pkg, new_pkgbin);
  685. if (ps->flags & pdb_recordavailable)
  686. db_pkgbin = &db_pkg->available;
  687. else
  688. db_pkgbin = &db_pkg->installed;
  689. if (((ps->flags & pdb_ignoreolder) || ps->type == pdb_file_available) &&
  690. dpkg_version_is_informative(&db_pkgbin->version) &&
  691. dpkg_version_compare(&new_pkgbin->version, &db_pkgbin->version) < 0)
  692. continue;
  693. pkg_parse_copy(ps, db_pkg, db_pkgbin, new_pkg, new_pkgbin);
  694. if (donep)
  695. *donep = db_pkg;
  696. pdone++;
  697. if (parse_EOF(ps))
  698. break;
  699. }
  700. varbuf_destroy(&fs.value);
  701. if (donep && !pdone)
  702. ohshit(_("no package information in `%.255s'"), ps->filename);
  703. return pdone;
  704. }
  705. /**
  706. * Parse a deb822 style file.
  707. *
  708. * donep may be NULL.
  709. * If donep is not NULL only one package's information is expected.
  710. */
  711. int
  712. parsedb(const char *filename, enum parsedbflags flags, struct pkginfo **pkgp)
  713. {
  714. struct parsedb_state *ps;
  715. int count;
  716. ps = parsedb_open(filename, flags);
  717. parsedb_load(ps);
  718. count = parsedb_parse(ps, pkgp);
  719. parsedb_close(ps);
  720. return count;
  721. }
  722. /**
  723. * Copy dependency links structures.
  724. *
  725. * This routine is used to update the ‘reverse’ dependency pointers when
  726. * new ‘forwards’ information has been constructed. It first removes all
  727. * the links based on the old information. The old information starts in
  728. * *updateme; after much brou-ha-ha the reverse structures are created
  729. * and *updateme is set to the value from newdepends.
  730. *
  731. * @param pkg The package we're doing this for. This is used to construct
  732. * correct uplinks.
  733. * @param updateme The forwards dependency pointer that we are to update.
  734. * This starts out containing the old forwards info, which we use to
  735. * unthread the old reverse links. After we're done it is updated.
  736. * @param newdepends The value that we ultimately want to have in updateme.
  737. * @param available The pkgbin to modify, available or installed.
  738. *
  739. * It is likely that the backward pointer for the package in question
  740. * (‘depended’) will be updated by this routine, but this will happen by
  741. * the routine traversing the dependency data structures. It doesn't need
  742. * to be told where to update that; I just mention it as something that
  743. * one should be cautious about.
  744. */
  745. void copy_dependency_links(struct pkginfo *pkg,
  746. struct dependency **updateme,
  747. struct dependency *newdepends,
  748. bool available)
  749. {
  750. struct dependency *dyp;
  751. struct deppossi *dop, **revdeps;
  752. /* Delete ‘backward’ (‘depended’) links from other packages to
  753. * dependencies listed in old version of this one. We do this by
  754. * going through all the dependencies in the old version of this
  755. * one and following them down to find which deppossi nodes to
  756. * remove. */
  757. for (dyp= *updateme; dyp; dyp= dyp->next) {
  758. for (dop= dyp->list; dop; dop= dop->next) {
  759. if (dop->rev_prev)
  760. dop->rev_prev->rev_next = dop->rev_next;
  761. else
  762. if (available)
  763. dop->ed->depended.available = dop->rev_next;
  764. else
  765. dop->ed->depended.installed = dop->rev_next;
  766. if (dop->rev_next)
  767. dop->rev_next->rev_prev = dop->rev_prev;
  768. }
  769. }
  770. /* Now fill in new ‘ed’ links from other packages to dependencies
  771. * listed in new version of this one, and set our uplinks correctly. */
  772. for (dyp= newdepends; dyp; dyp= dyp->next) {
  773. dyp->up= pkg;
  774. for (dop= dyp->list; dop; dop= dop->next) {
  775. revdeps = available ? &dop->ed->depended.available :
  776. &dop->ed->depended.installed;
  777. dop->rev_next = *revdeps;
  778. dop->rev_prev = NULL;
  779. if (*revdeps)
  780. (*revdeps)->rev_prev = dop;
  781. *revdeps = dop;
  782. }
  783. }
  784. /* Finally, we fill in the new value. */
  785. *updateme= newdepends;
  786. }