prepare got_object_parse_tree for sha256
[got-portable.git] / lib / object_qid.c
blob48ffcb76649f846cae64faca3924c1b5583714b2
1 /*
2 * Copyright (c) 2018, 2019, 2020, 2023 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "got_compat.h"
19 #include <sys/queue.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "got_object.h"
26 #include "got_error.h"
28 #include "got_lib_object_qid.h"
29 #include "got_lib_hash.h"
31 const struct got_error *
32 got_object_qid_alloc_partial(struct got_object_qid **qid)
35 * XXX(op) this should really be malloc(), but there are
36 * strange interactions in the fileindex and worktree code
37 * that are creating issues with some of the changes needed
38 * for sha256 support. This will have to be revisited once
39 * that code is fixed.
41 *qid = calloc(1, sizeof(**qid));
42 if (*qid == NULL)
43 return got_error_from_errno("calloc");
45 (*qid)->data = NULL;
46 return NULL;
49 const struct got_error *
50 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
52 *qid = calloc(1, sizeof(**qid));
53 if (*qid == NULL)
54 return got_error_from_errno("calloc");
56 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
57 return NULL;
60 void
61 got_object_qid_free(struct got_object_qid *qid)
63 free(qid);
66 void
67 got_object_id_queue_free(struct got_object_id_queue *ids)
69 struct got_object_qid *qid;
71 while (!STAILQ_EMPTY(ids)) {
72 qid = STAILQ_FIRST(ids);
73 STAILQ_REMOVE_HEAD(ids, entry);
74 got_object_qid_free(qid);
78 const struct got_error *
79 got_object_id_queue_copy(const struct got_object_id_queue *src,
80 struct got_object_id_queue *dest)
82 const struct got_error *err;
83 struct got_object_qid *qid;
85 STAILQ_FOREACH(qid, src, entry) {
86 struct got_object_qid *new;
88 * Deep-copy the object ID only. Let the caller deal
89 * with setting up the new->data pointer if needed.
91 err = got_object_qid_alloc(&new, &qid->id);
92 if (err) {
93 got_object_id_queue_free(dest);
94 return err;
96 STAILQ_INSERT_TAIL(dest, new, entry);
99 return NULL;