summaryrefslogtreecommitdiffstats
path: root/lib/dpkg/dump.c
blob: 05018baafc439f54fdb8bae9bd3a80be7ca87ab8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
 * libdpkg - Debian packaging suite library routines
 * dump.c - code to write in-core database to a file
 *
 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
 * Copyright © 2001 Wichert Akkerman
 * Copyright © 2006,2008-2014 Guillem Jover <guillem@debian.org>
 * Copyright © 2011 Linaro Limited
 * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <compat.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include <dpkg/i18n.h>
#include <dpkg/dpkg.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/pkg-array.h>
#include <dpkg/pkg-show.h>
#include <dpkg/string.h>
#include <dpkg/dir.h>
#include <dpkg/parsedump.h>

static inline void
varbuf_add_fieldname(struct varbuf *vb, const struct fieldinfo *fip)
{
  varbuf_add_str(vb, fip->name);
  varbuf_add_str(vb, ": ");
}

void
w_name(struct varbuf *vb,
       const struct pkginfo *pkg, const struct pkgbin *pkgbin,
       enum fwriteflags flags, const struct fieldinfo *fip)
{
  if (pkg->set->name == NULL)
    internerr("pkgset has no name");

  if (flags&fw_printheader)
    varbuf_add_str(vb, "Package: ");
  varbuf_add_str(vb, pkg->set->name);
  if (flags&fw_printheader)
    varbuf_add_char(vb, '\n');
}

void
w_version(struct varbuf *vb,
          const struct pkginfo *pkg, const struct pkgbin *pkgbin,
          enum fwriteflags flags, const struct fieldinfo *fip)
{
  if (!dpkg_version_is_informative(&pkgbin->version))
    return;
  if (flags&fw_printheader)
    varbuf_add_str(vb, "Version: ");
  varbufversion(vb, &pkgbin->version, vdew_nonambig);
  if (flags&fw_printheader)
    varbuf_add_char(vb, '\n');
}

void
w_configversion(struct varbuf *vb,
                const struct pkginfo *pkg, const struct pkgbin *pkgbin,
                enum fwriteflags flags, const struct fieldinfo *fip)
{
  if (pkgbin != &pkg->installed)
    return;
  if (!dpkg_version_is_informative(&pkg->configversion))
    return;
  if (pkg->status == PKG_STAT_INSTALLED ||
      pkg->status == PKG_STAT_NOTINSTALLED ||
      pkg->status == PKG_STAT_TRIGGERSPENDING)
    return;
  if (flags&fw_printheader)
    varbuf_add_str(vb, "Config-Version: ");
  varbufversion(vb, &pkg->configversion, vdew_nonambig);
  if (flags&fw_printheader)
    varbuf_add_char(vb, '\n');
}

void
w_null(struct varbuf *vb,
       const struct pkginfo *pkg, const struct pkgbin *pkgbin,
       enum fwriteflags flags, const struct fieldinfo *fip)
{
}

void
w_section(struct varbuf *vb,
          const struct pkginfo *pkg, const struct pkgbin *pkgbin,
          enum fwriteflags flags, const struct fieldinfo *fip)
{
  const char *value = pkg->section;

  if (str_is_unset(value))
    return;
  if (flags&fw_printheader)
    varbuf_add_str(vb, "Section: ");
  varbuf_add_str(vb, value);
  if (flags&fw_printheader)
    varbuf_add_char(vb, '\n');
}

void
w_charfield(struct varbuf *vb,
            const struct pkginfo *pkg, const struct pkgbin *pkgbin,
            enum fwriteflags flags, const struct fieldinfo *fip)
{
  const char *value = STRUCTFIELD(pkgbin, fip->integer, const char *);

  if (str_is_unset(value))
    return;
  if (flags & fw_printheader)
    varbuf_add_fieldname(vb, fip);
  varbuf_add_str(vb, value);
  if (flags&fw_printheader)
    varbuf_add_char(vb, '\n');
}

void
w_archives(struct varbuf *vb,
           const struct pkginfo *pkg, const struct pkgbin *pkgbin,
           enum fwriteflags flags, const struct fieldinfo *fip)
{
  struct archivedetails *archive;

  if (pkgbin != &pkg->available)
    return;
  archive = pkg->archives;
  if (!archive || !STRUCTFIELD(archive, fip->integer, const char *))
    return;

  if (flags&fw_printheader) {
    varbuf_add_str(vb, fip->name);
    varbuf_add_char(vb, ':');
  }

  while (archive) {
    varbuf_add_char(vb, ' ');
    varbuf_add_str(vb, STRUCTFIELD(archive, fip->integer, const char *));
    archive = archive->next;
  }

  if (flags&fw_printheader)
    varbuf_add_char(vb, '\n');
}

