dump.c 9.5 KB

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