summaryrefslogtreecommitdiffstats
path: root/libopkg/pkg_hash.c
blob: e02e1f25ce4db9d2281341b54fd47902a61242b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/* opkg_hash.c - the opkg package management system

   Steven M. Ayer
   
   Copyright (C) 2002 Compaq Computer Corporation

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
*/

#include "includes.h"
#include <errno.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#include "hash_table.h"
#include "pkg.h"
#include "opkg_message.h"
#include "pkg_vec.h"
#include "pkg_hash.h"
#include "pkg_parse.h"
#include "opkg_utils.h"
#include "libbb/libbb.h"

void
pkg_hash_init(const char *name, hash_table_t *hash, int len)
{
	hash_table_init(name, hash, len);
}

static void
free_pkgs(const char *key, void *entry, void *data)
{
	int i;
	abstract_pkg_t *ab_pkg;

	/* Each entry in the hash table is an abstract package, which contains
	 * a list of packages that provide the abstract package.
	 */
  
	ab_pkg = (abstract_pkg_t*) entry;

	if (ab_pkg->pkgs) {
		for (i = 0; i < ab_pkg->pkgs->len; i++) {
			pkg_deinit (ab_pkg->pkgs->pkgs[i]);
			free (ab_pkg->pkgs->pkgs[i]);
		}
	}

	abstract_pkg_vec_free (ab_pkg->provided_by);
	abstract_pkg_vec_free (ab_pkg->replaced_by);
	pkg_vec_free (ab_pkg->pkgs);
	free (ab_pkg->depended_upon_by);
	free (ab_pkg->name);
	free (ab_pkg);
}

void
pkg_hash_deinit(hash_table_t *hash)
{
	hash_table_foreach(hash, free_pkgs, NULL);
	hash_table_deinit(hash);
}


/* Find the default arch for a given package status file if none is given. */
static char *
pkg_get_default_arch(opkg_conf_t *conf)
{
     nv_pair_list_elt_t *l;
     char *def_arch = HOST_CPU_STR;		/* Default arch */
     int def_prio = 0;				/* Other archs override this */

     list_for_each_entry(l , &conf->arch_list.head, node) {
	  nv_pair_t *nv = (nv_pair_t *)l->data;
	  int priority = strtol(nv->value, NULL, 0);

	  /* Check if this arch has higher priority, and is valid */
	  if ((priority > def_prio) &&
	      (strcmp(nv->name, "all")) && (strcmp(nv->name, "noarch"))) {
	       /* Our new default */
	       def_prio = priority;
	       def_arch = nv->name;
	  }
     }

     return xstrdup(def_arch);
}

int
pkg_hash_add_from_file(opkg_conf_t *conf, const char *file_name,
			pkg_src_t *src, pkg_dest_t *dest, int is_status_file)
{
	hash_table_t *hash = &conf->pkg_hash;
	pkg_t *pkg;
	FILE *fp;
	char *buf;
	const size_t len = 4096;
	int ret = 0;

	fp = fopen(file_name, "r");
	if (fp == NULL) {
		fprintf(stderr, "%s: fopen(%s): %s\n",
			     __FUNCTION__, file_name, strerror(errno));
		return -1;
	}

	buf = xmalloc(len);

	do {
		pkg = pkg_new();
		pkg->src = src;
		pkg->dest = dest;

		ret = pkg_parse_from_stream_nomalloc(pkg, fp, PFM_ALL,
				&buf, len);
		if (ret) {
			pkg_deinit (pkg);
			free(pkg);
			if (ret == -1)
				break;
			if (ret == EINVAL)
				/* Probably a blank line, continue parsing. */
				ret = 0;
			continue;
		}

		if (!pkg->architecture) {
			char *version_str = pkg_version_str_alloc(pkg);
			pkg->architecture = pkg_get_default_arch(conf);
			opkg_message(conf, OPKG_ERROR, "Package %s version %s has no architecture specified, defaulting to %s.\n",
			pkg->name, version_str, pkg->architecture);
			free(version_str);
		}

		hash_insert_pkg(hash, pkg, is_status_file, conf);

	} while (!feof(fp));

	free(buf);
	fclose(fp);

	return ret;
}

