Blame


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