void
w_booleandefno(struct varbuf *vb,
               const struct pkginfo *pkg, const struct pkgbin *pkgbin,
               enum fwriteflags flags, const struct fieldinfo *fip)
{
  bool value = STRUCTFIELD(pkgbin, fip->integer, bool);

  if ((flags & fw_printheader) && !value)
    return;

  if (flags & fw_printheader)
    varbuf_add_fieldname(vb, fip);

  varbuf_add_str(vb, value ? "yes" : "no");

  if (flags & fw_printheader)
    varbuf_add_char(vb, '\n');
}

void
w_multiarch(struct varbuf *vb,
            const struct pkginfo *pkg, const struct pkgbin *pkgbin,
            enum fwriteflags flags, const struct fieldinfo *fip)
{
  int value = STRUCTFIELD(pkgbin, fip->integer, int);

  if ((flags & fw_printheader) && !value)
    return;

  if (flags & fw_printheader)
    varbuf_add_fieldname(vb, fip);

  varbuf_add_str(vb, multiarchinfos[value].name);

  if (flags & fw_printheader)
    varbuf_add_char(vb, '\n');
}

void
w_architecture(struct varbuf *vb,
               const struct pkginfo *pkg, const struct pkgbin *pkgbin,
               enum fwriteflags flags, const struct fieldinfo *fip)
{
  if (!pkgbin->arch)
    return;
  if (pkgbin->arch->type == DPKG_ARCH_NONE)
    return;
  if (pkgbin->arch->type == DPKG_ARCH_EMPTY)
    return;

  if (flags & fw_printheader)
    varbuf_add_fieldname(vb, fip);
  varbuf_add_str(vb, pkgbin->arch->name);
  if (flags & fw_printheader)
    varbuf_add_char(vb, '\n');
}

void
w_priority(struct varbuf *vb,
           const struct pkginfo *pkg, const struct pkgbin *pkgbin,
           enum fwriteflags flags, const struct fieldinfo *fip)
{
  if (pkg->priority == PKG_PRIO_UNKNOWN)
    return;

  if (pkg->priority > PKG_PRIO_UNKNOWN)
    internerr("package %s has out-of-range priority %d",
              pkgbin_name_const(pkg, pkgbin, pnaw_always), pkg->priority);

  if (flags&fw_printheader)
    varbuf_add_str(vb, "Priority: ");
  varbuf_add_str(vb, pkg_priority_name(pkg));
  if (flags&fw_printheader)
    varbuf_add_char(vb, '\n');
}

void
w_status(struct varbuf *vb,
         const struct pkginfo *pkg, const struct pkgbin *pkgbin,
         enum fwriteflags flags, const struct fieldinfo *fip)
{
  if (pkgbin != &pkg->installed)
    return;

  if (pkg->want > PKG_WANT_PURGE)
    internerr("package %s has unknown want state %d",
              pkgbin_name_const(pkg, pkgbin, pnaw_always), pkg->want);
  if (pkg->eflag > PKG_EFLAG_REINSTREQ)
    internerr("package %s has unknown error state %d",
              pkgbin_name_const(pkg, pkgbin, pnaw_always), pkg->eflag);

  switch (pkg->status) {
  case PKG_STAT_NOTINSTALLED:
  case PKG_STAT_CONFIGFILES:
    if (pkg->trigpend_head || pkg->trigaw.head)
      internerr("package %s in state %s, has awaited or pending triggers",
              pkgbin_name_const(pkg, pkgbin, pnaw_always), pkg_status_name(pkg));
    break;
  case PKG_STAT_HALFINSTALLED:
  case PKG_STAT_UNPACKED:
  case PKG_STAT_HALFCONFIGURED:
    if (pkg->trigpend_head)
      internerr("package %s in state %s, has pending triggers",
              pkgbin_name_const(pkg, pkgbin, pnaw_always), pkg_status_name(pkg));
    break;
  case PKG_STAT_TRIGGERSAWAITED:
    if (pkg->trigaw.head == NULL)
      internerr("package %s in state %s, has no awaited triggers",
                pkgbin_name_const(pkg, pkgbin, pnaw_always), pkg_status_name(pkg));
    break;
  case PKG_STAT_TRIGGERSPENDING:
    if (pkg->trigpend_head == NULL || pkg->trigaw.head)
      internerr("package %s in stata %s, has awaited or no pending triggers",
              pkgbin_name_const(pkg, pkgbin, pnaw_always), pkg_status_name(pkg));
    break;
  case PKG_STAT_INSTALLED:
    if (pkg->trigpend_head || pkg->trigaw.head)
      internerr("package %s in state %s, has awaited or pending triggers",
              pkgbin_name_const(pkg, pkgbin, pnaw_always), pkg_status_name(pkg));
    break;
  default:
    internerr("unknown package status '%d'", pkg->status);
  }

