Imported Upstream version 1.16.10
[services/dpkg.git] / lib / dpkg / dump.c
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  * Copyright © 2006,2008-2012 Guillem Jover <guillem@debian.org>
8  * Copyright © 2011 Linaro Limited
9  * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
10  *
11  * This is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25 /* FIXME: Don't write uninteresting packages. */
26
27 #include <config.h>
28 #include <compat.h>
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32
33 #include <assert.h>
34 #include <errno.h>
35 #include <ctype.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <stdbool.h>
41
42 #include <dpkg/i18n.h>
43 #include <dpkg/dpkg.h>
44 #include <dpkg/dpkg-db.h>
45 #include <dpkg/string.h>
46 #include <dpkg/dir.h>
47 #include <dpkg/parsedump.h>
48
49 void
50 w_name(struct varbuf *vb,
51        const struct pkginfo *pkg, const struct pkgbin *pkgbin,
52        enum fwriteflags flags, const struct fieldinfo *fip)
53 {
54   assert(pkg->set->name);
55   if (flags&fw_printheader)
56     varbuf_add_str(vb, "Package: ");
57   varbuf_add_str(vb, pkg->set->name);
58   if (flags&fw_printheader)
59     varbuf_add_char(vb, '\n');
60 }
61
62 void
63 w_version(struct varbuf *vb,
64           const struct pkginfo *pkg, const struct pkgbin *pkgbin,
65           enum fwriteflags flags, const struct fieldinfo *fip)
66 {
67   if (!dpkg_version_is_informative(&pkgbin->version))
68     return;
69   if (flags&fw_printheader)
70     varbuf_add_str(vb, "Version: ");
71   varbufversion(vb, &pkgbin->version, vdew_nonambig);
72   if (flags&fw_printheader)
73     varbuf_add_char(vb, '\n');
74 }
75
76 void
77 w_configversion(struct varbuf *vb,
78                 const struct pkginfo *pkg, const struct pkgbin *pkgbin,
79                 enum fwriteflags flags, const struct fieldinfo *fip)
80 {
81   if (pkgbin != &pkg->installed)
82     return;
83   if (!dpkg_version_is_informative(&pkg->configversion))
84     return;
85   if (pkg->status == stat_installed ||
86       pkg->status == stat_notinstalled ||
87       pkg->status == stat_triggerspending)
88     return;
89   if (flags&fw_printheader)
90     varbuf_add_str(vb, "Config-Version: ");
91   varbufversion(vb, &pkg->configversion, vdew_nonambig);
92   if (flags&fw_printheader)
93     varbuf_add_char(vb, '\n');
94 }
95
96 void
97 w_null(struct varbuf *vb,
98        const struct pkginfo *pkg, const struct pkgbin *pkgbin,
99        enum fwriteflags flags, const struct fieldinfo *fip)
100 {
101 }
102
103 void
104 w_section(struct varbuf *vb,
105           const struct pkginfo *pkg, const struct pkgbin *pkgbin,
106           enum fwriteflags flags, const struct fieldinfo *fip)
107 {
108   const char *value = pkg->section;
109
110   if (str_is_unset(value))
111     return;
112   if (flags&fw_printheader)
113     varbuf_add_str(vb, "Section: ");
114   varbuf_add_str(vb, value);
115   if (flags&fw_printheader)
116     varbuf_add_char(vb, '\n');
117 }
118
119 void
120 w_charfield(struct varbuf *vb,
121             const struct pkginfo *pkg, const struct pkgbin *pkgbin,
122             enum fwriteflags flags, const struct fieldinfo *fip)
123 {
124   const char *value = PKGPFIELD(pkgbin, fip->integer, const char *);
125
126   if (str_is_unset(value))
127     return;
128   if (flags&fw_printheader) {
129     varbuf_add_str(vb, fip->name);
130     varbuf_add_str(vb, ": ");
131   }
132   varbuf_add_str(vb, value);
133   if (flags&fw_printheader)
134     varbuf_add_char(vb, '\n');
135 }
136
137 void
138 w_filecharf(struct varbuf *vb,
139             const struct pkginfo *pkg, const struct pkgbin *pkgbin,
140             enum fwriteflags flags, const struct fieldinfo *fip)
141 {
142   struct filedetails *fdp;
143
144   if (pkgbin != &pkg->available)
145     return;
146   fdp = pkg->files;
147   if (!fdp || !FILEFFIELD(fdp,fip->integer,const char*)) return;
148
149   if (flags&fw_printheader) {
150     varbuf_add_str(vb, fip->name);
151     varbuf_add_char(vb, ':');
152   }
153
154   while (fdp) {
155     varbuf_add_char(vb, ' ');
156     varbuf_add_str(vb, FILEFFIELD(fdp, fip->integer, const char *));
157     fdp= fdp->next;
158   }
159
160   if (flags&fw_printheader)
161     varbuf_add_char(vb, '\n');
162 }
163
164 void
165 w_booleandefno(struct varbuf *vb,
166                const struct pkginfo *pkg, const struct pkgbin *pkgbin,
167                enum fwriteflags flags, const struct fieldinfo *fip)
168 {
169   bool value = PKGPFIELD(pkgbin, fip->integer, bool);
170
171   if ((flags & fw_printheader) && !value)
172     return;
173
174   if (flags & fw_printheader) {
175     varbuf_add_str(vb, fip->name);
176     varbuf_add_str(vb, ": ");
177   }
178
179   varbuf_add_str(vb, value ? "yes" : "no");
180
181   if (flags & fw_printheader)
182     varbuf_add_char(vb, '\n');
183 }
184
185 void
186 w_multiarch(struct varbuf *vb,
187             const struct pkginfo *pkg, const struct pkgbin *pkgbin,
188             enum fwriteflags flags, const struct fieldinfo *fip)
189 {
190   int value = PKGPFIELD(pkgbin, fip->integer, int);
191
192   if ((flags & fw_printheader) && !value)
193     return;
194
195   if (flags & fw_printheader) {
196     varbuf_add_str(vb, fip->name);
197     varbuf_add_str(vb, ": ");
198   }
199
200   varbuf_add_str(vb, multiarchinfos[value].name);
201
202   if (flags & fw_printheader)
203     varbuf_add_char(vb, '\n');
204 }
205
206 void
207 w_architecture(struct varbuf *vb,
208                const struct pkginfo *pkg, const struct pkgbin *pkgbin,
209                enum fwriteflags flags, const struct fieldinfo *fip)
210 {
211   if (!pkgbin->arch)
212     return;
213   if (pkgbin->arch->type == arch_none)
214     return;
215   if (pkgbin->arch->type == arch_empty)
216     return;
217
218   if (flags & fw_printheader) {
219     varbuf_add_str(vb, fip->name);
220     varbuf_add_str(vb, ": ");
221   }
222   varbuf_add_str(vb, pkgbin->arch->name);
223   if (flags & fw_printheader)
224     varbuf_add_char(vb, '\n');
225 }
226
227 void
228 w_priority(struct varbuf *vb,
229            const struct pkginfo *pkg, const struct pkgbin *pkgbin,
230            enum fwriteflags flags, const struct fieldinfo *fip)
231 {
232   if (pkg->priority == pri_unknown)
233     return;
234   assert(pkg->priority <= pri_unknown);
235   if (flags&fw_printheader)
236     varbuf_add_str(vb, "Priority: ");
237   varbuf_add_str(vb, pkg->priority == pri_other ?
238                      pkg->otherpriority :
239                      priorityinfos[pkg->priority].name);
240   if (flags&fw_printheader)
241     varbuf_add_char(vb, '\n');
242 }
243
244 void
245 w_status(struct varbuf *vb,
246          const struct pkginfo *pkg, const struct pkgbin *pkgbin,
247          enum fwriteflags flags, const struct fieldinfo *fip)
248 {
249   if (pkgbin != &pkg->installed)
250     return;
251   assert(pkg->want <= want_purge);
252   assert(pkg->eflag <= eflag_reinstreq);
253
254 #define PEND pkg->trigpend_head
255 #define AW pkg->trigaw.head
256   switch (pkg->status) {
257   case stat_notinstalled:
258   case stat_configfiles:
259     assert(!PEND);
260     assert(!AW);
261     break;
262   case stat_halfinstalled:
263   case stat_unpacked:
264   case stat_halfconfigured:
265     assert(!PEND);
266     break;
267   case stat_triggersawaited:
268     assert(AW);
269     break;
270   case stat_triggerspending:
271     assert(PEND);
272     assert(!AW);
273     break;
274   case stat_installed:
275     assert(!PEND);
276     assert(!AW);
277     break;
278   default:
279     internerr("unknown package status '%d'", pkg->status);
280   }
281 #undef PEND
282 #undef AW
283
284   if (flags&fw_printheader)
285     varbuf_add_str(vb, "Status: ");
286   varbuf_add_str(vb, wantinfos[pkg->want].name);
287   varbuf_add_char(vb, ' ');
288   varbuf_add_str(vb, eflaginfos[pkg->eflag].name);
289   varbuf_add_char(vb, ' ');
290   varbuf_add_str(vb, statusinfos[pkg->status].name);
291   if (flags&fw_printheader)
292     varbuf_add_char(vb, '\n');
293 }
294
295 void varbufdependency(struct varbuf *vb, struct dependency *dep) {
296   struct deppossi *dop;
297   const char *possdel;
298
299   possdel= "";
300   for (dop= dep->list; dop; dop= dop->next) {
301     assert(dop->up == dep);
302     varbuf_add_str(vb, possdel);
303     possdel = " | ";
304     varbuf_add_str(vb, dop->ed->name);
305     if (!dop->arch_is_implicit)
306       varbuf_add_archqual(vb, dop->arch);
307     if (dop->verrel != dpkg_relation_none) {
308       varbuf_add_str(vb, " (");
309       switch (dop->verrel) {
310       case dpkg_relation_eq:
311         varbuf_add_char(vb, '=');
312         break;
313       case dpkg_relation_ge:
314         varbuf_add_str(vb, ">=");
315         break;
316       case dpkg_relation_le:
317         varbuf_add_str(vb, "<=");
318         break;
319       case dpkg_relation_gt:
320         varbuf_add_str(vb, ">>");
321         break;
322       case dpkg_relation_lt:
323         varbuf_add_str(vb, "<<");
324         break;
325       default:
326         internerr("unknown dpkg_relation %d", dop->verrel);
327       }
328       varbuf_add_char(vb, ' ');
329       varbufversion(vb,&dop->version,vdew_nonambig);
330       varbuf_add_char(vb, ')');
331     }
332   }
333 }
334
335 void
336 w_dependency(struct varbuf *vb,
337              const struct pkginfo *pkg, const struct pkgbin *pkgbin,
338              enum fwriteflags flags, const struct fieldinfo *fip)
339 {
340   char fnbuf[50];
341   const char *depdel;
342   struct dependency *dyp;
343
344   if (flags&fw_printheader)
345     sprintf(fnbuf,"%s: ",fip->name);
346   else
347     fnbuf[0] = '\0';
348
349   depdel= fnbuf;
350   for (dyp = pkgbin->depends; dyp; dyp = dyp->next) {
351     if (dyp->type != fip->integer) continue;
352     assert(dyp->up == pkg);
353     varbuf_add_str(vb, depdel);
354     depdel = ", ";
355     varbufdependency(vb,dyp);
356   }
357   if ((flags&fw_printheader) && (depdel!=fnbuf))
358     varbuf_add_char(vb, '\n');
359 }
360
361 void
362 w_conffiles(struct varbuf *vb,
363             const struct pkginfo *pkg, const struct pkgbin *pkgbin,
364             enum fwriteflags flags, const struct fieldinfo *fip)
365 {
366   struct conffile *i;
367
368   if (!pkgbin->conffiles || pkgbin == &pkg->available)
369     return;
370   if (flags&fw_printheader)
371     varbuf_add_str(vb, "Conffiles:\n");
372   for (i = pkgbin->conffiles; i; i = i->next) {
373     if (i != pkgbin->conffiles)
374       varbuf_add_char(vb, '\n');
375     varbuf_add_char(vb, ' ');
376     varbuf_add_str(vb, i->name);
377     varbuf_add_char(vb, ' ');
378     varbuf_add_str(vb, i->hash);
379     if (i->obsolete)
380       varbuf_add_str(vb, " obsolete");
381   }
382   if (flags&fw_printheader)
383     varbuf_add_char(vb, '\n');
384 }
385
386 void
387 w_trigpend(struct varbuf *vb,
388            const struct pkginfo *pkg, const struct pkgbin *pkgbin,
389            enum fwriteflags flags, const struct fieldinfo *fip)
390 {
391   struct trigpend *tp;
392
393   if (pkgbin == &pkg->available || !pkg->trigpend_head)
394     return;
395
396   assert(pkg->status >= stat_triggersawaited &&
397          pkg->status <= stat_triggerspending);
398
399   if (flags & fw_printheader)
400     varbuf_add_str(vb, "Triggers-Pending:");
401   for (tp = pkg->trigpend_head; tp; tp = tp->next) {
402     varbuf_add_char(vb, ' ');
403     varbuf_add_str(vb, tp->name);
404   }
405   if (flags & fw_printheader)
406     varbuf_add_char(vb, '\n');
407 }
408
409 void
410 w_trigaw(struct varbuf *vb,
411          const struct pkginfo *pkg, const struct pkgbin *pkgbin,
412          enum fwriteflags flags, const struct fieldinfo *fip)
413 {
414   struct trigaw *ta;
415
416   if (pkgbin == &pkg->available || !pkg->trigaw.head)
417     return;
418
419   assert(pkg->status > stat_configfiles &&
420          pkg->status <= stat_triggersawaited);
421
422   if (flags & fw_printheader)
423     varbuf_add_str(vb, "Triggers-Awaited:");
424   for (ta = pkg->trigaw.head; ta; ta = ta->sameaw.next) {
425     varbuf_add_char(vb, ' ');
426     varbuf_add_pkgbin_name(vb, ta->pend, &ta->pend->installed, pnaw_nonambig);
427   }
428   if (flags & fw_printheader)
429     varbuf_add_char(vb, '\n');
430 }
431
432 void
433 varbufrecord(struct varbuf *vb,
434              const struct pkginfo *pkg, const struct pkgbin *pkgbin)
435 {
436   const struct fieldinfo *fip;
437   const struct arbitraryfield *afp;
438
439   for (fip= fieldinfos; fip->name; fip++) {
440     fip->wcall(vb, pkg, pkgbin, fw_printheader, fip);
441   }
442   for (afp = pkgbin->arbs; afp; afp = afp->next) {
443     varbuf_add_str(vb, afp->name);
444     varbuf_add_str(vb, ": ");
445     varbuf_add_str(vb, afp->value);
446     varbuf_add_char(vb, '\n');
447   }
448 }
449
450 void
451 writerecord(FILE *file, const char *filename,
452             const struct pkginfo *pkg, const struct pkgbin *pkgbin)
453 {
454   struct varbuf vb = VARBUF_INIT;
455
456   varbufrecord(&vb, pkg, pkgbin);
457   varbuf_end_str(&vb);
458   if (fputs(vb.buf,file) < 0) {
459     struct varbuf pkgname = VARBUF_INIT;
460     int errno_saved = errno;
461
462     varbuf_add_pkgbin_name(&pkgname, pkg, pkgbin, pnaw_nonambig);
463
464     errno = errno_saved;
465     ohshite(_("failed to write details of `%.50s' to `%.250s'"),
466             pkgname.buf, filename);
467   }
468
469   varbuf_destroy(&vb);
470 }
471
472 void
473 writedb(const char *filename, enum writedb_flags flags)
474 {
475   static char writebuf[8192];
476
477   struct pkgiterator *it;
478   struct pkginfo *pkg;
479   struct pkgbin *pkgbin;
480   const char *which;
481   struct atomic_file *file;
482   struct varbuf vb = VARBUF_INIT;
483
484   which = (flags & wdb_dump_available) ? "available" : "status";
485
486   file = atomic_file_new(filename, aff_backup);
487   atomic_file_open(file);
488   if (setvbuf(file->fp, writebuf, _IOFBF, sizeof(writebuf)))
489     ohshite(_("unable to set buffering on %s database file"), which);
490
491   it = pkg_db_iter_new();
492   while ((pkg = pkg_db_iter_next_pkg(it)) != NULL) {
493     pkgbin = (flags & wdb_dump_available) ? &pkg->available : &pkg->installed;
494     /* Don't dump records which have no useful content. */
495     if (!pkg_is_informative(pkg, pkgbin))
496       continue;
497     varbufrecord(&vb, pkg, pkgbin);
498     varbuf_add_char(&vb, '\n');
499     varbuf_end_str(&vb);
500     if (fputs(vb.buf, file->fp) < 0)
501       ohshite(_("failed to write %s database record about '%.50s' to '%.250s'"),
502               which, pkgbin_name(pkg, pkgbin, pnaw_nonambig), filename);
503     varbuf_reset(&vb);
504   }
505   pkg_db_iter_free(it);
506   varbuf_destroy(&vb);
507   if (flags & wdb_must_sync)
508     atomic_file_sync(file);
509
510   atomic_file_close(file);
511   atomic_file_commit(file);
512   atomic_file_free(file);
513
514   if (flags & wdb_must_sync)
515     dir_sync_path_parent(filename);
516 }