Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020, 2023 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sha1.h>
23 #include <sha2.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)
33 {
34 *qid = malloc(sizeof(**qid));
35 if (*qid == NULL)
36 return got_error_from_errno("malloc");
38 (*qid)->data = NULL;
39 return NULL;
40 }
42 const struct got_error *
43 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
44 {
45 *qid = calloc(1, sizeof(**qid));
46 if (*qid == NULL)
47 return got_error_from_errno("calloc");
49 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
50 return NULL;
51 }
53 void
54 got_object_qid_free(struct got_object_qid *qid)
55 {
56 free(qid);
57 }
59 void
60 got_object_id_queue_free(struct got_object_id_queue *ids)
61 {
62 struct got_object_qid *qid;
64 while (!STAILQ_EMPTY(ids)) {
65 qid = STAILQ_FIRST(ids);
66 STAILQ_REMOVE_HEAD(ids, entry);
67 got_object_qid_free(qid);
68 }
69 }
71 const struct got_error *
72 got_object_id_queue_copy(const struct got_object_id_queue *src,
73 struct got_object_id_queue *dest)
74 {
75 const struct got_error *err;
76 struct got_object_qid *qid;
78 STAILQ_FOREACH(qid, src, entry) {
79 struct got_object_qid *new;
80 /*
81 * Deep-copy the object ID only. Let the caller deal
82 * with setting up the new->data pointer if needed.
83 */
84 err = got_object_qid_alloc(&new, &qid->id);
85 if (err) {
86 got_object_id_queue_free(dest);
87 return err;
88 }
89 STAILQ_INSERT_TAIL(dest, new, entry);
90 }
92 return NULL;
93 }