  if (flags&fw_printheader)
    varbuf_add_str(vb, "Status: ");
  varbuf_add_str(vb, pkg_want_name(pkg));
  varbuf_add_char(vb, ' ');
  varbuf_add_str(vb, pkg_eflag_name(pkg));
  varbuf_add_char(vb, ' ');
  varbuf_add_str(vb, pkg_status_name(pkg));
  if (flags&fw_printheader)
    varbuf_add_char(vb, '\n');
}

void varbufdependency(struct varbuf *vb, struct dependency *dep) {
  struct deppossi *dop;
  const char *possdel;

  possdel= "";
  for (dop= dep->list; dop; dop= dop->next) {
    if (dop->up != dep)
      internerr("dependency and deppossi not linked properly");

    varbuf_add_str(vb, possdel);
    possdel = " | ";
    varbuf_add_str(vb, dop->ed->name);
    if (!dop->arch_is_implicit)
      varbuf_add_archqual(vb, dop->arch);
    if (dop->verrel != DPKG_RELATION_NONE) {
      varbuf_add_str(vb, " (");
      switch (dop->verrel) {
      case DPKG_RELATION_EQ:
        varbuf_add_char(vb, '=');
        break;
      case DPKG_RELATION_GE:
        varbuf_add_str(vb, ">=");
        break;
      case DPKG_RELATION_LE:
        varbuf_add_str(vb, "<=");
        break;
      case DPKG_RELATION_GT:
        varbuf_add_str(vb, ">>");
        break;
      case DPKG_RELATION_LT:
        varbuf_add_str(vb, "<<");
        break;
      default:
        internerr("unknown dpkg_relation %d", dop->verrel);
      }
      varbuf_add_char(vb, ' ');
      varbufversion(vb,&dop->version,vdew_nonambig);
      varbuf_add_char(vb, ')');
    }
  }
}

void
w_dependency(struct varbuf *vb,
             const struct pkginfo *pkg, const struct pkgbin *pkgbin,
             enum fwriteflags flags, const struct fieldinfo *fip)
{
  struct dependency *dyp;
  bool dep_found = false;

  for (dyp = pkgbin->depends; dyp; dyp = dyp->next) {
    if (dyp->type != fip->integer) continue;

    if (dyp->up != pkg)
      internerr("dependency and package %s not linked properly",
                pkgbin_name_const(pkg, pkgbin, pnaw_always));

    if (dep_found) {
      varbuf_add_str(vb, ", ");
    } else {
      if (flags & fw_printheader)
        varbuf_add_fieldname(vb, fip);
      dep_found = true;
    }
    varbufdependency(vb,dyp);
  }
  if ((flags & fw_printheader) && dep_found)
    varbuf_add_char(vb, '\n');
}

void
w_conffiles(struct varbuf *vb,
            const struct pkginfo *pkg, const struct pkgbin *pkgbin,
            enum fwriteflags flags, const struct fieldinfo *fip)
{
  struct conffile *i;

  if (!pkgbin->conffiles || pkgbin == &pkg->available)
    return;
  if (flags&fw_printheader)
    varbuf_add_str(vb, "Conffiles:\n");
  for (i = pkgbin->conffiles; i; i = i->next) {
    if (i != pkgbin->conffiles)
      varbuf_add_char(vb, '\n');
    varbuf_add_char(vb, ' ');
    varbuf_add_str(vb, i->name);
    varbuf_add_char(vb, ' ');
    varbuf_add_str(vb, i->hash);
    if (i->obsolete)
      varbuf_add_str(vb, " obsolete");
    if (i->remove_on_upgrade)
      varbuf_add_str(vb, " remove-on-upgrade");
  }
  if (flags&fw_printheader)
    varbuf_add_char(vb, '\n');
}

void
w_trigpend(struct varbuf *vb,
           const struct pkginfo *pkg, const struct pkgbin *pkgbin,
           enum fwriteflags flags, const struct fieldinfo *fip)
{
  struct trigpend *tp;

  if (pkgbin == &pkg->available || !pkg->trigpend_head)
    return;

  if (pkg->status < PKG_STAT_TRIGGERSAWAITED ||
      pkg->status > PKG_STAT_TRIGGERSPENDING)
    internerr("package %s in non-trigger state %s, has pending triggers",
              pkgbin_name_const(pkg, pkgbin, pnaw_always), pkg_status_name(pkg));

  if (flags & fw_printheader)
    varbuf_add_str(vb, "Triggers-Pending:");
  for (tp = pkg->trigpend_head; tp; tp = tp->next) {
    varbuf_add_char(vb, ' ');
    varbuf_add_str(vb, tp->name);
  }
  if (flags & fw_printheader)
    varbuf_add_char(vb, '\n');
}