static abstract_pkg_t *
abstract_pkg_fetch_by_name(hash_table_t * hash, const char * pkg_name)
{
	return (abstract_pkg_t *)hash_table_get(hash, pkg_name);
}

pkg_t *
pkg_hash_fetch_best_installation_candidate(opkg_conf_t *conf,
		abstract_pkg_t *apkg,
		int (*constraint_fcn)(pkg_t *pkg, void *cdata),
		void *cdata, int quiet, int *err)
{
     int i;
     int nprovides = 0;
     int nmatching = 0;
     pkg_vec_t *matching_pkgs;
     abstract_pkg_vec_t *matching_apkgs;
     abstract_pkg_vec_t *provided_apkg_vec;
     abstract_pkg_t **provided_apkgs;
     abstract_pkg_vec_t *providers;
     pkg_t *latest_installed_parent = NULL;
     pkg_t *latest_matching = NULL;
     pkg_t *priorized_matching = NULL;
     pkg_t *held_pkg = NULL;
     pkg_t *good_pkg_by_name = NULL;

     if (err)
       *err = 0;

     if (apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0))
	  return NULL;

     matching_pkgs = pkg_vec_alloc();
     matching_apkgs = abstract_pkg_vec_alloc();
     providers = abstract_pkg_vec_alloc();

     opkg_message(conf, OPKG_DEBUG, "best installation candidate for %s\n", apkg->name);

     provided_apkg_vec = apkg->provided_by;
     nprovides = provided_apkg_vec->len;
     provided_apkgs = provided_apkg_vec->pkgs;
     if (nprovides > 1)
	  opkg_message(conf, OPKG_DEBUG, " apkg=%s nprovides=%d\n", apkg->name, nprovides);

     /* accumulate all the providers */
     for (i = 0; i < nprovides; i++) {
	  abstract_pkg_t *provider_apkg = provided_apkgs[i];
	  opkg_message(conf, OPKG_DEBUG, " adding %s to providers\n", provider_apkg->name);
	  abstract_pkg_vec_insert(providers, provider_apkg);
     }
     nprovides = providers->len;

     for (i = 0; i < nprovides; i++) {
	  abstract_pkg_t *provider_apkg = abstract_pkg_vec_get(providers, i);
	  abstract_pkg_t *replacement_apkg = NULL;
	  pkg_vec_t *vec;

	  if (provider_apkg->replaced_by && provider_apkg->replaced_by->len) {
	       replacement_apkg = provider_apkg->replaced_by->pkgs[0];
	       if (provider_apkg->replaced_by->len > 1) {
		    opkg_message(conf, OPKG_NOTICE, "Multiple replacers for %s, using first one (%s)\n", 
				 provider_apkg->name, replacement_apkg->name);
	       }
	  }

	  if (replacement_apkg)
	       opkg_message(conf, OPKG_DEBUG, "   replacement_apkg=%s for provider_apkg=%s\n", 
			    replacement_apkg->name, provider_apkg->name);

	  if (replacement_apkg && (replacement_apkg != provider_apkg)) {
	       if (abstract_pkg_vec_contains(providers, replacement_apkg))
		    continue;
	       else
		    provider_apkg = replacement_apkg;
	  }

	  if (!(vec = provider_apkg->pkgs)) {
	       opkg_message(conf, OPKG_DEBUG, "   no pkgs for provider_apkg %s\n", provider_apkg->name);
	       continue;
	  }
    

	  /* now check for supported architecture */
	  {
	       int max_count = 0;
	       int i;

	       /* count packages matching max arch priority and keep track of last one */
	       for (i = 0; i < vec->len; i++) {
		    pkg_t *maybe = vec->pkgs[i];
		    opkg_message(conf, OPKG_DEBUG, "  %s arch=%s arch_priority=%d version=%s  \n",
				 maybe->name, maybe->architecture, maybe->arch_priority, maybe->version);
                    /* We make sure not to add the same package twice. Need to search for the reason why 
                       they show up twice sometimes. */
		    if ((maybe->arch_priority > 0) && (! pkg_vec_contains(matching_pkgs, maybe))) {
			 max_count++;
			 abstract_pkg_vec_insert(matching_apkgs, maybe->parent);
			 pkg_vec_insert(matching_pkgs, maybe);
		    }
	       }

		if (vec->len > 0 && matching_pkgs->len < 1)
		{
		  opkg_message (conf, OPKG_ERROR, "Packages were found, but none compatible with the architectures configured\n");
		  if (err)
		    *err = OPKG_PKG_HAS_NO_AVAILABLE_ARCH;
		}
	  }
     }

     if (matching_pkgs->len > 1)
	  pkg_vec_sort(matching_pkgs, pkg_name_version_and_architecture_compare);
     if (matching_apkgs->len > 1)
	  abstract_pkg_vec_sort(matching_pkgs, abstract_pkg_name_compare);

