parse.c 28 KB

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