Blame


1 54be8251 2018-06-04 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 54be8251 2018-06-04 stsp *
4 54be8251 2018-06-04 stsp * Permission to use, copy, modify, and distribute this software for any
5 54be8251 2018-06-04 stsp * purpose with or without fee is hereby granted, provided that the above
6 54be8251 2018-06-04 stsp * copyright notice and this permission notice appear in all copies.
7 54be8251 2018-06-04 stsp *
8 54be8251 2018-06-04 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 54be8251 2018-06-04 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 54be8251 2018-06-04 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 54be8251 2018-06-04 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 54be8251 2018-06-04 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 54be8251 2018-06-04 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 54be8251 2018-06-04 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 54be8251 2018-06-04 stsp */
16 54be8251 2018-06-04 stsp
17 54be8251 2018-06-04 stsp
18 54be8251 2018-06-04 stsp #include <stdlib.h>
19 54be8251 2018-06-04 stsp #include <string.h>
20 54be8251 2018-06-04 stsp #include <stdio.h>
21 54be8251 2018-06-04 stsp #include <zlib.h>
22 c6f420bf 2018-06-04 stsp #include <limits.h>
23 788c352e 2018-06-16 stsp #include <time.h>
24 54be8251 2018-06-04 stsp
25 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
26 dd038bc6 2021-09-21 thomas.ad
27 54be8251 2018-06-04 stsp #include "got_object.h"
28 54be8251 2018-06-04 stsp #include "got_error.h"
29 54be8251 2018-06-04 stsp
30 54be8251 2018-06-04 stsp #include "got_lib_delta.h"
31 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
32 54be8251 2018-06-04 stsp #include "got_lib_object.h"
33 54be8251 2018-06-04 stsp #include "got_lib_object_idset.h"
34 54be8251 2018-06-04 stsp
35 54be8251 2018-06-04 stsp struct got_object_idset_element {
36 e336e3d6 2018-11-05 stsp RB_ENTRY(got_object_idset_element) entry;
37 54be8251 2018-06-04 stsp struct got_object_id id;
38 54be8251 2018-06-04 stsp void *data; /* API user data */
39 54be8251 2018-06-04 stsp };
40 54be8251 2018-06-04 stsp
41 e336e3d6 2018-11-05 stsp RB_HEAD(got_object_idset_tree, got_object_idset_element);
42 984e8a45 2018-11-05 stsp
43 984e8a45 2018-11-05 stsp static int
44 984e8a45 2018-11-05 stsp cmp_elements(const struct got_object_idset_element *e1,
45 984e8a45 2018-11-05 stsp const struct got_object_idset_element *e2)
46 984e8a45 2018-11-05 stsp {
47 984e8a45 2018-11-05 stsp return got_object_id_cmp(&e1->id, &e2->id);
48 984e8a45 2018-11-05 stsp }
49 984e8a45 2018-11-05 stsp
50 e336e3d6 2018-11-05 stsp RB_PROTOTYPE(got_object_idset_tree, got_object_idset_element, entry,
51 984e8a45 2018-11-05 stsp cmp_elements);
52 984e8a45 2018-11-05 stsp
53 54be8251 2018-06-04 stsp struct got_object_idset {
54 984e8a45 2018-11-05 stsp struct got_object_idset_tree entries;
55 2bd394ff 2018-06-22 stsp int totelem;
56 069f84d5 2018-06-11 stsp #define GOT_OBJECT_IDSET_MAX_ELEM INT_MAX
57 54be8251 2018-06-04 stsp };
58 54be8251 2018-06-04 stsp
59 54be8251 2018-06-04 stsp struct got_object_idset *
60 60f2eee1 2018-07-08 stsp got_object_idset_alloc(void)
61 54be8251 2018-06-04 stsp {
62 54be8251 2018-06-04 stsp struct got_object_idset *set;
63 54be8251 2018-06-04 stsp
64 984e8a45 2018-11-05 stsp set = malloc(sizeof(*set));
65 54be8251 2018-06-04 stsp if (set == NULL)
66 54be8251 2018-06-04 stsp return NULL;
67 54be8251 2018-06-04 stsp
68 e336e3d6 2018-11-05 stsp RB_INIT(&set->entries);
69 984e8a45 2018-11-05 stsp set->totelem = 0;
70 54be8251 2018-06-04 stsp
71 54be8251 2018-06-04 stsp return set;
72 54be8251 2018-06-04 stsp }
73 54be8251 2018-06-04 stsp
74 54be8251 2018-06-04 stsp void
75 54be8251 2018-06-04 stsp got_object_idset_free(struct got_object_idset *set)
76 54be8251 2018-06-04 stsp {
77 54be8251 2018-06-04 stsp struct got_object_idset_element *entry;
78 54be8251 2018-06-04 stsp
79 e336e3d6 2018-11-05 stsp while ((entry = RB_MIN(got_object_idset_tree, &set->entries))) {
80 e336e3d6 2018-11-05 stsp RB_REMOVE(got_object_idset_tree, &set->entries, entry);
81 984e8a45 2018-11-05 stsp /* User data should be freed by caller. */
82 984e8a45 2018-11-05 stsp free(entry);
83 54be8251 2018-06-04 stsp }
84 984e8a45 2018-11-05 stsp
85 54be8251 2018-06-04 stsp free(set);
86 54be8251 2018-06-04 stsp }
87 54be8251 2018-06-04 stsp
88 54be8251 2018-06-04 stsp const struct got_error *
89 b36429ab 2018-11-05 stsp got_object_idset_add(struct got_object_idset *set, struct got_object_id *id,
90 b36429ab 2018-11-05 stsp void *data)
91 54be8251 2018-06-04 stsp {
92 b36429ab 2018-11-05 stsp struct got_object_idset_element *new;
93 54be8251 2018-06-04 stsp
94 2bd394ff 2018-06-22 stsp if (set->totelem >= GOT_OBJECT_IDSET_MAX_ELEM)
95 c6f420bf 2018-06-04 stsp return got_error(GOT_ERR_NO_SPACE);
96 c6f420bf 2018-06-04 stsp
97 294f39b0 2018-11-05 stsp new = malloc(sizeof(*new));
98 54be8251 2018-06-04 stsp if (new == NULL)
99 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
100 54be8251 2018-06-04 stsp
101 54be8251 2018-06-04 stsp memcpy(&new->id, id, sizeof(new->id));
102 54be8251 2018-06-04 stsp new->data = data;
103 54be8251 2018-06-04 stsp
104 e336e3d6 2018-11-05 stsp RB_INSERT(got_object_idset_tree, &set->entries, new);
105 b36429ab 2018-11-05 stsp set->totelem++;
106 b36429ab 2018-11-05 stsp return NULL;
107 54be8251 2018-06-04 stsp }
108 54be8251 2018-06-04 stsp
109 984e8a45 2018-11-05 stsp static struct got_object_idset_element *
110 984e8a45 2018-11-05 stsp find_element(struct got_object_idset *set, struct got_object_id *id)
111 54be8251 2018-06-04 stsp {
112 60f2eee1 2018-07-08 stsp struct got_object_idset_element *entry;
113 54be8251 2018-06-04 stsp
114 e336e3d6 2018-11-05 stsp entry = RB_ROOT(&set->entries);
115 984e8a45 2018-11-05 stsp while (entry) {
116 984e8a45 2018-11-05 stsp int cmp = got_object_id_cmp(id, &entry->id);
117 984e8a45 2018-11-05 stsp if (cmp < 0)
118 e336e3d6 2018-11-05 stsp entry = RB_LEFT(entry, entry);
119 984e8a45 2018-11-05 stsp else if (cmp > 0)
120 e336e3d6 2018-11-05 stsp entry = RB_RIGHT(entry, entry);
121 984e8a45 2018-11-05 stsp else
122 984e8a45 2018-11-05 stsp break;
123 54be8251 2018-06-04 stsp }
124 54be8251 2018-06-04 stsp
125 984e8a45 2018-11-05 stsp return entry;
126 54be8251 2018-06-04 stsp }
127 54be8251 2018-06-04 stsp
128 984e8a45 2018-11-05 stsp void *
129 984e8a45 2018-11-05 stsp got_object_idset_get(struct got_object_idset *set, struct got_object_id *id)
130 984e8a45 2018-11-05 stsp {
131 984e8a45 2018-11-05 stsp struct got_object_idset_element *entry = find_element(set, id);
132 984e8a45 2018-11-05 stsp return entry ? entry->data : NULL;
133 984e8a45 2018-11-05 stsp }
134 984e8a45 2018-11-05 stsp
135 54be8251 2018-06-04 stsp const struct got_error *
136 e7c810ea 2018-06-22 stsp got_object_idset_remove(void **data, struct got_object_idset *set,
137 54be8251 2018-06-04 stsp struct got_object_id *id)
138 54be8251 2018-06-04 stsp {
139 984e8a45 2018-11-05 stsp struct got_object_idset_element *entry;
140 54be8251 2018-06-04 stsp
141 441e144c 2018-06-22 stsp if (data)
142 441e144c 2018-06-22 stsp *data = NULL;
143 441e144c 2018-06-22 stsp
144 984e8a45 2018-11-05 stsp if (set->totelem == 0)
145 c6f420bf 2018-06-04 stsp return got_error(GOT_ERR_NO_OBJ);
146 c6f420bf 2018-06-04 stsp
147 f054b67a 2018-11-05 stsp if (id == NULL)
148 e336e3d6 2018-11-05 stsp entry = RB_ROOT(&set->entries);
149 f054b67a 2018-11-05 stsp else
150 f054b67a 2018-11-05 stsp entry = find_element(set, id);
151 984e8a45 2018-11-05 stsp if (entry == NULL)
152 ded8fbb8 2020-04-19 stsp return got_error_no_obj(id);
153 54be8251 2018-06-04 stsp
154 e336e3d6 2018-11-05 stsp RB_REMOVE(got_object_idset_tree, &set->entries, entry);
155 984e8a45 2018-11-05 stsp if (data)
156 984e8a45 2018-11-05 stsp *data = entry->data;
157 984e8a45 2018-11-05 stsp free(entry);
158 984e8a45 2018-11-05 stsp set->totelem--;
159 984e8a45 2018-11-05 stsp return NULL;
160 54be8251 2018-06-04 stsp }
161 54be8251 2018-06-04 stsp
162 54be8251 2018-06-04 stsp int
163 54be8251 2018-06-04 stsp got_object_idset_contains(struct got_object_idset *set,
164 54be8251 2018-06-04 stsp struct got_object_id *id)
165 54be8251 2018-06-04 stsp {
166 984e8a45 2018-11-05 stsp struct got_object_idset_element *entry = find_element(set, id);
167 984e8a45 2018-11-05 stsp return entry ? 1 : 0;
168 54be8251 2018-06-04 stsp }
169 54be8251 2018-06-04 stsp
170 cb103d04 2018-11-07 stsp const struct got_error *
171 cb103d04 2018-11-07 stsp got_object_idset_for_each(struct got_object_idset *set,
172 cb103d04 2018-11-07 stsp const struct got_error *(*cb)(struct got_object_id *, void *, void *),
173 cb103d04 2018-11-07 stsp void *arg)
174 54be8251 2018-06-04 stsp {
175 cb103d04 2018-11-07 stsp const struct got_error *err;
176 9489f1f7 2018-11-11 stsp struct got_object_idset_element *entry, *tmp;
177 54be8251 2018-06-04 stsp
178 9489f1f7 2018-11-11 stsp RB_FOREACH_SAFE(entry, got_object_idset_tree, &set->entries, tmp) {
179 cb103d04 2018-11-07 stsp err = (*cb)(&entry->id, entry->data, arg);
180 cb103d04 2018-11-07 stsp if (err)
181 cb103d04 2018-11-07 stsp return err;
182 cb103d04 2018-11-07 stsp }
183 cb103d04 2018-11-07 stsp return NULL;
184 54be8251 2018-06-04 stsp }
185 c6f420bf 2018-06-04 stsp
186 069f84d5 2018-06-11 stsp int
187 c6f420bf 2018-06-04 stsp got_object_idset_num_elements(struct got_object_idset *set)
188 c6f420bf 2018-06-04 stsp {
189 2bd394ff 2018-06-22 stsp return set->totelem;
190 c6f420bf 2018-06-04 stsp }
191 984e8a45 2018-11-05 stsp
192 e336e3d6 2018-11-05 stsp RB_GENERATE(got_object_idset_tree, got_object_idset_element, entry,
193 984e8a45 2018-11-05 stsp cmp_elements);