/* Here it is usefull, if ( matching_apkgs->len > 1 ), to test if one of this matching packages has the same name of the
   needed package. In this case, I would return it for install, otherwise I will continue with the procedure */
/* The problem is what to do when there are more than a mathing package, with the same name and several version ?
   Until now I always got the latest, but that breaks the downgrade option.
   If I stop at the first one, I would probably miss the new ones 
   Maybe the way is to have some kind of flag somewhere, to see if the package been asked to install is from a file,
   or from a Packages feed.
   It it is from a file it always need to be checked whatever version I have in feeds or everywhere, according to force-down or whatever options*/
/*Pigi*/

     for (i = 0; i < matching_pkgs->len; i++) {
	  pkg_t *matching = matching_pkgs->pkgs[i];
          if (constraint_fcn(matching, cdata)) {  /* We found it */
             opkg_message(conf, OPKG_DEBUG, " Found a valid candidate for the install: %s %s  \n", matching->name, matching->version) ;
             good_pkg_by_name = matching;
             if ( matching->provided_by_hand == 1 )    /* It has been provided by hand, so it is what user want */
                break;                                 
          }
     }


     for (i = 0; i < matching_pkgs->len; i++) {
	  pkg_t *matching = matching_pkgs->pkgs[i];
	  latest_matching = matching;
	  if (matching->parent->state_status == SS_INSTALLED || matching->parent->state_status == SS_UNPACKED)
	       latest_installed_parent = matching;
	  if (matching->state_flag & (SF_HOLD|SF_PREFER)) {
	       if (held_pkg)
		    opkg_message(conf, OPKG_NOTICE, "Multiple packages (%s and %s) providing same name marked HOLD or PREFER.  Using latest.\n",
				 held_pkg->name, matching->name);
	       held_pkg = matching;
	  }
     }

     if (!good_pkg_by_name && !held_pkg && !latest_installed_parent && matching_apkgs->len > 1 && !quiet) {
          int prio = 0;
          for (i = 0; i < matching_pkgs->len; i++) {
              pkg_t *matching = matching_pkgs->pkgs[i];
                  if (matching->arch_priority > prio) {
                      priorized_matching = matching;
                      prio = matching->arch_priority;
                      opkg_message(conf, OPKG_DEBUG, "Match with priority %i    %s\n", prio, matching->name);
                  }
              }
          
          }

     if (conf->verbosity >= OPKG_INFO && matching_apkgs->len > 1) {
	  opkg_message(conf, OPKG_INFO, "%s: for apkg=%s, %d matching pkgs\n",
		       __FUNCTION__, apkg->name, matching_pkgs->len);
	  for (i = 0; i < matching_pkgs->len; i++) {
	       pkg_t *matching = matching_pkgs->pkgs[i];
	       opkg_message(conf, OPKG_INFO, "    %s %s %s\n",
			    matching->name, matching->version, matching->architecture);
	  }
     }

     nmatching = matching_apkgs->len;

     pkg_vec_free(matching_pkgs);
     abstract_pkg_vec_free(matching_apkgs);
     abstract_pkg_vec_free(providers);

     if (good_pkg_by_name) {   /* We found a good candidate, we will install it */ 
	  return good_pkg_by_name;
     }
     if (held_pkg) {
	  opkg_message(conf, OPKG_INFO, "  using held package %s\n", held_pkg->name);
	  return held_pkg;
     }
     if (latest_installed_parent) {
	  opkg_message(conf, OPKG_INFO, "  using latest version of installed package %s\n", latest_installed_parent->name);
	  return latest_installed_parent;
     }
     if (priorized_matching) {
	  opkg_message(conf, OPKG_INFO, "  using priorized matching %s %s %s\n",
		       priorized_matching->name, priorized_matching->version, priorized_matching->architecture);
	  return priorized_matching;
     }
     if (nmatching > 1) {
	  opkg_message(conf, OPKG_INFO, "  no matching pkg out of matching_apkgs=%d\n", nmatching);
	  return NULL;
     }
     if (latest_matching) {
	  opkg_message(conf, OPKG_INFO, "  using latest matching %s %s %s\n",
		       latest_matching->name, latest_matching->version, latest_matching->architecture);
	  return latest_matching;
     }
     return NULL;
}

