dump.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * dump.c - code to write in-core database to a file
  4. *
  5. * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2001 Wichert Akkerman
  7. * Copyright © 2006,2008-2014 Guillem Jover <guillem@debian.org>
  8. * Copyright © 2011 Linaro Limited
  9. * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
  10. *
  11. * This is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. */
  24. /* FIXME: Don't write uninteresting packages. */
  25. #include <config.h>
  26. #include <compat.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <assert.h>
  30. #include <errno.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <stdbool.h>
  36. #include <dpkg/i18n.h>
  37. #include <dpkg/dpkg.h>
  38. #include <dpkg/dpkg-db.h>
  39. #include <dpkg/string.h>
  40. #include <dpkg/dir.h>
  41. #include <dpkg/parsedump.h>
  42. static inline void
  43. varbuf_add_fieldname(struct varbuf *vb, const struct fieldinfo *fip)
  44. {
  45. varbuf_add_str(vb, fip->name);
  46. varbuf_add_str(vb, ": ");
  47. }
  48. void
  49. w_name(struct varbuf *vb,
  50. const struct pkginfo *pkg, const struct pkgbin *pkgbin,
  51. enum fwriteflags flags, const struct fieldinfo *fip)
  52. {
  53. assert(pkg->set->name);
  54. if (flags&fw_printheader)
  55. varbuf_add_str(vb, "Package: ");
  56. varbuf_add_str(vb, pkg->set->name);
  57. if (flags&fw_printheader)
  58. varbuf_add_char(vb, '\n');
  59. }
  60. void
  61. w_version(struct varbuf *vb,
  62. const struct pkginfo *pkg, const struct pkgbin *pkgbin,
  63. enum fwriteflags flags, const struct fieldinfo *fip)
  64. {
  65. if (!dpkg_version_is_informative(&pkgbin->version))
  66. return;
  67. if (flags&fw_printheader)
  68. varbuf_add_str(vb, "Version: ");
  69. varbufversion(vb, &pkgbin->version, vdew_nonambig);
  70. if (flags&fw_printheader)
  71. varbuf_add_char(vb, '\n');
  72. }
  73. void
  74. w_configversion(struct varbuf *vb,
  75. const struct pkginfo *pkg, const struct pkgbin *pkgbin,
  76. enum fwriteflags flags, const struct fieldinfo *fip)
  77. {
  78. if (pkgbin != &pkg->installed)
  79. return;
  80. if (!dpkg_version_is_informative(&pkg->configversion))
  81. return;
  82. if (pkg->status == PKG_STAT_INSTALLED ||
  83. pkg->status == PKG_STAT_NOTINSTALLED ||
  84. pkg->status == PKG_STAT_TRIGGERSPENDING)
  85. return;
  86. if (flags&fw_printheader)
  87. varbuf_add_str(vb, "Config-Version: ");
  88. varbufversion(vb, &pkg->configversion, vdew_nonambig);
  89. if (flags&fw_printheader)
  90. varbuf_add_char(vb, '\n');
  91. }
  92. void
  93. w_null(struct varbuf *vb,
  94. const struct pkginfo *pkg, const struct pkgbin *pkgbin,
  95. enum fwriteflags flags, const struct fieldinfo *fip)
  96. {
  97. }
  98. void
  99. w_section(struct varbuf *vb,
  100. const struct pkginfo *pkg, const struct pkgbin *pkgbin,
  101. enum fwriteflags flags, const struct fieldinfo *fip)
  102. {
  103. const char *value = pkg->section;
  104. if (str_is_unset(value))
  105. return;
  106. if (flags&fw_printheader)
  107. varbuf_add_str(vb, "Section: ");
  108. varbuf_add_str(vb, value);
  109. if (flags&fw_printheader)
  110. varbuf_add_char(vb, '\n');
  111. }
  112. void
  113. w_charfield(struct varbuf *vb,
  114. const struct pkginfo *pkg, const struct pkgbin *pkgbin,
  115. enum fwriteflags flags, const struct fieldinfo *fip)
  116. {
  117. const char *value = STRUCTFIELD(pkgbin, fip->integer, const char *);
  118. if (str_is_unset(value))
  119. return;
  120. if (flags & fw_printheader)
  121. varbuf_add_fieldname(vb, fip);
  122. varbuf_add_str(vb, value);
  123. if (flags&fw_printheader)
  124. varbuf_add_char(vb, '\n');
  125. }
  126. void
  127. w_filecharf(struct varbuf *vb,
  128. const struct pkginfo *pkg, const struct pkgbin *pkgbin,
  129. enum fwriteflags flags, const struct fieldinfo *fip)
  130. {
  131. struct filedetails *fdp;
  132. if (pkgbin != &pkg->available)
  133. return;
  134. fdp = pkg->files;
  135. if (!fdp || !STRUCTFIELD(fdp, fip->integer, const char *))
  136. return;
  137. if (flags&fw_printheader) {
  138. varbuf_add_str(vb, fip->name);
  139. varbuf_add_char(vb, ':');
  140. }
  141. while (fdp) {
  142. varbuf_add_char(vb, ' ');
  143. varbuf_add_str(vb, STRUCTFIELD(fdp, fip->integer, const char *));
  144. fdp= fdp->next;
  145. }
  146. if (flags&fw_printheader)
  147. varbuf_add_char(vb, '\n');
  148. }
  149. void
  150. w_booleandefno(struct varbuf *vb,
  151. const struct pkginfo *pkg, const struct pkgbin *pkgbin,
  152. enum fwriteflags flags, const struct fieldinfo *fip)
  153. {
  154. bool value = STRUCTFIELD(pkgbin, fip->integer, bool);
  155. if ((flags & fw_printheader) && !value)
  156. return;
  157. if (flags & fw_printheader)
  158. varbuf_add_fieldname(vb, fip);
  159. varbuf_add_str(vb, value ? "yes" : "no");
  160. if (flags & fw_printheader)
  161. varbuf_add_char(vb, '\n');
  162. }
  163. void
  164. w_multiarch(struct varbuf *vb,
  165. const struct pkginfo *pkg, const struct pkgbin *pkgbin,
  166. enum fwriteflags flags, const struct fieldinfo *fip)
  167. {
  168. int value = STRUCTFIELD(pkgbin, fip->integer, int);
  169. if ((flags & fw_printheader) && !value)
  170. return;
  171. if (flags & fw_printheader)
  172. varbuf_add_fieldname(vb, fip);
  173. varbuf_add_str(vb, multiarchinfos[value].name);
  174. if (flags & fw_printheader)
  175. varbuf_add_char(vb, '\n');
  176. }
  177. void
  178. w_architecture(struct varbuf *vb,
  179. const struct pkginfo *pkg, const struct pkgbin *pkgbin,
  180. enum fwriteflags flags, const struct fieldinfo *fip)
  181. {
  182. if (!pkgbin->arch)
  183. return;
  184. if (pkgbin->arch->type == DPKG_ARCH_NONE)
  185. return;
  186. if (pkgbin->arch->type == DPKG_ARCH_EMPTY)
  187. return;
  188. if (flags & fw_printheader)
  189. varbuf_add_fieldname(vb, fip);
  190. varbuf_add_str(vb, pkgbin->arch->name);
  191. if (flags & fw_printheader)
  192. varbuf_add_char(vb, '\n');
  193. }
  194. void
  195. w_priority(struct varbuf *vb,
  196. const struct pkginfo *pkg, const struct pkgbin *pkgbin,
  197. enum fwriteflags flags, const struct fieldinfo *fip)
  198. {
  199. if (pkg->priority == PKG_PRIO_UNKNOWN)
  200. return;
  201. assert(pkg->priority <= PKG_PRIO_UNKNOWN);
  202. if (flags&fw_printheader)
  203. varbuf_add_str(vb, "Priority: ");
  204. varbuf_add_str(vb, pkg_priority_name(pkg));
  205. if (flags&fw_printheader)
  206. varbuf_add_char(vb, '\n');
  207. }
  208. void
  209. w_status(struct varbuf *vb,
  210. const struct pkginfo *pkg, const struct pkgbin *pkgbin,
  211. enum fwriteflags flags, const struct fieldinfo *fip)
  212. {
  213. if (pkgbin != &pkg->installed)
  214. return;
  215. assert(pkg->want <= PKG_WANT_PURGE);
  216. assert(pkg->eflag <= PKG_EFLAG_REINSTREQ);
  217. #define PEND pkg->trigpend_head
  218. #define AW pkg->trigaw.head
  219. switch (pkg->status) {
  220. case PKG_STAT_NOTINSTALLED:
  221. case PKG_STAT_CONFIGFILES:
  222. assert(!PEND);
  223. assert(!AW);
  224. break;
  225. case PKG_STAT_HALFINSTALLED:
  226. case PKG_STAT_UNPACKED:
  227. case PKG_STAT_HALFCONFIGURED:
  228. assert(!PEND);
  229. break;
  230. case PKG_STAT_TRIGGERSAWAITED:
  231. assert(AW);
  232. break;
  233. case PKG_STAT_TRIGGERSPENDING:
  234. assert(PEND);
  235. assert(!AW);
  236. break;
  237. case PKG_STAT_INSTALLED:
  238. assert(!PEND);
  239. assert(!AW);
  240. break;
  241. default:
  242. internerr("unknown package status '%d'", pkg->status);
  243. }
  244. #undef PEND
  245. #undef AW
  246. if (flags&fw_printheader)
  247. varbuf_add_str(vb, "Status: ");
  248. varbuf_add_str(vb, pkg_want_name(pkg));
  249. varbuf_add_char(vb, ' ');
  250. varbuf_add_str(vb, pkg_eflag_name(pkg));
  251. varbuf_add_char(vb, ' ');
  252. varbuf_add_str(vb, pkg_status_name(pkg));
  253. if (flags&fw_printheader)
  254. varbuf_add_char(vb, '\n');
  255. }
  256. void varbufdependency(struct varbuf *vb, struct dependency *dep) {
  257. struct deppossi *dop;
  258. const char *possdel;
  259. possdel= "";
  260. for (dop= dep->list; dop; dop= dop->next) {
  261. assert(dop->up == dep);
  262. varbuf_add_str(vb, possdel);
  263. possdel = " | ";
  264. varbuf_add_str(vb, dop->ed->name);
  265. if (!dop->arch_is_implicit)
  266. varbuf_add_archqual(vb, dop->arch);
  267. if (dop->verrel != DPKG_RELATION_NONE) {
  268. varbuf_add_str(vb, " (");
  269. switch (dop->verrel) {
  270. case DPKG_RELATION_EQ:
  271. varbuf_add_char(vb, '=');
  272. break;
  273. case DPKG_RELATION_GE:
  274. varbuf_add_str(vb, ">=");
  275. break;
  276. case DPKG_RELATION_LE:
  277. varbuf_add_str(vb, "<=");
  278. break;
  279. case DPKG_RELATION_GT:
  280. varbuf_add_str(vb, ">>");
  281. break;
  282. case DPKG_RELATION_LT:
  283. varbuf_add_str(vb, "<<");
  284. break;
  285. default:
  286. internerr("unknown dpkg_relation %d", dop->verrel);
  287. }
  288. varbuf_add_char(vb, ' ');
  289. varbufversion(vb,&dop->version,vdew_nonambig);
  290. varbuf_add_char(vb, ')');
  291. }
  292. }
  293. }
  294. void
  295. w_dependency(struct varbuf *vb,
  296. const struct pkginfo *pkg, const struct pkgbin *pkgbin,
  297. enum fwriteflags flags, const struct fieldinfo *fip)
  298. {
  299. struct dependency *dyp;
  300. bool dep_found = false;
  301. for (dyp = pkgbin->depends; dyp; dyp = dyp->next) {
  302. if (dyp->type != fip->integer) continue;
  303. assert(dyp->up == pkg);
  304. if (dep_found) {
  305. varbuf_add_str(vb, ", ");
  306. } else {
  307. if (flags & fw_printheader)
  308. varbuf_add_fieldname(vb, fip);
  309. dep_found = true;
  310. }
  311. varbufdependency(vb,dyp);
  312. }
  313. if ((flags & fw_printheader) && dep_found)
  314. varbuf_add_char(vb, '\n');
  315. }
  316. void
  317. w_conffiles(struct varbuf *vb,
  318. const struct pkginfo *pkg, const struct pkgbin *pkgbin,
  319. enum fwriteflags flags, const struct fieldinfo *fip)
  320. {
  321. struct conffile *i;
  322. if (!pkgbin->conffiles || pkgbin == &pkg->available)
  323. return;
  324. if (flags&fw_printheader)
  325. varbuf_add_str(vb, "Conffiles:\n");
  326. for (i = pkgbin->conffiles; i; i = i->next) {
  327. if (i != pkgbin->conffiles)
  328. varbuf_add_char(vb, '\n');
  329. varbuf_add_char(vb, ' ');
  330. varbuf_add_str(vb, i->name);
  331. varbuf_add_char(vb, ' ');
  332. varbuf_add_str(vb, i->hash);
  333. if (i->obsolete)
  334. varbuf_add_str(vb, " obsolete");
  335. }
  336. if (flags&fw_printheader)
  337. varbuf_add_char(vb, '\n');
  338. }
  339. void
  340. w_trigpend(struct varbuf *vb,
  341. const struct pkginfo *pkg, const struct pkgbin *pkgbin,
  342. enum fwriteflags flags, const struct fieldinfo *fip)
  343. {
  344. struct trigpend *tp;
  345. if (pkgbin == &pkg->available || !pkg->trigpend_head)
  346. return;
  347. assert(pkg->status >= PKG_STAT_TRIGGERSAWAITED &&
  348. pkg->status <= PKG_STAT_TRIGGERSPENDING);
  349. if (flags & fw_printheader)
  350. varbuf_add_str(vb, "Triggers-Pending:");
  351. for (tp = pkg->trigpend_head; tp; tp = tp->next) {
  352. varbuf_add_char(vb, ' ');
  353. varbuf_add_str(vb, tp->name);
  354. }
  355. if (flags & fw_printheader)
  356. varbuf_add_char(vb, '\n');
  357. }
  358. void
  359. w_trigaw(struct varbuf *vb,
  360. const struct pkginfo *pkg, const struct pkgbin *pkgbin,
  361. enum fwriteflags flags, const struct fieldinfo *fip)
  362. {
  363. struct trigaw *ta;
  364. if (pkgbin == &pkg->available || !pkg->trigaw.head)
  365. return;
  366. assert(pkg->status > PKG_STAT_CONFIGFILES &&
  367. pkg->status <= PKG_STAT_TRIGGERSAWAITED);
  368. if (flags & fw_printheader)
  369. varbuf_add_str(vb, "Triggers-Awaited:");
  370. for (ta = pkg->trigaw.head; ta; ta = ta->sameaw.next) {
  371. varbuf_add_char(vb, ' ');
  372. varbuf_add_pkgbin_name(vb, ta->pend, &ta->pend->installed, pnaw_nonambig);
  373. }
  374. if (flags & fw_printheader)
  375. varbuf_add_char(vb, '\n');
  376. }
  377. void
  378. varbuf_add_arbfield(struct varbuf *vb, const struct arbitraryfield *arbfield,
  379. enum fwriteflags flags)
  380. {
  381. if (flags & fw_printheader) {
  382. varbuf_add_str(vb, arbfield->name);
  383. varbuf_add_str(vb, ": ");
  384. }
  385. varbuf_add_str(vb, arbfield->value);
  386. if (flags & fw_printheader)
  387. varbuf_add_char(vb, '\n');
  388. }
  389. void
  390. varbufrecord(struct varbuf *vb,
  391. const struct pkginfo *pkg, const struct pkgbin *pkgbin)
  392. {
  393. const struct fieldinfo *fip;
  394. const struct arbitraryfield *afp;
  395. for (fip= fieldinfos; fip->name; fip++) {
  396. fip->wcall(vb, pkg, pkgbin, fw_printheader, fip);
  397. }
  398. for (afp = pkgbin->arbs; afp; afp = afp->next) {
  399. varbuf_add_arbfield(vb, afp, fw_printheader);
  400. }
  401. }
  402. void
  403. writerecord(FILE *file, const char *filename,
  404. const struct pkginfo *pkg, const struct pkgbin *pkgbin)
  405. {
  406. struct varbuf vb = VARBUF_INIT;
  407. varbufrecord(&vb, pkg, pkgbin);
  408. varbuf_end_str(&vb);
  409. if (fputs(vb.buf,file) < 0) {
  410. struct varbuf pkgname = VARBUF_INIT;
  411. int errno_saved = errno;
  412. varbuf_add_pkgbin_name(&pkgname, pkg, pkgbin, pnaw_nonambig);
  413. errno = errno_saved;
  414. ohshite(_("failed to write details of '%.50s' to '%.250s'"),
  415. pkgname.buf, filename);
  416. }
  417. varbuf_destroy(&vb);
  418. }
  419. void
  420. writedb(const char *filename, enum writedb_flags flags)
  421. {
  422. static char writebuf[8192];
  423. struct pkgiterator *iter;
  424. struct pkginfo *pkg;
  425. struct pkgbin *pkgbin;
  426. const char *which;
  427. struct atomic_file *file;
  428. struct varbuf vb = VARBUF_INIT;
  429. which = (flags & wdb_dump_available) ? "available" : "status";
  430. file = atomic_file_new(filename, ATOMIC_FILE_BACKUP);
  431. atomic_file_open(file);
  432. if (setvbuf(file->fp, writebuf, _IOFBF, sizeof(writebuf)))
  433. ohshite(_("unable to set buffering on %s database file"), which);
  434. iter = pkg_db_iter_new();
  435. while ((pkg = pkg_db_iter_next_pkg(iter)) != NULL) {
  436. pkgbin = (flags & wdb_dump_available) ? &pkg->available : &pkg->installed;
  437. /* Don't dump records which have no useful content. */
  438. if (!pkg_is_informative(pkg, pkgbin))
  439. continue;
  440. varbufrecord(&vb, pkg, pkgbin);
  441. varbuf_add_char(&vb, '\n');
  442. varbuf_end_str(&vb);
  443. if (fputs(vb.buf, file->fp) < 0)
  444. ohshite(_("failed to write %s database record about '%.50s' to '%.250s'"),
  445. which, pkgbin_name(pkg, pkgbin, pnaw_nonambig), filename);
  446. varbuf_reset(&vb);
  447. }
  448. pkg_db_iter_free(iter);
  449. varbuf_destroy(&vb);
  450. if (flags & wdb_must_sync)
  451. atomic_file_sync(file);
  452. atomic_file_close(file);
  453. atomic_file_commit(file);
  454. atomic_file_free(file);
  455. if (flags & wdb_must_sync)
  456. dir_sync_path_parent(filename);
  457. }