dump.c 13 KB

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