static int
pkg_name_constraint_fcn(pkg_t *pkg, void *cdata)
{
	const char *name = (const char *)cdata;

	if (strcmp(pkg->name, name) == 0)
		return 1;
	else
		return 0;   
}

static pkg_vec_t *
pkg_vec_fetch_by_name(hash_table_t *hash, const char *pkg_name)
{
	abstract_pkg_t * ab_pkg;

	if(!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name)))
		return NULL;

	if (ab_pkg->pkgs)
		return ab_pkg->pkgs;

	if (ab_pkg->provided_by) {
		abstract_pkg_t *abpkg =  abstract_pkg_vec_get(ab_pkg->provided_by, 0);
		if (abpkg != NULL)
			return abpkg->pkgs;
		else
			return ab_pkg->pkgs;
	}

	return NULL;
}


pkg_t *
pkg_hash_fetch_best_installation_candidate_by_name(opkg_conf_t *conf,
		const char *name, int *err)
{
	hash_table_t *hash = &conf->pkg_hash;
	abstract_pkg_t *apkg = NULL;

	if (!(apkg = abstract_pkg_fetch_by_name(hash, name)))
		return NULL;

	return pkg_hash_fetch_best_installation_candidate(conf, apkg,
				pkg_name_constraint_fcn, apkg->name, 0, err);
}


pkg_t *
pkg_hash_fetch_by_name_version(hash_table_t *hash, 
			const char *pkg_name, const char * version)
{
	pkg_vec_t * vec;
	int i;
	char *version_str = NULL;
    
	if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name)))
		return NULL;
    
	for(i = 0; i < vec->len; i++) {
		version_str = pkg_version_str_alloc(vec->pkgs[i]);
		if(!strcmp(version_str, version)) {
			free(version_str);
			break;
		}
		free(version_str);
	}

	if(i == vec->len)
		return NULL;
    
	return vec->pkgs[i];
}

pkg_t *
pkg_hash_fetch_installed_by_name_dest(hash_table_t *hash,
			const char *pkg_name, pkg_dest_t *dest)
{
	pkg_vec_t * vec;
	int i;

	if (!(vec = pkg_vec_fetch_by_name(hash, pkg_name))) {
		return NULL;
	}

	for (i = 0; i < vec->len; i++)
		if((vec->pkgs[i]->state_status == SS_INSTALLED
				|| vec->pkgs[i]->state_status == SS_UNPACKED)
				&& vec->pkgs[i]->dest == dest) {
			return vec->pkgs[i];
	}

	return NULL;
}

pkg_t *
pkg_hash_fetch_installed_by_name(hash_table_t *hash,
					const char *pkg_name)
{
	pkg_vec_t * vec;
	int i;

	if (!(vec = pkg_vec_fetch_by_name(hash, pkg_name))) {
		return NULL;
	}

	for (i = 0; i < vec->len; i++) {
		if (vec->pkgs[i]->state_status == SS_INSTALLED
				|| vec->pkgs[i]->state_status == SS_UNPACKED) {
			return vec->pkgs[i];
        	}
	}

	return NULL;
}


static void
pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
{
	int j;
	abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
	pkg_vec_t *all = (pkg_vec_t *)data;
	pkg_vec_t *pkg_vec = ab_pkg->pkgs;

	if (!pkg_vec)
		return;

	for (j = 0; j < pkg_vec->len; j++) {
		pkg_t *pkg = pkg_vec->pkgs[j];
		pkg_vec_insert(all, pkg);
	}
}

