dump.c 13 KB

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