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