Blame


1 2f43cd69 2023-04-14 stsp /*
2 2f43cd69 2023-04-14 stsp * Copyright (c) 2018, 2019, 2020, 2023 Stefan Sperling <stsp@openbsd.org>
3 2f43cd69 2023-04-14 stsp *
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.
7 2f43cd69 2023-04-14 stsp *
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.
15 2f43cd69 2023-04-14 stsp */
16 2f43cd69 2023-04-14 stsp
17 2f43cd69 2023-04-14 stsp #include <sys/queue.h>
18 2f43cd69 2023-04-14 stsp
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>
24 2f43cd69 2023-04-14 stsp
25 2f43cd69 2023-04-14 stsp #include "got_object.h"
26 2f43cd69 2023-04-14 stsp #include "got_error.h"
27 2f43cd69 2023-04-14 stsp
28 2f43cd69 2023-04-14 stsp #include "got_lib_object_qid.h"
29 2f43cd69 2023-04-14 stsp #include "got_lib_hash.h"
30 2f43cd69 2023-04-14 stsp
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)
33 2f43cd69 2023-04-14 stsp {
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");
37 2f43cd69 2023-04-14 stsp
38 2f43cd69 2023-04-14 stsp (*qid)->data = NULL;
39 2f43cd69 2023-04-14 stsp return NULL;
40 2f43cd69 2023-04-14 stsp }
41 2f43cd69 2023-04-14 stsp
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)
44 2f43cd69 2023-04-14 stsp {
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");
48 2f43cd69 2023-04-14 stsp
49 2f43cd69 2023-04-14 stsp memcpy(&(*qid)->id, id, sizeof((*qid)->id));
50 2f43cd69 2023-04-14 stsp return NULL;
51 2f43cd69 2023-04-14 stsp }
52 2f43cd69 2023-04-14 stsp
53 2f43cd69 2023-04-14 stsp void
54 2f43cd69 2023-04-14 stsp got_object_qid_free(struct got_object_qid *qid)
55 2f43cd69 2023-04-14 stsp {
56 2f43cd69 2023-04-14 stsp free(qid);
57 2f43cd69 2023-04-14 stsp }
58 2f43cd69 2023-04-14 stsp
59 2f43cd69 2023-04-14 stsp void
60 2f43cd69 2023-04-14 stsp got_object_id_queue_free(struct got_object_id_queue *ids)
61 2f43cd69 2023-04-14 stsp {
62 2f43cd69 2023-04-14 stsp struct got_object_qid *qid;
63 2f43cd69 2023-04-14 stsp
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);
68 2f43cd69 2023-04-14 stsp }
69 2f43cd69 2023-04-14 stsp }
70 2f43cd69 2023-04-14 stsp
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)
74 2f43cd69 2023-04-14 stsp {
75 2f43cd69 2023-04-14 stsp const struct got_error *err;
76 2f43cd69 2023-04-14 stsp struct got_object_qid *qid;
77 2f43cd69 2023-04-14 stsp
78 2f43cd69 2023-04-14 stsp STAILQ_FOREACH(qid, src, entry) {
79 2f43cd69 2023-04-14 stsp struct got_object_qid *new;
80 2f43cd69 2023-04-14 stsp /*
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.
83 2f43cd69 2023-04-14 stsp */
84 2f43cd69 2023-04-14 stsp err = got_object_qid_alloc(&new, &qid->id);
85 2f43cd69 2023-04-14 stsp if (err) {
86 2f43cd69 2023-04-14 stsp got_object_id_queue_free(dest);
87 2f43cd69 2023-04-14 stsp return err;
88 2f43cd69 2023-04-14 stsp }
89 2f43cd69 2023-04-14 stsp STAILQ_INSERT_TAIL(dest, new, entry);
90 2f43cd69 2023-04-14 stsp }
91 2f43cd69 2023-04-14 stsp
92 2f43cd69 2023-04-14 stsp return NULL;
93 2f43cd69 2023-04-14 stsp }