parse.c 30 KB

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