Blame


1 54be8251 2018-06-04 stsp /*
2 54be8251 2018-06-04 stsp * Copyright (c) 2018 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 54be8251 2018-06-04 stsp #include <string.h>
21 54be8251 2018-06-04 stsp #include <sha1.h>
22 54be8251 2018-06-04 stsp #include <stdio.h>
23 54be8251 2018-06-04 stsp #include <zlib.h>
24 c6f420bf 2018-06-04 stsp #include <limits.h>
25 788c352e 2018-06-16 stsp #include <time.h>
26 54be8251 2018-06-04 stsp
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 54be8251 2018-06-04 stsp #include "got_lib_zbuf.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 #ifndef nitems
36 54be8251 2018-06-04 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
37 54be8251 2018-06-04 stsp #endif
38 54be8251 2018-06-04 stsp
39 54be8251 2018-06-04 stsp struct got_object_idset_element {
40 54be8251 2018-06-04 stsp TAILQ_ENTRY(got_object_idset_element) entry;
41 54be8251 2018-06-04 stsp struct got_object_id id;
42 54be8251 2018-06-04 stsp void *data; /* API user data */
43 54be8251 2018-06-04 stsp };
44 54be8251 2018-06-04 stsp
45 54be8251 2018-06-04 stsp struct got_object_idset {
46 54be8251 2018-06-04 stsp /*
47 54be8251 2018-06-04 stsp * A set is implemented as a collection of 256 lists.
48 54be8251 2018-06-04 stsp * The value of the first byte of an object ID determines
49 54be8251 2018-06-04 stsp * which of these lists an object ID is stored in.
50 54be8251 2018-06-04 stsp */
51 54be8251 2018-06-04 stsp TAILQ_HEAD(, got_object_idset_element) entries[0xff + 1];
52 069f84d5 2018-06-11 stsp int nelem;
53 069f84d5 2018-06-11 stsp #define GOT_OBJECT_IDSET_MAX_ELEM INT_MAX
54 54be8251 2018-06-04 stsp };
55 54be8251 2018-06-04 stsp
56 54be8251 2018-06-04 stsp struct got_object_idset *
57 54be8251 2018-06-04 stsp got_object_idset_alloc(void)
58 54be8251 2018-06-04 stsp {
59 54be8251 2018-06-04 stsp struct got_object_idset *set;
60 54be8251 2018-06-04 stsp int i;
61 54be8251 2018-06-04 stsp
62 54be8251 2018-06-04 stsp set = calloc(1, sizeof(*set));
63 54be8251 2018-06-04 stsp if (set == NULL)
64 54be8251 2018-06-04 stsp return NULL;
65 54be8251 2018-06-04 stsp
66 54be8251 2018-06-04 stsp for (i = 0; i < nitems(set->entries); i++)
67 54be8251 2018-06-04 stsp TAILQ_INIT(&set->entries[i]);
68 54be8251 2018-06-04 stsp
69 54be8251 2018-06-04 stsp return set;
70 54be8251 2018-06-04 stsp }
71 54be8251 2018-06-04 stsp
72 54be8251 2018-06-04 stsp void
73 54be8251 2018-06-04 stsp got_object_idset_free(struct got_object_idset *set)
74 54be8251 2018-06-04 stsp {
75 54be8251 2018-06-04 stsp struct got_object_idset_element *entry;
76 54be8251 2018-06-04 stsp int i;
77 54be8251 2018-06-04 stsp
78 54be8251 2018-06-04 stsp for (i = 0; i < nitems(set->entries); i++) {
79 54be8251 2018-06-04 stsp while (!TAILQ_EMPTY(&set->entries[i])) {
80 54be8251 2018-06-04 stsp entry = TAILQ_FIRST(&set->entries[i]);
81 54be8251 2018-06-04 stsp TAILQ_REMOVE(&set->entries[i], entry, entry);
82 54be8251 2018-06-04 stsp /* User data should be freed by caller. */
83 54be8251 2018-06-04 stsp free(entry);
84 54be8251 2018-06-04 stsp }
85 54be8251 2018-06-04 stsp }
86 54be8251 2018-06-04 stsp free(set);
87 54be8251 2018-06-04 stsp }
88 54be8251 2018-06-04 stsp
89 54be8251 2018-06-04 stsp const struct got_error *
90 d5a90aac 2018-06-04 stsp got_object_idset_add(void **existing_data,
91 d5a90aac 2018-06-04 stsp struct got_object_idset *set, struct got_object_id *id, void *data)
92 54be8251 2018-06-04 stsp {
93 54be8251 2018-06-04 stsp struct got_object_idset_element *new, *entry;
94 54be8251 2018-06-04 stsp uint8_t i = id->sha1[0];
95 54be8251 2018-06-04 stsp
96 d5a90aac 2018-06-04 stsp if (existing_data)
97 d5a90aac 2018-06-04 stsp *existing_data = NULL;
98 d5a90aac 2018-06-04 stsp
99 c6f420bf 2018-06-04 stsp if (set->nelem >= GOT_OBJECT_IDSET_MAX_ELEM)
100 c6f420bf 2018-06-04 stsp return got_error(GOT_ERR_NO_SPACE);
101 c6f420bf 2018-06-04 stsp
102 54be8251 2018-06-04 stsp new = calloc(1, sizeof(*new));
103 54be8251 2018-06-04 stsp if (new == NULL)
104 54be8251 2018-06-04 stsp return got_error_from_errno();
105 54be8251 2018-06-04 stsp
106 54be8251 2018-06-04 stsp memcpy(&new->id, id, sizeof(new->id));
107 54be8251 2018-06-04 stsp new->data = data;
108 54be8251 2018-06-04 stsp
109 54be8251 2018-06-04 stsp if (TAILQ_EMPTY(&set->entries[i])) {
110 54be8251 2018-06-04 stsp TAILQ_INSERT_HEAD(&set->entries[i], new, entry);
111 c6f420bf 2018-06-04 stsp set->nelem++;
112 54be8251 2018-06-04 stsp return NULL;
113 54be8251 2018-06-04 stsp }
114 54be8251 2018-06-04 stsp
115 54be8251 2018-06-04 stsp /*
116 54be8251 2018-06-04 stsp * Keep the list sorted by ID so that iterations of
117 54be8251 2018-06-04 stsp * the set occur in a predictable order.
118 54be8251 2018-06-04 stsp */
119 54be8251 2018-06-04 stsp TAILQ_FOREACH(entry, &set->entries[i], entry) {
120 54be8251 2018-06-04 stsp int cmp = got_object_id_cmp(&new->id, &entry->id);
121 54be8251 2018-06-04 stsp struct got_object_idset_element *next;
122 54be8251 2018-06-04 stsp
123 54be8251 2018-06-04 stsp if (cmp == 0) {
124 54be8251 2018-06-04 stsp free(new);
125 d5a90aac 2018-06-04 stsp if (existing_data)
126 d5a90aac 2018-06-04 stsp *existing_data = entry->data;
127 54be8251 2018-06-04 stsp return got_error(GOT_ERR_OBJ_EXISTS);
128 54be8251 2018-06-04 stsp } else if (cmp < 0) {
129 54be8251 2018-06-04 stsp TAILQ_INSERT_BEFORE(entry, new, entry);
130 c6f420bf 2018-06-04 stsp set->nelem++;
131 54be8251 2018-06-04 stsp return NULL;
132 54be8251 2018-06-04 stsp }
133 54be8251 2018-06-04 stsp
134 54be8251 2018-06-04 stsp next = TAILQ_NEXT(entry, entry);
135 54be8251 2018-06-04 stsp if (next == NULL) {
136 54be8251 2018-06-04 stsp TAILQ_INSERT_AFTER(&set->entries[i], entry, new, entry);
137 c6f420bf 2018-06-04 stsp set->nelem++;
138 54be8251 2018-06-04 stsp return NULL;
139 54be8251 2018-06-04 stsp } else if (got_object_id_cmp(&new->id, &next->id) > 0) {
140 54be8251 2018-06-04 stsp TAILQ_INSERT_BEFORE(next, new, entry);
141 c6f420bf 2018-06-04 stsp set->nelem++;
142 54be8251 2018-06-04 stsp return NULL;
143 54be8251 2018-06-04 stsp }
144 54be8251 2018-06-04 stsp }
145 54be8251 2018-06-04 stsp
146 54be8251 2018-06-04 stsp return got_error(GOT_ERR_BAD_OBJ_ID); /* should not get here */
147 54be8251 2018-06-04 stsp }
148 54be8251 2018-06-04 stsp
149 54be8251 2018-06-04 stsp void *
150 45b73774 2018-06-04 stsp got_object_idset_get(struct got_object_idset *set, struct got_object_id *id)
151 54be8251 2018-06-04 stsp {
152 54be8251 2018-06-04 stsp struct got_object_idset_element *entry;
153 54be8251 2018-06-04 stsp uint8_t i = id->sha1[0];
154 54be8251 2018-06-04 stsp
155 54be8251 2018-06-04 stsp TAILQ_FOREACH(entry, &set->entries[i], entry) {
156 54be8251 2018-06-04 stsp if (got_object_id_cmp(&entry->id, id) == 0)
157 54be8251 2018-06-04 stsp return entry->data;
158 54be8251 2018-06-04 stsp }
159 54be8251 2018-06-04 stsp
160 54be8251 2018-06-04 stsp return NULL;
161 54be8251 2018-06-04 stsp }
162 54be8251 2018-06-04 stsp
163 54be8251 2018-06-04 stsp const struct got_error *
164 54be8251 2018-06-04 stsp got_object_idset_remove(struct got_object_idset *set,
165 54be8251 2018-06-04 stsp struct got_object_id *id)
166 54be8251 2018-06-04 stsp {
167 54be8251 2018-06-04 stsp struct got_object_idset_element *entry, *tmp;
168 ac4e69fb 2018-06-22 stsp uint8_t i = id->sha1[0];
169 54be8251 2018-06-04 stsp
170 c6f420bf 2018-06-04 stsp if (set->nelem == 0)
171 c6f420bf 2018-06-04 stsp return got_error(GOT_ERR_NO_OBJ);
172 c6f420bf 2018-06-04 stsp
173 54be8251 2018-06-04 stsp TAILQ_FOREACH_SAFE(entry, &set->entries[i], entry, tmp) {
174 ac4e69fb 2018-06-22 stsp if (got_object_id_cmp(&entry->id, id) == 0) {
175 54be8251 2018-06-04 stsp TAILQ_REMOVE(&set->entries[i], entry, entry);
176 c6f420bf 2018-06-04 stsp set->nelem--;
177 54be8251 2018-06-04 stsp return NULL;
178 54be8251 2018-06-04 stsp }
179 54be8251 2018-06-04 stsp }
180 54be8251 2018-06-04 stsp
181 54be8251 2018-06-04 stsp return got_error(GOT_ERR_NO_OBJ);
182 54be8251 2018-06-04 stsp }
183 54be8251 2018-06-04 stsp
184 27c21a11 2018-06-22 stsp const struct got_error *
185 27c21a11 2018-06-22 stsp got_object_idset_remove_random(void **data, struct got_object_idset *set)
186 27c21a11 2018-06-22 stsp {
187 27c21a11 2018-06-22 stsp struct got_object_idset_element *entry, *tmp;
188 27c21a11 2018-06-22 stsp int i, n;
189 27c21a11 2018-06-22 stsp
190 27c21a11 2018-06-22 stsp *data = NULL;
191 27c21a11 2018-06-22 stsp
192 27c21a11 2018-06-22 stsp if (set->nelem == 0)
193 27c21a11 2018-06-22 stsp return got_error(GOT_ERR_NO_OBJ);
194 27c21a11 2018-06-22 stsp
195 27c21a11 2018-06-22 stsp n = arc4random_uniform(set->nelem);
196 27c21a11 2018-06-22 stsp for (i = 0; i < nitems(set->entries); i++) {
197 27c21a11 2018-06-22 stsp TAILQ_FOREACH_SAFE(entry, &set->entries[i], entry, tmp) {
198 27c21a11 2018-06-22 stsp if (--n == 0) {
199 27c21a11 2018-06-22 stsp TAILQ_REMOVE(&set->entries[i], entry, entry);
200 27c21a11 2018-06-22 stsp *data = entry->data;
201 27c21a11 2018-06-22 stsp free(entry);
202 27c21a11 2018-06-22 stsp set->nelem--;
203 27c21a11 2018-06-22 stsp return NULL;
204 27c21a11 2018-06-22 stsp }
205 27c21a11 2018-06-22 stsp }
206 27c21a11 2018-06-22 stsp
207 27c21a11 2018-06-22 stsp }
208 27c21a11 2018-06-22 stsp
209 27c21a11 2018-06-22 stsp return got_error(GOT_ERR_NO_OBJ);
210 27c21a11 2018-06-22 stsp }
211 27c21a11 2018-06-22 stsp
212 54be8251 2018-06-04 stsp int
213 54be8251 2018-06-04 stsp got_object_idset_contains(struct got_object_idset *set,
214 54be8251 2018-06-04 stsp struct got_object_id *id)
215 54be8251 2018-06-04 stsp {
216 54be8251 2018-06-04 stsp struct got_object_idset_element *entry;
217 54be8251 2018-06-04 stsp uint8_t i = id->sha1[0];
218 54be8251 2018-06-04 stsp
219 54be8251 2018-06-04 stsp TAILQ_FOREACH(entry, &set->entries[i], entry) {
220 54be8251 2018-06-04 stsp if (got_object_id_cmp(&entry->id, id) == 0)
221 54be8251 2018-06-04 stsp return 1;
222 54be8251 2018-06-04 stsp }
223 54be8251 2018-06-04 stsp
224 54be8251 2018-06-04 stsp return 0;
225 54be8251 2018-06-04 stsp }
226 54be8251 2018-06-04 stsp
227 54be8251 2018-06-04 stsp void got_object_idset_for_each(struct got_object_idset *set,
228 917bfd05 2018-06-10 stsp void (*cb)(struct got_object_id *, void *, void *), void *arg)
229 54be8251 2018-06-04 stsp {
230 956a5d5a 2018-06-04 stsp struct got_object_idset_element *entry;
231 54be8251 2018-06-04 stsp int i;
232 54be8251 2018-06-04 stsp
233 54be8251 2018-06-04 stsp for (i = 0; i < nitems(set->entries); i++) {
234 956a5d5a 2018-06-04 stsp TAILQ_FOREACH(entry, &set->entries[i], entry)
235 917bfd05 2018-06-10 stsp cb(&entry->id, entry->data, arg);
236 54be8251 2018-06-04 stsp }
237 54be8251 2018-06-04 stsp }
238 c6f420bf 2018-06-04 stsp
239 069f84d5 2018-06-11 stsp int
240 c6f420bf 2018-06-04 stsp got_object_idset_num_elements(struct got_object_idset *set)
241 c6f420bf 2018-06-04 stsp {
242 c6f420bf 2018-06-04 stsp return set->nelem;
243 c6f420bf 2018-06-04 stsp }