void
w_trigaw(struct varbuf *vb,
         const struct pkginfo *pkg, const struct pkgbin *pkgbin,
         enum fwriteflags flags, const struct fieldinfo *fip)
{
  struct trigaw *ta;

  if (pkgbin == &pkg->available || !pkg->trigaw.head)
    return;

  if (pkg->status <= PKG_STAT_CONFIGFILES ||
      pkg->status > PKG_STAT_TRIGGERSAWAITED)
    internerr("package %s in state %s, has awaited triggers",
              pkgbin_name_const(pkg, pkgbin, pnaw_always), pkg_status_name(pkg));

  if (flags & fw_printheader)
    varbuf_add_str(vb, "Triggers-Awaited:");
  for (ta = pkg->trigaw.head; ta; ta = ta->sameaw.next) {
    varbuf_add_char(vb, ' ');
    varbuf_add_pkgbin_name(vb, ta->pend, &ta->pend->installed, pnaw_nonambig);
  }
  if (flags & fw_printheader)
    varbuf_add_char(vb, '\n');
}

void
varbuf_add_arbfield(struct varbuf *vb, const struct arbitraryfield *arbfield,
                    enum fwriteflags flags)
{
  if (flags & fw_printheader) {
    varbuf_add_str(vb, arbfield->name);
    varbuf_add_str(vb, ": ");
  }
  varbuf_add_str(vb, arbfield->value);
  if (flags & fw_printheader)
    varbuf_add_char(vb, '\n');
}

void
varbuf_stanza(struct varbuf *vb,
              const struct pkginfo *pkg, const struct pkgbin *pkgbin)
{
  const struct fieldinfo *fip;
  const struct arbitraryfield *afp;

  for (fip= fieldinfos; fip->name; fip++) {
    fip->wcall(vb, pkg, pkgbin, fw_printheader, fip);
  }
  for (afp = pkgbin->arbs; afp; afp = afp->next) {
    varbuf_add_arbfield(vb, afp, fw_printheader);
  }
}

void
write_stanza(FILE *file, const char *filename,
             const struct pkginfo *pkg, const struct pkgbin *pkgbin)
{
  struct varbuf vb = VARBUF_INIT;

  varbuf_stanza(&vb, pkg, pkgbin);
  varbuf_end_str(&vb);

  if (fputs(vb.buf, file) < 0)
    ohshite(_("failed to write details of '%.50s' to '%.250s'"),
            pkgbin_name_const(pkg, pkgbin, pnaw_nonambig), filename);

  varbuf_destroy(&vb);
}

void
writedb_stanzas(FILE *fp, const char *filename, enum writedb_flags flags)
{
  static char writebuf[8192];

  struct pkg_array array;
  struct pkginfo *pkg;
  struct pkgbin *pkgbin;
  const char *which;
  struct varbuf vb = VARBUF_INIT;
  int i;

  which = (flags & wdb_dump_available) ? "available" : "status";

  if (setvbuf(fp, writebuf, _IOFBF, sizeof(writebuf)))
    ohshite(_("unable to set buffering on %s database file"), which);

  pkg_array_init_from_hash(&array);
  pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);

  for (i = 0; i < array.n_pkgs; i++) {
    pkg = array.pkgs[i];
    pkgbin = (flags & wdb_dump_available) ? &pkg->available : &pkg->installed;

    /* Don't dump stanzas which have no useful content. */
    if (!pkg_is_informative(pkg, pkgbin))
      continue;

    varbuf_stanza(&vb, pkg, pkgbin);
    varbuf_add_char(&vb, '\n');
    varbuf_end_str(&vb);
    if (fputs(vb.buf, fp) < 0)
      ohshite(_("failed to write %s database stanza about '%s' to '%s'"),
              which, pkgbin_name(pkg, pkgbin, pnaw_nonambig), filename);
    varbuf_reset(&vb);
  }

  pkg_array_destroy(&array);
  varbuf_destroy(&vb);
}

void
writedb(const char *filename, enum writedb_flags flags)
{
  struct atomic_file *file;
  enum atomic_file_flags atomic_flags = ATOMIC_FILE_BACKUP;

  if (flags & wdb_dump_available)
    atomic_flags = 0;

  file = atomic_file_new(filename, atomic_flags);
  atomic_file_open(file);

  writedb_stanzas(file->fp, filename, flags);

  if (flags & wdb_must_sync)
    atomic_file_sync(file);

  atomic_file_close(file);
  atomic_file_commit(file);
  atomic_file_free(file);

  if (flags & wdb_must_sync)
    dir_sync_path_parent(filename);
}