dump.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. *
  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. /* FIXME: don't write uninteresting packages */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <assert.h>
  27. #include <errno.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <stdbool.h>
  34. #include <dpkg/i18n.h>
  35. #include <dpkg/dpkg.h>
  36. #include <dpkg/dpkg-db.h>
  37. #include <dpkg/dir.h>
  38. #include <dpkg/parsedump.h>
  39. void w_name(struct varbuf *vb,
  40. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  41. enum fwriteflags flags, const struct fieldinfo *fip) {
  42. assert(pigp->name);
  43. if (flags&fw_printheader)
  44. varbufaddstr(vb,"Package: ");
  45. varbufaddstr(vb, pigp->name);
  46. if (flags&fw_printheader)
  47. varbufaddc(vb,'\n');
  48. }
  49. void w_version(struct varbuf *vb,
  50. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  51. enum fwriteflags flags, const struct fieldinfo *fip) {
  52. /* Epoch and revision information is printed in version field too. */
  53. if (!informativeversion(&pifp->version)) return;
  54. if (flags&fw_printheader)
  55. varbufaddstr(vb,"Version: ");
  56. varbufversion(vb,&pifp->version,vdew_nonambig);
  57. if (flags&fw_printheader)
  58. varbufaddc(vb,'\n');
  59. }
  60. void w_configversion(struct varbuf *vb,
  61. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  62. enum fwriteflags flags, const struct fieldinfo *fip) {
  63. if (pifp != &pigp->installed) return;
  64. if (!informativeversion(&pigp->configversion)) return;
  65. if (pigp->status == stat_installed ||
  66. pigp->status == stat_notinstalled ||
  67. pigp->status == stat_triggerspending ||
  68. pigp->status == stat_triggersawaited)
  69. return;
  70. if (flags&fw_printheader)
  71. varbufaddstr(vb,"Config-Version: ");
  72. varbufversion(vb,&pigp->configversion,vdew_nonambig);
  73. if (flags&fw_printheader)
  74. varbufaddc(vb,'\n');
  75. }
  76. void w_null(struct varbuf *vb,
  77. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  78. enum fwriteflags flags, const struct fieldinfo *fip) {
  79. }
  80. void w_section(struct varbuf *vb,
  81. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  82. enum fwriteflags flags, const struct fieldinfo *fip) {
  83. const char *value= pigp->section;
  84. if (!value || !*value) return;
  85. if (flags&fw_printheader)
  86. varbufaddstr(vb,"Section: ");
  87. varbufaddstr(vb,value);
  88. if (flags&fw_printheader)
  89. varbufaddc(vb,'\n');
  90. }
  91. void w_charfield(struct varbuf *vb,
  92. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  93. enum fwriteflags flags, const struct fieldinfo *fip) {
  94. const char *value = PKGPFIELD(pifp, fip->integer, const char *);
  95. if (!value || !*value) return;
  96. if (flags&fw_printheader) {
  97. varbufaddstr(vb,fip->name);
  98. varbufaddstr(vb, ": ");
  99. }
  100. varbufaddstr(vb,value);
  101. if (flags&fw_printheader)
  102. varbufaddc(vb,'\n');
  103. }
  104. void w_filecharf(struct varbuf *vb,
  105. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  106. enum fwriteflags flags, const struct fieldinfo *fip) {
  107. struct filedetails *fdp;
  108. if (pifp != &pigp->available) return;
  109. fdp= pigp->files;
  110. if (!fdp || !FILEFFIELD(fdp,fip->integer,const char*)) return;
  111. if (flags&fw_printheader) {
  112. varbufaddstr(vb,fip->name);
  113. varbufaddc(vb,':');
  114. }
  115. while (fdp) {
  116. varbufaddc(vb,' ');
  117. varbufaddstr(vb,FILEFFIELD(fdp,fip->integer,const char*));
  118. fdp= fdp->next;
  119. }
  120. if (flags&fw_printheader)
  121. varbufaddc(vb,'\n');
  122. }
  123. void w_booleandefno(struct varbuf *vb,
  124. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  125. enum fwriteflags flags, const struct fieldinfo *fip) {
  126. bool value = PKGPFIELD(pifp, fip->integer, bool);
  127. if (!(flags&fw_printheader)) {
  128. varbufaddstr(vb, value ? "yes" : "no");
  129. return;
  130. }
  131. if (!value) return;
  132. assert(value == true);
  133. varbufaddstr(vb,fip->name); varbufaddstr(vb, ": yes\n");
  134. }
  135. void w_priority(struct varbuf *vb,
  136. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  137. enum fwriteflags flags, const struct fieldinfo *fip) {
  138. if (pigp->priority == pri_unknown) return;
  139. assert(pigp->priority <= pri_unknown);
  140. if (flags&fw_printheader)
  141. varbufaddstr(vb,"Priority: ");
  142. varbufaddstr(vb,
  143. pigp->priority == pri_other
  144. ? pigp->otherpriority
  145. : priorityinfos[pigp->priority].name);
  146. if (flags&fw_printheader)
  147. varbufaddc(vb,'\n');
  148. }
  149. void w_status(struct varbuf *vb,
  150. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  151. enum fwriteflags flags, const struct fieldinfo *fip) {
  152. if (pifp != &pigp->installed) return;
  153. assert(pigp->want <= want_purge);
  154. assert(pigp->eflag <= eflag_reinstreq);
  155. #define PEND pigp->trigpend_head
  156. #define AW pigp->trigaw.head
  157. switch (pigp->status) {
  158. case stat_notinstalled:
  159. case stat_configfiles:
  160. assert(!PEND);
  161. assert(!AW);
  162. break;
  163. case stat_halfinstalled:
  164. case stat_unpacked:
  165. case stat_halfconfigured:
  166. assert(!PEND);
  167. break;
  168. case stat_triggersawaited:
  169. assert(AW);
  170. break;
  171. case stat_triggerspending:
  172. assert(PEND);
  173. assert(!AW);
  174. break;
  175. case stat_installed:
  176. assert(!PEND);
  177. assert(!AW);
  178. break;
  179. default:
  180. internerr("unknown package status '%d'", pigp->status);
  181. }
  182. #undef PEND
  183. #undef AW
  184. if (flags&fw_printheader)
  185. varbufaddstr(vb,"Status: ");
  186. varbufaddstr(vb,wantinfos[pigp->want].name); varbufaddc(vb,' ');
  187. varbufaddstr(vb,eflaginfos[pigp->eflag].name); varbufaddc(vb,' ');
  188. varbufaddstr(vb,statusinfos[pigp->status].name);
  189. if (flags&fw_printheader)
  190. varbufaddc(vb,'\n');
  191. }
  192. void varbufdependency(struct varbuf *vb, struct dependency *dep) {
  193. struct deppossi *dop;
  194. const char *possdel;
  195. possdel= "";
  196. for (dop= dep->list; dop; dop= dop->next) {
  197. assert(dop->up == dep);
  198. varbufaddstr(vb,possdel); possdel= " | ";
  199. varbufaddstr(vb,dop->ed->name);
  200. if (dop->verrel != dvr_none) {
  201. varbufaddstr(vb," (");
  202. switch (dop->verrel) {
  203. case dvr_exact: varbufaddc(vb,'='); break;
  204. case dvr_laterequal: varbufaddstr(vb,">="); break;
  205. case dvr_earlierequal: varbufaddstr(vb,"<="); break;
  206. case dvr_laterstrict: varbufaddstr(vb,">>"); break;
  207. case dvr_earlierstrict: varbufaddstr(vb,"<<"); break;
  208. default:
  209. internerr("unknown verrel '%d'", dop->verrel);
  210. }
  211. varbufaddc(vb,' ');
  212. varbufversion(vb,&dop->version,vdew_nonambig);
  213. varbufaddc(vb,')');
  214. }
  215. }
  216. }
  217. void w_dependency(struct varbuf *vb,
  218. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  219. enum fwriteflags flags, const struct fieldinfo *fip) {
  220. char fnbuf[50];
  221. const char *depdel;
  222. struct dependency *dyp;
  223. if (flags&fw_printheader)
  224. sprintf(fnbuf,"%s: ",fip->name);
  225. else
  226. fnbuf[0] = '\0';
  227. depdel= fnbuf;
  228. for (dyp= pifp->depends; dyp; dyp= dyp->next) {
  229. if (dyp->type != fip->integer) continue;
  230. assert(dyp->up == pigp);
  231. varbufaddstr(vb,depdel); depdel= ", ";
  232. varbufdependency(vb,dyp);
  233. }
  234. if ((flags&fw_printheader) && (depdel!=fnbuf))
  235. varbufaddc(vb,'\n');
  236. }
  237. void w_conffiles(struct varbuf *vb,
  238. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  239. enum fwriteflags flags, const struct fieldinfo *fip) {
  240. struct conffile *i;
  241. if (!pifp->conffiles || pifp == &pigp->available)
  242. return;
  243. if (flags&fw_printheader)
  244. varbufaddstr(vb,"Conffiles:\n");
  245. for (i=pifp->conffiles; i; i= i->next) {
  246. if (i!=pifp->conffiles) varbufaddc(vb,'\n');
  247. varbufaddc(vb,' '); varbufaddstr(vb,i->name); varbufaddc(vb,' ');
  248. varbufaddstr(vb,i->hash);
  249. if (i->obsolete) varbufaddstr(vb," obsolete");
  250. }
  251. if (flags&fw_printheader)
  252. varbufaddc(vb,'\n');
  253. }
  254. void
  255. w_trigpend(struct varbuf *vb,
  256. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  257. enum fwriteflags flags, const struct fieldinfo *fip)
  258. {
  259. struct trigpend *tp;
  260. if (pifp == &pigp->available || !pigp->trigpend_head)
  261. return;
  262. assert(pigp->status >= stat_triggersawaited &&
  263. pigp->status <= stat_triggerspending);
  264. if (flags & fw_printheader)
  265. varbufaddstr(vb, "Triggers-Pending:");
  266. for (tp = pigp->trigpend_head; tp; tp = tp->next) {
  267. varbufaddc(vb, ' ');
  268. varbufaddstr(vb, tp->name);
  269. }
  270. if (flags & fw_printheader)
  271. varbufaddc(vb, '\n');
  272. }
  273. void
  274. w_trigaw(struct varbuf *vb,
  275. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  276. enum fwriteflags flags, const struct fieldinfo *fip)
  277. {
  278. struct trigaw *ta;
  279. if (pifp == &pigp->available || !pigp->trigaw.head)
  280. return;
  281. assert(pigp->status > stat_configfiles &&
  282. pigp->status <= stat_triggersawaited);
  283. if (flags & fw_printheader)
  284. varbufaddstr(vb, "Triggers-Awaited:");
  285. for (ta = pigp->trigaw.head; ta; ta = ta->sameaw.next) {
  286. varbufaddc(vb, ' ');
  287. varbufaddstr(vb, ta->pend->name);
  288. }
  289. if (flags & fw_printheader)
  290. varbufaddc(vb, '\n');
  291. }
  292. void varbufrecord(struct varbuf *vb,
  293. const struct pkginfo *pigp, const struct pkginfoperfile *pifp) {
  294. const struct fieldinfo *fip;
  295. const struct arbitraryfield *afp;
  296. for (fip= fieldinfos; fip->name; fip++) {
  297. fip->wcall(vb,pigp,pifp,fw_printheader,fip);
  298. }
  299. for (afp = pifp->arbs; afp; afp = afp->next) {
  300. varbufaddstr(vb, afp->name);
  301. varbufaddstr(vb, ": ");
  302. varbufaddstr(vb, afp->value);
  303. varbufaddc(vb, '\n');
  304. }
  305. }
  306. void writerecord(FILE *file, const char *filename,
  307. const struct pkginfo *pigp, const struct pkginfoperfile *pifp) {
  308. struct varbuf vb = VARBUF_INIT;
  309. varbufrecord(&vb,pigp,pifp);
  310. varbufaddc(&vb,'\0');
  311. if (fputs(vb.buf,file) < 0)
  312. ohshite(_("failed to write details of `%.50s' to `%.250s'"), pigp->name,
  313. filename);
  314. varbuf_destroy(&vb);
  315. }
  316. void
  317. writedb(const char *filename, bool available, bool mustsync)
  318. {
  319. static char writebuf[8192];
  320. struct pkgiterator *it;
  321. struct pkginfo *pigp;
  322. struct pkginfoperfile *pifp;
  323. char *oldfn, *newfn;
  324. const char *which;
  325. FILE *file;
  326. struct varbuf vb = VARBUF_INIT;
  327. int old_umask;
  328. which = available ? "available" : "status";
  329. oldfn= m_malloc(strlen(filename)+sizeof(OLDDBEXT));
  330. strcpy(oldfn,filename); strcat(oldfn,OLDDBEXT);
  331. newfn= m_malloc(strlen(filename)+sizeof(NEWDBEXT));
  332. strcpy(newfn,filename); strcat(newfn,NEWDBEXT);
  333. old_umask = umask(022);
  334. file= fopen(newfn,"w");
  335. umask(old_umask);
  336. if (!file)
  337. ohshite(_("failed to open '%s' for writing %s database"), filename, which);
  338. if (setvbuf(file,writebuf,_IOFBF,sizeof(writebuf)))
  339. ohshite(_("unable to set buffering on %s database file"), which);
  340. it= iterpkgstart();
  341. while ((pigp= iterpkgnext(it)) != NULL) {
  342. pifp= available ? &pigp->available : &pigp->installed;
  343. /* Don't dump records which have no useful content. */
  344. if (!informative(pigp,pifp)) continue;
  345. varbufrecord(&vb,pigp,pifp);
  346. varbufaddc(&vb,'\n'); varbufaddc(&vb,0);
  347. if (fputs(vb.buf,file) < 0)
  348. ohshite(_("failed to write %s database record about '%.50s' to '%.250s'"),
  349. which, pigp->name, filename);
  350. varbufreset(&vb);
  351. }
  352. iterpkgend(it);
  353. varbuf_destroy(&vb);
  354. if (mustsync) {
  355. if (fflush(file))
  356. ohshite(_("failed to flush %s database to '%.250s'"), which, filename);
  357. if (fsync(fileno(file)))
  358. ohshite(_("failed to fsync %s database to '%.250s'"), which, filename);
  359. }
  360. if (fclose(file))
  361. ohshite(_("failed to close '%.250s' after writing %s database"),
  362. filename, which);
  363. unlink(oldfn);
  364. if (link(filename,oldfn) && errno != ENOENT)
  365. ohshite(_("failed to link '%.250s' to '%.250s' for backup of %s database"),
  366. filename, oldfn, which);
  367. if (rename(newfn,filename))
  368. ohshite(_("failed to install '%.250s' as '%.250s' containing %s database"),
  369. newfn, filename, which);
  370. if (mustsync)
  371. dir_sync_path_parent(filename);
  372. free(newfn);
  373. free(oldfn);
  374. }