summaryrefslogtreecommitdiff
path: root/src/libostree/ostree-repo-traverse.c
blob: d0edd65d290c5a9d9f4e4011c6834f352c77e920 (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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/*
 * Copyright (C) 2011 Colin Walters <walters@verbum.org>
 *
 * SPDX-License-Identifier: LGPL-2.0+
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Colin Walters <walters@verbum.org>
 */

#include "config.h"

#include "libglnx.h"
#include "ostree.h"
#include "otutil.h"

struct _OstreeRepoRealCommitTraverseIter {
  gboolean initialized;
  OstreeRepo *repo;
  GVariant *commit;
  GVariant *current_dir;
  const char *name;
  OstreeRepoCommitIterResult state;
  guint idx;
  char checksum_content[OSTREE_SHA256_STRING_LEN+1];
  char checksum_meta[OSTREE_SHA256_STRING_LEN+1];
};

/**
 * ostree_repo_commit_traverse_iter_init_commit:
 * @iter: An iter
 * @repo: A repo
 * @commit: Variant of type %OSTREE_OBJECT_TYPE_COMMIT
 * @flags: Flags
 * @error: Error
 *
 * Initialize (in place) an iterator over the root of a commit object.
 */
gboolean
ostree_repo_commit_traverse_iter_init_commit (OstreeRepoCommitTraverseIter   *iter,
                                              OstreeRepo                     *repo,
                                              GVariant                       *commit,
                                              OstreeRepoCommitTraverseFlags   flags,
                                              GError                        **error)
{
  struct _OstreeRepoRealCommitTraverseIter *real =
    (struct _OstreeRepoRealCommitTraverseIter*)iter;

  memset (real, 0, sizeof (*real));
  real->initialized = TRUE;
  real->repo = g_object_ref (repo);
  real->commit = g_variant_ref (commit);
  real->current_dir = NULL;
  real->idx = 0;

  g_autoptr(GVariant) content_csum_bytes = NULL;
  g_variant_get_child (commit, 6, "@ay", &content_csum_bytes);
  const guchar *csum = ostree_checksum_bytes_peek_validate (content_csum_bytes, error);
  if (!csum)
    return FALSE;
  ostree_checksum_inplace_from_bytes (csum, real->checksum_content);

  g_autoptr(GVariant) meta_csum_bytes = NULL;
  g_variant_get_child (commit, 7, "@ay", &meta_csum_bytes);
  csum = ostree_checksum_bytes_peek_validate (meta_csum_bytes, error);
  if (!csum)
    return FALSE;
  ostree_checksum_inplace_from_bytes (csum, real->checksum_meta);

  return TRUE;
}

/**
 * ostree_repo_commit_traverse_iter_init_dirtree:
 * @iter: An iter
 * @repo: A repo
 * @dirtree: Variant of type %OSTREE_OBJECT_TYPE_DIR_TREE
 * @flags: Flags
 * @error: Error
 *
 * Initialize (in place) an iterator over a directory tree.
 */
gboolean
ostree_repo_commit_traverse_iter_init_dirtree (OstreeRepoCommitTraverseIter   *iter,
                                               OstreeRepo                     *repo,
                                               GVariant                       *dirtree,
                                               OstreeRepoCommitTraverseFlags   flags,
                                               GError                        **error)
{
  struct _OstreeRepoRealCommitTraverseIter *real =
    (struct _OstreeRepoRealCommitTraverseIter*)iter;

  memset (real, 0, sizeof (*real));
  real->initialized = TRUE;
  real->repo = g_object_ref (repo);
  real->current_dir = g_variant_ref (dirtree);
  real->idx = 0;

  return TRUE;
}

/**
 * ostree_repo_commit_traverse_iter_next:
 * @iter: An iter
 * @cancellable: Cancellable
 * @error: Error
 *
 * Step the interator to the next item.  Files will be returned first,
 * then subdirectories.  Call this in a loop; upon encountering
 * %OSTREE_REPO_COMMIT_ITER_RESULT_END, there will be no more files or
 * directories.  If %OSTREE_REPO_COMMIT_ITER_RESULT_DIR is returned,
 * then call ostree_repo_commit_traverse_iter_get_dir() to retrieve
 * data for that directory.  Similarly, if
 * %OSTREE_REPO_COMMIT_ITER_RESULT_FILE is returned, call
 * ostree_repo_commit_traverse_iter_get_file().
 * 
 * If %OSTREE_REPO_COMMIT_ITER_RESULT_ERROR is returned, it is a
 * program error to call any further API on @iter except for
 * ostree_repo_commit_traverse_iter_clear().
 */
OstreeRepoCommitIterResult
ostree_repo_commit_traverse_iter_next (OstreeRepoCommitTraverseIter *iter,
                                       GCancellable                 *cancellable,
                                       GError                      **error)
{
  struct _OstreeRepoRealCommitTraverseIter *real =
    (struct _OstreeRepoRealCommitTraverseIter*)iter;
  OstreeRepoCommitIterResult res = OSTREE_REPO_COMMIT_ITER_RESULT_ERROR;

  if (!real->current_dir)
    {
      if (!ostree_repo_load_variant (real->repo, OSTREE_OBJECT_TYPE_DIR_TREE,
                                     real->checksum_content,
                                     &real->current_dir,
                                     error))
        goto out;
      res = OSTREE_REPO_COMMIT_ITER_RESULT_DIR;
    }
  else
    {
      guint nfiles;
      guint ndirs;
      guint idx;
      const guchar *csum;
      g_autoptr(GVariant) content_csum_v = NULL;
      g_autoptr(GVariant) meta_csum_v = NULL;
      g_autoptr(GVariant) files_variant = NULL;
      g_autoptr(GVariant) dirs_variant = NULL;

      files_variant = g_variant_get_child_value (real->current_dir, 0);
      dirs_variant = g_variant_get_child_value (real->current_dir, 1);

      nfiles = g_variant_n_children (files_variant);
      ndirs = g_variant_n_children (dirs_variant);
      if (real->idx < nfiles)
        {
          idx = real->idx;
          g_variant_get_child (files_variant, idx, "(&s@ay)",
                               &real->name,
                               &content_csum_v);

          csum = ostree_checksum_bytes_peek_validate (content_csum_v, error);
          if (!csum)
            goto out;
          ostree_checksum_inplace_from_bytes (csum, real->checksum_content);

          res = OSTREE_REPO_COMMIT_ITER_RESULT_FILE;

          real->idx++;
        }
      else if (real->idx < nfiles + ndirs)
        {
          idx = real->idx - nfiles;

          g_variant_get_child (dirs_variant, idx, "(&s@ay@ay)",
                               &real->name, &content_csum_v, &meta_csum_v);

          csum = ostree_checksum_bytes_peek_validate (content_csum_v, error);
          if (!csum)
            goto out;
          ostree_checksum_inplace_from_bytes (csum, real->checksum_content);

          csum = ostree_checksum_bytes_peek_validate (meta_csum_v, error);
          if (!csum)
            goto out;
          ostree_checksum_inplace_from_bytes (csum, real->checksum_meta);
          
          res = OSTREE_REPO_COMMIT_ITER_RESULT_DIR;

          real->idx++;
        }
      else
        res = OSTREE_REPO_COMMIT_ITER_RESULT_END;
    }
  
  real->state = res;
 out:
  return res;
}

/**
 * ostree_repo_commit_traverse_iter_get_file:
 * @iter: An iter
 * @out_name: (out) (transfer none): Name of current file
 * @out_checksum: (out) (transfer none): Checksum of current file
 *
 * Return information on the current file.  This function may only be
 * called if %OSTREE_REPO_COMMIT_ITER_RESULT_FILE was returned from
 * ostree_repo_commit_traverse_iter_next().
 */
void
ostree_repo_commit_traverse_iter_get_file (OstreeRepoCommitTraverseIter *iter,
                                           char                        **out_name,
                                           char                        **out_checksum)
{
  struct _OstreeRepoRealCommitTraverseIter *real =
    (struct _OstreeRepoRealCommitTraverseIter*)iter;
  *out_name = (char*)real->name;
  *out_checksum = (char*)real->checksum_content;
}

/**
 * ostree_repo_commit_traverse_iter_get_dir:
 * @iter: An iter
 * @out_name: (out) (transfer none): Name of current dir
 * @out_content_checksum: (out) (transfer none): Checksum of current content
 * @out_meta_checksum: (out) (transfer none): Checksum of current metadata
 *
 * Return information on the current directory.  This function may
 * only be called if %OSTREE_REPO_COMMIT_ITER_RESULT_DIR was returned
 * from ostree_repo_commit_traverse_iter_next().
 */
void
ostree_repo_commit_traverse_iter_get_dir (OstreeRepoCommitTraverseIter *iter,
                                          char                        **out_name,
                                          char                        **out_content_checksum,
                                          char                        **out_meta_checksum)
{
  struct _OstreeRepoRealCommitTraverseIter *real =
    (struct _OstreeRepoRealCommitTraverseIter*)iter;
  *out_name = (char*)real->name;
  *out_content_checksum = (char*)real->checksum_content;
  *out_meta_checksum = (char*)real->checksum_meta;
}

void
ostree_repo_commit_traverse_iter_clear (OstreeRepoCommitTraverseIter *iter)
{
  struct _OstreeRepoRealCommitTraverseIter *real =
    (struct _OstreeRepoRealCommitTraverseIter*)iter;
  g_clear_object (&real->repo);
  g_clear_pointer (&real->commit, g_variant_unref);
  g_clear_pointer (&real->current_dir, g_variant_unref);
}

void
ostree_repo_commit_traverse_iter_cleanup (void *p)
{
  OstreeRepoCommitTraverseIter *iter = p;
  struct _OstreeRepoRealCommitTraverseIter *real =
    (struct _OstreeRepoRealCommitTraverseIter*)iter;
  if (real->initialized)
    {
      ostree_repo_commit_traverse_iter_clear (iter);
      real->initialized = FALSE;
    }
}

/**
 * ostree_repo_traverse_new_reachable:
 *
 * This hash table is a set of #GVariant which can be accessed via
 * ostree_object_name_deserialize().
 *
 * Returns: (transfer container) (element-type GVariant GVariant): A new hash table
 */
GHashTable *
ostree_repo_traverse_new_reachable (void)
{
  return g_hash_table_new_full (ostree_hash_object_name, g_variant_equal,
                                NULL, (GDestroyNotify)g_variant_unref);
}

/**
 * ostree_repo_traverse_new_parents:
 *
 * This hash table is a mapping from #GVariant which can be accessed
 * via ostree_object_name_deserialize() to a #GVariant containing either
 * a similar #GVariant or and array of them, listing the parents of the key.
 *
 * Returns: (transfer container) (element-type GVariant GVariant): A new hash table
 *
 * Since: 2018.5
 */
GHashTable *
ostree_repo_traverse_new_parents (void)
{
  return g_hash_table_new_full (ostree_hash_object_name, g_variant_equal,
                                (GDestroyNotify)g_variant_unref, (GDestroyNotify)g_variant_unref);
}

static void
parents_get_commits (GHashTable *parents_ht, GVariant *object, GHashTable *res)
{
  const char *checksum;
  OstreeObjectType type;

  if (object == NULL)
    return;

  ostree_object_name_deserialize (object, &checksum, &type);
  if (type == OSTREE_OBJECT_TYPE_COMMIT)
    g_hash_table_add (res, g_strdup (checksum));
  else
    {
      GVariant *parents = g_hash_table_lookup (parents_ht, object);

      if (parents == NULL)
        g_debug ("Unexpected NULL parent");
      else if (g_variant_is_of_type (parents, G_VARIANT_TYPE_ARRAY))
        {
          gsize i, len = g_variant_n_children (parents);

          for (i = 0; i < len; i++)
            {
              g_autoptr(GVariant) parent = g_variant_get_child_value (parents, i);
              parents_get_commits (parents_ht, parent, res);
            }
        }
      else
        parents_get_commits (parents_ht, parents, res);
    }
}

/**
 * ostree_repo_traverse_parents_get_commits:
 *
 * Gets all the commits that a certain object belongs to, as recorded
 * by a parents table gotten from ostree_repo_traverse_commit_union_with_parents.
 *
 * Returns: (transfer full) (array zero-terminated=1): An array of checksums for
 * the commits the key belongs to.
 *
 * Since: 2018.5
 */
char **
ostree_repo_traverse_parents_get_commits (GHashTable *parents, GVariant *object)
{
  g_autoptr(GHashTable) res = g_hash_table_new (g_str_hash, g_str_equal);

  parents_get_commits (parents, object, res);

  return (char **)g_hash_table_get_keys_as_array (res, NULL);
}

static gboolean
traverse_dirtree (OstreeRepo           *repo,
                  const char           *checksum,
                  GVariant             *parent_key,
                  GHashTable           *inout_reachable,
                  GHashTable           *inout_parents,
                  gboolean              ignore_missing_dirs,
                  GCancellable         *cancellable,
                  GError              **error);

static void
add_parent_ref (GHashTable  *inout_parents,
                GVariant    *key,
                GVariant    *parent_key)
{
  GVariant *old_parents;

  if (inout_parents == NULL)
    return;

  old_parents = g_hash_table_lookup (inout_parents, key);
  if (old_parents == NULL)
    {
      /* For the common case of a single pointer we skip using an array to save memory. */
      g_hash_table_insert (inout_parents, g_variant_ref (key), g_variant_ref (parent_key));
    }
  else
    {
      g_autofree GVariant **new_parents = NULL;
      gsize i, len = 0;

      if (g_variant_is_of_type (old_parents, G_VARIANT_TYPE_ARRAY))
        {
          gsize old_parents_len = g_variant_n_children (old_parents);
          new_parents = g_new (GVariant *,  old_parents_len + 1);
          for (i = 0; i < old_parents_len ; i++)
            {
              g_autoptr(GVariant) old_parent = g_variant_get_child_value (old_parents, i);
              if (!g_variant_equal (old_parent, parent_key))
                new_parents[len++] = g_steal_pointer (&old_parent);
            }
        }
      else
        {
          new_parents = g_new (GVariant *, 2);
          if (!g_variant_equal (old_parents, parent_key))
            new_parents[len++] = g_variant_ref (old_parents);
        }
      new_parents[len++] = g_variant_ref (parent_key);
      g_hash_table_insert (inout_parents, g_variant_ref (key),
                           g_variant_ref_sink (g_variant_new_array (G_VARIANT_TYPE ("(su)"), new_parents , len)));
      for (i = 0; i < len; i++)
        g_variant_unref (new_parents[i]);
    }
}


static gboolean
traverse_iter (OstreeRepo                          *repo,
               OstreeRepoCommitTraverseIter        *iter,
               GVariant                            *parent_key,
               GHashTable                          *inout_reachable,
               GHashTable                          *inout_parents,
               gboolean                             ignore_missing_dirs,
               GCancellable                        *cancellable,
               GError                             **error)
{
  while (TRUE)
    {
      g_autoptr(GVariant) key = NULL;
      g_autoptr(GError) local_error = NULL;
      OstreeRepoCommitIterResult iterres =
        ostree_repo_commit_traverse_iter_next (iter, cancellable, &local_error);

      if (iterres == OSTREE_REPO_COMMIT_ITER_RESULT_ERROR)
        {
          /* There is only one kind of not-found error, which is
             failing to load the dirmeta itself, if so, we ignore that
             (and the whole subtree) if told to. */
          if (ignore_missing_dirs &&
              g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
            {
              g_debug ("Ignoring not-found dirmeta");
              return TRUE;  /* Note early return */
            }

          g_propagate_error (error, g_steal_pointer (&local_error));
          return FALSE;
        }
      else if (iterres == OSTREE_REPO_COMMIT_ITER_RESULT_END)
        break;
      else if (iterres == OSTREE_REPO_COMMIT_ITER_RESULT_FILE)
        {
          char *name;
          char *checksum;

          ostree_repo_commit_traverse_iter_get_file (iter, &name, &checksum);

          g_debug ("Found file object %s", checksum);
          key = g_variant_ref_sink (ostree_object_name_serialize (checksum, OSTREE_OBJECT_TYPE_FILE));
          add_parent_ref (inout_parents, key, parent_key);
          g_hash_table_add (inout_reachable, g_steal_pointer (&key));
        }
      else if (iterres == OSTREE_REPO_COMMIT_ITER_RESULT_DIR)
        {
          char *name;
          char *content_checksum;
          char *meta_checksum;

          ostree_repo_commit_traverse_iter_get_dir (iter, &name, &content_checksum,
                                                    &meta_checksum);

          g_debug ("Found dirtree object %s", content_checksum);
          g_debug ("Found dirmeta object %s", meta_checksum);
          key = g_variant_ref_sink (ostree_object_name_serialize (meta_checksum, OSTREE_OBJECT_TYPE_DIR_META));
          add_parent_ref (inout_parents, key, parent_key);
          g_hash_table_add (inout_reachable, g_steal_pointer (&key));

          key = g_variant_ref_sink (ostree_object_name_serialize (content_checksum, OSTREE_OBJECT_TYPE_DIR_TREE));
          add_parent_ref (inout_parents, key, parent_key);
          if (!g_hash_table_lookup (inout_reachable, key))
            {
              if (!traverse_dirtree (repo, content_checksum, key, inout_reachable, inout_parents,
                                     ignore_missing_dirs, cancellable, error))
                return FALSE;

              g_hash_table_add (inout_reachable, g_steal_pointer (&key));
            }
        }
      else
        g_assert_not_reached ();
    }

  return TRUE;
}

static gboolean
traverse_dirtree (OstreeRepo           *repo,
                  const char           *checksum,
                  GVariant             *parent_key,
                  GHashTable           *inout_reachable,
                  GHashTable           *inout_parents,
                  gboolean              ignore_missing_dirs,
                  GCancellable         *cancellable,
                  GError              **error)
{
  g_autoptr(GError) local_error = NULL;

  g_autoptr(GVariant) dirtree = NULL;
  if (!ostree_repo_load_variant (repo, OSTREE_OBJECT_TYPE_DIR_TREE, checksum,
                                 &dirtree, &local_error))
    {
      if (ignore_missing_dirs &&
          g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
        {
          g_debug ("Ignoring not-found dirmeta %s", checksum);
          return TRUE; /* Early return */
        }

      g_propagate_error (error, g_steal_pointer (&local_error));
      return FALSE;
    }

  g_debug ("Traversing dirtree %s", checksum);
  ostree_cleanup_repo_commit_traverse_iter
    OstreeRepoCommitTraverseIter iter = { 0, };
  if (!ostree_repo_commit_traverse_iter_init_dirtree (&iter, repo, dirtree,
                                                      OSTREE_REPO_COMMIT_TRAVERSE_FLAG_NONE,
                                                      error))
    return FALSE;

  if (!traverse_iter (repo, &iter, parent_key, inout_reachable, inout_parents, ignore_missing_dirs, cancellable, error))
    return FALSE;

  return TRUE;
}

/**
 * ostree_repo_traverse_commit_union_with_parents: (skip)
 * @repo: Repo
 * @commit_checksum: ASCII SHA256 checksum
 * @maxdepth: Traverse this many parent commits, -1 for unlimited
 * @inout_reachable: Set of reachable objects
 * @inout_parents: Map from object to parent object
 * @cancellable: Cancellable
 * @error: Error
 *
 * Update the set @inout_reachable containing all objects reachable
 * from @commit_checksum, traversing @maxdepth parent commits.
 *
 * Additionally this constructs a mapping from each object to the parents
 * of the object, which can be used to track which commits an object
 * belongs to.
 *
 * Since: 2018.5
 */
gboolean
ostree_repo_traverse_commit_union_with_parents (OstreeRepo      *repo,
                                                const char      *commit_checksum,
                                                int              maxdepth,
                                                GHashTable      *inout_reachable,
                                                GHashTable      *inout_parents,
                                                GCancellable    *cancellable,
                                                GError         **error)
{
  g_autofree char *tmp_checksum = NULL;

  while (TRUE)
    {
      g_autoptr(GVariant) key =
        g_variant_ref_sink (ostree_object_name_serialize (commit_checksum, OSTREE_OBJECT_TYPE_COMMIT));

      if (g_hash_table_contains (inout_reachable, key))
        break;

      g_autoptr(GVariant) commit = NULL;
      if (!ostree_repo_load_variant_if_exists (repo, OSTREE_OBJECT_TYPE_COMMIT,
                                               commit_checksum, &commit,
                                               error))
        return FALSE;

      /* Just return if the parent isn't found; we do expect most
       * people to have partial repositories.
       */
      if (!commit)
        break;

      /* See if the commit is partial, if so it's not an error to lack objects */
      OstreeRepoCommitState commitstate;
      if (!ostree_repo_load_commit (repo, commit_checksum, NULL, &commitstate,
                                    error))
        return FALSE;

      gboolean ignore_missing_dirs = FALSE;
      if ((commitstate & OSTREE_REPO_COMMIT_STATE_PARTIAL) != 0)
        ignore_missing_dirs = TRUE;

      g_hash_table_add (inout_reachable, g_variant_ref (key));

      g_debug ("Traversing commit %s", commit_checksum);
      ostree_cleanup_repo_commit_traverse_iter
        OstreeRepoCommitTraverseIter iter = { 0, };
      if (!ostree_repo_commit_traverse_iter_init_commit (&iter, repo, commit,
                                                         OSTREE_REPO_COMMIT_TRAVERSE_FLAG_NONE,
                                                         error))
        return FALSE;

      if (!traverse_iter (repo, &iter, key, inout_reachable, inout_parents, ignore_missing_dirs, cancellable, error))
        return FALSE;

      gboolean recurse = FALSE;
      if (maxdepth == -1 || maxdepth > 0)
        {
          g_free (tmp_checksum);
          tmp_checksum = ostree_commit_get_parent (commit);
          if (tmp_checksum)
            {
              commit_checksum = tmp_checksum;
              if (maxdepth > 0)
                maxdepth -= 1;
              recurse = TRUE;
            }
        }
      if (!recurse)
        break;
    }

  return TRUE;
}

/**
 * ostree_repo_traverse_commit_union: (skip)
 * @repo: Repo
 * @commit_checksum: ASCII SHA256 checksum
 * @maxdepth: Traverse this many parent commits, -1 for unlimited
 * @inout_reachable: Set of reachable objects
 * @cancellable: Cancellable
 * @error: Error
 *
 * Update the set @inout_reachable containing all objects reachable
 * from @commit_checksum, traversing @maxdepth parent commits.
 */
gboolean
ostree_repo_traverse_commit_union (OstreeRepo      *repo,
                                   const char      *commit_checksum,
                                   int              maxdepth,
                                   GHashTable      *inout_reachable,
                                   GCancellable    *cancellable,
                                   GError         **error)
{
  return
    ostree_repo_traverse_commit_union_with_parents (repo, commit_checksum, maxdepth,
                                                    inout_reachable, NULL,
                                                    cancellable, error);
}

/**
 * ostree_repo_traverse_commit:
 * @repo: Repo
 * @commit_checksum: ASCII SHA256 checksum
 * @maxdepth: Traverse this many parent commits, -1 for unlimited
 * @out_reachable: (out) (transfer container) (element-type GVariant GVariant): Set of reachable objects
 * @cancellable: Cancellable
 * @error: Error
 *
 * Create a new set @out_reachable containing all objects reachable
 * from @commit_checksum, traversing @maxdepth parent commits.
 */
gboolean
ostree_repo_traverse_commit (OstreeRepo      *repo,
                             const char      *commit_checksum,
                             int              maxdepth,
                             GHashTable     **out_reachable,
                             GCancellable    *cancellable,
                             GError         **error)
{
  g_autoptr(GHashTable) ret_reachable = ostree_repo_traverse_new_reachable ();
  if (!ostree_repo_traverse_commit_union (repo, commit_checksum, maxdepth,
                                          ret_reachable, cancellable, error))
    return FALSE;

  if (out_reachable)
    *out_reachable = g_steal_pointer (&ret_reachable);
  return TRUE;
}