Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 623 lines (531 sloc) 15.348 kb
584c433 @yanhan Initial commit.
yanhan authored
1 #include <alpm.h>
2
3 #include "conf.h"
d4befbe @yanhan New --crawl option
yanhan authored
4 #include "curl.h"
584c433 @yanhan Initial commit.
yanhan authored
5 #include "environment.h"
d4befbe @yanhan New --crawl option
yanhan authored
6 #include "graph.h"
bb1e229 @yanhan Shift pw_hashdb to its own file
yanhan authored
7 #include "hashdb.h"
584c433 @yanhan Initial commit.
yanhan authored
8 #include "package.h"
9 #include "powaur.h"
10 #include "query.h"
11 #include "util.h"
12
13 /* -Qi */
14 static int query_info(pmdb_t *localdb, alpm_list_t *targets)
15 {
16 int ret, hits, found, pkgcount;
17 alpm_list_t *i, *k, *dbcache;
18 pmpkg_t *pkg;
19
20 ret = pkgcount = hits = found = 0;
21 dbcache = alpm_db_get_pkgcache(localdb);
22
23 for (i = targets; i; i = i->next, ++pkgcount) {
24 found = 0;
25 for (k = dbcache; k; k = k->next) {
26 pkg = k->data;
27 if (!strcmp(i->data, alpm_pkg_get_name(pkg))) {
28 if (hits++) {
29 printf("\n");
30 }
31
32 found = 1;
33 pacman_pkgdump(pkg, PKG_FROM_LOCAL);
34 break;
35 }
36 }
37
38 if (!found) {
39 if (pkgcount) {
40 printf("\n");
41 }
42
43 ret = -1;
44 pw_fprintf(PW_LOG_ERROR, stderr, "package %s not found\n", i->data);
45 }
46 }
47
48 return ret;
49 }
50
51 /* -Qs, only 1 target for now */
52 static int query_search(pmdb_t *localdb, const char *pkgname)
53 {
54 int ret, found;
fdcc4cf @yanhan Introduce colorized output for -Q and -S
yanhan authored
55 const char *repo;
584c433 @yanhan Initial commit.
yanhan authored
56 alpm_list_t *i, *k, *dbcache, *groups;
fdcc4cf @yanhan Introduce colorized output for -Q and -S
yanhan authored
57 alpm_list_t *syncdbs;
584c433 @yanhan Initial commit.
yanhan authored
58 pmpkg_t *pkg;
59
60 dbcache = alpm_db_get_pkgcache(localdb);
fdcc4cf @yanhan Introduce colorized output for -Q and -S
yanhan authored
61 syncdbs = alpm_option_get_syncdbs();
584c433 @yanhan Initial commit.
yanhan authored
62
63 for (k = dbcache; k; k = k->next) {
64 pkg = k->data;
fdcc4cf @yanhan Introduce colorized output for -Q and -S
yanhan authored
65 groups = NULL;
584c433 @yanhan Initial commit.
yanhan authored
66
fdcc4cf @yanhan Introduce colorized output for -Q and -S
yanhan authored
67 if (!strcmp(pkgname, alpm_pkg_get_name(pkg))) {
68 repo = which_db(syncdbs, pkgname, &groups);
25865b4 @yanhan Colorized output for -Q and -S
yanhan authored
69 color_repo(repo);
70 printf("%s%s %s%s", color.bold, pkgname,
fdcc4cf @yanhan Introduce colorized output for -Q and -S
yanhan authored
71 color.bgreen, alpm_pkg_get_version(pkg));
25865b4 @yanhan Colorized output for -Q and -S
yanhan authored
72 color_groups(groups);
73 printf("%s %s\n", TAB, alpm_pkg_get_desc(pkg));
584c433 @yanhan Initial commit.
yanhan authored
74 found = 1;
75 }
76 }
77
78 return found ? 0: -1;
79 }
80
81 int powaur_query(alpm_list_t *targets)
82 {
83 pmdb_t *localdb = alpm_option_get_localdb();
b848e42 @yanhan Add error functions and wrapper functions
yanhan authored
84 if (!localdb) {
85 return error(PW_ERR_INIT_LOCALDB);
86 }
584c433 @yanhan Initial commit.
yanhan authored
87
88 alpm_list_t *dblist = NULL;
25865b4 @yanhan Colorized output for -Q and -S
yanhan authored
89 alpm_list_t *i, *j, *dbcache;
fdcc4cf @yanhan Introduce colorized output for -Q and -S
yanhan authored
90 pmpkg_t *pkg, *spkg;
584c433 @yanhan Initial commit.
yanhan authored
91 int ret = 0, found;
92
93 /* -i and -s conflicting options */
94 if (config->op_q_info && config->op_q_search) {
95 pw_fprintf(PW_LOG_ERROR, stderr, "-i (info) and -s (search) are "
96 "mutually exclusive.\n");
97 return -1;
98 }
99
100 /* No targets */
101 if (targets == NULL) {
102 dblist = alpm_list_add(dblist, localdb);
103
104 if (config->op_q_info) {
25865b4 @yanhan Colorized output for -Q and -S
yanhan authored
105 /* -Qi, detailed info */
106 ret = pacman_db_dump(PKG_FROM_LOCAL, DUMP_Q_INFO);
584c433 @yanhan Initial commit.
yanhan authored
107 } else if (config->op_q_search) {
25865b4 @yanhan Colorized output for -Q and -S
yanhan authored
108 /* -Qs
109 * repo/pkg ver (grp)
110 * desc
111 */
112 ret = pacman_db_dump(PKG_FROM_LOCAL, DUMP_Q_SEARCH);
584c433 @yanhan Initial commit.
yanhan authored
113 } else {
25865b4 @yanhan Colorized output for -Q and -S
yanhan authored
114 /* -Q
115 * repo/pkg ver (grp)
116 */
117 ret = pacman_db_dump(PKG_FROM_LOCAL, DUMP_Q);
584c433 @yanhan Initial commit.
yanhan authored
118 }
119
120 alpm_list_free(dblist);
121 return ret;
122 }
123
124 if (config->op_q_info) {
125 ret = query_info(localdb, targets);
126 } else if (config->op_q_search) {
127 ret = query_search(localdb, targets->data);
128 } else {
129 /* Plain -Q */
25865b4 @yanhan Colorized output for -Q and -S
yanhan authored
130 alpm_list_t *sdbs = alpm_option_get_syncdbs();
584c433 @yanhan Initial commit.
yanhan authored
131 dbcache = alpm_db_get_pkgcache(localdb);
132
133 for (i = targets; i; i = i->next) {
134 found = 0;
135 for (j = dbcache; j; j = j->next) {
136 pkg = j->data;
137 if (!strcmp(i->data, alpm_pkg_get_name(pkg))) {
25865b4 @yanhan Colorized output for -Q and -S
yanhan authored
138 print_pkg_pretty(sdbs, pkg, DUMP_Q);
584c433 @yanhan Initial commit.
yanhan authored
139 found = 1;
140 break;
141 }
142 }
143
144 if (!found) {
145 printf("package \"%s\" not found\n", i->data);
146 ret = -1;
147 }
148 }
149 }
150
151 return ret;
152 }
d4befbe @yanhan New --crawl option
yanhan authored
153
f60d87d @yanhan Enhance provides resolution
yanhan authored
154 /* Change provided package to a package which provides it.
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
155 * For AUR packages, this also downloads and extracts PKGBUILD in cwd.
156 * In addition, the "normalized" packages will be cached in hashdb->pkg_from
157 *
158 * @param curl curl handle
f60d87d @yanhan Enhance provides resolution
yanhan authored
159 * @param hashdb hash database
160 * @param pkg package name
8f276db @yanhan Change FORCE_DL and NOFORCE
yanhan authored
161 * @param resolve_lvl level of dep resolution. RESOLVE_THOROUGH forces
162 * downloading of AUR PKGBUILDs
f60d87d @yanhan Enhance provides resolution
yanhan authored
163 *
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
164 * returns the "normalized" package if present, NULL on failure
f60d87d @yanhan Enhance provides resolution
yanhan authored
165 */
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
166 static const char *normalize_package(CURL *curl, struct pw_hashdb *hashdb,
8f276db @yanhan Change FORCE_DL and NOFORCE
yanhan authored
167 const char *pkgname, int resolve_lvl)
f60d87d @yanhan Enhance provides resolution
yanhan authored
168 {
169 const char *provided = NULL;
170 struct pkgpair pkgpair;
171 struct pkgpair *pkgptr;
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
172 enum pkgfrom_t *pkgfrom;
f60d87d @yanhan Enhance provides resolution
yanhan authored
173
174 pkgpair.pkgname = pkgname;
175 pkgpair.pkg = NULL;
176
152566f @yanhan -S: Only resolve new / outdated AUR packages
yanhan authored
177 /* If we know where pkg is from and it's not AUR / it's from AUR and
178 * already downloaded, done */
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
179 pkgfrom = hashmap_search(hashdb->pkg_from, (void *) pkgname);
180 if (pkgfrom) {
181 if (*pkgfrom != PKG_FROM_AUR ||
182 hash_search(hashdb->aur_downloaded, (void *) pkgname)) {
183 return pkgname;
184 }
185
186 goto search_aur;
187 }
188
189 /* If it's in local db and not AUR, done */
190 if (hash_search(hashdb->local, &pkgpair)) {
191 if (hash_search(hashdb->aur, &pkgpair)) {
192 goto search_aur;
193 }
194 hashmap_insert(hashdb->pkg_from, (void *) pkgname, &hashdb->pkg_from_local);
f60d87d @yanhan Enhance provides resolution
yanhan authored
195 return pkgname;
196 }
197
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
198 /* Search provides cache */
f60d87d @yanhan Enhance provides resolution
yanhan authored
199 provided = hashmap_search(hashdb->provides_cache, (void *) pkgname);
200 if (provided) {
201 return provided;
202 }
203
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
204 /* Search local provides */
f60d87d @yanhan Enhance provides resolution
yanhan authored
205 pkgptr = hashbst_tree_search(hashdb->local_provides, (void *) pkgname,
206 hashdb->local, provides_search);
207 if (pkgptr) {
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
208 /* Cache in provides and pkg_from */
f60d87d @yanhan Enhance provides resolution
yanhan authored
209 hashmap_insert(hashdb->provides_cache, (void *) pkgname,
210 (void *) pkgptr->pkgname);
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
211 hashmap_insert(hashdb->pkg_from, (void *) pkgptr->pkgname, &hashdb->pkg_from_local);
212 return pkgptr->pkgname;
f60d87d @yanhan Enhance provides resolution
yanhan authored
213 }
214
305baf1 @yanhan Improved -G dependency resolution
yanhan authored
215 /* Search sync provides tree in local db
216 * TODO: Is there a meaning to this?
217 * local provides are obtained from local packages.
218 * sync provides are obtained from sync packages.
219 * So searching for sync provides in local database is kind of...
220 */
f60d87d @yanhan Enhance provides resolution
yanhan authored
221 pkgptr = hashbst_tree_search(hashdb->sync_provides, (void *) pkgname,
222 hashdb->local, provides_search);
223
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
224 if (pkgptr) {
225 /* Cache in pkg_from */
226 hashmap_insert(hashdb->pkg_from, (void *) pkgptr->pkgname, &hashdb->pkg_from_local);
227 return pkgptr->pkgname;
228 }
229
230 /* Search sync db */
231 if (hash_search(hashdb->sync, &pkgpair)) {
232 hashmap_insert(hashdb->pkg_from, (void *) pkgname, &hashdb->pkg_from_sync);
233 return pkgname;
f60d87d @yanhan Enhance provides resolution
yanhan authored
234 }
235
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
236 /* Sync provides */
237 pkgptr = hashbst_tree_search(hashdb->sync_provides, (void *) pkgname,
238 hashdb->sync, provides_search);
f60d87d @yanhan Enhance provides resolution
yanhan authored
239 if (pkgptr) {
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
240 hashmap_insert(hashdb->pkg_from, (void *) pkgptr->pkgname,
241 &hashdb->pkg_from_sync);
f60d87d @yanhan Enhance provides resolution
yanhan authored
242 hashmap_insert(hashdb->provides_cache, (void *) pkgname, (void *) pkgptr->pkgname);
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
243 return pkgptr->pkgname;
244 }
245
246 search_aur:
247 pkgpair.pkgname = pkgname;
248 pkgpair.pkg = NULL;
8f276db @yanhan Change FORCE_DL and NOFORCE
yanhan authored
249
250 /* For non RESOLVE_THOROUGH, don't bother downloading PKGBUILD of updated
251 * AUR packages
252 */
253 if (resolve_lvl != RESOLVE_THOROUGH) {
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
254 if (hash_search(hashdb->aur, &pkgpair) &&
255 !hash_search(hashdb->aur_outdated, (void *) pkgname)) {
256 goto done;
257 }
f60d87d @yanhan Enhance provides resolution
yanhan authored
258 }
259
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
260 /* Download and extract from AUR */
afd1711 @yanhan Disable downloading messages for --crawl
yanhan authored
261 if (dl_extract_single_package(curl, pkgname, NULL, 0)) {
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
262 return NULL;
263 }
264
265 hash_insert(hashdb->aur_downloaded, (void *) pkgname);
266 hashmap_insert(hashdb->pkg_from, (void *) pkgname, &hashdb->pkg_from_aur);
267
f60d87d @yanhan Enhance provides resolution
yanhan authored
268 done:
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
269 return pkgname;
f60d87d @yanhan Enhance provides resolution
yanhan authored
270 }
271
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
272 /* Resolve dependencies for a given package
273 * @param curl curl handle
274 * @param hashdb hash database
f60d87d @yanhan Enhance provides resolution
yanhan authored
275 * @param curpkg current package we are resolving
276 * @param dep_list pointer to list to store resulting dependencies
8f276db @yanhan Change FORCE_DL and NOFORCE
yanhan authored
277 * @param resolve_lvl level of dep resolution. RESOLVE_THOROUGH forces
278 * downloading of AUR PKGBUILDs
f60d87d @yanhan Enhance provides resolution
yanhan authored
279 *
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
280 * returns -1 on error, 0 on success
d4befbe @yanhan New --crawl option
yanhan authored
281 */
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
282 static int crawl_resolve(CURL *curl, struct pw_hashdb *hashdb, struct pkgpair *curpkg,
8f276db @yanhan Change FORCE_DL and NOFORCE
yanhan authored
283 alpm_list_t **dep_list, int resolve_lvl)
d4befbe @yanhan New --crawl option
yanhan authored
284 {
285 alpm_list_t *i, *depmod_list, *deps = NULL;
f60d87d @yanhan Enhance provides resolution
yanhan authored
286 struct pkgpair *pkgpair;
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
287 struct pkgpair tmppkg;
d4befbe @yanhan New --crawl option
yanhan authored
288 void *pkg_provides;
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
289 void *memlist_ptr;
f60d87d @yanhan Enhance provides resolution
yanhan authored
290 const char *cache_result;
291 const char *depname, *final_pkgname;
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
292 char cwd[PATH_MAX];
f60d87d @yanhan Enhance provides resolution
yanhan authored
293
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
294 /* Normalize package before doing anything else */
8f276db @yanhan Change FORCE_DL and NOFORCE
yanhan authored
295 final_pkgname = normalize_package(curl, hashdb, curpkg->pkgname, resolve_lvl);
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
296 if (!final_pkgname) {
297 return -1;
298 }
299
300 enum pkgfrom_t *from = hashmap_search(hashdb->pkg_from, (void *) final_pkgname);
301 if (!from) {
302 die("Failed to find out where package \"%s\" is from!\n", final_pkgname);
f60d87d @yanhan Enhance provides resolution
yanhan authored
303 }
304
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
305 switch (*from) {
306 case PKG_FROM_LOCAL:
307 tmppkg.pkgname = final_pkgname;
308 pkgpair = hash_search(hashdb->local, &tmppkg);
309 goto get_deps;
310 case PKG_FROM_SYNC:
311 tmppkg.pkgname = final_pkgname;
312 pkgpair = hash_search(hashdb->sync, &tmppkg);
313 goto get_deps;
314 default:
315 goto aur_deps;
d4befbe @yanhan New --crawl option
yanhan authored
316 }
317
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
318 aur_uptodate:
319 tmppkg.pkgname = final_pkgname;
320 tmppkg.pkg = NULL;
321 pkgpair = hash_search(hashdb->aur, &tmppkg);
322
323 get_deps:
d4befbe @yanhan New --crawl option
yanhan authored
324 if (!pkgpair) {
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
325 /* Shouldn't happen */
326 die("Unable to find package \"%s\" in local/sync db!", final_pkgname);
d4befbe @yanhan New --crawl option
yanhan authored
327 }
328
329 depmod_list = alpm_pkg_get_depends(pkgpair->pkg);
330 for (i = depmod_list; i; i = i->next) {
8f276db @yanhan Change FORCE_DL and NOFORCE
yanhan authored
331 depname = normalize_package(curl, hashdb, alpm_dep_get_name(i->data),
332 resolve_lvl);
00b165a @yanhan Check for return value of normalize_package
yanhan authored
333 /* Possibility of normalize_package fail due to AUR download failing */
334 if (!depname) {
335 alpm_list_free(deps);
336 return -1;
337 }
f60d87d @yanhan Enhance provides resolution
yanhan authored
338 deps = alpm_list_add(deps, (void *) depname);
d4befbe @yanhan New --crawl option
yanhan authored
339 }
340
f60d87d @yanhan Enhance provides resolution
yanhan authored
341 if (dep_list) {
342 *dep_list = deps;
343 } else {
344 alpm_list_free(deps);
d4befbe @yanhan New --crawl option
yanhan authored
345 }
346
f60d87d @yanhan Enhance provides resolution
yanhan authored
347 return 0;
d4befbe @yanhan New --crawl option
yanhan authored
348
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
349 aur_deps:
350 tmppkg.pkgname = final_pkgname;
351 tmppkg.pkg = NULL;
352
353 /* For installed AUR packages which are up to date */
8f276db @yanhan Change FORCE_DL and NOFORCE
yanhan authored
354 if (resolve_lvl != RESOLVE_THOROUGH) {
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
355 if (hash_search(hashdb->aur, &tmppkg) &&
356 !hash_search(hashdb->aur_outdated, (void *) final_pkgname)) {
357 /* NOTE: top goto ! */
358 goto aur_uptodate;
f60d87d @yanhan Enhance provides resolution
yanhan authored
359 }
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
360 }
361
8f276db @yanhan Change FORCE_DL and NOFORCE
yanhan authored
362 /* RESOLVE_THOROUGH / out to date AUR package.
363 * Download pkgbuild and extract deps */
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
364 if (!getcwd(cwd, PATH_MAX)) {
365 return error(PW_ERR_GETCWD);
366 }
f60d87d @yanhan Enhance provides resolution
yanhan authored
367
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
368 if (chdir(final_pkgname)) {
369 return error(PW_ERR_CHDIR);
d4befbe @yanhan New --crawl option
yanhan authored
370 }
371
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
372 deps = grab_dependencies("PKGBUILD");
373 if (chdir(cwd)) {
374 alpm_list_free(deps);
375 return error(PW_ERR_RESTORECWD);
376 }
377
378 if (dep_list) {
379 const char *normdep;
380 alpm_list_t *new_deps = NULL;
381
382 /* Transfer control to memlist and normalize packages */
383 for (i = deps; i; i = i->next) {
384 memlist_ptr = memlist_add(hashdb->strpool, &i->data);
8f276db @yanhan Change FORCE_DL and NOFORCE
yanhan authored
385 normdep = normalize_package(curl, hashdb, memlist_ptr, resolve_lvl);
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
386 new_deps = alpm_list_add(new_deps, (void *) normdep);
387 }
388
389 *dep_list = new_deps;
390 }
391
392 alpm_list_free(deps);
f60d87d @yanhan Enhance provides resolution
yanhan authored
393 return 0;
d4befbe @yanhan New --crawl option
yanhan authored
394 }
395
152566f @yanhan -S: Only resolve new / outdated AUR packages
yanhan authored
396 /* Depending on resolve_lvl, decide if we want to continue resolving the package
397 *
398 * If resolve_lvl is RESOLVE_IMMEDIATE, only out of date / new AUR packages
399 * will be resolved.
400 */
401 static void should_we_continue_resolving(CURL *curl,
402 struct pw_hashdb *hashdb,
403 struct stack *st,
404 struct pkgpair *deppkg,
405 int resolve_lvl)
406 {
407 if (resolve_lvl == RESOLVE_THOROUGH) {
408 stack_push(st, deppkg);
409 return;
410 }
411
412 /* RESOLVE_IMMEDIATE. Normalize package. Only continue resolving
413 * new AUR packages
414 */
415 const char *pkgname;
416 pkgname = normalize_package(curl, hashdb, deppkg->pkgname, resolve_lvl);
417
418 /* Continue resolving for new AUR packages */
419 if (hash_search(hashdb->aur_downloaded, (void *) pkgname)) {
420 stack_push(st, deppkg);
421 }
422 }
423
452b554 @yanhan Print immediate dependencies for -S
yanhan authored
424 /* Adds immediate dependencies to hashdb->immediate_deps
425 * This enables us to print the immediate deps instead of the entire huge
426 * dependency graph.
427 */
428 static void add_immediate_deps(struct pw_hashdb *hashdb, const char *pkgname,
152566f @yanhan -S: Only resolve new / outdated AUR packages
yanhan authored
429 alpm_list_t *deps, struct hash_table *immediate)
452b554 @yanhan Print immediate dependencies for -S
yanhan authored
430 {
431 enum pkgfrom_t *from = NULL;
432 alpm_list_t *i;
433 struct pkgpair pkgpair;
434
435 from = hashmap_search(hashdb->pkg_from, (void *) pkgname);
436 if (!from) {
437 return;
438 } else if (*from == PKG_FROM_AUR) {
439 pkgpair.pkgname = pkgname;
440 /* Applies to new AUR packages and outdated AUR packages */
441 if (!hash_search(hashdb->aur, &pkgpair) ||
442 hash_search(hashdb->aur_outdated, (void *) pkgname)) {
443 for (i = deps; i; i = i->next) {
152566f @yanhan -S: Only resolve new / outdated AUR packages
yanhan authored
444 if (!hash_search(immediate, (void *) i->data)) {
445 hashdb->immediate_deps = alpm_list_add(hashdb->immediate_deps, i->data);
446 hash_insert(immediate, (void *) i->data);
447 }
452b554 @yanhan Print immediate dependencies for -S
yanhan authored
448 }
449 }
450 }
451 }
452
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
453 void build_dep_graph(struct graph **graph, struct pw_hashdb *hashdb,
8f276db @yanhan Change FORCE_DL and NOFORCE
yanhan authored
454 alpm_list_t *targets, int resolve_lvl)
d4befbe @yanhan New --crawl option
yanhan authored
455 {
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
456 if (!graph) {
457 return;
458 }
459
460 if (!*graph) {
461 *graph = graph_new((pw_hash_fn) sdbm, (pw_hashcmp_fn) strcmp);
462 }
463
d4befbe @yanhan New --crawl option
yanhan authored
464 struct stack *st = stack_new(sizeof(struct pkgpair));
f60d87d @yanhan Enhance provides resolution
yanhan authored
465 struct hash_table *resolved = hash_new(HASH_TABLE, (pw_hash_fn) sdbm,
466 (pw_hashcmp_fn) strcmp);
152566f @yanhan -S: Only resolve new / outdated AUR packages
yanhan authored
467 struct hash_table *immediate = hash_new(HASH_TABLE, (pw_hash_fn) sdbm,
468 (pw_hashcmp_fn) strcmp);
f60d87d @yanhan Enhance provides resolution
yanhan authored
469 int ret;
d4befbe @yanhan New --crawl option
yanhan authored
470 struct pkgpair pkgpair, deppkg;
471 alpm_list_t *i;
472 alpm_list_t *deps;
473
474 CURL *curl;
afa537d @yanhan Change curl_easy_init to use curl_easy_new
yanhan authored
475 curl = curl_easy_new();
d4befbe @yanhan New --crawl option
yanhan authored
476 if (!curl) {
477 error(PW_ERR_CURL_INIT);
478 return;
479 }
480
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
481 /* Push all packages down stack */
482 for (i = targets; i; i = i->next) {
483 pkgpair.pkgname = i->data;
484 pkgpair.pkg = NULL;
485 stack_push(st, &pkgpair);
486 }
d4befbe @yanhan New --crawl option
yanhan authored
487
488 while (!stack_empty(st)) {
489 stack_pop(st, &pkgpair);
f60d87d @yanhan Enhance provides resolution
yanhan authored
490 deps = NULL;
491
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
492 if (hash_search(resolved, (void *) pkgpair.pkgname)) {
f60d87d @yanhan Enhance provides resolution
yanhan authored
493 goto cleanup_deps;
494 }
d4befbe @yanhan New --crawl option
yanhan authored
495
8f276db @yanhan Change FORCE_DL and NOFORCE
yanhan authored
496 ret = crawl_resolve(curl, hashdb, &pkgpair, &deps, resolve_lvl);
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
497 if (ret) {
498 pw_fprintf(PW_LOG_ERROR, stderr, "Error in resolving packages.\n");
499 goto cleanup;
500 }
501
d4befbe @yanhan New --crawl option
yanhan authored
502 for (i = deps; i; i = i->next) {
503 deppkg.pkgname = i->data;
504 deppkg.pkg = NULL;
152566f @yanhan -S: Only resolve new / outdated AUR packages
yanhan authored
505
506 /* immediate vs thorough resolve */
507 should_we_continue_resolving(curl, hashdb, st, &deppkg, resolve_lvl);
508
d4befbe @yanhan New --crawl option
yanhan authored
509 /* dep --> current */
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
510 graph_add_edge(*graph, i->data, (void *) pkgpair.pkgname);
d4befbe @yanhan New --crawl option
yanhan authored
511 }
512
f60d87d @yanhan Enhance provides resolution
yanhan authored
513 hash_insert(resolved, (void *) pkgpair.pkgname);
452b554 @yanhan Print immediate dependencies for -S
yanhan authored
514
515 /* Add immediate dependencies, for pretty printing purposes */
152566f @yanhan -S: Only resolve new / outdated AUR packages
yanhan authored
516 add_immediate_deps(hashdb, pkgpair.pkgname, deps, immediate);
f60d87d @yanhan Enhance provides resolution
yanhan authored
517 cleanup_deps:
d4befbe @yanhan New --crawl option
yanhan authored
518 alpm_list_free(deps);
519 }
520
b31fc30 @yanhan Factor out dep resolution
yanhan authored
521 cleanup:
522 hash_free(resolved);
152566f @yanhan -S: Only resolve new / outdated AUR packages
yanhan authored
523 hash_free(immediate);
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
524 stack_free(st);
b31fc30 @yanhan Factor out dep resolution
yanhan authored
525 curl_easy_cleanup(curl);
526 }
527
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
528 void print_topo_order(struct graph *graph, struct stack *topost)
b31fc30 @yanhan Factor out dep resolution
yanhan authored
529 {
d4befbe @yanhan New --crawl option
yanhan authored
530 int idx;
531 int cnt = 0;
b31fc30 @yanhan Factor out dep resolution
yanhan authored
532 const char *curpkg;
f60d87d @yanhan Enhance provides resolution
yanhan authored
533
d4befbe @yanhan New --crawl option
yanhan authored
534 while (!stack_empty(topost)) {
535 stack_pop(topost, &idx);
536 curpkg = graph_get_vertex_data(graph, idx);
537 if (!curpkg) {
538 continue;
539 }
540
541 if (cnt++ > 0) {
542 printf(" -> %s", curpkg);
543 } else {
544 printf("%s", curpkg);
545 }
546 }
547
548 printf("\n");
549 }
550
551 int powaur_crawl(alpm_list_t *targets)
552 {
de78568 @yanhan Change --crawl to use powaur_dir
yanhan authored
553 int ret = 0;
554 char cwd[PATH_MAX];
555 if (!getcwd(cwd, PATH_MAX)) {
556 return error(PW_ERR_GETCWD);
557 }
558
559 if (chdir(powaur_dir)) {
560 return error(PW_ERR_CHDIR, powaur_dir);
561 }
562
bb1e229 @yanhan Shift pw_hashdb to its own file
yanhan authored
563 struct pw_hashdb *hashdb = build_hashdb();
564 if (!hashdb) {
565 pw_fprintf(PW_LOG_ERROR, stderr, "Unable to build hash database!\n");
de78568 @yanhan Change --crawl to use powaur_dir
yanhan authored
566 ret = -1;
d4befbe @yanhan New --crawl option
yanhan authored
567 }
568
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
569 alpm_list_t *i, *target_pkgs;
b31fc30 @yanhan Factor out dep resolution
yanhan authored
570 struct graph *graph;
571 struct stack *topost = stack_new(sizeof(int));
572 int have_cycles;
d4befbe @yanhan New --crawl option
yanhan authored
573 for (i = targets; i; i = i->next) {
b31fc30 @yanhan Factor out dep resolution
yanhan authored
574 stack_reset(topost);
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
575 graph = NULL;
576 target_pkgs = alpm_list_add(NULL, i->data);
8f276db @yanhan Change FORCE_DL and NOFORCE
yanhan authored
577 build_dep_graph(&graph, hashdb, target_pkgs, RESOLVE_THOROUGH);
b31fc30 @yanhan Factor out dep resolution
yanhan authored
578 if (have_cycles) {
579 printf("Cyclic dependencies for package \"%s\"\n", i->data);
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
580 }
581
582 graph_toposort(graph, topost);
583 if (stack_empty(topost)) {
584 printf("Package \"%s\" has no dependencies.\n", i->data);
b31fc30 @yanhan Factor out dep resolution
yanhan authored
585 } else {
586 printf("\n");
587 pw_printf(PW_LOG_INFO, "\"%s\" topological order: ", i->data);
588 print_topo_order(graph, topost);
589 }
590
591 graph_free(graph);
c2c1ee1 @yanhan Complete partial dep resolution for -Su
yanhan authored
592 alpm_list_free(target_pkgs);
d4befbe @yanhan New --crawl option
yanhan authored
593 }
594
b31fc30 @yanhan Factor out dep resolution
yanhan authored
595 stack_free(topost);
f60d87d @yanhan Enhance provides resolution
yanhan authored
596 hashdb_free(hashdb);
de78568 @yanhan Change --crawl to use powaur_dir
yanhan authored
597
598 if (chdir(cwd)) {
599 return error(PW_ERR_RESTORECWD);
600 }
601 return ret;
d4befbe @yanhan New --crawl option
yanhan authored
602 }
23d7fbe @yanhan Add --list-aur option
yanhan authored
603
be9faff @yanhan Change pkgpair_print to pkgpair_print_color
yanhan authored
604 void pkgpair_print_color(void *p)
605 {
606 struct pkgpair *pkgpair_ptr = p;
607 printf("%s%s%s %s%s%s\n", color.bold, pkgpair_ptr->pkgname, color.nocolor,
608 color.bgreen, alpm_pkg_get_version(pkgpair_ptr->pkg), color.nocolor);
609 }
610
23d7fbe @yanhan Add --list-aur option
yanhan authored
611 int powaur_list_aur(void)
612 {
613 struct pw_hashdb *hashdb = build_hashdb();
614 if (!hashdb) {
615 pw_fprintf(PW_LOG_ERROR, stderr, "Failed to build hashdb!\n");
616 return -1;
617 }
618
be9faff @yanhan Change pkgpair_print to pkgpair_print_color
yanhan authored
619 hash_walk(hashdb->aur, pkgpair_print_color);
23d7fbe @yanhan Add --list-aur option
yanhan authored
620 hashdb_free(hashdb);
621 return 0;
622 }
Something went wrong with that request. Please try again.