dump.c 14 KB

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