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 984e8a45 2018-11-05 stsp #include <stddef.h>
18 54be8251 2018-06-04 stsp #include <sys/queue.h>
19 984e8a45 2018-11-05 stsp #include <sys/tree.h>
20 54be8251 2018-06-04 stsp
21 54be8251 2018-06-04 stsp #include <stdlib.h>
22 54be8251 2018-06-04 stsp #include <string.h>
23 54be8251 2018-06-04 stsp #include <sha1.h>
24 54be8251 2018-06-04 stsp #include <stdio.h>
25 54be8251 2018-06-04 stsp #include <zlib.h>
26 c6f420bf 2018-06-04 stsp #include <limits.h>
27 788c352e 2018-06-16 stsp #include <time.h>
28 54be8251 2018-06-04 stsp
29 54be8251 2018-06-04 stsp #include "got_object.h"
30 54be8251 2018-06-04 stsp #include "got_error.h"
31 54be8251 2018-06-04 stsp
32 54be8251 2018-06-04 stsp #include "got_lib_delta.h"
33 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
34 54be8251 2018-06-04 stsp #include "got_lib_object.h"
35 54be8251 2018-06-04 stsp #include "got_lib_object_idset.h"
36 54be8251 2018-06-04 stsp
37 54be8251 2018-06-04 stsp #ifndef nitems
38 54be8251 2018-06-04 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
39 54be8251 2018-06-04 stsp #endif
40 54be8251 2018-06-04 stsp
41 54be8251 2018-06-04 stsp struct got_object_idset_element {
42 984e8a45 2018-11-05 stsp RBT_ENTRY(got_object_idset_element) entry;
43 54be8251 2018-06-04 stsp struct got_object_id id;
44 54be8251 2018-06-04 stsp void *data; /* API user data */
45 54be8251 2018-06-04 stsp };
46 54be8251 2018-06-04 stsp
47 984e8a45 2018-11-05 stsp RBT_HEAD(got_object_idset_tree, got_object_idset_element);
48 984e8a45 2018-11-05 stsp
49 984e8a45 2018-11-05 stsp static int
50 984e8a45 2018-11-05 stsp cmp_elements(const struct got_object_idset_element *e1,
51 984e8a45 2018-11-05 stsp const struct got_object_idset_element *e2)
52 984e8a45 2018-11-05 stsp {
53 984e8a45 2018-11-05 stsp return got_object_id_cmp(&e1->id, &e2->id);
54 984e8a45 2018-11-05 stsp }
55 984e8a45 2018-11-05 stsp
56 984e8a45 2018-11-05 stsp RBT_PROTOTYPE(got_object_idset_tree, got_object_idset_element, entry,
57 984e8a45 2018-11-05 stsp cmp_elements);
58 984e8a45 2018-11-05 stsp
59 54be8251 2018-06-04 stsp struct got_object_idset {
60 984e8a45 2018-11-05 stsp struct got_object_idset_tree entries;
61 2bd394ff 2018-06-22 stsp int totelem;
62 069f84d5 2018-06-11 stsp #define GOT_OBJECT_IDSET_MAX_ELEM INT_MAX
63 54be8251 2018-06-04 stsp };
64 54be8251 2018-06-04 stsp
65 54be8251 2018-06-04 stsp struct got_object_idset *
66 60f2eee1 2018-07-08 stsp got_object_idset_alloc(void)
67 54be8251 2018-06-04 stsp {
68 54be8251 2018-06-04 stsp struct got_object_idset *set;
69 54be8251 2018-06-04 stsp
70 984e8a45 2018-11-05 stsp set = malloc(sizeof(*set));
71 54be8251 2018-06-04 stsp if (set == NULL)
72 54be8251 2018-06-04 stsp return NULL;
73 54be8251 2018-06-04 stsp
74 984e8a45 2018-11-05 stsp RBT_INIT(got_object_idset_tree, &set->entries);
75 984e8a45 2018-11-05 stsp set->totelem = 0;
76 54be8251 2018-06-04 stsp
77 54be8251 2018-06-04 stsp return set;
78 54be8251 2018-06-04 stsp }
79 54be8251 2018-06-04 stsp
80 54be8251 2018-06-04 stsp void
81 54be8251 2018-06-04 stsp got_object_idset_free(struct got_object_idset *set)
82 54be8251 2018-06-04 stsp {
83 54be8251 2018-06-04 stsp struct got_object_idset_element *entry;
84 54be8251 2018-06-04 stsp
85 984e8a45 2018-11-05 stsp while ((entry = RBT_MIN(got_object_idset_tree, &set->entries))) {
86 984e8a45 2018-11-05 stsp RBT_REMOVE(got_object_idset_tree, &set->entries, entry);
87 984e8a45 2018-11-05 stsp /* User data should be freed by caller. */
88 984e8a45 2018-11-05 stsp free(entry);
89 54be8251 2018-06-04 stsp }
90 984e8a45 2018-11-05 stsp
91 54be8251 2018-06-04 stsp free(set);
92 54be8251 2018-06-04 stsp }
93 54be8251 2018-06-04 stsp
94 54be8251 2018-06-04 stsp const struct got_error *
95 b36429ab 2018-11-05 stsp got_object_idset_add(struct got_object_idset *set, struct got_object_id *id,
96 b36429ab 2018-11-05 stsp void *data)
97 54be8251 2018-06-04 stsp {
98 b36429ab 2018-11-05 stsp struct got_object_idset_element *new;
99 54be8251 2018-06-04 stsp
100 2bd394ff 2018-06-22 stsp if (set->totelem >= GOT_OBJECT_IDSET_MAX_ELEM)
101 c6f420bf 2018-06-04 stsp return got_error(GOT_ERR_NO_SPACE);
102 c6f420bf 2018-06-04 stsp
103 294f39b0 2018-11-05 stsp new = malloc(sizeof(*new));
104 54be8251 2018-06-04 stsp if (new == NULL)
105 54be8251 2018-06-04 stsp return got_error_from_errno();
106 54be8251 2018-06-04 stsp
107 54be8251 2018-06-04 stsp memcpy(&new->id, id, sizeof(new->id));
108 54be8251 2018-06-04 stsp new->data = data;
109 54be8251 2018-06-04 stsp
110 984e8a45 2018-11-05 stsp RBT_INSERT(got_object_idset_tree, &set->entries, new);
111 b36429ab 2018-11-05 stsp set->totelem++;
112 b36429ab 2018-11-05 stsp return NULL;
113 54be8251 2018-06-04 stsp }
114 54be8251 2018-06-04 stsp
115 984e8a45 2018-11-05 stsp static struct got_object_idset_element *
116 984e8a45 2018-11-05 stsp find_element(struct got_object_idset *set, struct got_object_id *id)
117 54be8251 2018-06-04 stsp {
118 60f2eee1 2018-07-08 stsp struct got_object_idset_element *entry;
119 54be8251 2018-06-04 stsp
120 984e8a45 2018-11-05 stsp entry = RBT_ROOT(got_object_idset_tree, &set->entries);
121 984e8a45 2018-11-05 stsp while (entry) {
122 984e8a45 2018-11-05 stsp int cmp = got_object_id_cmp(id, &entry->id);
123 984e8a45 2018-11-05 stsp if (cmp < 0)
124 984e8a45 2018-11-05 stsp entry = RBT_LEFT(got_object_idset_tree, entry);
125 984e8a45 2018-11-05 stsp else if (cmp > 0)
126 984e8a45 2018-11-05 stsp entry = RBT_RIGHT(got_object_idset_tree, entry);
127 984e8a45 2018-11-05 stsp else
128 984e8a45 2018-11-05 stsp break;
129 54be8251 2018-06-04 stsp }
130 54be8251 2018-06-04 stsp
131 984e8a45 2018-11-05 stsp return entry;
132 54be8251 2018-06-04 stsp }
133 54be8251 2018-06-04 stsp
134 984e8a45 2018-11-05 stsp void *
135 984e8a45 2018-11-05 stsp got_object_idset_get(struct got_object_idset *set, struct got_object_id *id)
136 984e8a45 2018-11-05 stsp {
137 984e8a45 2018-11-05 stsp struct got_object_idset_element *entry = find_element(set, id);
138 984e8a45 2018-11-05 stsp return entry ? entry->data : NULL;
139 984e8a45 2018-11-05 stsp }
140 984e8a45 2018-11-05 stsp
141 54be8251 2018-06-04 stsp const struct got_error *
142 e7c810ea 2018-06-22 stsp got_object_idset_remove(void **data, struct got_object_idset *set,
143 54be8251 2018-06-04 stsp struct got_object_id *id)
144 54be8251 2018-06-04 stsp {
145 984e8a45 2018-11-05 stsp struct got_object_idset_element *entry;
146 54be8251 2018-06-04 stsp
147 441e144c 2018-06-22 stsp if (data)
148 441e144c 2018-06-22 stsp *data = NULL;
149 441e144c 2018-06-22 stsp
150 984e8a45 2018-11-05 stsp if (set->totelem == 0)
151 c6f420bf 2018-06-04 stsp return got_error(GOT_ERR_NO_OBJ);
152 c6f420bf 2018-06-04 stsp
153 f054b67a 2018-11-05 stsp if (id == NULL)
154 f054b67a 2018-11-05 stsp entry = RBT_ROOT(got_object_idset_tree, &set->entries);
155 f054b67a 2018-11-05 stsp else
156 f054b67a 2018-11-05 stsp entry = find_element(set, id);
157 984e8a45 2018-11-05 stsp if (entry == NULL)
158 984e8a45 2018-11-05 stsp return got_error(GOT_ERR_NO_OBJ);
159 54be8251 2018-06-04 stsp
160 984e8a45 2018-11-05 stsp RBT_REMOVE(got_object_idset_tree, &set->entries, entry);
161 984e8a45 2018-11-05 stsp if (data)
162 984e8a45 2018-11-05 stsp *data = entry->data;
163 984e8a45 2018-11-05 stsp free(entry);
164 984e8a45 2018-11-05 stsp set->totelem--;
165 984e8a45 2018-11-05 stsp return NULL;
166 54be8251 2018-06-04 stsp }
167 54be8251 2018-06-04 stsp
168 54be8251 2018-06-04 stsp int
169 54be8251 2018-06-04 stsp got_object_idset_contains(struct got_object_idset *set,
170 54be8251 2018-06-04 stsp struct got_object_id *id)
171 54be8251 2018-06-04 stsp {
172 984e8a45 2018-11-05 stsp struct got_object_idset_element *entry = find_element(set, id);
173 984e8a45 2018-11-05 stsp return entry ? 1 : 0;
174 54be8251 2018-06-04 stsp }
175 54be8251 2018-06-04 stsp
176 54be8251 2018-06-04 stsp void got_object_idset_for_each(struct got_object_idset *set,
177 917bfd05 2018-06-10 stsp void (*cb)(struct got_object_id *, void *, void *), void *arg)
178 54be8251 2018-06-04 stsp {
179 956a5d5a 2018-06-04 stsp struct got_object_idset_element *entry;
180 54be8251 2018-06-04 stsp
181 984e8a45 2018-11-05 stsp RBT_FOREACH(entry, got_object_idset_tree, &set->entries)
182 984e8a45 2018-11-05 stsp (*cb)(&entry->id, entry->data, arg);
183 54be8251 2018-06-04 stsp }
184 c6f420bf 2018-06-04 stsp
185 069f84d5 2018-06-11 stsp int
186 c6f420bf 2018-06-04 stsp got_object_idset_num_elements(struct got_object_idset *set)
187 c6f420bf 2018-06-04 stsp {
188 2bd394ff 2018-06-22 stsp return set->totelem;
189 c6f420bf 2018-06-04 stsp }
190 984e8a45 2018-11-05 stsp
191 984e8a45 2018-11-05 stsp RBT_GENERATE(got_object_idset_tree, got_object_idset_element, entry,
192 984e8a45 2018-11-05 stsp cmp_elements);