dump.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 || pigp->status == stat_notinstalled) return;
  62. if (flags&fw_printheader)
  63. varbufaddstr(vb,"Config-Version: ");
  64. varbufversion(vb,&pigp->configversion,vdew_nonambig);
  65. if (flags&fw_printheader)
  66. varbufaddc(vb,'\n');
  67. }
  68. void w_null(struct varbuf *vb,
  69. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  70. enum fwriteflags flags, const struct fieldinfo *fip) {
  71. }
  72. void w_section(struct varbuf *vb,
  73. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  74. enum fwriteflags flags, const struct fieldinfo *fip) {
  75. const char *value= pigp->section;
  76. if (!value || !*value) return;
  77. if (flags&fw_printheader)
  78. varbufaddstr(vb,"Section: ");
  79. varbufaddstr(vb,value);
  80. if (flags&fw_printheader)
  81. varbufaddc(vb,'\n');
  82. }
  83. void w_charfield(struct varbuf *vb,
  84. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  85. enum fwriteflags flags, const struct fieldinfo *fip) {
  86. const char *value= pifp->valid ? PKGPFIELD(pifp,fip->integer,const char*) : NULL;
  87. if (!value || !*value) return;
  88. if (flags&fw_printheader) {
  89. varbufaddstr(vb,fip->name);
  90. varbufaddstr(vb, ": ");
  91. }
  92. varbufaddstr(vb,value);
  93. if (flags&fw_printheader)
  94. varbufaddc(vb,'\n');
  95. }
  96. void w_filecharf(struct varbuf *vb,
  97. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  98. enum fwriteflags flags, const struct fieldinfo *fip) {
  99. struct filedetails *fdp;
  100. if (pifp != &pigp->available) return;
  101. fdp= pigp->files;
  102. if (!fdp || !FILEFFIELD(fdp,fip->integer,const char*)) return;
  103. if (flags&fw_printheader) {
  104. varbufaddstr(vb,fip->name);
  105. varbufaddc(vb,':');
  106. }
  107. while (fdp) {
  108. varbufaddc(vb,' ');
  109. varbufaddstr(vb,FILEFFIELD(fdp,fip->integer,const char*));
  110. fdp= fdp->next;
  111. }
  112. if (flags&fw_printheader)
  113. varbufaddc(vb,'\n');
  114. }
  115. void w_booleandefno(struct varbuf *vb,
  116. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  117. enum fwriteflags flags, const struct fieldinfo *fip) {
  118. int value= pifp->valid ? PKGPFIELD(pifp,fip->integer,int) : -1;
  119. if (!(flags&fw_printheader)) {
  120. varbufaddstr(vb, (value==1) ? "yes" : "no");
  121. return;
  122. }
  123. if (!value) return;
  124. assert(value==1);
  125. varbufaddstr(vb,fip->name); varbufaddstr(vb, ": yes\n");
  126. }
  127. void w_priority(struct varbuf *vb,
  128. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  129. enum fwriteflags flags, const struct fieldinfo *fip) {
  130. if (pigp->priority == pri_unknown) return;
  131. assert(pigp->priority <= pri_unknown);
  132. if (flags&fw_printheader)
  133. varbufaddstr(vb,"Priority: ");
  134. varbufaddstr(vb,
  135. pigp->priority == pri_other
  136. ? pigp->otherpriority
  137. : priorityinfos[pigp->priority].name);
  138. if (flags&fw_printheader)
  139. varbufaddc(vb,'\n');
  140. }
  141. void w_status(struct varbuf *vb,
  142. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  143. enum fwriteflags flags, const struct fieldinfo *fip) {
  144. if (pifp != &pigp->installed) return;
  145. assert(pigp->want <= want_purge);
  146. assert(pigp->eflag <= eflagv_reinstreq); /* hold and hold-reinstreq NOT allowed */
  147. assert(pigp->status <= stat_configfiles);
  148. if (flags&fw_printheader)
  149. varbufaddstr(vb,"Status: ");
  150. varbufaddstr(vb,wantinfos[pigp->want].name); varbufaddc(vb,' ');
  151. varbufaddstr(vb,eflaginfos[pigp->eflag].name); varbufaddc(vb,' ');
  152. varbufaddstr(vb,statusinfos[pigp->status].name);
  153. if (flags&fw_printheader)
  154. varbufaddc(vb,'\n');
  155. }
  156. void varbufdependency(struct varbuf *vb, struct dependency *dep) {
  157. struct deppossi *dop;
  158. const char *possdel;
  159. possdel= "";
  160. for (dop= dep->list; dop; dop= dop->next) {
  161. assert(dop->up == dep);
  162. varbufaddstr(vb,possdel); possdel= " | ";
  163. varbufaddstr(vb,dop->ed->name);
  164. if (dop->verrel != dvr_none) {
  165. varbufaddstr(vb," (");
  166. switch (dop->verrel) {
  167. case dvr_exact: varbufaddc(vb,'='); break;
  168. case dvr_laterequal: varbufaddstr(vb,">="); break;
  169. case dvr_earlierequal: varbufaddstr(vb,"<="); break;
  170. case dvr_laterstrict: varbufaddstr(vb,">>"); break;
  171. case dvr_earlierstrict: varbufaddstr(vb,"<<"); break;
  172. default: internerr("unknown verrel");
  173. }
  174. varbufaddc(vb,' ');
  175. varbufversion(vb,&dop->version,vdew_nonambig);
  176. varbufaddc(vb,')');
  177. }
  178. }
  179. }
  180. void w_dependency(struct varbuf *vb,
  181. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  182. enum fwriteflags flags, const struct fieldinfo *fip) {
  183. char fnbuf[50];
  184. const char *depdel;
  185. struct dependency *dyp;
  186. if (!pifp->valid) return;
  187. if (flags&fw_printheader)
  188. sprintf(fnbuf,"%s: ",fip->name);
  189. else
  190. fnbuf[0] = '\0';
  191. depdel= fnbuf;
  192. for (dyp= pifp->depends; dyp; dyp= dyp->next) {
  193. if (dyp->type != fip->integer) continue;
  194. assert(dyp->up == pigp);
  195. varbufaddstr(vb,depdel); depdel= ", ";
  196. varbufdependency(vb,dyp);
  197. }
  198. if ((flags&fw_printheader) && (depdel!=fnbuf))
  199. varbufaddc(vb,'\n');
  200. }
  201. void w_conffiles(struct varbuf *vb,
  202. const struct pkginfo *pigp, const struct pkginfoperfile *pifp,
  203. enum fwriteflags flags, const struct fieldinfo *fip) {
  204. struct conffile *i;
  205. if (!pifp->valid || !pifp->conffiles || pifp == &pigp->available) return;
  206. if (flags&fw_printheader)
  207. varbufaddstr(vb,"Conffiles:\n");
  208. for (i=pifp->conffiles; i; i= i->next) {
  209. if (i!=pifp->conffiles) varbufaddc(vb,'\n');
  210. varbufaddc(vb,' '); varbufaddstr(vb,i->name); varbufaddc(vb,' ');
  211. varbufaddstr(vb,i->hash);
  212. if (i->obsolete) varbufaddstr(vb," obsolete");
  213. }
  214. if (flags&fw_printheader)
  215. varbufaddc(vb,'\n');
  216. }
  217. void varbufrecord(struct varbuf *vb,
  218. const struct pkginfo *pigp, const struct pkginfoperfile *pifp) {
  219. const struct fieldinfo *fip;
  220. const struct arbitraryfield *afp;
  221. for (fip= fieldinfos; fip->name; fip++) {
  222. fip->wcall(vb,pigp,pifp,fw_printheader,fip);
  223. }
  224. if (pifp->valid) {
  225. for (afp= pifp->arbs; afp; afp= afp->next) {
  226. varbufaddstr(vb,afp->name); varbufaddstr(vb,": ");
  227. varbufaddstr(vb,afp->value); varbufaddc(vb,'\n');
  228. }
  229. }
  230. }
  231. void writerecord(FILE *file, const char *filename,
  232. const struct pkginfo *pigp, const struct pkginfoperfile *pifp) {
  233. struct varbuf vb;
  234. varbufinit(&vb);
  235. varbufrecord(&vb,pigp,pifp);
  236. varbufaddc(&vb,'\0');
  237. if (fputs(vb.buf,file) < 0)
  238. ohshite(_("failed to write details of `%.50s' to `%.250s'"), pigp->name,
  239. filename);
  240. varbuffree(&vb);
  241. }
  242. void writedb(const char *filename, int available, int mustsync) {
  243. static char writebuf[8192];
  244. struct pkgiterator *it;
  245. struct pkginfo *pigp;
  246. struct pkginfoperfile *pifp;
  247. char *oldfn, *newfn;
  248. const char *which;
  249. FILE *file;
  250. struct varbuf vb;
  251. int old_umask;
  252. which= available ? "available" : "status";
  253. oldfn= m_malloc(strlen(filename)+sizeof(OLDDBEXT));
  254. strcpy(oldfn,filename); strcat(oldfn,OLDDBEXT);
  255. newfn= m_malloc(strlen(filename)+sizeof(NEWDBEXT));
  256. strcpy(newfn,filename); strcat(newfn,NEWDBEXT);
  257. varbufinit(&vb);
  258. old_umask = umask(022);
  259. file= fopen(newfn,"w");
  260. umask(old_umask);
  261. if (!file) ohshite(_("failed to open `%s' for writing %s information"),filename,which);
  262. if (setvbuf(file,writebuf,_IOFBF,sizeof(writebuf)))
  263. ohshite(_("unable to set buffering on status file"));
  264. it= iterpkgstart();
  265. while ((pigp= iterpkgnext(it)) != NULL) {
  266. pifp= available ? &pigp->available : &pigp->installed;
  267. /* Don't dump records which have no useful content. */
  268. if (!informative(pigp,pifp)) continue;
  269. if (!pifp->valid) blankpackageperfile(pifp);
  270. varbufrecord(&vb,pigp,pifp);
  271. varbufaddc(&vb,'\n'); varbufaddc(&vb,0);
  272. if (fputs(vb.buf,file) < 0)
  273. ohshite(_("failed to write %s record about `%.50s' to `%.250s'"),
  274. which, pigp->name, filename);
  275. varbufreset(&vb);
  276. }
  277. iterpkgend(it);
  278. varbuffree(&vb);
  279. if (mustsync) {
  280. if (fflush(file))
  281. ohshite(_("failed to flush %s information to `%.250s'"), which, filename);
  282. if (fsync(fileno(file)))
  283. ohshite(_("failed to fsync %s information to `%.250s'"), which, filename);
  284. }
  285. if (fclose(file)) ohshite(_("failed to close `%.250s' after writing %s information"),
  286. filename, which);
  287. unlink(oldfn);
  288. if (link(filename,oldfn) && errno != ENOENT)
  289. ohshite(_("failed to link `%.250s' to `%.250s' for backup of %s info"),
  290. filename, oldfn, which);
  291. if (rename(newfn,filename))
  292. ohshite(_("failed to install `%.250s' as `%.250s' containing %s info"),
  293. newfn, filename, which);
  294. }