dump.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 <iwj10@cus.cam.ac.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. /* fixme: don't write uninteresting packages */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <unistd.h>
  26. #include <ctype.h>
  27. #include <assert.h>
  28. #include "config.h"
  29. #include "dpkg.h"
  30. #include "dpkg-db.h"
  31. #include "parsedump.h"
  32. void w_name(struct varbuf *vb,
  33. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  34. const struct fieldinfo *fip) {
  35. assert(pigp->name);
  36. varbufaddstr(vb,"Package: "); varbufaddstr(vb, pigp->name);
  37. varbufaddc(vb,'\n');
  38. }
  39. void w_version(struct varbuf *vb,
  40. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  41. const struct fieldinfo *fip) {
  42. /* Epoch and revision information is printed in version field too. */
  43. if (!informativeversion(&pifp->version)) return;
  44. varbufaddstr(vb,"Version: ");
  45. varbufversion(vb,&pifp->version,vdew_nonambig);
  46. varbufaddc(vb,'\n');
  47. }
  48. void w_configversion(struct varbuf *vb,
  49. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  50. const struct fieldinfo *fip) {
  51. if (pifp != &pigp->installed) return;
  52. if (!informativeversion(&pigp->configversion)) return;
  53. if (pigp->status == stat_installed || pigp->status == stat_notinstalled) return;
  54. varbufaddstr(vb,"Config-Version: ");
  55. varbufversion(vb,&pigp->configversion,vdew_nonambig);
  56. varbufaddc(vb,'\n');
  57. }
  58. void w_null(struct varbuf *vb,
  59. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  60. const struct fieldinfo *fip) {
  61. }
  62. void w_section(struct varbuf *vb,
  63. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  64. const struct fieldinfo *fip) {
  65. const char *value= pigp->section;
  66. if (!value || !*value) return;
  67. varbufaddstr(vb,"Section: ");
  68. varbufaddstr(vb,value);
  69. varbufaddc(vb,'\n');
  70. }
  71. void w_charfield(struct varbuf *vb,
  72. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  73. const struct fieldinfo *fip) {
  74. const char *value= pifp->valid ? PKGPFIELD(pifp,fip->integer,char*) : 0;
  75. if (!value || !*value) return;
  76. varbufaddstr(vb,fip->name); varbufaddstr(vb, ": "); varbufaddstr(vb,value);
  77. varbufaddc(vb,'\n');
  78. }
  79. void w_filecharf(struct varbuf *vb,
  80. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  81. const struct fieldinfo *fip) {
  82. struct filedetails *fdp;
  83. if (pifp != &pigp->available) return;
  84. fdp= pigp->files;
  85. if (!fdp || !FILEFFIELD(fdp,fip->integer,char*)) return;
  86. varbufaddstr(vb,fip->name); varbufaddc(vb,':');
  87. while (fdp) {
  88. varbufaddc(vb,' ');
  89. varbufaddstr(vb,FILEFFIELD(fdp,fip->integer,char*));
  90. fdp= fdp->next;
  91. }
  92. varbufaddc(vb,'\n');
  93. }
  94. void w_booleandefno(struct varbuf *vb,
  95. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  96. const struct fieldinfo *fip) {
  97. int value= pifp->valid ? PKGPFIELD(pifp,fip->integer,int) : 0;
  98. if (!value) return;
  99. assert(value==1);
  100. varbufaddstr(vb,"Essential: yes\n");
  101. }
  102. void w_priority(struct varbuf *vb,
  103. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  104. const struct fieldinfo *fip) {
  105. if (pigp->priority == pri_unknown) return;
  106. assert(pigp->priority <= pri_unknown);
  107. varbufaddstr(vb,"Priority: ");
  108. varbufaddstr(vb,
  109. pigp->priority == pri_other
  110. ? pigp->otherpriority
  111. : priorityinfos[pigp->priority].name);
  112. varbufaddc(vb,'\n');
  113. }
  114. void w_status(struct varbuf *vb,
  115. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  116. const struct fieldinfo *fip) {
  117. if (pifp != &pigp->installed) return;
  118. assert(pigp->want <= want_purge &&
  119. pigp->eflag <= eflagv_reinstreq && /* hold and hold-reinstreq NOT allowed */
  120. pigp->status <= stat_configfiles);
  121. varbufaddstr(vb,"Status: ");
  122. varbufaddstr(vb,wantinfos[pigp->want].name); varbufaddc(vb,' ');
  123. varbufaddstr(vb,eflaginfos[pigp->eflag].name); varbufaddc(vb,' ');
  124. varbufaddstr(vb,statusinfos[pigp->status].name); varbufaddc(vb,'\n');
  125. }
  126. void varbufdependency(struct varbuf *vb, struct dependency *dep) {
  127. struct deppossi *dop;
  128. const char *possdel;
  129. possdel= "";
  130. for (dop= dep->list; dop; dop= dop->next) {
  131. assert(dop->up == dep);
  132. varbufaddstr(vb,possdel); possdel= " | ";
  133. varbufaddstr(vb,dop->ed->name);
  134. if (dop->verrel != dvr_none) {
  135. varbufaddstr(vb," (");
  136. switch (dop->verrel) {
  137. case dvr_exact: varbufaddc(vb,'='); break;
  138. case dvr_laterequal: varbufaddstr(vb,">="); break;
  139. case dvr_earlierequal: varbufaddstr(vb,"<="); break;
  140. case dvr_laterstrict: varbufaddstr(vb,">>"); break;
  141. case dvr_earlierstrict: varbufaddstr(vb,"<<"); break;
  142. default: internerr("unknown verrel");
  143. }
  144. varbufaddc(vb,' ');
  145. varbufversion(vb,&dop->version,vdew_nonambig);
  146. varbufaddc(vb,')');
  147. }
  148. }
  149. }
  150. void w_dependency(struct varbuf *vb,
  151. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  152. const struct fieldinfo *fip) {
  153. char fnbuf[50];
  154. const char *depdel;
  155. struct dependency *dyp;
  156. if (!pifp->valid) return;
  157. sprintf(fnbuf,"%s: ",fip->name); depdel= fnbuf;
  158. for (dyp= pifp->depends; dyp; dyp= dyp->next) {
  159. if (dyp->type != fip->integer) continue;
  160. assert(dyp->up == pigp);
  161. varbufaddstr(vb,depdel); depdel= ", ";
  162. varbufdependency(vb,dyp);
  163. }
  164. if (depdel != fnbuf) varbufaddc(vb,'\n');
  165. }
  166. void w_conffiles(struct varbuf *vb,
  167. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  168. const struct fieldinfo *fip) {
  169. struct conffile *i;
  170. if (!pifp->valid || !pifp->conffiles || pifp == &pigp->available) return;
  171. varbufaddstr(vb,"Conffiles:\n");
  172. for (i=pifp->conffiles; i; i= i->next) {
  173. varbufaddc(vb,' '); varbufaddstr(vb,i->name); varbufaddc(vb,' ');
  174. varbufaddstr(vb,i->hash); varbufaddc(vb,'\n');
  175. }
  176. }
  177. void varbufrecord(struct varbuf *vb,
  178. const struct pkginfo *pigp, const struct pkginfoperfile *pifp) {
  179. const struct fieldinfo *fip;
  180. const struct arbitraryfield *afp;
  181. for (fip= fieldinfos; fip->name; fip++) {
  182. fip->wcall(vb,pigp,pifp,fip);
  183. }
  184. if (pifp->valid) {
  185. for (afp= pifp->arbs; afp; afp= afp->next) {
  186. varbufaddstr(vb,afp->name); varbufaddstr(vb,": ");
  187. varbufaddstr(vb,afp->value); varbufaddc(vb,'\n');
  188. }
  189. }
  190. }
  191. void writerecord(FILE *file, const char *filename,
  192. const struct pkginfo *pigp, const struct pkginfoperfile *pifp) {
  193. struct varbuf vb;
  194. varbufinit(&vb);
  195. varbufrecord(&vb,pigp,pifp);
  196. varbufaddc(&vb,'\0');
  197. if (!fputs(vb.buf,file))
  198. ohshite("failed to write details of `%.50s' to `%.250s'", pigp->name, filename);
  199. }
  200. void writedb(const char *filename, int available, int mustsync) {
  201. static char writebuf[8192];
  202. struct pkgiterator *it;
  203. struct pkginfo *pigp;
  204. struct pkginfoperfile *pifp;
  205. char *oldfn, *newfn;
  206. const char *which;
  207. FILE *file;
  208. struct varbuf vb;
  209. which= available ? "available" : "status";
  210. oldfn= m_malloc(strlen(filename)+sizeof(OLDDBEXT));
  211. strcpy(oldfn,filename); strcat(oldfn,OLDDBEXT);
  212. newfn= m_malloc(strlen(filename)+sizeof(NEWDBEXT));
  213. strcpy(newfn,filename); strcat(newfn,NEWDBEXT);
  214. varbufinit(&vb);
  215. file= fopen(newfn,"w");
  216. if (!file) ohshite("failed to open `%s' for writing %s information",filename,which);
  217. if (setvbuf(file,writebuf,_IOFBF,sizeof(writebuf)))
  218. ohshite("unable to set buffering on status file");
  219. it= iterpkgstart();
  220. while ((pigp= iterpkgnext(it)) != 0) {
  221. pifp= available ? &pigp->available : &pigp->installed;
  222. /* Don't dump records which have no useful content. */
  223. if (!informative(pigp,pifp)) continue;
  224. if (!pifp->valid) blankpackageperfile(pifp);
  225. varbufrecord(&vb,pigp,pifp);
  226. varbufaddc(&vb,'\n'); varbufaddc(&vb,0);
  227. if (!fputs(vb.buf,file))
  228. ohshite("failed to write %s record about `%.50s' to `%.250s'",
  229. which, pigp->name, filename);
  230. varbufreset(&vb);
  231. }
  232. varbuffree(&vb);
  233. if (mustsync) {
  234. if (fflush(file))
  235. ohshite("failed to flush %s information to `%.250s'", which, filename);
  236. if (fsync(fileno(file)))
  237. ohshite("failed to fsync %s information to `%.250s'", which, filename);
  238. }
  239. if (fclose(file)) ohshite("failed to close `%.250s' after writing %s information",
  240. filename, which);
  241. unlink(oldfn);
  242. if (link(filename,oldfn) && errno != ENOENT)
  243. ohshite("failed to link `%.250s' to `%.250s' for backup of %s info",
  244. filename, oldfn, which);
  245. if (rename(newfn,filename))
  246. ohshite("failed to install `%.250s' as `%.250s' containing %s info",
  247. newfn, filename, which);
  248. }