Blame


1 54be8251 2018-06-04 stsp /*
2 cbc287dc 2022-04-19 stsp * Copyright (c) 2022 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 #include <sys/queue.h>
18 54be8251 2018-06-04 stsp
19 54be8251 2018-06-04 stsp #include <stdlib.h>
20 cbc287dc 2022-04-19 stsp #include <stdint.h>
21 54be8251 2018-06-04 stsp #include <string.h>
22 54be8251 2018-06-04 stsp #include <sha1.h>
23 54be8251 2018-06-04 stsp #include <stdio.h>
24 54be8251 2018-06-04 stsp #include <zlib.h>
25 c6f420bf 2018-06-04 stsp #include <limits.h>
26 788c352e 2018-06-16 stsp #include <time.h>
27 cbc287dc 2022-04-19 stsp #include <errno.h>
28 cbc287dc 2022-04-19 stsp #include <siphash.h>
29 54be8251 2018-06-04 stsp
30 54be8251 2018-06-04 stsp #include "got_object.h"
31 54be8251 2018-06-04 stsp #include "got_error.h"
32 54be8251 2018-06-04 stsp
33 54be8251 2018-06-04 stsp #include "got_lib_delta.h"
34 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
35 54be8251 2018-06-04 stsp #include "got_lib_object.h"
36 54be8251 2018-06-04 stsp #include "got_lib_object_idset.h"
37 cbc287dc 2022-04-19 stsp #include "got_lib_object_parse.h"
38 54be8251 2018-06-04 stsp
39 cbc287dc 2022-04-19 stsp #define GOT_OBJECT_IDSET_MIN_BUCKETS 64
40 54be8251 2018-06-04 stsp
41 54be8251 2018-06-04 stsp struct got_object_idset {
42 cbc287dc 2022-04-19 stsp struct got_object_id_queue *ids;
43 cbc287dc 2022-04-19 stsp size_t nbuckets;
44 cbc287dc 2022-04-19 stsp unsigned int totelem;
45 cbc287dc 2022-04-19 stsp unsigned int flags;
46 cbc287dc 2022-04-19 stsp #define GOT_OBJECT_IDSET_F_TRAVERSAL 0x01
47 cbc287dc 2022-04-19 stsp #define GOT_OBJECT_IDSET_F_NOMEM 0x02
48 cbc287dc 2022-04-19 stsp SIPHASH_KEY key;
49 54be8251 2018-06-04 stsp };
50 54be8251 2018-06-04 stsp
51 54be8251 2018-06-04 stsp struct got_object_idset *
52 60f2eee1 2018-07-08 stsp got_object_idset_alloc(void)
53 54be8251 2018-06-04 stsp {
54 54be8251 2018-06-04 stsp struct got_object_idset *set;
55 cbc287dc 2022-04-19 stsp int i;
56 54be8251 2018-06-04 stsp
57 984e8a45 2018-11-05 stsp set = malloc(sizeof(*set));
58 54be8251 2018-06-04 stsp if (set == NULL)
59 54be8251 2018-06-04 stsp return NULL;
60 54be8251 2018-06-04 stsp
61 cbc287dc 2022-04-19 stsp set->ids = calloc(sizeof(set->ids[0]), GOT_OBJECT_IDSET_MIN_BUCKETS);
62 cbc287dc 2022-04-19 stsp if (set->ids == NULL) {
63 cbc287dc 2022-04-19 stsp free(set);
64 cbc287dc 2022-04-19 stsp return NULL;
65 cbc287dc 2022-04-19 stsp }
66 cbc287dc 2022-04-19 stsp for (i = 0; i < GOT_OBJECT_IDSET_MIN_BUCKETS; i++)
67 cbc287dc 2022-04-19 stsp STAILQ_INIT(&set->ids[i]);
68 54be8251 2018-06-04 stsp
69 cbc287dc 2022-04-19 stsp set->totelem = 0;
70 cbc287dc 2022-04-19 stsp set->nbuckets = GOT_OBJECT_IDSET_MIN_BUCKETS;
71 cbc287dc 2022-04-19 stsp set->flags = 0;
72 cbc287dc 2022-04-19 stsp arc4random_buf(&set->key, sizeof(set->key));
73 54be8251 2018-06-04 stsp return set;
74 54be8251 2018-06-04 stsp }
75 54be8251 2018-06-04 stsp
76 54be8251 2018-06-04 stsp void
77 54be8251 2018-06-04 stsp got_object_idset_free(struct got_object_idset *set)
78 54be8251 2018-06-04 stsp {
79 cbc287dc 2022-04-19 stsp size_t i;
80 cbc287dc 2022-04-19 stsp struct got_object_qid *qid;
81 54be8251 2018-06-04 stsp
82 cbc287dc 2022-04-19 stsp for (i = 0; i < set->nbuckets; i++) {
83 cbc287dc 2022-04-19 stsp while (!STAILQ_EMPTY(&set->ids[i])) {
84 cbc287dc 2022-04-19 stsp qid = STAILQ_FIRST(&set->ids[i]);
85 cbc287dc 2022-04-19 stsp STAILQ_REMOVE(&set->ids[i], qid, got_object_qid, entry);
86 cbc287dc 2022-04-19 stsp got_object_qid_free(qid);
87 cbc287dc 2022-04-19 stsp }
88 54be8251 2018-06-04 stsp }
89 cbc287dc 2022-04-19 stsp /* User data should be freed by caller. */
90 cbc287dc 2022-04-19 stsp free(set->ids);
91 54be8251 2018-06-04 stsp free(set);
92 54be8251 2018-06-04 stsp }
93 54be8251 2018-06-04 stsp
94 cbc287dc 2022-04-19 stsp static uint64_t
95 cbc287dc 2022-04-19 stsp idset_hash(struct got_object_idset *set, struct got_object_id *id)
96 54be8251 2018-06-04 stsp {
97 cbc287dc 2022-04-19 stsp return SipHash24(&set->key, id->sha1, sizeof(id->sha1));
98 cbc287dc 2022-04-19 stsp }
99 54be8251 2018-06-04 stsp
100 cbc287dc 2022-04-19 stsp static const struct got_error *
101 cbc287dc 2022-04-19 stsp idset_resize(struct got_object_idset *set, size_t nbuckets)
102 cbc287dc 2022-04-19 stsp {
103 cbc287dc 2022-04-19 stsp struct got_object_id_queue *ids;
104 cbc287dc 2022-04-19 stsp size_t i;
105 c6f420bf 2018-06-04 stsp
106 cbc287dc 2022-04-19 stsp ids = calloc(nbuckets, sizeof(ids[0]));
107 cbc287dc 2022-04-19 stsp if (ids == NULL) {
108 cbc287dc 2022-04-19 stsp if (errno != ENOMEM)
109 cbc287dc 2022-04-19 stsp return got_error_from_errno("calloc");
110 cbc287dc 2022-04-19 stsp /* Proceed with our current amount of hash buckets. */
111 cbc287dc 2022-04-19 stsp set->flags |= GOT_OBJECT_IDSET_F_NOMEM;
112 cbc287dc 2022-04-19 stsp return NULL;
113 cbc287dc 2022-04-19 stsp }
114 54be8251 2018-06-04 stsp
115 cbc287dc 2022-04-19 stsp for (i = 0; i < nbuckets; i++)
116 cbc287dc 2022-04-19 stsp STAILQ_INIT(&ids[i]);
117 54be8251 2018-06-04 stsp
118 cbc287dc 2022-04-19 stsp arc4random_buf(&set->key, sizeof(set->key));
119 cbc287dc 2022-04-19 stsp
120 cbc287dc 2022-04-19 stsp for (i = 0; i < set->nbuckets; i++) {
121 cbc287dc 2022-04-19 stsp while (!STAILQ_EMPTY(&set->ids[i])) {
122 cbc287dc 2022-04-19 stsp struct got_object_qid *qid;
123 cbc287dc 2022-04-19 stsp uint64_t idx;
124 cbc287dc 2022-04-19 stsp qid = STAILQ_FIRST(&set->ids[i]);
125 cbc287dc 2022-04-19 stsp STAILQ_REMOVE(&set->ids[i], qid, got_object_qid, entry);
126 d7b5a0e8 2022-04-20 stsp idx = idset_hash(set, &qid->id) % nbuckets;
127 cbc287dc 2022-04-19 stsp STAILQ_INSERT_HEAD(&ids[idx], qid, entry);
128 cbc287dc 2022-04-19 stsp }
129 a7472cb3 2022-04-14 stsp }
130 a7472cb3 2022-04-14 stsp
131 cbc287dc 2022-04-19 stsp free(set->ids);
132 cbc287dc 2022-04-19 stsp set->ids = ids;
133 cbc287dc 2022-04-19 stsp set->nbuckets = nbuckets;
134 b36429ab 2018-11-05 stsp return NULL;
135 54be8251 2018-06-04 stsp }
136 54be8251 2018-06-04 stsp
137 cbc287dc 2022-04-19 stsp static const struct got_error *
138 cbc287dc 2022-04-19 stsp idset_grow(struct got_object_idset *set)
139 54be8251 2018-06-04 stsp {
140 cbc287dc 2022-04-19 stsp size_t nbuckets;
141 54be8251 2018-06-04 stsp
142 cbc287dc 2022-04-19 stsp if (set->flags & GOT_OBJECT_IDSET_F_NOMEM)
143 cbc287dc 2022-04-19 stsp return NULL;
144 cbc287dc 2022-04-19 stsp
145 cbc287dc 2022-04-19 stsp if (set->nbuckets >= UINT_MAX / 2)
146 cbc287dc 2022-04-19 stsp nbuckets = UINT_MAX;
147 cbc287dc 2022-04-19 stsp else
148 cbc287dc 2022-04-19 stsp nbuckets = set->nbuckets * 2;
149 cbc287dc 2022-04-19 stsp
150 cbc287dc 2022-04-19 stsp return idset_resize(set, nbuckets);
151 cbc287dc 2022-04-19 stsp }
152 cbc287dc 2022-04-19 stsp
153 cbc287dc 2022-04-19 stsp const struct got_error *
154 cbc287dc 2022-04-19 stsp got_object_idset_add(struct got_object_idset *set, struct got_object_id *id,
155 cbc287dc 2022-04-19 stsp void *data)
156 cbc287dc 2022-04-19 stsp {
157 cbc287dc 2022-04-19 stsp const struct got_error *err;
158 cbc287dc 2022-04-19 stsp struct got_object_qid *qid;
159 cbc287dc 2022-04-19 stsp uint64_t idx;
160 cbc287dc 2022-04-19 stsp struct got_object_id_queue *head;
161 cbc287dc 2022-04-19 stsp
162 cbc287dc 2022-04-19 stsp /* This function may resize the set. */
163 cbc287dc 2022-04-19 stsp if (set->flags & GOT_OBJECT_IDSET_F_TRAVERSAL)
164 cbc287dc 2022-04-19 stsp return got_error_msg(GOT_ERR_NOT_IMPL,
165 cbc287dc 2022-04-19 stsp "cannot add elements to idset during traversal");
166 cbc287dc 2022-04-19 stsp
167 cbc287dc 2022-04-19 stsp if (set->totelem == UINT_MAX)
168 cbc287dc 2022-04-19 stsp return got_error(GOT_ERR_NO_SPACE);
169 5e91dae4 2022-08-30 stsp
170 cbc287dc 2022-04-19 stsp err = got_object_qid_alloc_partial(&qid);
171 cbc287dc 2022-04-19 stsp if (err)
172 cbc287dc 2022-04-19 stsp return err;
173 d7b5a0e8 2022-04-20 stsp memcpy(&qid->id, id, sizeof(qid->id));
174 cbc287dc 2022-04-19 stsp qid->data = data;
175 cbc287dc 2022-04-19 stsp
176 cbc287dc 2022-04-19 stsp idx = idset_hash(set, id) % set->nbuckets;
177 cbc287dc 2022-04-19 stsp head = &set->ids[idx];
178 cbc287dc 2022-04-19 stsp STAILQ_INSERT_HEAD(head, qid, entry);
179 cbc287dc 2022-04-19 stsp set->totelem++;
180 cbc287dc 2022-04-19 stsp
181 cbc287dc 2022-04-19 stsp if (set->nbuckets < set->totelem)
182 cbc287dc 2022-04-19 stsp err = idset_grow(set);
183 cbc287dc 2022-04-19 stsp
184 cbc287dc 2022-04-19 stsp return err;
185 cbc287dc 2022-04-19 stsp }
186 cbc287dc 2022-04-19 stsp
187 cbc287dc 2022-04-19 stsp static struct got_object_qid *
188 cbc287dc 2022-04-19 stsp find_element(struct got_object_idset *set, struct got_object_id *id)
189 cbc287dc 2022-04-19 stsp {
190 cbc287dc 2022-04-19 stsp uint64_t idx = idset_hash(set, id) % set->nbuckets;
191 cbc287dc 2022-04-19 stsp struct got_object_id_queue *head = &set->ids[idx];
192 cbc287dc 2022-04-19 stsp struct got_object_qid *qid;
193 cbc287dc 2022-04-19 stsp
194 cbc287dc 2022-04-19 stsp STAILQ_FOREACH(qid, head, entry) {
195 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(&qid->id, id) == 0)
196 cbc287dc 2022-04-19 stsp return qid;
197 54be8251 2018-06-04 stsp }
198 54be8251 2018-06-04 stsp
199 cbc287dc 2022-04-19 stsp return NULL;
200 54be8251 2018-06-04 stsp }
201 54be8251 2018-06-04 stsp
202 984e8a45 2018-11-05 stsp void *
203 984e8a45 2018-11-05 stsp got_object_idset_get(struct got_object_idset *set, struct got_object_id *id)
204 984e8a45 2018-11-05 stsp {
205 cbc287dc 2022-04-19 stsp struct got_object_qid *qid = find_element(set, id);
206 cbc287dc 2022-04-19 stsp return qid ? qid->data : NULL;
207 984e8a45 2018-11-05 stsp }
208 984e8a45 2018-11-05 stsp
209 54be8251 2018-06-04 stsp const struct got_error *
210 e7c810ea 2018-06-22 stsp got_object_idset_remove(void **data, struct got_object_idset *set,
211 54be8251 2018-06-04 stsp struct got_object_id *id)
212 54be8251 2018-06-04 stsp {
213 cbc287dc 2022-04-19 stsp uint64_t idx;
214 cbc287dc 2022-04-19 stsp struct got_object_id_queue *head;
215 cbc287dc 2022-04-19 stsp struct got_object_qid *qid;
216 54be8251 2018-06-04 stsp
217 441e144c 2018-06-22 stsp if (data)
218 441e144c 2018-06-22 stsp *data = NULL;
219 441e144c 2018-06-22 stsp
220 984e8a45 2018-11-05 stsp if (set->totelem == 0)
221 c6f420bf 2018-06-04 stsp return got_error(GOT_ERR_NO_OBJ);
222 c6f420bf 2018-06-04 stsp
223 0ae61b79 2022-03-21 stsp if (id == NULL) {
224 cbc287dc 2022-04-19 stsp /* Remove a "random" element. */
225 cbc287dc 2022-04-19 stsp for (idx = 0; idx < set->nbuckets; idx++) {
226 cbc287dc 2022-04-19 stsp head = &set->ids[idx];
227 cbc287dc 2022-04-19 stsp qid = STAILQ_FIRST(head);
228 cbc287dc 2022-04-19 stsp if (qid)
229 cbc287dc 2022-04-19 stsp break;
230 cbc287dc 2022-04-19 stsp }
231 0ae61b79 2022-03-21 stsp } else {
232 cbc287dc 2022-04-19 stsp idx = idset_hash(set, id) % set->nbuckets;
233 cbc287dc 2022-04-19 stsp head = &set->ids[idx];
234 cbc287dc 2022-04-19 stsp STAILQ_FOREACH(qid, head, entry) {
235 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(&qid->id, id) == 0)
236 cbc287dc 2022-04-19 stsp break;
237 cbc287dc 2022-04-19 stsp }
238 cbc287dc 2022-04-19 stsp if (qid == NULL)
239 0ae61b79 2022-03-21 stsp return got_error_no_obj(id);
240 0ae61b79 2022-03-21 stsp }
241 54be8251 2018-06-04 stsp
242 984e8a45 2018-11-05 stsp if (data)
243 cbc287dc 2022-04-19 stsp *data = qid->data;
244 cbc287dc 2022-04-19 stsp STAILQ_REMOVE(head, qid, got_object_qid, entry);
245 cbc287dc 2022-04-19 stsp got_object_qid_free(qid);
246 984e8a45 2018-11-05 stsp set->totelem--;
247 cbc287dc 2022-04-19 stsp
248 984e8a45 2018-11-05 stsp return NULL;
249 54be8251 2018-06-04 stsp }
250 54be8251 2018-06-04 stsp
251 54be8251 2018-06-04 stsp int
252 54be8251 2018-06-04 stsp got_object_idset_contains(struct got_object_idset *set,
253 54be8251 2018-06-04 stsp struct got_object_id *id)
254 54be8251 2018-06-04 stsp {
255 cbc287dc 2022-04-19 stsp struct got_object_qid *qid = find_element(set, id);
256 cbc287dc 2022-04-19 stsp return qid ? 1 : 0;
257 54be8251 2018-06-04 stsp }
258 54be8251 2018-06-04 stsp
259 cb103d04 2018-11-07 stsp const struct got_error *
260 cb103d04 2018-11-07 stsp got_object_idset_for_each(struct got_object_idset *set,
261 cb103d04 2018-11-07 stsp const struct got_error *(*cb)(struct got_object_id *, void *, void *),
262 cb103d04 2018-11-07 stsp void *arg)
263 54be8251 2018-06-04 stsp {
264 cbc287dc 2022-04-19 stsp const struct got_error *err = NULL;
265 cbc287dc 2022-04-19 stsp struct got_object_id_queue *head;
266 cbc287dc 2022-04-19 stsp struct got_object_qid *qid, *tmp;
267 cbc287dc 2022-04-19 stsp size_t i;
268 54be8251 2018-06-04 stsp
269 cbc287dc 2022-04-19 stsp set->flags |= GOT_OBJECT_IDSET_F_TRAVERSAL;
270 cbc287dc 2022-04-19 stsp for (i = 0; i < set->nbuckets; i++) {
271 cbc287dc 2022-04-19 stsp head = &set->ids[i];
272 cbc287dc 2022-04-19 stsp STAILQ_FOREACH_SAFE(qid, head, entry, tmp) {
273 d7b5a0e8 2022-04-20 stsp err = (*cb)(&qid->id, qid->data, arg);
274 cbc287dc 2022-04-19 stsp if (err)
275 cbc287dc 2022-04-19 stsp goto done;
276 cbc287dc 2022-04-19 stsp }
277 cb103d04 2018-11-07 stsp }
278 cbc287dc 2022-04-19 stsp done:
279 cbc287dc 2022-04-19 stsp set->flags &= ~GOT_OBJECT_IDSET_F_TRAVERSAL;
280 cbc287dc 2022-04-19 stsp return err;
281 54be8251 2018-06-04 stsp }
282 c6f420bf 2018-06-04 stsp
283 069f84d5 2018-06-11 stsp int
284 c6f420bf 2018-06-04 stsp got_object_idset_num_elements(struct got_object_idset *set)
285 c6f420bf 2018-06-04 stsp {
286 2bd394ff 2018-06-22 stsp return set->totelem;
287 c6f420bf 2018-06-04 stsp }