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 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 #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 2bd394ff 2018-06-22 stsp int nelem[0xff + 1];
53 2bd394ff 2018-06-22 stsp int totelem;
54 069f84d5 2018-06-11 stsp #define GOT_OBJECT_IDSET_MAX_ELEM INT_MAX
55 54be8251 2018-06-04 stsp };
56 54be8251 2018-06-04 stsp
57 54be8251 2018-06-04 stsp struct got_object_idset *
58 60f2eee1 2018-07-08 stsp got_object_idset_alloc(void)
59 54be8251 2018-06-04 stsp {
60 54be8251 2018-06-04 stsp struct got_object_idset *set;
61 54be8251 2018-06-04 stsp int i;
62 54be8251 2018-06-04 stsp
63 54be8251 2018-06-04 stsp set = calloc(1, sizeof(*set));
64 54be8251 2018-06-04 stsp if (set == NULL)
65 54be8251 2018-06-04 stsp return NULL;
66 54be8251 2018-06-04 stsp
67 54be8251 2018-06-04 stsp for (i = 0; i < nitems(set->entries); i++)
68 54be8251 2018-06-04 stsp TAILQ_INIT(&set->entries[i]);
69 54be8251 2018-06-04 stsp
70 54be8251 2018-06-04 stsp return set;
71 54be8251 2018-06-04 stsp }
72 54be8251 2018-06-04 stsp
73 54be8251 2018-06-04 stsp void
74 54be8251 2018-06-04 stsp got_object_idset_free(struct got_object_idset *set)
75 54be8251 2018-06-04 stsp {
76 54be8251 2018-06-04 stsp struct got_object_idset_element *entry;
77 54be8251 2018-06-04 stsp int i;
78 54be8251 2018-06-04 stsp
79 54be8251 2018-06-04 stsp for (i = 0; i < nitems(set->entries); i++) {
80 54be8251 2018-06-04 stsp while (!TAILQ_EMPTY(&set->entries[i])) {
81 54be8251 2018-06-04 stsp entry = TAILQ_FIRST(&set->entries[i]);
82 54be8251 2018-06-04 stsp TAILQ_REMOVE(&set->entries[i], entry, entry);
83 54be8251 2018-06-04 stsp /* User data should be freed by caller. */
84 54be8251 2018-06-04 stsp free(entry);
85 54be8251 2018-06-04 stsp }
86 54be8251 2018-06-04 stsp }
87 54be8251 2018-06-04 stsp free(set);
88 54be8251 2018-06-04 stsp }
89 54be8251 2018-06-04 stsp
90 54be8251 2018-06-04 stsp const struct got_error *
91 b36429ab 2018-11-05 stsp got_object_idset_add(struct got_object_idset *set, struct got_object_id *id,
92 b36429ab 2018-11-05 stsp void *data)
93 54be8251 2018-06-04 stsp {
94 b36429ab 2018-11-05 stsp struct got_object_idset_element *new;
95 54be8251 2018-06-04 stsp uint8_t i = id->sha1[0];
96 54be8251 2018-06-04 stsp
97 2bd394ff 2018-06-22 stsp if (set->totelem >= GOT_OBJECT_IDSET_MAX_ELEM)
98 c6f420bf 2018-06-04 stsp return got_error(GOT_ERR_NO_SPACE);
99 c6f420bf 2018-06-04 stsp
100 294f39b0 2018-11-05 stsp new = malloc(sizeof(*new));
101 54be8251 2018-06-04 stsp if (new == NULL)
102 54be8251 2018-06-04 stsp return got_error_from_errno();
103 54be8251 2018-06-04 stsp
104 54be8251 2018-06-04 stsp memcpy(&new->id, id, sizeof(new->id));
105 54be8251 2018-06-04 stsp new->data = data;
106 54be8251 2018-06-04 stsp
107 b36429ab 2018-11-05 stsp TAILQ_INSERT_HEAD(&set->entries[i], new, entry);
108 b36429ab 2018-11-05 stsp set->nelem[i]++;
109 b36429ab 2018-11-05 stsp set->totelem++;
110 b36429ab 2018-11-05 stsp return NULL;
111 54be8251 2018-06-04 stsp }
112 54be8251 2018-06-04 stsp
113 54be8251 2018-06-04 stsp void *
114 45b73774 2018-06-04 stsp got_object_idset_get(struct got_object_idset *set, struct got_object_id *id)
115 54be8251 2018-06-04 stsp {
116 60f2eee1 2018-07-08 stsp struct got_object_idset_element *entry;
117 54be8251 2018-06-04 stsp uint8_t i = id->sha1[0];
118 54be8251 2018-06-04 stsp
119 60f2eee1 2018-07-08 stsp TAILQ_FOREACH(entry, &set->entries[i], entry) {
120 60f2eee1 2018-07-08 stsp if (got_object_id_cmp(&entry->id, id) == 0)
121 60f2eee1 2018-07-08 stsp return entry->data;
122 54be8251 2018-06-04 stsp }
123 54be8251 2018-06-04 stsp
124 54be8251 2018-06-04 stsp return NULL;
125 54be8251 2018-06-04 stsp }
126 54be8251 2018-06-04 stsp
127 54be8251 2018-06-04 stsp const struct got_error *
128 e7c810ea 2018-06-22 stsp got_object_idset_remove(void **data, struct got_object_idset *set,
129 54be8251 2018-06-04 stsp struct got_object_id *id)
130 54be8251 2018-06-04 stsp {
131 54be8251 2018-06-04 stsp struct got_object_idset_element *entry, *tmp;
132 ac4e69fb 2018-06-22 stsp uint8_t i = id->sha1[0];
133 54be8251 2018-06-04 stsp
134 441e144c 2018-06-22 stsp if (data)
135 441e144c 2018-06-22 stsp *data = NULL;
136 441e144c 2018-06-22 stsp
137 2bd394ff 2018-06-22 stsp if (set->nelem[i] == 0)
138 c6f420bf 2018-06-04 stsp return got_error(GOT_ERR_NO_OBJ);
139 c6f420bf 2018-06-04 stsp
140 54be8251 2018-06-04 stsp TAILQ_FOREACH_SAFE(entry, &set->entries[i], entry, tmp) {
141 ac4e69fb 2018-06-22 stsp if (got_object_id_cmp(&entry->id, id) == 0) {
142 54be8251 2018-06-04 stsp TAILQ_REMOVE(&set->entries[i], entry, entry);
143 e7c810ea 2018-06-22 stsp if (data)
144 e7c810ea 2018-06-22 stsp *data = entry->data;
145 4a9bc5ca 2018-06-22 stsp free(entry);
146 2bd394ff 2018-06-22 stsp set->nelem[i]--;
147 2bd394ff 2018-06-22 stsp set->totelem--;
148 54be8251 2018-06-04 stsp return NULL;
149 54be8251 2018-06-04 stsp }
150 54be8251 2018-06-04 stsp }
151 54be8251 2018-06-04 stsp
152 54be8251 2018-06-04 stsp return got_error(GOT_ERR_NO_OBJ);
153 54be8251 2018-06-04 stsp }
154 54be8251 2018-06-04 stsp
155 54be8251 2018-06-04 stsp int
156 54be8251 2018-06-04 stsp got_object_idset_contains(struct got_object_idset *set,
157 54be8251 2018-06-04 stsp struct got_object_id *id)
158 54be8251 2018-06-04 stsp {
159 54be8251 2018-06-04 stsp struct got_object_idset_element *entry;
160 54be8251 2018-06-04 stsp uint8_t i = id->sha1[0];
161 54be8251 2018-06-04 stsp
162 54be8251 2018-06-04 stsp TAILQ_FOREACH(entry, &set->entries[i], entry) {
163 54be8251 2018-06-04 stsp if (got_object_id_cmp(&entry->id, id) == 0)
164 54be8251 2018-06-04 stsp return 1;
165 54be8251 2018-06-04 stsp }
166 54be8251 2018-06-04 stsp
167 54be8251 2018-06-04 stsp return 0;
168 54be8251 2018-06-04 stsp }
169 54be8251 2018-06-04 stsp
170 54be8251 2018-06-04 stsp void got_object_idset_for_each(struct got_object_idset *set,
171 917bfd05 2018-06-10 stsp void (*cb)(struct got_object_id *, void *, void *), void *arg)
172 54be8251 2018-06-04 stsp {
173 956a5d5a 2018-06-04 stsp struct got_object_idset_element *entry;
174 54be8251 2018-06-04 stsp int i;
175 54be8251 2018-06-04 stsp
176 54be8251 2018-06-04 stsp for (i = 0; i < nitems(set->entries); i++) {
177 956a5d5a 2018-06-04 stsp TAILQ_FOREACH(entry, &set->entries[i], entry)
178 917bfd05 2018-06-10 stsp cb(&entry->id, entry->data, arg);
179 54be8251 2018-06-04 stsp }
180 54be8251 2018-06-04 stsp }
181 c6f420bf 2018-06-04 stsp
182 069f84d5 2018-06-11 stsp int
183 c6f420bf 2018-06-04 stsp got_object_idset_num_elements(struct got_object_idset *set)
184 c6f420bf 2018-06-04 stsp {
185 2bd394ff 2018-06-22 stsp return set->totelem;
186 c6f420bf 2018-06-04 stsp }