2 2f43cd69 2023-04-14 stsp * Copyright (c) 2018, 2019, 2020, 2023 Stefan Sperling <stsp@openbsd.org>
4 2f43cd69 2023-04-14 stsp * Permission to use, copy, modify, and distribute this software for any
5 2f43cd69 2023-04-14 stsp * purpose with or without fee is hereby granted, provided that the above
6 2f43cd69 2023-04-14 stsp * copyright notice and this permission notice appear in all copies.
8 2f43cd69 2023-04-14 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 2f43cd69 2023-04-14 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 2f43cd69 2023-04-14 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 2f43cd69 2023-04-14 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 2f43cd69 2023-04-14 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 2f43cd69 2023-04-14 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 2f43cd69 2023-04-14 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 2f43cd69 2023-04-14 stsp #include <sys/queue.h>
19 2f43cd69 2023-04-14 stsp #include <stdio.h>
20 2f43cd69 2023-04-14 stsp #include <stdlib.h>
21 2f43cd69 2023-04-14 stsp #include <string.h>
22 2f43cd69 2023-04-14 stsp #include <sha1.h>
23 2f43cd69 2023-04-14 stsp #include <sha2.h>
25 2f43cd69 2023-04-14 stsp #include "got_object.h"
26 2f43cd69 2023-04-14 stsp #include "got_error.h"
28 2f43cd69 2023-04-14 stsp #include "got_lib_object_qid.h"
29 2f43cd69 2023-04-14 stsp #include "got_lib_hash.h"
31 2f43cd69 2023-04-14 stsp const struct got_error *
32 2f43cd69 2023-04-14 stsp got_object_qid_alloc_partial(struct got_object_qid **qid)
34 2f43cd69 2023-04-14 stsp *qid = malloc(sizeof(**qid));
35 2f43cd69 2023-04-14 stsp if (*qid == NULL)
36 2f43cd69 2023-04-14 stsp return got_error_from_errno("malloc");
38 2f43cd69 2023-04-14 stsp (*qid)->data = NULL;
39 2f43cd69 2023-04-14 stsp return NULL;
42 2f43cd69 2023-04-14 stsp const struct got_error *
43 2f43cd69 2023-04-14 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
45 2f43cd69 2023-04-14 stsp *qid = calloc(1, sizeof(**qid));
46 2f43cd69 2023-04-14 stsp if (*qid == NULL)
47 2f43cd69 2023-04-14 stsp return got_error_from_errno("calloc");
49 2f43cd69 2023-04-14 stsp memcpy(&(*qid)->id, id, sizeof((*qid)->id));
50 2f43cd69 2023-04-14 stsp return NULL;
54 2f43cd69 2023-04-14 stsp got_object_qid_free(struct got_object_qid *qid)
60 2f43cd69 2023-04-14 stsp got_object_id_queue_free(struct got_object_id_queue *ids)
62 2f43cd69 2023-04-14 stsp struct got_object_qid *qid;
64 2f43cd69 2023-04-14 stsp while (!STAILQ_EMPTY(ids)) {
65 2f43cd69 2023-04-14 stsp qid = STAILQ_FIRST(ids);
66 2f43cd69 2023-04-14 stsp STAILQ_REMOVE_HEAD(ids, entry);
67 2f43cd69 2023-04-14 stsp got_object_qid_free(qid);
71 2f43cd69 2023-04-14 stsp const struct got_error *
72 2f43cd69 2023-04-14 stsp got_object_id_queue_copy(const struct got_object_id_queue *src,
73 2f43cd69 2023-04-14 stsp struct got_object_id_queue *dest)
75 2f43cd69 2023-04-14 stsp const struct got_error *err;
76 2f43cd69 2023-04-14 stsp struct got_object_qid *qid;
78 2f43cd69 2023-04-14 stsp STAILQ_FOREACH(qid, src, entry) {
79 2f43cd69 2023-04-14 stsp struct got_object_qid *new;
81 2f43cd69 2023-04-14 stsp * Deep-copy the object ID only. Let the caller deal
82 2f43cd69 2023-04-14 stsp * with setting up the new->data pointer if needed.
84 2f43cd69 2023-04-14 stsp err = got_object_qid_alloc(&new, &qid->id);
86 2f43cd69 2023-04-14 stsp got_object_id_queue_free(dest);
89 2f43cd69 2023-04-14 stsp STAILQ_INSERT_TAIL(dest, new, entry);
92 2f43cd69 2023-04-14 stsp return NULL;