parse.c 30 KB

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