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