portable: release 0.93
[got-portable.git] / lib / object_qid.c
blob4908999633fa7889a6f84f36e6fcaa39d08bae03
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)
34 *qid = malloc(sizeof(**qid));
35 if (*qid == NULL)
36 return got_error_from_errno("malloc");
38 (*qid)->data = NULL;
39 return NULL;
42 const struct got_error *
43 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
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;
53 void
54 got_object_qid_free(struct got_object_qid *qid)
56 free(qid);
59 void
60 got_object_id_queue_free(struct got_object_id_queue *ids)
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);
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)
75 const struct got_error *err;
76 struct got_object_qid *qid;
78 STAILQ_FOREACH(qid, src, entry) {
79 struct got_object_qid *new;
81 * Deep-copy the object ID only. Let the caller deal
82 * with setting up the new->data pointer if needed.
84 err = got_object_qid_alloc(&new, &qid->id);
85 if (err) {
86 got_object_id_queue_free(dest);
87 return err;
89 STAILQ_INSERT_TAIL(dest, new, entry);
92 return NULL;