void
pkg_hash_fetch_available(hash_table_t *hash, pkg_vec_t *all)
{
	hash_table_foreach(hash, pkg_hash_fetch_available_helper, all);
}

static void
pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry, void *data)
{
	abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
	pkg_vec_t *all = (pkg_vec_t *)data;
	pkg_vec_t *pkg_vec = ab_pkg->pkgs;
	int j;

	if (!pkg_vec)
		return;

	for (j = 0; j < pkg_vec->len; j++) {
		pkg_t *pkg = pkg_vec->pkgs[j];
		if (pkg->state_status == SS_INSTALLED
				|| pkg->state_status == SS_UNPACKED)
			pkg_vec_insert(all, pkg);
	}
}

void
pkg_hash_fetch_all_installed(hash_table_t *hash, pkg_vec_t *all)
{
	hash_table_foreach(hash, pkg_hash_fetch_all_installed_helper, all);
}

/*
 * This assumes that the abstract pkg doesn't exist.
 */
static abstract_pkg_t *
add_new_abstract_pkg_by_name(hash_table_t *hash, const char *pkg_name)
{
	abstract_pkg_t *ab_pkg;

	ab_pkg = abstract_pkg_new();

	ab_pkg->name = xstrdup(pkg_name);
	hash_table_insert(hash, pkg_name, ab_pkg);

	return ab_pkg;
}


abstract_pkg_t *
ensure_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name)
{
	abstract_pkg_t * ab_pkg;

	if (!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name)))
		ab_pkg = add_new_abstract_pkg_by_name(hash, pkg_name);

	return ab_pkg;
}

pkg_t *
hash_insert_pkg(hash_table_t *hash, pkg_t *pkg, int set_status,
		opkg_conf_t *conf)
{
	abstract_pkg_t * ab_pkg;

	if(!pkg)
		return pkg;

	buildDepends(hash, pkg);

	ab_pkg = ensure_abstract_pkg_by_name(hash, pkg->name);

	if (set_status) {
		if (pkg->state_status == SS_INSTALLED) {
			ab_pkg->state_status = SS_INSTALLED;
		} else if (pkg->state_status == SS_UNPACKED) {
			ab_pkg->state_status = SS_UNPACKED;
		}
	}

	if(!ab_pkg->pkgs)
		ab_pkg->pkgs = pkg_vec_alloc();

	buildProvides(hash, ab_pkg, pkg);

	/* Need to build the conflicts graph before replaces for correct
	 * calculation of replaced_by relation.
	 */
	buildConflicts(hash, ab_pkg, pkg);

	buildReplaces(hash, ab_pkg, pkg);

	buildDependedUponBy(pkg, ab_pkg);

	/* pkg_vec_insert_merge might munge package, but it returns an
	 * unmunged pkg.
	 */
	pkg = pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status,conf );
	pkg->parent = ab_pkg;

	return pkg;
}


pkg_t *
file_hash_get_file_owner(opkg_conf_t *conf, const char *file_name)
{
	return hash_table_get(&conf->file_hash, file_name); 
}

void
file_hash_set_file_owner(opkg_conf_t *conf, const char *file_name,
		pkg_t *owning_pkg)
{
	pkg_t *old_owning_pkg = hash_table_get(&conf->file_hash, file_name);
	int file_name_len = strlen(file_name);

	if (file_name[file_name_len -1] == '/')
		return;

	if (conf->offline_root) {
		int len = strlen(conf->offline_root);
		if (strncmp(file_name, conf->offline_root, len) == 0) {
			file_name += len;
		}
	}

	hash_table_insert(&conf->file_hash, file_name, owning_pkg); 

	if (old_owning_pkg) {
		pkg_get_installed_files(conf, old_owning_pkg);
		str_list_remove_elt(old_owning_pkg->installed_files, file_name);
		pkg_free_installed_files(old_owning_pkg);

		/* mark this package to have its filelist written */
		old_owning_pkg->state_flag |= SF_FILELIST_CHANGED;
		owning_pkg->state_flag |= SF_FILELIST_CHANGED;
	}
}