1 /* opkg_hash.c - the opkg package management system
2 
3    Steven M. Ayer
4 
5    Copyright (C) 2002 Compaq Computer Corporation
6 
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 */
17 
18 #include <stdio.h>
19 
20 #include "hash_table.h"
21 #include "pkg.h"
22 #include "opkg_message.h"
23 #include "pkg_vec.h"
24 #include "pkg_hash.h"
25 #include "parse_util.h"
26 #include "pkg_parse.h"
27 #include "opkg_utils.h"
28 #include "sprintf_alloc.h"
29 #include "file_util.h"
30 #include "libbb/libbb.h"
31 #include "libbb/gzip.h"
32 
pkg_hash_init(void)33 void pkg_hash_init(void)
34 {
35 	hash_table_init("pkg-hash", &conf->pkg_hash,
36 			OPKG_CONF_DEFAULT_HASH_LEN);
37 }
38 
free_pkgs(const char * key,void * entry,void * data)39 static void free_pkgs(const char *key, void *entry, void *data)
40 {
41 	int i;
42 	abstract_pkg_t *ab_pkg;
43 
44 	/* Each entry in the hash table is an abstract package, which contains
45 	 * a list of packages that provide the abstract package.
46 	 */
47 
48 	ab_pkg = (abstract_pkg_t *) entry;
49 
50 	if (ab_pkg->pkgs) {
51 		for (i = 0; i < ab_pkg->pkgs->len; i++) {
52 			pkg_deinit(ab_pkg->pkgs->pkgs[i]);
53 			free(ab_pkg->pkgs->pkgs[i]);
54 		}
55 	}
56 
57 	abstract_pkg_vec_free(ab_pkg->provided_by);
58 	abstract_pkg_vec_free(ab_pkg->replaced_by);
59 	pkg_vec_free(ab_pkg->pkgs);
60 	free(ab_pkg->depended_upon_by);
61 	free(ab_pkg->name);
62 	free(ab_pkg);
63 }
64 
pkg_hash_deinit(void)65 void pkg_hash_deinit(void)
66 {
67 	hash_table_foreach(&conf->pkg_hash, free_pkgs, NULL);
68 	hash_table_deinit(&conf->pkg_hash);
69 }
70 
dist_hash_add_from_file(const char * lists_dir,pkg_src_t * dist)71 int dist_hash_add_from_file(const char *lists_dir, pkg_src_t * dist)
72 {
73 	nv_pair_list_elt_t *l;
74 	char *list_file, *subname;
75 
76 	list_for_each_entry(l, &conf->arch_list.head, node) {
77 		nv_pair_t *nv = (nv_pair_t *) l->data;
78 		sprintf_alloc(&subname, "%s-%s", dist->name, nv->name);
79 		sprintf_alloc(&list_file, "%s/%s", lists_dir, subname);
80 
81 		if (file_exists(list_file)) {
82 			if (pkg_hash_add_from_file(list_file, dist, NULL, 0, 0)) {
83 				free(list_file);
84 				return -1;
85 			}
86 			pkg_src_list_append(&conf->pkg_src_list, subname,
87 					    dist->value, "__dummy__", 0);
88 		}
89 
90 		free(list_file);
91 	}
92 
93 	return 0;
94 }
95 
96 int
pkg_hash_add_from_file(const char * file_name,pkg_src_t * src,pkg_dest_t * dest,int is_status_file,int state_flags)97 pkg_hash_add_from_file(const char *file_name,
98 		       pkg_src_t * src, pkg_dest_t * dest, int is_status_file, int state_flags)
99 {
100 	pkg_t *pkg;
101 	FILE *fp;
102 	char *buf;
103 	const size_t len = 4096;
104 	int ret = 0;
105 	struct gzip_handle zh;
106 
107 	if (src && src->gzip) {
108 		fp = gzip_fdopen(&zh, file_name);
109 	} else {
110 		fp = fopen(file_name, "r");
111 	}
112 
113 	if (fp == NULL) {
114 		opkg_perror(ERROR, "Failed to open %s", file_name);
115 		return -1;
116 	}
117 
118 	buf = xmalloc(len);
119 
120 	do {
121 		pkg = pkg_new();
122 		pkg->src = src;
123 		pkg->dest = dest;
124 		pkg->state_flag |= state_flags;
125 
126 		ret = parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, 0,
127 						 &buf, len);
128 
129 		if (pkg->name == NULL) {
130 			/* probably just a blank line */
131 			ret = 1;
132 		}
133 
134 		if (ret) {
135 			pkg_deinit(pkg);
136 			free(pkg);
137 			if (ret == -1)
138 				break;
139 			if (ret == 1)
140 				/* Probably a blank line, continue parsing. */
141 				ret = 0;
142 			continue;
143 		}
144 
145 		if (!(pkg->state_flag & SF_NEED_DETAIL)) {
146 			//opkg_msg(DEBUG, "Package %s is unrelated, ignoring.\n", pkg->name);
147 			pkg_deinit(pkg);
148 			free(pkg);
149 			continue;
150 		}
151 
152 		if (!pkg_get_architecture(pkg) || !pkg_get_arch_priority(pkg)) {
153 			char *version_str = pkg_version_str_alloc(pkg);
154 			opkg_msg(NOTICE, "Package %s version %s has no "
155 				 "valid architecture, ignoring.\n",
156 				 pkg->name, version_str);
157 			free(version_str);
158 			continue;
159 		}
160 
161 		hash_insert_pkg(pkg, is_status_file);
162 
163 	} while (!feof(fp));
164 
165 	free(buf);
166 	fclose(fp);
167 
168 	if (src && src->gzip)
169 		gzip_close(&zh);
170 
171 	return ret;
172 }
173 
174 /*
175  * Load in feed files from the cached "src" and/or "src/gz" locations.
176  */
pkg_hash_load_feeds(int state_flags)177 int pkg_hash_load_feeds(int state_flags)
178 {
179 	pkg_src_list_elt_t *iter;
180 	pkg_src_t *src;
181 	char *list_file, *lists_dir;
182 
183 	opkg_msg(INFO, "\n");
184 
185 	lists_dir = conf->restrict_to_default_dest ?
186 	    conf->default_dest->lists_dir : conf->lists_dir;
187 
188 	for (iter = void_list_first(&conf->pkg_src_list); iter;
189 	     iter = void_list_next(&conf->pkg_src_list, iter)) {
190 
191 		src = (pkg_src_t *) iter->data;
192 
193 		sprintf_alloc(&list_file, "%s/%s", lists_dir, src->name);
194 
195 		if (file_exists(list_file)) {
196 			if (pkg_hash_add_from_file(list_file, src, NULL, 0, state_flags)) {
197 				free(list_file);
198 				return -1;
199 			}
200 		}
201 		free(list_file);
202 	}
203 
204 	return 0;
205 }
206 
207 /*
208  * Load in status files from the configured "dest"s.
209  */
pkg_hash_load_status_files(void)210 int pkg_hash_load_status_files(void)
211 {
212 	pkg_dest_list_elt_t *iter;
213 	pkg_dest_t *dest;
214 
215 	opkg_msg(INFO, "\n");
216 
217 	for (iter = void_list_first(&conf->pkg_dest_list); iter;
218 	     iter = void_list_next(&conf->pkg_dest_list, iter)) {
219 
220 		dest = (pkg_dest_t *) iter->data;
221 
222 		if (file_exists(dest->status_file_name)) {
223 			if (pkg_hash_add_from_file
224 			    (dest->status_file_name, NULL, dest, 1, SF_NEED_DETAIL))
225 				return -1;
226 		}
227 	}
228 
229 	return 0;
230 }
231 
232 static void
pkg_hash_load_package_details_helper(const char * pkg_name,void * entry,void * data)233 pkg_hash_load_package_details_helper(const char *pkg_name, void *entry, void *data)
234 {
235 	int *count = data;
236 	abstract_pkg_t *ab_pkg = (abstract_pkg_t *) entry;
237 
238 	if (ab_pkg->state_flag & SF_NEED_DETAIL) {
239 		if (ab_pkg->state_flag & SF_MARKED) {
240 			opkg_msg(DEBUG, "skipping already seen flagged abpkg %s\n",
241 			         ab_pkg->name);
242 			return;
243 		}
244 
245 		opkg_msg(DEBUG, "found yet incomplete flagged abpkg %s\n",
246 		         ab_pkg->name);
247 
248 		(*count)++;
249 		ab_pkg->state_flag |= SF_MARKED;
250 	}
251 }
252 
pkg_hash_load_package_details(void)253 int pkg_hash_load_package_details(void)
254 {
255 	int n_need_detail;
256 
257 	while (1) {
258 		pkg_hash_load_feeds(0);
259 
260 		n_need_detail = 0;
261 		hash_table_foreach(&conf->pkg_hash, pkg_hash_load_package_details_helper, &n_need_detail);
262 
263 		if (n_need_detail > 0)
264 			opkg_msg(DEBUG, "Found %d packages requiring details, reloading feeds\n", n_need_detail);
265 		else
266 			break;
267 	}
268 
269 	return 0;
270 }
271 
pkg_hash_fetch_best_installation_candidate(abstract_pkg_t * apkg,int (* constraint_fcn)(pkg_t * pkg,void * cdata),void * cdata,int quiet)272 pkg_t *pkg_hash_fetch_best_installation_candidate(abstract_pkg_t * apkg,
273 						  int (*constraint_fcn) (pkg_t *
274 									 pkg,
275 									 void
276 									 *cdata),
277 						  void *cdata, int quiet)
278 {
279 	int i, j;
280 	int nprovides = 0;
281 	int nmatching = 0;
282 	int wrong_arch_found = 0;
283 	int arch_priority;
284 	pkg_vec_t *matching_pkgs;
285 	abstract_pkg_vec_t *matching_apkgs;
286 	abstract_pkg_vec_t *provided_apkg_vec;
287 	abstract_pkg_t **provided_apkgs;
288 	abstract_pkg_vec_t *providers;
289 	pkg_t *latest_installed_parent = NULL;
290 	pkg_t *latest_matching = NULL;
291 	pkg_t *priorized_matching = NULL;
292 	pkg_t *held_pkg = NULL;
293 	pkg_t *good_pkg_by_name = NULL;
294 
295 	if (apkg == NULL || apkg->provided_by == NULL
296 	    || (apkg->provided_by->len == 0))
297 		return NULL;
298 
299 	matching_pkgs = pkg_vec_alloc();
300 	matching_apkgs = abstract_pkg_vec_alloc();
301 	providers = abstract_pkg_vec_alloc();
302 
303 	opkg_msg(DEBUG, "Best installation candidate for %s:\n", apkg->name);
304 
305 	provided_apkg_vec = apkg->provided_by;
306 	nprovides = provided_apkg_vec->len;
307 	provided_apkgs = provided_apkg_vec->pkgs;
308 	if (nprovides > 1)
309 		opkg_msg(DEBUG, "apkg=%s nprovides=%d.\n", apkg->name,
310 			 nprovides);
311 
312 	/* accumulate all the providers */
313 	for (i = 0; i < nprovides; i++) {
314 		abstract_pkg_t *provider_apkg = provided_apkgs[i];
315 		opkg_msg(DEBUG, "Adding %s to providers.\n",
316 			 provider_apkg->name);
317 		abstract_pkg_vec_insert(providers, provider_apkg);
318 	}
319 	nprovides = providers->len;
320 
321 	for (i = 0; i < nprovides; i++) {
322 		abstract_pkg_t *provider_apkg =
323 		    abstract_pkg_vec_get(providers, i);
324 		abstract_pkg_t *replacement_apkg = NULL;
325 		pkg_vec_t *vec;
326 
327 		if (provider_apkg->replaced_by
328 		    && provider_apkg->replaced_by->len) {
329 			replacement_apkg = provider_apkg->replaced_by->pkgs[0];
330 			if (provider_apkg->replaced_by->len > 1) {
331 				opkg_msg(NOTICE, "Multiple replacers for %s, "
332 					 "using first one (%s).\n",
333 					 provider_apkg->name,
334 					 replacement_apkg->name);
335 			}
336 		}
337 
338 		if (replacement_apkg)
339 			opkg_msg(DEBUG,
340 				 "replacement_apkg=%s for provider_apkg=%s.\n",
341 				 replacement_apkg->name, provider_apkg->name);
342 
343 		if (replacement_apkg && (replacement_apkg != provider_apkg)) {
344 			if (abstract_pkg_vec_contains
345 			    (providers, replacement_apkg))
346 				continue;
347 			else
348 				provider_apkg = replacement_apkg;
349 		}
350 
351 		if (!(vec = provider_apkg->pkgs)) {
352 			opkg_msg(DEBUG, "No pkgs for provider_apkg %s.\n",
353 				 provider_apkg->name);
354 			continue;
355 		}
356 
357 		/* now check for supported architecture */
358 		{
359 			int max_count = 0;
360 
361 			/* count packages matching max arch priority and keep track of last one */
362 			for (j = 0; j < vec->len; j++) {
363 				pkg_t *maybe = vec->pkgs[j];
364 				arch_priority = pkg_get_arch_priority(maybe);
365 
366 				opkg_msg(DEBUG,
367 					 "%s arch=%s arch_priority=%d version=%s.\n",
368 					 maybe->name, pkg_get_architecture(maybe),
369 					 arch_priority, pkg_get_string(maybe, PKG_VERSION));
370 				/* We make sure not to add the same package twice. Need to search for the reason why
371 				   they show up twice sometimes. */
372 				if ((arch_priority > 0)
373 				    &&
374 				    (!pkg_vec_contains(matching_pkgs, maybe))) {
375 					max_count++;
376 					abstract_pkg_vec_insert(matching_apkgs,
377 								maybe->parent);
378 					pkg_vec_insert(matching_pkgs, maybe);
379 				}
380 			}
381 
382 			if (vec->len > 0 && matching_pkgs->len < 1)
383 				wrong_arch_found = 1;
384 		}
385 	}
386 
387 	if (matching_pkgs->len < 1) {
388 		if (wrong_arch_found)
389 			opkg_msg(ERROR, "Packages for %s found, but"
390 				 " incompatible with the architectures configured\n",
391 				 apkg->name);
392 		pkg_vec_free(matching_pkgs);
393 		abstract_pkg_vec_free(matching_apkgs);
394 		abstract_pkg_vec_free(providers);
395 		return NULL;
396 	}
397 
398 	if (matching_pkgs->len > 1)
399 		pkg_vec_sort(matching_pkgs,
400 			     pkg_name_version_and_architecture_compare);
401 	if (matching_apkgs->len > 1)
402 		abstract_pkg_vec_sort(matching_apkgs, abstract_pkg_name_compare);
403 
404 	for (i = 0; i < matching_pkgs->len; i++) {
405 		pkg_t *matching = matching_pkgs->pkgs[i];
406 		if (constraint_fcn(matching, cdata)) {
407 			opkg_msg(DEBUG, "Candidate: %s %s.\n",
408 				 matching->name, pkg_get_string(matching, PKG_VERSION));
409 			good_pkg_by_name = matching;
410 			/* It has been provided by hand, so it is what user want */
411 			if (matching->provided_by_hand == 1)
412 				break;
413 		}
414 	}
415 
416 	for (i = 0; i < matching_pkgs->len; i++) {
417 		pkg_t *matching = matching_pkgs->pkgs[i];
418 		latest_matching = matching;
419 		if (matching->parent->state_status == SS_INSTALLED
420 		    || matching->parent->state_status == SS_UNPACKED)
421 			latest_installed_parent = matching;
422 		if (matching->state_flag & (SF_HOLD | SF_PREFER)) {
423 			if (held_pkg)
424 				opkg_msg(NOTICE,
425 					 "Multiple packages (%s and %s) providing"
426 					 " same name marked HOLD or PREFER. "
427 					 "Using latest.\n", held_pkg->name,
428 					 matching->name);
429 			held_pkg = matching;
430 		}
431 	}
432 
433 	if (!good_pkg_by_name && !held_pkg && !latest_installed_parent
434 	    && matching_apkgs->len > 1 && !quiet) {
435 		int prio = 0;
436 		for (i = 0; i < matching_pkgs->len; i++) {
437 			pkg_t *matching = matching_pkgs->pkgs[i];
438 			arch_priority = pkg_get_arch_priority(matching);
439 			if (arch_priority > prio) {
440 				priorized_matching = matching;
441 				prio = arch_priority;
442 				opkg_msg(DEBUG, "Match %s with priority %i.\n",
443 					 matching->name, prio);
444 			}
445 		}
446 
447 	}
448 
449 	if (conf->verbosity >= INFO && matching_apkgs->len > 1) {
450 		opkg_msg(INFO, "%d matching pkgs for apkg=%s:\n",
451 			 matching_pkgs->len, apkg->name);
452 		for (i = 0; i < matching_pkgs->len; i++) {
453 			pkg_t *matching = matching_pkgs->pkgs[i];
454 			opkg_msg(INFO, "%s %s %s\n",
455 				 matching->name, pkg_get_string(matching, PKG_VERSION),
456 				 pkg_get_architecture(matching));
457 		}
458 	}
459 
460 	nmatching = matching_apkgs->len;
461 
462 	pkg_vec_free(matching_pkgs);
463 	abstract_pkg_vec_free(matching_apkgs);
464 	abstract_pkg_vec_free(providers);
465 
466 	if (good_pkg_by_name) {	/* We found a good candidate, we will install it */
467 		return good_pkg_by_name;
468 	}
469 	if (held_pkg) {
470 		opkg_msg(INFO, "Using held package %s.\n", held_pkg->name);
471 		return held_pkg;
472 	}
473 	if (latest_installed_parent) {
474 		opkg_msg(INFO,
475 			 "Using latest version of installed package %s.\n",
476 			 latest_installed_parent->name);
477 		return latest_installed_parent;
478 	}
479 	if (priorized_matching) {
480 		opkg_msg(INFO, "Using priorized matching %s %s %s.\n",
481 			 priorized_matching->name, pkg_get_string(priorized_matching, PKG_VERSION),
482 			 pkg_get_architecture(priorized_matching));
483 		return priorized_matching;
484 	}
485 	if (nmatching > 1) {
486 		opkg_msg(INFO, "No matching pkg out of %d matching_apkgs.\n",
487 			 nmatching);
488 		return NULL;
489 	}
490 	if (latest_matching) {
491 		opkg_msg(INFO, "Using latest matching %s %s %s.\n",
492 			 latest_matching->name, pkg_get_string(latest_matching, PKG_VERSION),
493 			 pkg_get_architecture(latest_matching));
494 		return latest_matching;
495 	}
496 	return NULL;
497 }
498 
pkg_name_constraint_fcn(pkg_t * pkg,void * cdata)499 static int pkg_name_constraint_fcn(pkg_t * pkg, void *cdata)
500 {
501 	const char *name = (const char *)cdata;
502 
503 	if (strcmp(pkg->name, name) == 0)
504 		return 1;
505 	else
506 		return 0;
507 }
508 
pkg_vec_fetch_by_name(const char * pkg_name)509 static pkg_vec_t *pkg_vec_fetch_by_name(const char *pkg_name)
510 {
511 	abstract_pkg_t *ab_pkg;
512 
513 	if (!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name)))
514 		return NULL;
515 
516 	if (ab_pkg->pkgs)
517 		return ab_pkg->pkgs;
518 
519 	if (ab_pkg->provided_by) {
520 		abstract_pkg_t *abpkg =
521 		    abstract_pkg_vec_get(ab_pkg->provided_by, 0);
522 		if (abpkg != NULL)
523 			return abpkg->pkgs;
524 		else
525 			return ab_pkg->pkgs;
526 	}
527 
528 	return NULL;
529 }
530 
pkg_hash_fetch_best_installation_candidate_by_name(const char * name)531 pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(const char *name)
532 {
533 	abstract_pkg_t *apkg = NULL;
534 
535 	if (!(apkg = abstract_pkg_fetch_by_name(name)))
536 		return NULL;
537 
538 	return pkg_hash_fetch_best_installation_candidate(apkg,
539 							  pkg_name_constraint_fcn,
540 							  apkg->name, 0);
541 }
542 
pkg_hash_fetch_by_name_version(const char * pkg_name,const char * version)543 pkg_t *pkg_hash_fetch_by_name_version(const char *pkg_name, const char *version)
544 {
545 	pkg_vec_t *vec;
546 	int i;
547 	char *version_str = NULL;
548 
549 	if (!(vec = pkg_vec_fetch_by_name(pkg_name)))
550 		return NULL;
551 
552 	for (i = 0; i < vec->len; i++) {
553 		version_str = pkg_version_str_alloc(vec->pkgs[i]);
554 		if (!strcmp(version_str, version)) {
555 			free(version_str);
556 			break;
557 		}
558 		free(version_str);
559 	}
560 
561 	if (i == vec->len)
562 		return NULL;
563 
564 	return vec->pkgs[i];
565 }
566 
pkg_hash_fetch_installed_by_name_dest(const char * pkg_name,pkg_dest_t * dest)567 pkg_t *pkg_hash_fetch_installed_by_name_dest(const char *pkg_name,
568 					     pkg_dest_t * dest)
569 {
570 	pkg_vec_t *vec;
571 	int i;
572 
573 	if (!(vec = pkg_vec_fetch_by_name(pkg_name))) {
574 		return NULL;
575 	}
576 
577 	for (i = 0; i < vec->len; i++)
578 		if ((vec->pkgs[i]->state_status == SS_INSTALLED
579 		     || vec->pkgs[i]->state_status == SS_UNPACKED)
580 		    && vec->pkgs[i]->dest == dest) {
581 			return vec->pkgs[i];
582 		}
583 
584 	return NULL;
585 }
586 
pkg_hash_fetch_installed_by_name(const char * pkg_name)587 pkg_t *pkg_hash_fetch_installed_by_name(const char *pkg_name)
588 {
589 	pkg_vec_t *vec;
590 	int i;
591 
592 	if (!(vec = pkg_vec_fetch_by_name(pkg_name))) {
593 		return NULL;
594 	}
595 
596 	for (i = 0; i < vec->len; i++) {
597 		if (vec->pkgs[i]->state_status == SS_INSTALLED
598 		    || vec->pkgs[i]->state_status == SS_UNPACKED) {
599 			return vec->pkgs[i];
600 		}
601 	}
602 
603 	return NULL;
604 }
605 
606 static void
pkg_hash_fetch_available_helper(const char * pkg_name,void * entry,void * data)607 pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
608 {
609 	int j;
610 	abstract_pkg_t *ab_pkg = (abstract_pkg_t *) entry;
611 	pkg_vec_t *all = (pkg_vec_t *) data;
612 	pkg_vec_t *pkg_vec = ab_pkg->pkgs;
613 
614 	if (!pkg_vec)
615 		return;
616 
617 	for (j = 0; j < pkg_vec->len; j++) {
618 		pkg_t *pkg = pkg_vec->pkgs[j];
619 		pkg_vec_insert(all, pkg);
620 	}
621 }
622 
pkg_hash_fetch_available(pkg_vec_t * all)623 void pkg_hash_fetch_available(pkg_vec_t * all)
624 {
625 	hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_available_helper,
626 			   all);
627 }
628 
629 static void
pkg_hash_fetch_all_installed_helper(const char * pkg_name,void * entry,void * data)630 pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry,
631 				    void *data)
632 {
633 	abstract_pkg_t *ab_pkg = (abstract_pkg_t *) entry;
634 	pkg_vec_t *all = (pkg_vec_t *) data;
635 	pkg_vec_t *pkg_vec = ab_pkg->pkgs;
636 	int j;
637 
638 	if (!pkg_vec)
639 		return;
640 
641 	for (j = 0; j < pkg_vec->len; j++) {
642 		pkg_t *pkg = pkg_vec->pkgs[j];
643 		if (pkg->state_status == SS_INSTALLED
644 		    || pkg->state_status == SS_UNPACKED)
645 			pkg_vec_insert(all, pkg);
646 	}
647 }
648 
pkg_hash_fetch_all_installed(pkg_vec_t * all)649 void pkg_hash_fetch_all_installed(pkg_vec_t * all)
650 {
651 	hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_all_installed_helper,
652 			   all);
653 }
654 
655 /*
656  * This assumes that the abstract pkg doesn't exist.
657  */
add_new_abstract_pkg_by_name(const char * pkg_name)658 static abstract_pkg_t *add_new_abstract_pkg_by_name(const char *pkg_name)
659 {
660 	abstract_pkg_t *ab_pkg;
661 
662 	ab_pkg = abstract_pkg_new();
663 
664 	ab_pkg->name = xstrdup(pkg_name);
665 	hash_table_insert(&conf->pkg_hash, pkg_name, ab_pkg);
666 
667 	return ab_pkg;
668 }
669 
ensure_abstract_pkg_by_name(const char * pkg_name)670 abstract_pkg_t *ensure_abstract_pkg_by_name(const char *pkg_name)
671 {
672 	abstract_pkg_t *ab_pkg;
673 
674 	if (!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name)))
675 		ab_pkg = add_new_abstract_pkg_by_name(pkg_name);
676 
677 	return ab_pkg;
678 }
679 
hash_insert_pkg(pkg_t * pkg,int set_status)680 void hash_insert_pkg(pkg_t * pkg, int set_status)
681 {
682 	abstract_pkg_t *ab_pkg;
683 
684 	ab_pkg = ensure_abstract_pkg_by_name(pkg->name);
685 	if (!ab_pkg->pkgs)
686 		ab_pkg->pkgs = pkg_vec_alloc();
687 
688 	if (pkg->state_status == SS_INSTALLED) {
689 		ab_pkg->state_status = SS_INSTALLED;
690 	} else if (pkg->state_status == SS_UNPACKED) {
691 		ab_pkg->state_status = SS_UNPACKED;
692 	}
693 
694 	buildDepends(pkg);
695 
696 	buildProvides(ab_pkg, pkg);
697 
698 	init_providelist(pkg, NULL);
699 
700 	/* Need to build the conflicts graph before replaces for correct
701 	 * calculation of replaced_by relation.
702 	 */
703 	buildConflicts(pkg);
704 
705 	buildReplaces(ab_pkg, pkg);
706 
707 	buildDependedUponBy(pkg, ab_pkg);
708 
709 	pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status);
710 	pkg->parent = ab_pkg;
711 }
712 
strip_offline_root(const char * file_name)713 static const char *strip_offline_root(const char *file_name)
714 {
715 	unsigned int len;
716 
717 	if (conf->offline_root) {
718 		len = strlen(conf->offline_root);
719 		if (strncmp(file_name, conf->offline_root, len) == 0)
720 			file_name += len;
721 	}
722 
723 	return file_name;
724 }
725 
file_hash_remove(const char * file_name)726 void file_hash_remove(const char *file_name)
727 {
728 	file_name = strip_offline_root(file_name);
729 	hash_table_remove(&conf->file_hash, file_name);
730 }
731 
file_hash_get_file_owner(const char * file_name)732 pkg_t *file_hash_get_file_owner(const char *file_name)
733 {
734 	file_name = strip_offline_root(file_name);
735 	return hash_table_get(&conf->file_hash, file_name);
736 }
737 
file_hash_set_file_owner(const char * file_name,pkg_t * owning_pkg)738 void file_hash_set_file_owner(const char *file_name, pkg_t * owning_pkg)
739 {
740 	pkg_t *old_owning_pkg;
741 	int file_name_len = strlen(file_name);
742 
743 	if (file_name[file_name_len - 1] == '/')
744 		return;
745 
746 	file_name = strip_offline_root(file_name);
747 
748 	old_owning_pkg = hash_table_get(&conf->file_hash, file_name);
749 	hash_table_insert(&conf->file_hash, file_name, owning_pkg);
750 
751 	if (old_owning_pkg) {
752 		pkg_get_installed_files(old_owning_pkg);
753 		str_list_remove_elt(old_owning_pkg->installed_files, file_name);
754 		pkg_free_installed_files(old_owning_pkg);
755 
756 		/* mark this package to have its filelist written */
757 		old_owning_pkg->state_flag |= SF_FILELIST_CHANGED;
758 		owning_pkg->state_flag |= SF_FILELIST_CHANGED;
759 	}
760 }
761