XBPS Library API 20250713
The X Binary Package System
plist.c
1/*-
2 * Copyright (c) 2008-2015 Juan Romero Pardines.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <errno.h>
27#include <pthread.h>
28#include <stdbool.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33
34#include "xbps_api_impl.h"
35
36struct thread_data {
37 pthread_t thread;
38 xbps_array_t array;
39 xbps_dictionary_t dict;
40 struct xbps_handle *xhp;
41 unsigned int start;
42 unsigned int arraycount;
43 unsigned int *reserved;
44 pthread_mutex_t *reserved_lock;
45 unsigned int slicecount;
46 int (*fn)(struct xbps_handle *, xbps_object_t, const char *, void *, bool *);
47 void *fn_arg;
48 int r;
49};
50
51/**
52 * @file lib/plist.c
53 * @brief PropertyList generic routines
54 * @defgroup plist PropertyList generic functions
55 *
56 * These functions manipulate plist files and objects shared by almost
57 * all library functions.
58 */
59static void *
60array_foreach_thread(void *arg)
61{
62 xbps_object_t obj, pkgd;
63 struct thread_data *thd = arg;
64 const char *key;
65 int r;
66 bool loop_done = false;
67 unsigned i = thd->start;
68 unsigned int end = i + thd->slicecount;
69
70 while(i < thd->arraycount) {
71 /* process pkgs from start until end */
72 for (; i < end && i < thd->arraycount; i++) {
73 obj = xbps_array_get(thd->array, i);
74 if (xbps_object_type(thd->dict) == XBPS_TYPE_DICTIONARY) {
75 pkgd = xbps_dictionary_get_keysym(thd->dict, obj);
76 key = xbps_dictionary_keysym_cstring_nocopy(obj);
77 /* ignore internal objs */
78 if (strncmp(key, "_XBPS_", 6) == 0)
79 continue;
80 } else {
81 pkgd = obj;
82 key = NULL;
83 }
84 r = (*thd->fn)(thd->xhp, pkgd, key, thd->fn_arg, &loop_done);
85 if (r != 0 || loop_done) {
86 thd->r = r;
87 return NULL;
88 }
89 }
90 /* Reserve more elements to compute */
91 pthread_mutex_lock(thd->reserved_lock);
92 i = *thd->reserved;
93 end = i + thd->slicecount;
94 *thd->reserved = end;
95 pthread_mutex_unlock(thd->reserved_lock);
96 }
97 return NULL;
98}
99
100int
102 xbps_array_t array,
103 xbps_dictionary_t dict,
104 int (*fn)(struct xbps_handle *, xbps_object_t, const char *, void *, bool *),
105 void *arg)
106{
107 struct thread_data *thd;
108 unsigned int arraycount, slicecount;
109 int r, error = 0, i, maxthreads;
110 unsigned int reserved;
111 pthread_mutex_t reserved_lock = PTHREAD_MUTEX_INITIALIZER;
112
113 assert(fn != NULL);
114
115 if (xbps_object_type(array) != XBPS_TYPE_ARRAY)
116 return -EINVAL;
117
118 arraycount = xbps_array_count(array);
119 if (arraycount == 0)
120 return 0;
121
122 maxthreads = (int)sysconf(_SC_NPROCESSORS_ONLN);
123 if (maxthreads <= 1 || arraycount <= 1) /* use single threaded routine */
124 return xbps_array_foreach_cb(xhp, array, dict, fn, arg);
125
126 thd = calloc(maxthreads, sizeof(*thd));
127 if (!thd)
128 return xbps_error_oom();
129
130 // maxthread is boundchecked to be > 1
131 if((unsigned int)maxthreads >= arraycount) {
132 maxthreads = arraycount;
133 slicecount = 1;
134 } else {
135 slicecount = arraycount / maxthreads;
136 if (slicecount > 32) {
137 slicecount = 32;
138 }
139 }
140
141 reserved = slicecount * maxthreads;
142
143 for (i = 0; i < maxthreads; i++) {
144 thd[i].array = array;
145 thd[i].dict = dict;
146 thd[i].xhp = xhp;
147 thd[i].fn = fn;
148 thd[i].fn_arg = arg;
149 thd[i].start = i * slicecount;
150 thd[i].reserved = &reserved;
151 thd[i].reserved_lock = &reserved_lock;
152 thd[i].slicecount = slicecount;
153 thd[i].arraycount = arraycount;
154
155 r = -pthread_create(&thd[i].thread, NULL, array_foreach_thread, &thd[i]);
156 if (r < 0) {
158 "failed to create thread: %s\n", strerror(-r));
159 break;
160 }
161 }
162
163 // if we are unable to create any threads, just do single threaded.
164 if (i == 0) {
165 pthread_mutex_destroy(&reserved_lock);
166 free(thd);
167 return xbps_array_foreach_cb(xhp, array, dict, fn, arg);
168 }
169
170 /* wait for all threads that were created successfully */
171 for (int c = 0; c < i; c++) {
172 r = -pthread_join(thd[c].thread, NULL);
173 if (r < 0) {
175 "failed to wait on thread: %s\n", strerror(-r));
176 error++;
177 }
178 }
179
180 pthread_mutex_destroy(&reserved_lock);
181
182 if (error != 0) {
183 free(thd);
184 return -EAGAIN;
185 }
186
187 r = 0;
188 for (int j = 0; j < i; j++) {
189 if (thd[j].r == 0)
190 continue;
191 r = thd[j].r;
192 break;
193 }
194
195 free(thd);
196 return r;
197}
198
199int
201 xbps_array_t array,
202 xbps_dictionary_t dict,
203 int (*fn)(struct xbps_handle *, xbps_object_t, const char *, void *, bool *),
204 void *arg)
205{
206 xbps_dictionary_t pkgd;
207 xbps_object_t obj;
208 const char *key;
209 int r = 0;
210 bool loop_done = false;
211
212 for (unsigned int i = 0; i < xbps_array_count(array); i++) {
213 obj = xbps_array_get(array, i);
214 if (xbps_object_type(dict) == XBPS_TYPE_DICTIONARY) {
215 pkgd = xbps_dictionary_get_keysym(dict, obj);
216 key = xbps_dictionary_keysym_cstring_nocopy(obj);
217 /* ignore internal objs */
218 if (strncmp(key, "_XBPS_", 6) == 0)
219 continue;
220 } else {
221 pkgd = obj;
222 key = NULL;
223 }
224 r = (*fn)(xhp, pkgd, key, arg, &loop_done);
225 if (r != 0 || loop_done)
226 return r;
227 }
228 return 0;
229}
230
231xbps_object_iterator_t
232xbps_array_iter_from_dict(xbps_dictionary_t dict, const char *key)
233{
234 xbps_array_t array;
235
236 assert(xbps_object_type(dict) == XBPS_TYPE_DICTIONARY);
237 assert(key != NULL);
238
239 array = xbps_dictionary_get(dict, key);
240 if (xbps_object_type(array) != XBPS_TYPE_ARRAY) {
241 errno = EINVAL;
242 return NULL;
243 }
244
245 return xbps_array_iterator(array);
246}
247
248static int
249array_replace_dict(xbps_array_t array,
250 xbps_dictionary_t dict,
251 const char *str,
252 bool bypattern)
253{
254 xbps_object_t obj;
255 const char *pkgver, *pkgname;
256
257 assert(xbps_object_type(array) == XBPS_TYPE_ARRAY);
258 assert(xbps_object_type(dict) == XBPS_TYPE_DICTIONARY);
259 assert(str != NULL);
260
261 for (unsigned int i = 0; i < xbps_array_count(array); i++) {
262 obj = xbps_array_get(array, i);
263 if (obj == NULL) {
264 continue;
265 }
266 if (!xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver)) {
267 continue;
268 }
269 if (bypattern) {
270 /* pkgpattern match */
271 if (xbps_pkgpattern_match(pkgver, str)) {
272 if (!xbps_array_set(array, i, dict)) {
273 return EINVAL;
274 }
275 return 0;
276 }
277 } else {
278 /* pkgname match */
279 xbps_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
280 if (strcmp(pkgname, str) == 0) {
281 if (!xbps_array_set(array, i, dict)) {
282 return EINVAL;
283 }
284 return 0;
285 }
286 }
287 }
288 /* no match */
289 return ENOENT;
290}
291
292int HIDDEN
293xbps_array_replace_dict_by_name(xbps_array_t array,
294 xbps_dictionary_t dict,
295 const char *pkgver)
296{
297 return array_replace_dict(array, dict, pkgver, false);
298}
299
300int HIDDEN
301xbps_array_replace_dict_by_pattern(xbps_array_t array,
302 xbps_dictionary_t dict,
303 const char *pattern)
304{
305 return array_replace_dict(array, dict, pattern, true);
306}
Generic XBPS structure handler for initialization.
Definition xbps.h:560
#define xbps_error_oom()
Log out of memory condition.
Definition xbps.h:776
void void xbps_error_printf(const char *fmt,...)
Prints error messages to stderr.
Definition log.c:98
xbps_object_iterator_t xbps_array_iter_from_dict(xbps_dictionary_t dict, const char *key)
Definition plist.c:232
int xbps_array_foreach_cb_multi(struct xbps_handle *xhp, xbps_array_t array, xbps_dictionary_t dict, int(*fn)(struct xbps_handle *, xbps_object_t, const char *, void *, bool *), void *arg)
Definition plist.c:101
int xbps_array_foreach_cb(struct xbps_handle *xhp, xbps_array_t array, xbps_dictionary_t dict, int(*fn)(struct xbps_handle *, xbps_object_t, const char *, void *, bool *), void *arg)
Definition plist.c:200
int xbps_pkgpattern_match(const char *pkgver, const char *pattern)
Definition util.c:530