dump.c 13 KB

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