parse.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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 != PKG_STAT_HALFINSTALLED as while
  166. * unpacking an unselected package, it will not have yet all data in
  167. * place. But we cannot check for > PKG_STAT_HALFINSTALLED as
  168. * PKG_STAT_CONFIGFILES always should have those fields. */
  169. if ((ps->flags & pdb_recordavailable) ||
  170. (pkg->status != PKG_STAT_NOTINSTALLED &&
  171. pkg->status != PKG_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 PKG_STAT_CONFIGFILES, ignore those too for now. */
  179. if ((ps->flags & pdb_recordavailable) ||
  180. pkg->status > PKG_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 == PKG_STAT_INSTALLED ||
  214. pkg->status == PKG_STAT_NOTINSTALLED)
  215. parse_error(ps,
  216. _("Config-Version for package with inappropriate Status"));
  217. } else {
  218. if (pkg->status == PKG_STAT_INSTALLED)
  219. pkg->configversion = pkgbin->version;
  220. }
  221. }
  222. if (pkg->trigaw.head &&
  223. (pkg->status <= PKG_STAT_CONFIGFILES ||
  224. pkg->status >= PKG_STAT_TRIGGERSPENDING))
  225. parse_error(ps,
  226. _("package has status %s but triggers are awaited"),
  227. pkg_status_name(pkg));
  228. else if (pkg->status == PKG_STAT_TRIGGERSAWAITED && !pkg->trigaw.head)
  229. parse_error(ps,
  230. _("package has status triggers-awaited but no triggers awaited"));
  231. if (pkg->trigpend_head &&
  232. !(pkg->status == PKG_STAT_TRIGGERSPENDING ||
  233. pkg->status == PKG_STAT_TRIGGERSAWAITED))
  234. parse_error(ps,
  235. _("package has status %s but triggers are pending"),
  236. pkg_status_name(pkg));
  237. else if (pkg->status == PKG_STAT_TRIGGERSPENDING && !pkg->trigpend_head)
  238. parse_error(ps,
  239. _("package has status triggers-pending but no triggers "
  240. "pending"));
  241. /* FIXME: There was a bug that could make a not-installed package have
  242. * conffiles, so we check for them here and remove them (rather than
  243. * calling it an error, which will do at some point). */
  244. if (!(ps->flags & pdb_recordavailable) &&
  245. pkg->status == PKG_STAT_NOTINSTALLED &&
  246. pkgbin->conffiles) {
  247. parse_warn(ps,
  248. _("Package which in state not-installed has conffiles, "
  249. "forgetting them"));
  250. pkgbin->conffiles = NULL;
  251. }
  252. /* XXX: Mark not-installed leftover packages for automatic removal on
  253. * next database dump. This code can be removed after dpkg 1.16.x, when
  254. * there's guarantee that no leftover is found on the status file on
  255. * major distributions. */
  256. if (!(ps->flags & pdb_recordavailable) &&
  257. pkg->status == PKG_STAT_NOTINSTALLED &&
  258. pkg->eflag == PKG_EFLAG_OK &&
  259. (pkg->want == PKG_WANT_PURGE ||
  260. pkg->want == PKG_WANT_DEINSTALL ||
  261. pkg->want == PKG_WANT_HOLD)) {
  262. pkg_set_want(pkg, PKG_WANT_UNKNOWN);
  263. }
  264. /* XXX: Mark not-installed non-arch-qualified selections for automatic
  265. * removal, as they do not make sense in a multiarch enabled world, and
  266. * might cause those selections to be unreferencable from command-line
  267. * interfaces when there's other more specific selections. */
  268. if (ps->type == pdb_file_status &&
  269. pkg->status == PKG_STAT_NOTINSTALLED &&
  270. pkg->eflag == PKG_EFLAG_OK &&
  271. pkg->want == PKG_WANT_INSTALL &&
  272. pkgbin->arch->type == DPKG_ARCH_EMPTY)
  273. pkg->want = PKG_WANT_UNKNOWN;
  274. }
  275. struct pkgcount {
  276. int single;
  277. int multi;
  278. int total;
  279. };
  280. static void
  281. parse_count_pkg_instance(struct pkgcount *count,
  282. struct pkginfo *pkg, struct pkgbin *pkgbin)
  283. {
  284. if (pkg->status == PKG_STAT_NOTINSTALLED)
  285. return;
  286. if (pkgbin->multiarch == PKG_MULTIARCH_SAME)
  287. count->multi++;
  288. else
  289. count->single++;
  290. count->total++;
  291. }
  292. /**
  293. * Lookup the package set slot for the parsed package.
  294. *
  295. * Perform various checks, to make sure the database is always in a sane
  296. * state, and to not allow breaking it.
  297. */
  298. static struct pkgset *
  299. parse_find_set_slot(struct parsedb_state *ps,
  300. struct pkginfo *new_pkg, struct pkgbin *new_pkgbin)
  301. {
  302. struct pkgcount count = { .single = 0, .multi = 0, .total = 0 };
  303. struct pkgset *set;
  304. struct pkginfo *pkg;
  305. set = pkg_db_find_set(new_pkg->set->name);
  306. /* Sanity checks: verify that the db is in a consistent state. */
  307. if (ps->type == pdb_file_status)
  308. parse_count_pkg_instance(&count, new_pkg, new_pkgbin);
  309. count.total = 0;
  310. for (pkg = &set->pkg; pkg; pkg = pkg->arch_next)
  311. parse_count_pkg_instance(&count, pkg, &pkg->installed);
  312. if (count.single > 1)
  313. parse_error(ps, _("multiple non-coinstallable package instances present; "
  314. "most probably due to an upgrade from an unofficial dpkg"));
  315. if (count.single > 0 && count.multi > 0)
  316. parse_error(ps, _("mixed non-coinstallable and coinstallable package "
  317. "instances present; most probably due to an upgrade "
  318. "from an unofficial dpkg"));
  319. if (pkgset_installed_instances(set) != count.total)
  320. internerr("in-core pkgset '%s' with inconsistent number of instances",
  321. set->name);
  322. return set;
  323. }
  324. /**
  325. * Lookup the package slot for the parsed package.
  326. *
  327. * Cross-grading (i.e. switching arch) is only possible when parsing an
  328. * update entry or when installing a new package.
  329. *
  330. * Most of the time each pkginfo in a pkgset has the same architecture for
  331. * both the installed and available pkgbin members. But when cross-grading
  332. * there's going to be a temporary discrepancy, because we reuse the single
  333. * instance and fill the available pkgbin with the candidate pkgbin, until
  334. * that is copied over the installed pkgbin.
  335. *
  336. * If there's 0 or > 1 package instances, then we match against the pkginfo
  337. * slot architecture, because cross-grading is just not possible.
  338. *
  339. * If there's 1 instance, we are cross-grading and both installed and
  340. * candidate are not PKG_MULTIARCH_SAME, we have to reuse the existing single
  341. * slot regardless of the arch differing between the two. If we are not
  342. * cross-grading, then we use the entry with the matching arch.
  343. */
  344. static struct pkginfo *
  345. parse_find_pkg_slot(struct parsedb_state *ps,
  346. struct pkginfo *new_pkg, struct pkgbin *new_pkgbin)
  347. {
  348. struct pkgset *db_set;
  349. struct pkginfo *db_pkg;
  350. db_set = parse_find_set_slot(ps, new_pkg, new_pkgbin);
  351. if (ps->type == pdb_file_available) {
  352. /* If there's a single package installed and the new package is not
  353. * “Multi-Arch: same”, then we preserve the previous behaviour of
  354. * possible architecture switch, for example from native to all. */
  355. if (pkgset_installed_instances(db_set) == 1 &&
  356. new_pkgbin->multiarch != PKG_MULTIARCH_SAME)
  357. return pkg_db_get_singleton(db_set);
  358. else
  359. return pkg_db_get_pkg(db_set, new_pkgbin->arch);
  360. } else {
  361. bool selection = false;
  362. /* If the package is part of the status file, and it's not installed
  363. * then this means it's just a selection. */
  364. if (ps->type == pdb_file_status && new_pkg->status == PKG_STAT_NOTINSTALLED)
  365. selection = true;
  366. /* Verify we don't allow something that will mess up the db. */
  367. if (pkgset_installed_instances(db_set) > 1 &&
  368. !selection && new_pkgbin->multiarch != PKG_MULTIARCH_SAME)
  369. ohshit(_("%s %s (Multi-Arch: %s) is not co-installable with "
  370. "%s which has multiple installed instances"),
  371. pkgbin_name(new_pkg, new_pkgbin, pnaw_always),
  372. versiondescribe(&new_pkgbin->version, vdew_nonambig),
  373. multiarchinfos[new_pkgbin->multiarch].name, db_set->name);
  374. /* If we are parsing the status file, use a slot per arch. */
  375. if (ps->type == pdb_file_status)
  376. return pkg_db_get_pkg(db_set, new_pkgbin->arch);
  377. /* If we are doing an update, from the log or a new package, then
  378. * handle cross-grades. */
  379. if (pkgset_installed_instances(db_set) == 1) {
  380. db_pkg = pkg_db_get_singleton(db_set);
  381. if (db_pkg->installed.multiarch == PKG_MULTIARCH_SAME &&
  382. new_pkgbin->multiarch == PKG_MULTIARCH_SAME)
  383. return pkg_db_get_pkg(db_set, new_pkgbin->arch);
  384. else
  385. return db_pkg;
  386. } else {
  387. return pkg_db_get_pkg(db_set, new_pkgbin->arch);
  388. }
  389. }
  390. }
  391. /**
  392. * Copy into the in-core database the package being constructed.
  393. */
  394. static void
  395. pkg_parse_copy(struct parsedb_state *ps,
  396. struct pkginfo *dst_pkg, struct pkgbin *dst_pkgbin,
  397. struct pkginfo *src_pkg, struct pkgbin *src_pkgbin)
  398. {
  399. /* Copy the priority and section across, but don't overwrite existing
  400. * values if the pdb_weakclassification flag is set. */
  401. if (str_is_set(src_pkg->section) &&
  402. !((ps->flags & pdb_weakclassification) &&
  403. str_is_set(dst_pkg->section)))
  404. dst_pkg->section = src_pkg->section;
  405. if (src_pkg->priority != PKG_PRIO_UNKNOWN &&
  406. !((ps->flags & pdb_weakclassification) &&
  407. dst_pkg->priority != PKG_PRIO_UNKNOWN)) {
  408. dst_pkg->priority = src_pkg->priority;
  409. if (src_pkg->priority == PKG_PRIO_OTHER)
  410. dst_pkg->otherpriority = src_pkg->otherpriority;
  411. }
  412. /* Sort out the dependency mess. */
  413. copy_dependency_links(dst_pkg, &dst_pkgbin->depends, src_pkgbin->depends,
  414. (ps->flags & pdb_recordavailable) ? true : false);
  415. /* Copy across data. */
  416. memcpy(dst_pkgbin, src_pkgbin, sizeof(struct pkgbin));
  417. if (!(ps->flags & pdb_recordavailable)) {
  418. struct trigaw *ta;
  419. pkg_set_want(dst_pkg, src_pkg->want);
  420. pkg_copy_eflags(dst_pkg, src_pkg);
  421. pkg_set_status(dst_pkg, src_pkg->status);
  422. dst_pkg->configversion = src_pkg->configversion;
  423. dst_pkg->files = NULL;
  424. dst_pkg->trigpend_head = src_pkg->trigpend_head;
  425. dst_pkg->trigaw = src_pkg->trigaw;
  426. for (ta = dst_pkg->trigaw.head; ta; ta = ta->sameaw.next) {
  427. assert(ta->aw == src_pkg);
  428. ta->aw = dst_pkg;
  429. /* ->othertrigaw_head is updated by trig_note_aw in *(pkg_db_find())
  430. * rather than in dst_pkg. */
  431. }
  432. } else if (!(ps->flags & pdb_ignorefiles)) {
  433. dst_pkg->files = src_pkg->files;
  434. }
  435. }
  436. /**
  437. * Return a descriptive parser type.
  438. */
  439. static enum parsedbtype
  440. parse_get_type(struct parsedb_state *ps, enum parsedbflags flags)
  441. {
  442. if (flags & pdb_recordavailable) {
  443. if (flags & pdb_single_stanza)
  444. return pdb_file_control;
  445. else
  446. return pdb_file_available;
  447. } else {
  448. if (flags & pdb_single_stanza)
  449. return pdb_file_update;
  450. else
  451. return pdb_file_status;
  452. }
  453. }
  454. /**
  455. * Create a new deb822 parser context.
  456. */
  457. struct parsedb_state *
  458. parsedb_new(const char *filename, int fd, enum parsedbflags flags)
  459. {
  460. struct parsedb_state *ps;
  461. ps = m_malloc(sizeof(*ps));
  462. ps->filename = filename;
  463. ps->type = parse_get_type(ps, flags);
  464. ps->flags = flags;
  465. ps->fd = fd;
  466. ps->lno = 0;
  467. ps->pkg = NULL;
  468. ps->pkgbin = NULL;
  469. return ps;
  470. }
  471. /**
  472. * Open a file for deb822 parsing.
  473. */
  474. struct parsedb_state *
  475. parsedb_open(const char *filename, enum parsedbflags flags)
  476. {
  477. struct parsedb_state *ps;
  478. int fd;
  479. /* Special case stdin handling. */
  480. if (flags & pdb_dash_is_stdin && strcmp(filename, "-") == 0)
  481. return parsedb_new(filename, STDIN_FILENO, flags);
  482. fd = open(filename, O_RDONLY);
  483. if (fd == -1)
  484. ohshite(_("failed to open package info file `%.255s' for reading"),
  485. filename);
  486. ps = parsedb_new(filename, fd, flags | pdb_close_fd);
  487. push_cleanup(cu_closefd, ~ehflag_normaltidy, NULL, 0, 1, &ps->fd);
  488. return ps;
  489. }
  490. /**
  491. * Load data for package deb822 style parsing.
  492. */
  493. void
  494. parsedb_load(struct parsedb_state *ps)
  495. {
  496. struct stat st;
  497. if (fstat(ps->fd, &st) == -1)
  498. ohshite(_("can't stat package info file `%.255s'"), ps->filename);
  499. if (S_ISFIFO(st.st_mode)) {
  500. struct varbuf buf = VARBUF_INIT;
  501. struct dpkg_error err;
  502. off_t size;
  503. size = fd_vbuf_copy(ps->fd, &buf, -1, &err);
  504. if (size < 0)
  505. ohshit(_("reading package info file '%s': %s"), ps->filename, err.str);
  506. varbuf_end_str(&buf);
  507. ps->dataptr = varbuf_detach(&buf);
  508. ps->endptr = ps->dataptr + size;
  509. } else if (st.st_size > 0) {
  510. #ifdef USE_MMAP
  511. ps->dataptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, ps->fd, 0);
  512. if (ps->dataptr == MAP_FAILED)
  513. ohshite(_("can't mmap package info file `%.255s'"), ps->filename);
  514. #else
  515. ps->dataptr = m_malloc(st.st_size);
  516. if (fd_read(ps->fd, ps->dataptr, st.st_size) < 0)
  517. ohshite(_("reading package info file '%.255s'"), ps->filename);
  518. #endif
  519. ps->endptr = ps->dataptr + st.st_size;
  520. } else {
  521. ps->dataptr = ps->endptr = NULL;
  522. }
  523. ps->data = ps->dataptr;
  524. }
  525. /**
  526. * Parse an RFC-822 style stanza.
  527. */
  528. bool
  529. parse_stanza(struct parsedb_state *ps, struct field_state *fs,
  530. parse_field_func *parse_field, void *parse_obj)
  531. {
  532. int c;
  533. /* Skip adjacent new lines. */
  534. while (!parse_EOF(ps)) {
  535. c = parse_getc(ps);
  536. if (c != '\n' && c != MSDOS_EOF_CHAR)
  537. break;
  538. ps->lno++;
  539. }
  540. /* Nothing relevant parsed, bail out. */
  541. if (parse_EOF(ps))
  542. return false;
  543. /* Loop per field. */
  544. for (;;) {
  545. bool blank_line;
  546. /* Scan field name. */
  547. fs->fieldstart = ps->dataptr - 1;
  548. while (!parse_EOF(ps) && !isspace(c) && c != ':' && c != MSDOS_EOF_CHAR)
  549. c = parse_getc(ps);
  550. fs->fieldlen = ps->dataptr - fs->fieldstart - 1;
  551. if (fs->fieldlen == 0)
  552. parse_error(ps, _("empty field name"));
  553. if (fs->fieldstart[0] == '-')
  554. parse_error(ps, _("field name '%.*s' cannot start with hyphen"),
  555. fs->fieldlen, fs->fieldstart);
  556. /* Skip spaces before ‘:’. */
  557. while (!parse_EOF(ps) && c != '\n' && isspace(c))
  558. c = parse_getc(ps);
  559. /* Validate ‘:’. */
  560. if (parse_EOF(ps))
  561. parse_error(ps,
  562. _("EOF after field name `%.*s'"), fs->fieldlen, fs->fieldstart);
  563. if (c == '\n')
  564. parse_error(ps,
  565. _("newline in field name `%.*s'"), fs->fieldlen, fs->fieldstart);
  566. if (c == MSDOS_EOF_CHAR)
  567. parse_error(ps,
  568. _("MSDOS EOF (^Z) in field name `%.*s'"),
  569. fs->fieldlen, fs->fieldstart);
  570. if (c != ':')
  571. parse_error(ps,
  572. _("field name `%.*s' must be followed by colon"),
  573. fs->fieldlen, fs->fieldstart);
  574. /* Skip space after ‘:’ but before value and EOL. */
  575. while (!parse_EOF(ps)) {
  576. c = parse_getc(ps);
  577. if (c == '\n' || !isspace(c))
  578. break;
  579. }
  580. if (parse_EOF(ps))
  581. parse_error(ps,
  582. _("EOF before value of field `%.*s' (missing final newline)"),
  583. fs->fieldlen, fs->fieldstart);
  584. if (c == MSDOS_EOF_CHAR)
  585. parse_error(ps,
  586. _("MSDOS EOF char in value of field `%.*s' (missing newline?)"),
  587. fs->fieldlen, fs->fieldstart);
  588. blank_line = false;
  589. /* Scan field value. */
  590. fs->valuestart = ps->dataptr - 1;
  591. for (;;) {
  592. if (c == '\n' || c == MSDOS_EOF_CHAR) {
  593. if (blank_line)
  594. parse_error(ps,
  595. _("blank line in value of field '%.*s'"),
  596. fs->fieldlen, fs->fieldstart);
  597. ps->lno++;
  598. if (parse_EOF(ps))
  599. break;
  600. c = parse_getc(ps);
  601. /* Found double EOL, or start of new field. */
  602. if (parse_EOF(ps) || c == '\n' || !isspace(c))
  603. break;
  604. parse_ungetc(c, ps);
  605. blank_line = true;
  606. } else if (blank_line && !isspace(c)) {
  607. blank_line = false;
  608. }
  609. if (parse_EOF(ps))
  610. parse_error(ps,
  611. _("EOF during value of field `%.*s' (missing final newline)"),
  612. fs->fieldlen, fs->fieldstart);
  613. c = parse_getc(ps);
  614. }
  615. fs->valuelen = ps->dataptr - fs->valuestart - 1;
  616. /* Trim ending space on value. */
  617. while (fs->valuelen && isspace(*(fs->valuestart + fs->valuelen - 1)))
  618. fs->valuelen--;
  619. parse_field(ps, fs, parse_obj);
  620. if (parse_EOF(ps) || c == '\n' || c == MSDOS_EOF_CHAR)
  621. break;
  622. } /* Loop per field. */
  623. if (c == '\n')
  624. ps->lno++;
  625. return true;
  626. }
  627. /**
  628. * Teardown a package deb822 parser context.
  629. */
  630. void
  631. parsedb_close(struct parsedb_state *ps)
  632. {
  633. if (ps->flags & pdb_close_fd) {
  634. pop_cleanup(ehflag_normaltidy);
  635. if (close(ps->fd))
  636. ohshite(_("failed to close after read: `%.255s'"), ps->filename);
  637. }
  638. if (ps->data != NULL) {
  639. #ifdef USE_MMAP
  640. munmap(ps->data, ps->endptr - ps->data);
  641. #else
  642. free(ps->data);
  643. #endif
  644. }
  645. free(ps);
  646. }
  647. /**
  648. * Parse deb822 style package data from a buffer.
  649. *
  650. * donep may be NULL.
  651. * If donep is not NULL only one package's information is expected.
  652. */
  653. int
  654. parsedb_parse(struct parsedb_state *ps, struct pkginfo **donep)
  655. {
  656. struct pkgset tmp_set;
  657. struct pkginfo *new_pkg, *db_pkg;
  658. struct pkgbin *new_pkgbin, *db_pkgbin;
  659. struct pkg_parse_object pkg_obj;
  660. int fieldencountered[array_count(fieldinfos)];
  661. int pdone;
  662. struct field_state fs;
  663. memset(&fs, 0, sizeof(fs));
  664. fs.fieldencountered = fieldencountered;
  665. new_pkg = &tmp_set.pkg;
  666. if (ps->flags & pdb_recordavailable)
  667. new_pkgbin = &new_pkg->available;
  668. else
  669. new_pkgbin = &new_pkg->installed;
  670. ps->pkg = new_pkg;
  671. ps->pkgbin = new_pkgbin;
  672. pkg_obj.pkg = new_pkg;
  673. pkg_obj.pkgbin = new_pkgbin;
  674. pdone= 0;
  675. /* Loop per package. */
  676. for (;;) {
  677. memset(fieldencountered, 0, sizeof(fieldencountered));
  678. pkgset_blank(&tmp_set);
  679. if (!parse_stanza(ps, &fs, pkg_parse_field, &pkg_obj))
  680. break;
  681. if (pdone && donep)
  682. parse_error(ps,
  683. _("several package info entries found, only one allowed"));
  684. pkg_parse_verify(ps, new_pkg, new_pkgbin);
  685. db_pkg = parse_find_pkg_slot(ps, new_pkg, new_pkgbin);
  686. if (ps->flags & pdb_recordavailable)
  687. db_pkgbin = &db_pkg->available;
  688. else
  689. db_pkgbin = &db_pkg->installed;
  690. if (((ps->flags & pdb_ignoreolder) || ps->type == pdb_file_available) &&
  691. dpkg_version_is_informative(&db_pkgbin->version) &&
  692. dpkg_version_compare(&new_pkgbin->version, &db_pkgbin->version) < 0)
  693. continue;
  694. pkg_parse_copy(ps, db_pkg, db_pkgbin, new_pkg, new_pkgbin);
  695. if (donep)
  696. *donep = db_pkg;
  697. pdone++;
  698. if (parse_EOF(ps))
  699. break;
  700. }
  701. varbuf_destroy(&fs.value);
  702. if (donep && !pdone)
  703. ohshit(_("no package information in `%.255s'"), ps->filename);
  704. return pdone;
  705. }
  706. /**
  707. * Parse a deb822 style file.
  708. *
  709. * donep may be NULL.
  710. * If donep is not NULL only one package's information is expected.
  711. */
  712. int
  713. parsedb(const char *filename, enum parsedbflags flags, struct pkginfo **pkgp)
  714. {
  715. struct parsedb_state *ps;
  716. int count;
  717. ps = parsedb_open(filename, flags);
  718. parsedb_load(ps);
  719. count = parsedb_parse(ps, pkgp);
  720. parsedb_close(ps);
  721. return count;
  722. }
  723. /**
  724. * Copy dependency links structures.
  725. *
  726. * This routine is used to update the ‘reverse’ dependency pointers when
  727. * new ‘forwards’ information has been constructed. It first removes all
  728. * the links based on the old information. The old information starts in
  729. * *updateme; after much brou-ha-ha the reverse structures are created
  730. * and *updateme is set to the value from newdepends.
  731. *
  732. * @param pkg The package we're doing this for. This is used to construct
  733. * correct uplinks.
  734. * @param updateme The forwards dependency pointer that we are to update.
  735. * This starts out containing the old forwards info, which we use to
  736. * unthread the old reverse links. After we're done it is updated.
  737. * @param newdepends The value that we ultimately want to have in updateme.
  738. * @param available The pkgbin to modify, available or installed.
  739. *
  740. * It is likely that the backward pointer for the package in question
  741. * (‘depended’) will be updated by this routine, but this will happen by
  742. * the routine traversing the dependency data structures. It doesn't need
  743. * to be told where to update that; I just mention it as something that
  744. * one should be cautious about.
  745. */
  746. void copy_dependency_links(struct pkginfo *pkg,
  747. struct dependency **updateme,
  748. struct dependency *newdepends,
  749. bool available)
  750. {
  751. struct dependency *dyp;
  752. struct deppossi *dop, **revdeps;
  753. /* Delete ‘backward’ (‘depended’) links from other packages to
  754. * dependencies listed in old version of this one. We do this by
  755. * going through all the dependencies in the old version of this
  756. * one and following them down to find which deppossi nodes to
  757. * remove. */
  758. for (dyp= *updateme; dyp; dyp= dyp->next) {
  759. for (dop= dyp->list; dop; dop= dop->next) {
  760. if (dop->rev_prev)
  761. dop->rev_prev->rev_next = dop->rev_next;
  762. else
  763. if (available)
  764. dop->ed->depended.available = dop->rev_next;
  765. else
  766. dop->ed->depended.installed = dop->rev_next;
  767. if (dop->rev_next)
  768. dop->rev_next->rev_prev = dop->rev_prev;
  769. }
  770. }
  771. /* Now fill in new ‘ed’ links from other packages to dependencies
  772. * listed in new version of this one, and set our uplinks correctly. */
  773. for (dyp= newdepends; dyp; dyp= dyp->next) {
  774. dyp->up= pkg;
  775. for (dop= dyp->list; dop; dop= dop->next) {
  776. revdeps = available ? &dop->ed->depended.available :
  777. &dop->ed->depended.installed;
  778. dop->rev_next = *revdeps;
  779. dop->rev_prev = NULL;
  780. if (*revdeps)
  781. (*revdeps)->rev_prev = dop;
  782. *revdeps = dop;
  783. }
  784. }
  785. /* Finally, we fill in the new value. */
  786. *updateme= newdepends;
  787. }