opkg: (leak fixing, day 2) lots and lots of memory leaks fixed
[project/opkg-lede.git] / libopkg / opkg.c
1 /* opkg.c - the opkg package management system
2
3 Thomas Wood <thomas@openedhand.com>
4
5 Copyright (C) 2008 OpenMoko Inc
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 <config.h>
19 #include <fnmatch.h>
20
21 #include "opkg.h"
22 #include "opkg_conf.h"
23 #include "args.h"
24
25 #include "opkg_install.h"
26 #include "opkg_configure.h"
27 #include "opkg_download.h"
28 #include "opkg_remove.h"
29 #include "opkg_upgrade.h"
30
31 #include "sprintf_alloc.h"
32 #include "file_util.h"
33
34 #include <libbb/libbb.h>
35
36 struct _opkg_t
37 {
38 args_t *args;
39 opkg_conf_t *conf;
40 opkg_option_t *options;
41 };
42
43 #define opkg_assert(expr) if (!(expr)) { \
44 printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
45 __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
46
47 #define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (opkg, &d, user_data);
48 #define SSTRCMP(x,y) (x && y) ? strcmp (x, y) : 0
49
50 /** Private Functions ***/
51
52 static opkg_package_t*
53 old_pkg_to_new (pkg_t *old)
54 {
55 opkg_package_t *new;
56
57 new = opkg_package_new ();
58
59 #define sstrdup(x) (x) ? strdup (x) : NULL;
60
61 new->name = sstrdup (old->name);
62 new->version = pkg_version_str_alloc (old);
63 new->architecture = sstrdup (old->architecture);
64 if (old->src)
65 new->repository = sstrdup (old->src->name);
66 new->description = sstrdup (old->description);
67 new->tags = sstrdup (old->tags);
68 new->url = sstrdup (old->url);
69
70 new->size = (old->size) ? atoi (old->size) : 0;
71 new->installed = (old->state_status == SS_INSTALLED);
72
73 return new;
74 }
75
76 static int
77 opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
78 {
79 pkg_vec_t *all;
80 int i;
81 pkg_t *pkg;
82 int r, err = 0;
83
84 all = pkg_vec_alloc ();
85 pkg_hash_fetch_available (&conf->pkg_hash, all);
86
87 for (i = 0; i < all->len; i++)
88 {
89 pkg = all->pkgs[i];
90
91 if (pkg_name && fnmatch (pkg_name, pkg->name, 0))
92 continue;
93
94 if (pkg->state_status == SS_UNPACKED)
95 {
96 r = opkg_configure (conf, pkg);
97 if (r == 0)
98 {
99 pkg->state_status = SS_INSTALLED;
100 pkg->parent->state_status = SS_INSTALLED;
101 pkg->state_flag &= ~SF_PREFER;
102 }
103 else
104 {
105 if (!err)
106 err = r;
107 }
108 }
109 }
110
111 pkg_vec_free (all);
112 return err;
113 }
114
115 struct _curl_cb_data
116 {
117 opkg_progress_callback_t cb;
118 opkg_progress_data_t *progress_data;
119 opkg_t *opkg;
120 void *user_data;
121 int start_range;
122 int finish_range;
123 };
124
125 int
126 curl_progress_cb (struct _curl_cb_data *cb_data,
127 double t, /* dltotal */
128 double d, /* dlnow */
129 double ultotal,
130 double ulnow)
131 {
132 int p = (t) ? d * 100 / t : 0;
133 static int prev = -1;
134 int progress = 0;
135
136 /* prevent the same value being sent twice (can occur due to rounding) */
137 if (p == prev)
138 return 0;
139 prev = p;
140
141 if (t < 1)
142 return 0;
143
144 progress = cb_data->start_range + (d / t * ((cb_data->finish_range - cb_data->start_range)));
145 cb_data->progress_data->percentage = progress;
146
147 (cb_data->cb)(cb_data->opkg,
148 cb_data->progress_data,
149 cb_data->user_data);
150
151 return 0;
152 }
153
154
155 /*** Public API ***/
156
157 opkg_package_t *
158 opkg_package_new ()
159 {
160
161 opkg_package_t *p;
162
163 p = malloc (sizeof (opkg_package_t));
164 memset (p, 0, sizeof (opkg_package_t));
165
166 return p;
167 }
168
169 void
170 opkg_package_free (opkg_package_t *p)
171 {
172 free (p->name);
173 free (p->version);
174 free (p->architecture);
175 free (p->description);
176 free (p->tags);
177 free (p->url);
178 free (p->repository);
179
180 free (p);
181 }
182
183 opkg_t *
184 opkg_new ()
185 {
186 opkg_t *opkg;
187 opkg = malloc (sizeof (opkg_t));
188
189 opkg->args = malloc (sizeof (args_t));
190 args_init (opkg->args);
191
192 opkg->conf = malloc (sizeof (opkg_conf_t));
193 opkg_conf_init (opkg->conf, opkg->args);
194
195 opkg_init_options_array (opkg->conf, &opkg->options);
196 return opkg;
197 }
198
199 void
200 opkg_free (opkg_t *opkg)
201 {
202 opkg_assert (opkg != NULL);
203
204 opkg_conf_deinit (opkg->conf);
205 args_deinit (opkg->args);
206 free (opkg->options);
207 free (opkg->args);
208 free (opkg->conf);
209 free (opkg);
210 }
211
212 int
213 opkg_re_read_config_files (opkg_t *opkg)
214 {
215 args_t *a;
216 opkg_conf_t *c;
217
218 opkg_assert (opkg != NULL);
219
220 a = opkg->args;
221 c = opkg->conf;
222
223 /* Unfortunatly, the easiest way to re-read the config files right now is to
224 * throw away opkg->conf and start again */
225
226 /* copy the settings we need to keep */
227 a->autoremove = c->autoremove;
228 a->force_depends = c->force_depends;
229 a->force_defaults = c->force_defaults;
230 a->force_overwrite = c->force_overwrite;
231 a->force_downgrade = c->force_downgrade;
232 a->force_reinstall = c->force_reinstall;
233 a->force_removal_of_dependent_packages = c->force_removal_of_dependent_packages;
234 a->force_removal_of_essential_packages = c->force_removal_of_essential_packages;
235 a->nodeps = c->nodeps;
236 a->noaction = c->noaction;
237 a->query_all = c->query_all;
238 a->multiple_providers = c->multiple_providers;
239 a->verbosity = c->verbosity;
240
241 if (c->offline_root)
242 {
243 if (a->offline_root) free (a->offline_root);
244 a->offline_root = strdup (c->offline_root);
245 }
246
247 if (c->offline_root_pre_script_cmd)
248 {
249 if (a->offline_root_pre_script_cmd) free (a->offline_root_pre_script_cmd);
250 a->offline_root_pre_script_cmd = strdup (c->offline_root_pre_script_cmd);
251 }
252
253 if (c->offline_root_post_script_cmd)
254 {
255 if (a->offline_root_post_script_cmd) free (a->offline_root_post_script_cmd);
256 a->offline_root_post_script_cmd = strdup (c->offline_root_post_script_cmd);
257 }
258
259 /* throw away old opkg_conf and start again */
260 opkg_conf_deinit (opkg->conf);
261 opkg_conf_init (opkg->conf, opkg->args);
262
263 free (opkg->options);
264 opkg_init_options_array (opkg->conf, &opkg->options);
265
266 return 0;
267 }
268
269 void
270 opkg_get_option (opkg_t *opkg, char *option, void **value)
271 {
272 int i = 0;
273 opkg_option_t *options;
274
275 opkg_assert (opkg != NULL);
276 opkg_assert (option != NULL);
277 opkg_assert (value != NULL);
278
279 options = opkg->options;
280
281 /* look up the option
282 * TODO: this would be much better as a hash table
283 */
284 while (options[i].name)
285 {
286 if (strcmp (options[i].name, option) != 0)
287 {
288 i++;
289 continue;
290 }
291 }
292
293 /* get the option */
294 switch (options[i].type)
295 {
296 case OPKG_OPT_TYPE_BOOL:
297 *((int *) value) = *((int *) options[i].value);
298 return;
299
300 case OPKG_OPT_TYPE_INT:
301 *((int *) value) = *((int *) options[i].value);
302 return;
303
304 case OPKG_OPT_TYPE_STRING:
305 *((char **)value) = strdup (options[i].value);
306 return;
307 }
308
309 }
310
311 void
312 opkg_set_option (opkg_t *opkg, char *option, void *value)
313 {
314 int i = 0, found = 0;
315 opkg_option_t *options;
316
317 opkg_assert (opkg != NULL);
318 opkg_assert (option != NULL);
319 opkg_assert (value != NULL);
320
321 options = opkg->options;
322
323 /* look up the option
324 * TODO: this would be much better as a hash table
325 */
326 while (options[i].name)
327 {
328 if (strcmp (options[i].name, option) == 0)
329 {
330 found = 1;
331 break;
332 }
333 i++;
334 }
335
336 if (!found)
337 {
338 /* XXX: Warning: Option not found */
339 return;
340 }
341
342 /* set the option */
343 switch (options[i].type)
344 {
345 case OPKG_OPT_TYPE_BOOL:
346 if (*((int *) value) == 0)
347 *((int *)options[i].value) = 0;
348 else
349 *((int *)options[i].value) = 1;
350 return;
351
352 case OPKG_OPT_TYPE_INT:
353 *((int *) options[i].value) = *((int *) value);
354 return;
355
356 case OPKG_OPT_TYPE_STRING:
357 *((char **)options[i].value) = strdup (value);
358 return;
359 }
360
361 }
362
363 int
364 opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
365 {
366 int err;
367 char *stripped_filename;
368 opkg_progress_data_t pdata;
369 pkg_t *old, *new;
370 pkg_vec_t *deps, *all;
371 int i, ndepends;
372 char **unresolved = NULL;
373
374 opkg_assert (opkg != NULL);
375 opkg_assert (package_name != NULL);
376
377 /* ... */
378 pkg_info_preinstall_check (opkg->conf);
379
380
381 /* check to ensure package is not already installed */
382 old = pkg_hash_fetch_installed_by_name(&opkg->conf->pkg_hash, package_name);
383 if (old)
384 {
385 /* XXX: Error: Package is already installed. */
386 return OPKG_PACKAGE_ALREADY_INSTALLED;
387 }
388
389 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, package_name);
390 if (!new)
391 {
392 /* XXX: Error: Could not find package to install */
393 return OPKG_PACKAGE_NOT_FOUND;
394 }
395
396 new->state_flag |= SF_USER;
397
398 pdata.action = OPKG_INSTALL;
399 pdata.package = old_pkg_to_new (new);
400
401 progress (pdata, 0);
402
403 /* find dependancies and download them */
404 deps = pkg_vec_alloc ();
405 /* this function does not return the original package, so we insert it later */
406 ndepends = pkg_hash_fetch_unsatisfied_dependencies (opkg->conf, new, deps, &unresolved);
407 if (unresolved)
408 {
409 /* XXX: Error: Could not satisfy dependencies */
410 pkg_vec_free (deps);
411 return OPKG_DEPENDANCIES_FAILED;
412 }
413
414 /* insert the package we are installing so that we download it */
415 pkg_vec_insert (deps, new);
416
417 /* download package and dependancies */
418 for (i = 0; i < deps->len; i++)
419 {
420 pkg_t *pkg;
421 struct _curl_cb_data cb_data;
422 char *url;
423
424 pkg = deps->pkgs[i];
425 if (pkg->local_filename)
426 continue;
427
428 opkg_package_free (pdata.package);
429 pdata.package = old_pkg_to_new (pkg);
430 pdata.action = OPKG_DOWNLOAD;
431
432 if (pkg->src == NULL)
433 {
434 /* XXX: Error: Package not available from any configured src */
435 return OPKG_PACKAGE_NOT_AVAILABLE;
436 }
437
438 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
439
440 /* Get the filename part, without any directory */
441 stripped_filename = strrchr(pkg->filename, '/');
442 if ( ! stripped_filename )
443 stripped_filename = pkg->filename;
444
445 sprintf_alloc(&pkg->local_filename, "%s/%s", opkg->conf->tmp_dir, stripped_filename);
446
447 cb_data.cb = progress_callback;
448 cb_data.progress_data = &pdata;
449 cb_data.opkg = opkg;
450 cb_data.user_data = user_data;
451 /* 75% of "install" progress is for downloading */
452 cb_data.start_range = 75 * i / deps->len;
453 cb_data.finish_range = 75 * (i + 1) / deps->len;
454
455 err = opkg_download(opkg->conf, url, pkg->local_filename,
456 (curl_progress_func) curl_progress_cb, &cb_data);
457 free(url);
458
459 }
460 pkg_vec_free (deps);
461
462 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
463 all = pkg_vec_alloc ();
464 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
465 for (i = 0; i < all->len; i++)
466 {
467 all->pkgs[i]->parent->dependencies_checked = 0;
468 }
469 pkg_vec_free (all);
470
471
472 /* 75% of "install" progress is for downloading */
473 opkg_package_free (pdata.package);
474 pdata.package = old_pkg_to_new (new);
475 pdata.action = OPKG_INSTALL;
476 progress (pdata, 75);
477
478 /* unpack the package */
479 err = opkg_install_pkg(opkg->conf, new, 0);
480
481 if (err)
482 return err;
483
484 progress (pdata, 75);
485
486 /* run configure scripts, etc. */
487 err = opkg_configure_packages (opkg->conf, NULL);
488 if (err)
489 return err;
490
491 /* write out status files and file lists */
492 opkg_conf_write_status_files (opkg->conf);
493 pkg_write_changed_filelists (opkg->conf);
494
495 progress (pdata, 100);
496 opkg_package_free (pdata.package);
497 return 0;
498 }
499
500 int
501 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
502 {
503 pkg_t *pkg = NULL;
504 pkg_t *pkg_to_remove;
505 opkg_progress_data_t pdata;
506
507 opkg_assert (opkg != NULL);
508 opkg_assert (package_name != NULL);
509
510
511
512 pkg_info_preinstall_check (opkg->conf);
513
514
515 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
516
517 if (pkg == NULL)
518 {
519 /* XXX: Error: Package not installed. */
520 return OPKG_PACKAGE_NOT_INSTALLED;
521 }
522
523 pdata.action = OPKG_REMOVE;
524 pdata.package = old_pkg_to_new (pkg);
525 progress (pdata, 0);
526
527
528 if (pkg->state_status == SS_NOT_INSTALLED)
529 {
530 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
531 return OPKG_PACKAGE_NOT_INSTALLED;
532 }
533 progress (pdata, 25);
534
535 if (opkg->conf->restrict_to_default_dest)
536 {
537 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
538 pkg->name,
539 opkg->conf->default_dest);
540 }
541 else
542 {
543 pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
544 }
545
546
547 progress (pdata, 75);
548
549 opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
550
551 /* write out status files and file lists */
552 opkg_conf_write_status_files (opkg->conf);
553 pkg_write_changed_filelists (opkg->conf);
554
555
556 progress (pdata, 100);
557 opkg_package_free (pdata.package);
558 return 0;
559 }
560
561 int
562 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
563 {
564 pkg_t *pkg;
565 opkg_progress_data_t pdata;
566
567
568
569 opkg_assert (opkg != NULL);
570 opkg_assert (package_name != NULL);
571
572 pkg_info_preinstall_check (opkg->conf);
573
574 if (opkg->conf->restrict_to_default_dest)
575 {
576 pkg = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
577 package_name,
578 opkg->conf->default_dest);
579 if (pkg == NULL)
580 {
581 /* XXX: Error: Package not installed in default_dest */
582 return OPKG_PACKAGE_NOT_INSTALLED;
583 }
584 }
585 else
586 {
587 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash,
588 package_name);
589 }
590
591 if (!pkg)
592 {
593 /* XXX: Error: Package not installed */
594 return OPKG_PACKAGE_NOT_INSTALLED;
595 }
596
597 pdata.action = OPKG_INSTALL;
598 pdata.package = old_pkg_to_new (pkg);
599 progress (pdata, 0);
600
601 opkg_upgrade_pkg (opkg->conf, pkg);
602 progress (pdata, 75);
603
604 opkg_configure_packages (opkg->conf, NULL);
605 progress (pdata, 100);
606 opkg_package_free (pdata.package);
607 return 0;
608 }
609
610 int
611 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
612 {
613 pkg_vec_t *installed;
614 int err = 0;
615 int i;
616 pkg_t *pkg;
617 opkg_progress_data_t pdata;
618
619 pdata.action = OPKG_INSTALL;
620 pdata.package = NULL;
621
622 opkg_assert (opkg != NULL);
623 progress (pdata, 0);
624
625 installed = pkg_vec_alloc ();
626 pkg_info_preinstall_check (opkg->conf);
627
628 pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed);
629 for (i = 0; i < installed->len; i++)
630 {
631 pkg = installed->pkgs[i];
632
633 pdata.package = old_pkg_to_new (pkg);
634 progress (pdata, 99 * i / installed->len);
635 opkg_package_free (pdata.package);
636
637 err += opkg_upgrade_pkg (opkg->conf, pkg);
638 }
639 pkg_vec_free (installed);
640
641 if (err)
642 return 1;
643
644 err = opkg_configure_packages (opkg->conf, NULL);
645 if (err)
646 return 1;
647
648 pdata.package = NULL;
649 progress (pdata, 100);
650 return 0;
651 }
652
653 int
654 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
655 {
656 char *tmp;
657 int err, result = 0;
658 char *lists_dir;
659 pkg_src_list_elt_t *iter;
660 pkg_src_t *src;
661 int sources_list_count, sources_done;
662 opkg_progress_data_t pdata;
663 char *tmp_file_name = NULL;
664
665 opkg_assert (opkg != NULL);
666
667 pdata.action = OPKG_DOWNLOAD;
668 pdata.package = NULL;
669 progress (pdata, 0);
670
671 sprintf_alloc (&lists_dir, "%s",
672 (opkg->conf->restrict_to_default_dest)
673 ? opkg->conf->default_dest->lists_dir
674 : opkg->conf->lists_dir);
675
676 if (!file_is_dir (lists_dir))
677 {
678 if (file_exists (lists_dir))
679 {
680 /* XXX: Error: file exists but is not a directory */
681 free (lists_dir);
682 return 1;
683 }
684
685 err = file_mkdir_hier (lists_dir, 0755);
686 if (err)
687 {
688 /* XXX: Error: failed to create directory */
689 free (lists_dir);
690 return 1;
691 }
692 }
693
694 tmp = strdup ("/tmp/opkg.XXXXXX");
695
696 if (mkdtemp (tmp) == NULL)
697 {
698 /* XXX: Error: could not create temporary file name */
699 free (lists_dir);
700 free (tmp);
701 return 1;
702 }
703
704 /* cout the number of sources so we can give some progress updates */
705 sources_list_count = 0;
706 sources_done = 0;
707 iter = opkg->conf->pkg_src_list.head;
708 while (iter)
709 {
710 sources_list_count++;
711 iter = iter->next;
712 }
713
714 for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
715 {
716 char *url, *list_file_name;
717
718 src = iter->data;
719
720 if (src->extra_data) /* debian style? */
721 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
722 src->gzip ? "Packages.gz" : "Packages");
723 else
724 sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
725
726 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
727 if (src->gzip)
728 {
729 FILE *in, *out;
730 struct _curl_cb_data cb_data;
731
732 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
733
734 /* XXX: Note: downloading url */
735
736 cb_data.cb = progress_callback;
737 cb_data.progress_data = &pdata;
738 cb_data.opkg = opkg;
739 cb_data.user_data = user_data;
740 cb_data.start_range = 100 * sources_done / sources_list_count;
741 cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
742
743 err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
744
745 if (err == 0)
746 {
747 /* XXX: Note: Inflating downloaded file */
748 in = fopen (tmp_file_name, "r");
749 out = fopen (list_file_name, "w");
750 if (in && out)
751 unzip (in, out);
752 else
753 err = 1;
754 if (in)
755 fclose (in);
756 if (out)
757 fclose (out);
758 unlink (tmp_file_name);
759 }
760 }
761 else
762 err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
763 free (tmp_file_name);
764
765 if (err)
766 {
767 /* XXX: Error: download error */
768 result = OPKG_DOWNLOAD_FAILED;
769 }
770 free (url);
771
772 #ifdef HAVE_GPGME
773 /* download detached signitures to verify the package lists */
774 /* get the url for the sig file */
775 if (src->extra_data) /* debian style? */
776 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
777 "Packages.sig");
778 else
779 sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
780
781 /* create temporary file for it */
782 sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
783
784 err = opkg_download (opkg->conf, url, tmp_file_name, NULL, NULL);
785 if (err)
786 {
787 /* XXX: Warning: Download failed */
788 }
789 else
790 {
791 int err;
792 err = opkg_verify_file (opkg->conf, list_file_name, tmp_file_name);
793 if (err == 0)
794 {
795 /* XXX: Notice: Signature check passed */
796 }
797 else
798 {
799 /* XXX: Warning: Signature check failed */
800 }
801 }
802 unlink (tmp_file_name);
803 free (tmp_file_name);
804 free (url);
805 #else
806 /* XXX: Note: Signiture check for %s skipped because GPG support was not
807 * enabled in this build
808 */
809 #endif
810 free (list_file_name);
811
812 sources_done++;
813 progress (pdata, 100 * sources_done / sources_list_count);
814 }
815
816 rmdir (tmp);
817 free (tmp);
818 free (lists_dir);
819
820 /* Now re-read the package lists to update package hash tables. */
821 opkg_re_read_config_files (opkg);
822
823 return result;
824 }
825
826
827 int
828 opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
829 {
830 pkg_vec_t *all;
831 int i;
832
833 opkg_assert (opkg);
834 opkg_assert (callback);
835
836 all = pkg_vec_alloc ();
837 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
838 for (i = 0; i < all->len; i++)
839 {
840 pkg_t *pkg;
841 opkg_package_t *package;
842
843 pkg = all->pkgs[i];
844
845 package = old_pkg_to_new (pkg);
846 callback (opkg, package, user_data);
847 }
848
849 pkg_vec_free (all);
850
851 return 0;
852 }
853
854 int
855 opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
856 {
857 pkg_vec_t *all;
858 int i;
859
860 opkg_assert (opkg);
861 opkg_assert (callback);
862
863 all = pkg_vec_alloc ();
864 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
865 for (i = 0; i < all->len; i++)
866 {
867 pkg_t *old, *new;
868 int cmp;
869 opkg_package_t *package;
870
871 old = all->pkgs[i];
872
873 if (old->state_status != SS_INSTALLED)
874 continue;
875
876 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, old->name);
877 if (new == NULL) {
878 /* XXX: Notice: Assuming locally install package is up to date */
879 continue;
880 }
881
882 cmp = pkg_compare_versions(old, new);
883
884 if (cmp < 0)
885 {
886 package = old_pkg_to_new (new);
887 callback (opkg, package, user_data);
888 }
889 }
890
891 pkg_vec_free (all);
892
893 return 0;
894 }
895
896 opkg_package_t*
897 opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *arch, const char *repo)
898 {
899 pkg_vec_t *all;
900 opkg_package_t *package = NULL;
901 int i;
902 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
903
904 opkg_assert (opkg);
905
906 all = pkg_vec_alloc ();
907 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
908 for (i = 0; i < all->len; i++)
909 {
910 pkg_t *pkg;
911 char *pkgv;
912
913 pkg = all->pkgs[i];
914
915 /* check name */
916 if (sstrcmp (pkg->name, name))
917 continue;
918
919 /* check version */
920 pkgv = pkg_version_str_alloc (pkg);
921 if (sstrcmp (pkgv, ver))
922 {
923 free (pkgv);
924 continue;
925 }
926 free (pkgv);
927
928 /* check architecture */
929 if (arch)
930 {
931 if (sstrcmp (pkg->architecture, arch))
932 continue;
933 }
934
935 /* check repository */
936 if (repo)
937 {
938 if (sstrcmp (pkg->src->name, repo))
939 continue;
940 }
941
942 /* match found */
943 package = old_pkg_to_new (pkg);
944 break;
945 }
946
947 pkg_vec_free (all);
948
949 return package;
950 }