parse.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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-2011 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #ifdef USE_MMAP
  26. #include <sys/mman.h>
  27. #endif
  28. #include <assert.h>
  29. #include <fcntl.h>
  30. #include <ctype.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <stdarg.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <dpkg/macros.h>
  37. #include <dpkg/i18n.h>
  38. #include <dpkg/dpkg.h>
  39. #include <dpkg/dpkg-db.h>
  40. #include <dpkg/parsedump.h>
  41. #include <dpkg/fdio.h>
  42. /**
  43. * Fields information.
  44. */
  45. const struct fieldinfo fieldinfos[]= {
  46. /* Note: Capitalization of field name strings is important. */
  47. { "Package", f_name, w_name },
  48. { "Essential", f_boolean, w_booleandefno, PKGIFPOFF(essential) },
  49. { "Status", f_status, w_status },
  50. { "Multi-Arch", f_multiarch, w_multiarch, PKGIFPOFF(multiarch) },
  51. { "Priority", f_priority, w_priority },
  52. { "Section", f_section, w_section },
  53. { "Installed-Size", f_charfield, w_charfield, PKGIFPOFF(installedsize) },
  54. { "Origin", f_charfield, w_charfield, PKGIFPOFF(origin) },
  55. { "Maintainer", f_charfield, w_charfield, PKGIFPOFF(maintainer) },
  56. { "Bugs", f_charfield, w_charfield, PKGIFPOFF(bugs) },
  57. { "Architecture", f_architecture, w_architecture },
  58. { "Source", f_charfield, w_charfield, PKGIFPOFF(source) },
  59. { "Version", f_version, w_version, PKGIFPOFF(version) },
  60. { "Revision", f_revision, w_null },
  61. { "Config-Version", f_configversion, w_configversion },
  62. { "Replaces", f_dependency, w_dependency, dep_replaces },
  63. { "Provides", f_dependency, w_dependency, dep_provides },
  64. { "Depends", f_dependency, w_dependency, dep_depends },
  65. { "Pre-Depends", f_dependency, w_dependency, dep_predepends },
  66. { "Recommends", f_dependency, w_dependency, dep_recommends },
  67. { "Suggests", f_dependency, w_dependency, dep_suggests },
  68. { "Breaks", f_dependency, w_dependency, dep_breaks },
  69. { "Conflicts", f_dependency, w_dependency, dep_conflicts },
  70. { "Enhances", f_dependency, w_dependency, dep_enhances },
  71. { "Conffiles", f_conffiles, w_conffiles },
  72. { "Filename", f_filecharf, w_filecharf, FILEFOFF(name) },
  73. { "Size", f_filecharf, w_filecharf, FILEFOFF(size) },
  74. { "MD5sum", f_filecharf, w_filecharf, FILEFOFF(md5sum) },
  75. { "MSDOS-Filename", f_filecharf, w_filecharf, FILEFOFF(msdosname) },
  76. { "Description", f_charfield, w_charfield, PKGIFPOFF(description) },
  77. { "Triggers-Pending", f_trigpend, w_trigpend },
  78. { "Triggers-Awaited", f_trigaw, w_trigaw },
  79. /* Note that aliases are added to the nicknames table. */
  80. { NULL }
  81. };
  82. static const struct nickname nicknames[] = {
  83. /* Note: Capitalization of these strings is important. */
  84. { .nick = "Recommended", .canon = "Recommends" },
  85. { .nick = "Optional", .canon = "Suggests" },
  86. { .nick = "Class", .canon = "Priority" },
  87. { .nick = "Package-Revision", .canon = "Revision" },
  88. { .nick = "Package_Revision", .canon = "Revision" },
  89. { .nick = NULL }
  90. };
  91. /**
  92. * Package object being parsed.
  93. *
  94. * Structure used to hold the parsed data for the package being constructed,
  95. * before it gets properly inserted into the package database.
  96. */
  97. struct pkg_parse_object {
  98. struct pkginfo *pkg;
  99. struct pkgbin *pkgbin;
  100. };
  101. /**
  102. * Parse the field and value into the package being constructed.
  103. */
  104. static void
  105. pkg_parse_field(struct parsedb_state *ps, struct field_state *fs,
  106. void *parse_obj)
  107. {
  108. struct pkg_parse_object *pkg_obj = parse_obj;
  109. const struct nickname *nick;
  110. const struct fieldinfo *fip;
  111. int *ip;
  112. for (nick = nicknames; nick->nick; nick++)
  113. if (strncasecmp(nick->nick, fs->fieldstart, fs->fieldlen) == 0 &&
  114. nick->nick[fs->fieldlen] == '\0')
  115. break;
  116. if (nick->nick) {
  117. fs->fieldstart = nick->canon;
  118. fs->fieldlen = strlen(fs->fieldstart);
  119. }
  120. for (fip = fieldinfos, ip = fs->fieldencountered; fip->name; fip++, ip++)
  121. if (strncasecmp(fip->name, fs->fieldstart, fs->fieldlen) == 0)
  122. break;
  123. if (fip->name) {
  124. if ((*ip)++)
  125. parse_error(ps,
  126. _("duplicate value for `%s' field"), fip->name);
  127. varbuf_reset(&fs->value);
  128. varbuf_add_buf(&fs->value, fs->valuestart, fs->valuelen);
  129. varbuf_end_str(&fs->value);
  130. fip->rcall(pkg_obj->pkg, pkg_obj->pkgbin, ps, fs->value.buf, fip);
  131. } else {
  132. struct arbitraryfield *arp, **larpp;
  133. if (fs->fieldlen < 2)
  134. parse_error(ps,
  135. _("user-defined field name `%.*s' too short"),
  136. fs->fieldlen, fs->fieldstart);
  137. larpp = &pkg_obj->pkgbin->arbs;
  138. while ((arp = *larpp) != NULL) {
  139. if (!strncasecmp(arp->name, fs->fieldstart, fs->fieldlen))
  140. parse_error(ps,
  141. _("duplicate value for user-defined field `%.*s'"),
  142. fs->fieldlen, fs->fieldstart);
  143. larpp = &arp->next;
  144. }
  145. arp = nfmalloc(sizeof(struct arbitraryfield));
  146. arp->name = nfstrnsave(fs->fieldstart, fs->fieldlen);
  147. arp->value = nfstrnsave(fs->valuestart, fs->valuelen);
  148. arp->next = NULL;
  149. *larpp = arp;
  150. }
  151. }
  152. /**
  153. * Verify and fixup the package structure being constructed.
  154. */
  155. static void
  156. pkg_parse_verify(struct parsedb_state *ps,
  157. struct pkginfo *pkg, struct pkgbin *pkgbin)
  158. {
  159. struct dependency *dep;
  160. struct deppossi *dop;
  161. parse_must_have_field(ps, pkg->set->name, "package name");
  162. /* XXX: We need to check for status != stat_halfinstalled as while
  163. * unpacking an unselected package, it will not have yet all data in
  164. * place. But we cannot check for > stat_halfinstalled as stat_configfiles
  165. * always should have those fields. */
  166. if ((ps->flags & pdb_recordavailable) ||
  167. (pkg->status != stat_notinstalled &&
  168. pkg->status != stat_halfinstalled)) {
  169. parse_ensure_have_field(ps, &pkgbin->description, "description");
  170. parse_ensure_have_field(ps, &pkgbin->maintainer, "maintainer");
  171. parse_must_have_field(ps, pkgbin->version.version, "version");
  172. }
  173. /* XXX: Versions before dpkg 1.10.19 did not preserve the Architecture
  174. * field in the status file. So there's still live systems with packages
  175. * in stat_configfiles, ignore those too for now. */
  176. if ((ps->flags & pdb_recordavailable) ||
  177. pkg->status > stat_halfinstalled) {
  178. /* We always want usable architecture information (as long as the package
  179. * is in such a state that it make sense), so that it can be used safely
  180. * on string comparisons and the like. */
  181. if (pkgbin->arch == NULL)
  182. parse_warn(ps, _("missing %s"), "architecture");
  183. else if (pkgbin->arch->type == arch_none)
  184. parse_warn(ps, _("empty value for %s"), "architecture");
  185. }
  186. if (pkgbin->arch == NULL)
  187. pkgbin->arch = dpkg_arch_get(arch_none);
  188. if (pkgbin->arch->type == arch_all && pkgbin->multiarch == multiarch_same)
  189. parse_error(ps, _("package has field '%s' but is architecture all"),
  190. "Multi-Arch: same");
  191. /* Initialize deps to be arch-specific unless stated otherwise. */
  192. for (dep = pkgbin->depends; dep; dep = dep->next)
  193. for (dop = dep->list; dop; dop = dop->next)
  194. if (!dop->arch)
  195. dop->arch = pkgbin->arch;
  196. /* Check the Config-Version information:
  197. * If there is a Config-Version it is definitely to be used, but
  198. * there shouldn't be one if the package is ‘installed’ (in which case
  199. * the Version and/or Revision will be copied) or if the package is
  200. * ‘not-installed’ (in which case there is no Config-Version). */
  201. if (!(ps->flags & pdb_recordavailable)) {
  202. if (pkg->configversion.version) {
  203. if (pkg->status == stat_installed || pkg->status == stat_notinstalled)
  204. parse_error(ps,
  205. _("Configured-Version for package with inappropriate Status"));
  206. } else {
  207. if (pkg->status == stat_installed)
  208. pkg->configversion = pkgbin->version;
  209. }
  210. }
  211. if (pkg->trigaw.head &&
  212. (pkg->status <= stat_configfiles ||
  213. pkg->status >= stat_triggerspending))
  214. parse_error(ps,
  215. _("package has status %s but triggers are awaited"),
  216. statusinfos[pkg->status].name);
  217. else if (pkg->status == stat_triggersawaited && !pkg->trigaw.head)
  218. parse_error(ps,
  219. _("package has status triggers-awaited but no triggers awaited"));
  220. if (pkg->trigpend_head &&
  221. !(pkg->status == stat_triggerspending ||
  222. pkg->status == stat_triggersawaited))
  223. parse_error(ps,
  224. _("package has status %s but triggers are pending"),
  225. statusinfos[pkg->status].name);
  226. else if (pkg->status == stat_triggerspending && !pkg->trigpend_head)
  227. parse_error(ps,
  228. _("package has status triggers-pending but no triggers "
  229. "pending"));
  230. /* FIXME: There was a bug that could make a not-installed package have
  231. * conffiles, so we check for them here and remove them (rather than
  232. * calling it an error, which will do at some point). */
  233. if (!(ps->flags & pdb_recordavailable) &&
  234. pkg->status == stat_notinstalled &&
  235. pkgbin->conffiles) {
  236. parse_warn(ps,
  237. _("Package which in state not-installed has conffiles, "
  238. "forgetting them"));
  239. pkgbin->conffiles = NULL;
  240. }
  241. /* XXX: Mark not-installed leftover packages for automatic removal on
  242. * next database dump. This code can be removed after dpkg 1.16.x, when
  243. * there's guarantee that no leftover is found on the status file on
  244. * major distributions. */
  245. if (!(ps->flags & pdb_recordavailable) &&
  246. pkg->status == stat_notinstalled &&
  247. pkg->eflag == eflag_ok &&
  248. (pkg->want == want_purge ||
  249. pkg->want == want_deinstall ||
  250. pkg->want == want_hold)) {
  251. pkg->want = want_unknown;
  252. }
  253. }
  254. /**
  255. * Copy into the in-core database the package being constructed.
  256. */
  257. static void
  258. pkg_parse_copy(struct parsedb_state *ps,
  259. struct pkginfo *dst_pkg, struct pkgbin *dst_pkgbin,
  260. struct pkginfo *src_pkg, struct pkgbin *src_pkgbin)
  261. {
  262. /* Copy the priority and section across, but don't overwrite existing
  263. * values if the pdb_weakclassification flag is set. */
  264. if (src_pkg->section && *src_pkg->section &&
  265. !((ps->flags & pdb_weakclassification) &&
  266. dst_pkg->section && *dst_pkg->section))
  267. dst_pkg->section = src_pkg->section;
  268. if (src_pkg->priority != pri_unknown &&
  269. !((ps->flags & pdb_weakclassification) &&
  270. dst_pkg->priority != pri_unknown)) {
  271. dst_pkg->priority = src_pkg->priority;
  272. if (src_pkg->priority == pri_other)
  273. dst_pkg->otherpriority = src_pkg->otherpriority;
  274. }
  275. /* Sort out the dependency mess. */
  276. copy_dependency_links(dst_pkg, &dst_pkgbin->depends, src_pkgbin->depends,
  277. (ps->flags & pdb_recordavailable) ? true : false);
  278. /* Copy across data. */
  279. memcpy(dst_pkgbin, src_pkgbin, sizeof(struct pkgbin));
  280. if (!(ps->flags & pdb_recordavailable)) {
  281. struct trigaw *ta;
  282. dst_pkg->want = src_pkg->want;
  283. dst_pkg->eflag = src_pkg->eflag;
  284. dst_pkg->status = src_pkg->status;
  285. dst_pkg->configversion = src_pkg->configversion;
  286. dst_pkg->files = NULL;
  287. dst_pkg->trigpend_head = src_pkg->trigpend_head;
  288. dst_pkg->trigaw = src_pkg->trigaw;
  289. for (ta = dst_pkg->trigaw.head; ta; ta = ta->sameaw.next) {
  290. assert(ta->aw == src_pkg);
  291. ta->aw = dst_pkg;
  292. /* ->othertrigaw_head is updated by trig_note_aw in *(pkg_db_find())
  293. * rather than in dst_pkg. */
  294. }
  295. } else if (!(ps->flags & pdb_ignorefiles)) {
  296. dst_pkg->files = src_pkg->files;
  297. }
  298. }
  299. /**
  300. * Open a file for RFC-822 parsing.
  301. */
  302. void
  303. parse_open(struct parsedb_state *ps, const char *filename,
  304. enum parsedbflags flags)
  305. {
  306. static int fd;
  307. struct stat st;
  308. ps->filename = filename;
  309. ps->flags = flags;
  310. ps->lno = 0;
  311. ps->pkg = NULL;
  312. ps->pkgbin = NULL;
  313. fd = open(filename, O_RDONLY);
  314. if (fd == -1)
  315. ohshite(_("failed to open package info file `%.255s' for reading"),
  316. filename);
  317. push_cleanup(cu_closefd, ~ehflag_normaltidy, NULL, 0, 1, &fd);
  318. if (fstat(fd, &st) == -1)
  319. ohshite(_("can't stat package info file `%.255s'"), filename);
  320. if (st.st_size > 0) {
  321. #ifdef USE_MMAP
  322. ps->dataptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  323. if (ps->dataptr == MAP_FAILED)
  324. ohshite(_("can't mmap package info file `%.255s'"), filename);
  325. #else
  326. ps->dataptr = m_malloc(st.st_size);
  327. if (fd_read(fd, ps->dataptr, st.st_size) < 0)
  328. ohshite(_("reading package info file '%.255s'"), filename);
  329. #endif
  330. ps->data = ps->dataptr;
  331. ps->endptr = ps->dataptr + st.st_size;
  332. } else {
  333. ps->data = ps->dataptr = ps->endptr = NULL;
  334. }
  335. pop_cleanup(ehflag_normaltidy);
  336. if (close(fd))
  337. ohshite(_("failed to close after read: `%.255s'"), filename);
  338. }
  339. /**
  340. * Parse an RFC-822 style stanza.
  341. */
  342. bool
  343. parse_stanza(struct parsedb_state *ps, struct field_state *fs,
  344. parse_field_func *parse_field, void *parse_obj)
  345. {
  346. int c;
  347. /* Skip adjacent new lines. */
  348. while (!parse_EOF(ps)) {
  349. c = parse_getc(ps);
  350. if (c != '\n' && c != MSDOS_EOF_CHAR)
  351. break;
  352. ps->lno++;
  353. }
  354. /* Nothing relevant parsed, bail out. */
  355. if (parse_EOF(ps))
  356. return false;
  357. /* Loop per field. */
  358. for (;;) {
  359. bool blank_line;
  360. /* Scan field name. */
  361. fs->fieldstart = ps->dataptr - 1;
  362. while (!parse_EOF(ps) && !isspace(c) && c != ':' && c != MSDOS_EOF_CHAR)
  363. c = parse_getc(ps);
  364. fs->fieldlen = ps->dataptr - fs->fieldstart - 1;
  365. /* Skip spaces before ‘:’. */
  366. while (!parse_EOF(ps) && c != '\n' && isspace(c))
  367. c = parse_getc(ps);
  368. /* Validate ‘:’. */
  369. if (parse_EOF(ps))
  370. parse_error(ps,
  371. _("EOF after field name `%.*s'"), fs->fieldlen, fs->fieldstart);
  372. if (c == '\n')
  373. parse_error(ps,
  374. _("newline in field name `%.*s'"), fs->fieldlen, fs->fieldstart);
  375. if (c == MSDOS_EOF_CHAR)
  376. parse_error(ps,
  377. _("MSDOS EOF (^Z) in field name `%.*s'"),
  378. fs->fieldlen, fs->fieldstart);
  379. if (c != ':')
  380. parse_error(ps,
  381. _("field name `%.*s' must be followed by colon"),
  382. fs->fieldlen, fs->fieldstart);
  383. /* Skip space after ‘:’ but before value and EOL. */
  384. while (!parse_EOF(ps)) {
  385. c = parse_getc(ps);
  386. if (c == '\n' || !isspace(c))
  387. break;
  388. }
  389. if (parse_EOF(ps))
  390. parse_error(ps,
  391. _("EOF before value of field `%.*s' (missing final newline)"),
  392. fs->fieldlen, fs->fieldstart);
  393. if (c == MSDOS_EOF_CHAR)
  394. parse_error(ps,
  395. _("MSDOS EOF char in value of field `%.*s' (missing newline?)"),
  396. fs->fieldlen, fs->fieldstart);
  397. blank_line = false;
  398. /* Scan field value. */
  399. fs->valuestart = ps->dataptr - 1;
  400. for (;;) {
  401. if (c == '\n' || c == MSDOS_EOF_CHAR) {
  402. if (blank_line)
  403. parse_error(ps,
  404. _("blank line in value of field '%.*s'"),
  405. fs->fieldlen, fs->fieldstart);
  406. ps->lno++;
  407. if (parse_EOF(ps))
  408. break;
  409. c = parse_getc(ps);
  410. /* Found double EOL, or start of new field. */
  411. if (parse_EOF(ps) || c == '\n' || !isspace(c))
  412. break;
  413. parse_ungetc(c, ps);
  414. c = '\n';
  415. blank_line = true;
  416. } else if (blank_line && !isspace(c)) {
  417. blank_line = false;
  418. }
  419. if (parse_EOF(ps))
  420. parse_error(ps,
  421. _("EOF during value of field `%.*s' (missing final newline)"),
  422. fs->fieldlen, fs->fieldstart);
  423. c = parse_getc(ps);
  424. }
  425. fs->valuelen = ps->dataptr - fs->valuestart - 1;
  426. /* Trim ending space on value. */
  427. while (fs->valuelen && isspace(*(fs->valuestart + fs->valuelen - 1)))
  428. fs->valuelen--;
  429. parse_field(ps, fs, parse_obj);
  430. if (parse_EOF(ps) || c == '\n' || c == MSDOS_EOF_CHAR)
  431. break;
  432. } /* Loop per field. */
  433. if (c == '\n')
  434. ps->lno++;
  435. return true;
  436. }
  437. /**
  438. * Close an RFC-822 parser context.
  439. */
  440. void
  441. parse_close(struct parsedb_state *ps)
  442. {
  443. if (ps->data != NULL) {
  444. #ifdef USE_MMAP
  445. munmap(ps->data, ps->endptr - ps->data);
  446. #else
  447. free(ps->data);
  448. #endif
  449. }
  450. }
  451. /**
  452. * Parse an RFC-822 style file.
  453. *
  454. * donep may be NULL.
  455. * If donep is not NULL only one package's information is expected.
  456. */
  457. int parsedb(const char *filename, enum parsedbflags flags,
  458. struct pkginfo **donep)
  459. {
  460. struct pkgset tmp_set;
  461. struct pkginfo *new_pkg, *db_pkg;
  462. struct pkgbin *new_pkgbin, *db_pkgbin;
  463. struct pkg_parse_object pkg_obj;
  464. int fieldencountered[array_count(fieldinfos)];
  465. int pdone;
  466. struct parsedb_state ps;
  467. struct field_state fs;
  468. memset(&fs, 0, sizeof(fs));
  469. fs.fieldencountered = fieldencountered;
  470. parse_open(&ps, filename, flags);
  471. new_pkg = &tmp_set.pkg;
  472. if (flags & pdb_recordavailable)
  473. new_pkgbin = &new_pkg->available;
  474. else
  475. new_pkgbin = &new_pkg->installed;
  476. ps.pkg = new_pkg;
  477. ps.pkgbin = new_pkgbin;
  478. pkg_obj.pkg = new_pkg;
  479. pkg_obj.pkgbin = new_pkgbin;
  480. pdone= 0;
  481. /* Loop per package. */
  482. for (;;) {
  483. memset(fieldencountered, 0, sizeof(fieldencountered));
  484. pkgset_blank(&tmp_set);
  485. if (!parse_stanza(&ps, &fs, pkg_parse_field, &pkg_obj))
  486. break;
  487. if (pdone && donep)
  488. parse_error(&ps,
  489. _("several package info entries found, only one allowed"));
  490. pkg_parse_verify(&ps, new_pkg, new_pkgbin);
  491. db_pkg = pkg_db_find_pkg(new_pkg->set->name, new_pkgbin->arch);
  492. if (flags & pdb_recordavailable)
  493. db_pkgbin = &db_pkg->available;
  494. else
  495. db_pkgbin = &db_pkg->installed;
  496. if ((flags & pdb_ignoreolder) &&
  497. versioncompare(&new_pkgbin->version, &db_pkgbin->version) < 0)
  498. continue;
  499. pkg_parse_copy(&ps, db_pkg, db_pkgbin, new_pkg, new_pkgbin);
  500. if (donep)
  501. *donep = db_pkg;
  502. pdone++;
  503. if (parse_EOF(&ps))
  504. break;
  505. }
  506. parse_close(&ps);
  507. varbuf_destroy(&fs.value);
  508. if (donep && !pdone) ohshit(_("no package information in `%.255s'"),filename);
  509. return pdone;
  510. }
  511. /**
  512. * Copy dependency links structures.
  513. *
  514. * This routine is used to update the ‘reverse’ dependency pointers when
  515. * new ‘forwards’ information has been constructed. It first removes all
  516. * the links based on the old information. The old information starts in
  517. * *updateme; after much brou-ha-ha the reverse structures are created
  518. * and *updateme is set to the value from newdepends.
  519. *
  520. * @param pkg The package we're doing this for. This is used to construct
  521. * correct uplinks.
  522. * @param updateme The forwards dependency pointer that we are to update.
  523. * This starts out containing the old forwards info, which we use to
  524. * unthread the old reverse links. After we're done it is updated.
  525. * @param newdepends The value that we ultimately want to have in updateme.
  526. * @param available The pkgbin to modify, available or installed.
  527. *
  528. * It is likely that the backward pointer for the package in question
  529. * (‘depended’) will be updated by this routine, but this will happen by
  530. * the routine traversing the dependency data structures. It doesn't need
  531. * to be told where to update that; I just mention it as something that
  532. * one should be cautious about.
  533. */
  534. void copy_dependency_links(struct pkginfo *pkg,
  535. struct dependency **updateme,
  536. struct dependency *newdepends,
  537. bool available)
  538. {
  539. struct dependency *dyp;
  540. struct deppossi *dop, **revdeps;
  541. /* Delete ‘backward’ (‘depended’) links from other packages to
  542. * dependencies listed in old version of this one. We do this by
  543. * going through all the dependencies in the old version of this
  544. * one and following them down to find which deppossi nodes to
  545. * remove. */
  546. for (dyp= *updateme; dyp; dyp= dyp->next) {
  547. for (dop= dyp->list; dop; dop= dop->next) {
  548. if (dop->rev_prev)
  549. dop->rev_prev->rev_next = dop->rev_next;
  550. else
  551. if (available)
  552. dop->ed->depended.available = dop->rev_next;
  553. else
  554. dop->ed->depended.installed = dop->rev_next;
  555. if (dop->rev_next)
  556. dop->rev_next->rev_prev = dop->rev_prev;
  557. }
  558. }
  559. /* Now fill in new ‘ed’ links from other packages to dependencies
  560. * listed in new version of this one, and set our uplinks correctly. */
  561. for (dyp= newdepends; dyp; dyp= dyp->next) {
  562. dyp->up= pkg;
  563. for (dop= dyp->list; dop; dop= dop->next) {
  564. revdeps = available ? &dop->ed->depended.available :
  565. &dop->ed->depended.installed;
  566. dop->rev_next = *revdeps;
  567. dop->rev_prev = NULL;
  568. if (*revdeps)
  569. (*revdeps)->rev_prev = dop;
  570. *revdeps = dop;
  571. }
  572. }
  573. /* Finally, we fill in the new value. */
  574. *updateme= newdepends;
  575. }