opkg: consolidate error enums and add an error for when no package matches
[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 #include "opkg_error.h"
31
32 #include "sprintf_alloc.h"
33 #include "file_util.h"
34
35 #include <libbb/libbb.h>
36
37 struct _opkg_t
38 {
39   args_t *args;
40   opkg_conf_t *conf;
41   opkg_option_t *options;
42 };
43
44 #define opkg_assert(expr) if (!(expr)) { \
45     printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
46             __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
47
48 #define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (opkg, &d, user_data);
49 #define SSTRCMP(x,y) (x && y) ? strcmp (x, y) : 0
50
51 /** Private Functions ***/
52
53 static opkg_package_t*
54 old_pkg_to_new (pkg_t *old)
55 {
56   opkg_package_t *new;
57
58   new = opkg_package_new ();
59
60 #define sstrdup(x) (x) ? strdup (x) : NULL;
61
62   new->name = sstrdup (old->name);
63   new->version = pkg_version_str_alloc (old);
64   new->architecture = sstrdup (old->architecture);
65   if (old->src)
66     new->repository = sstrdup (old->src->name);
67   new->description = sstrdup (old->description);
68   new->tags = sstrdup (old->tags);
69   new->url = sstrdup (old->url);
70
71   new->size = (old->size) ? atoi (old->size) : 0;
72   new->installed = (old->state_status == SS_INSTALLED);
73
74   return new;
75 }
76
77 static int
78 opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
79 {
80   pkg_vec_t *all;
81   int i;
82   pkg_t *pkg;
83   int r, err = 0;
84
85   all = pkg_vec_alloc ();
86   pkg_hash_fetch_available (&conf->pkg_hash, all);
87
88   for (i = 0; i < all->len; i++)
89   {
90     pkg = all->pkgs[i];
91
92     if (pkg_name && fnmatch (pkg_name, pkg->name, 0))
93       continue;
94
95     if (pkg->state_status == SS_UNPACKED)
96     {
97       r = opkg_configure (conf, pkg);
98       if (r == 0)
99       {
100         pkg->state_status = SS_INSTALLED;
101         pkg->parent->state_status = SS_INSTALLED;
102         pkg->state_flag &= ~SF_PREFER;
103       }
104       else
105       {
106         if (!err)
107           err = r;
108       }
109     }
110   }
111
112   pkg_vec_free (all);
113   return err;
114 }
115
116 struct _curl_cb_data
117 {
118   opkg_progress_callback_t cb;
119   opkg_progress_data_t *progress_data;
120   opkg_t *opkg;
121   void *user_data;
122   int start_range;
123   int finish_range;
124 };
125
126 int
127 curl_progress_cb (struct _curl_cb_data *cb_data,
128                   double t,   /* dltotal */
129                   double d,   /* dlnow */
130                   double ultotal,
131                   double ulnow)
132 {
133   int p = (t) ? d * 100 / t : 0;
134   static int prev = -1;
135   int progress = 0;
136
137   /* prevent the same value being sent twice (can occur due to rounding) */
138   if (p == prev)
139     return 0;
140   prev = p;
141
142   if (t < 1)
143     return 0;
144
145   progress = cb_data->start_range + (d / t * ((cb_data->finish_range - cb_data->start_range)));
146   cb_data->progress_data->percentage = progress;
147
148   (cb_data->cb)(cb_data->opkg,
149                 cb_data->progress_data,
150                 cb_data->user_data);
151
152   return 0;
153 }
154
155
156 /*** Public API ***/
157
158 opkg_package_t *
159 opkg_package_new ()
160 {
161
162   opkg_package_t *p;
163
164   p = malloc (sizeof (opkg_package_t));
165   memset (p, 0, sizeof (opkg_package_t));
166
167   return p;
168 }
169
170 void
171 opkg_package_free (opkg_package_t *p)
172 {
173   free (p->name);
174   free (p->version);
175   free (p->architecture);
176   free (p->description);
177   free (p->tags);
178   free (p->url);
179   free (p->repository);
180
181   free (p);
182 }
183
184 opkg_t *
185 opkg_new ()
186 {
187   opkg_t *opkg;
188   opkg = malloc (sizeof (opkg_t));
189
190   opkg->args = malloc (sizeof (args_t));
191   args_init (opkg->args);
192
193   opkg->conf = malloc (sizeof (opkg_conf_t));
194   opkg_conf_init (opkg->conf, opkg->args);
195
196   opkg_init_options_array (opkg->conf, &opkg->options);
197   return opkg;
198 }
199
200 void
201 opkg_free (opkg_t *opkg)
202 {
203   opkg_assert (opkg != NULL);
204
205   opkg_conf_deinit (opkg->conf);
206   args_deinit (opkg->args);
207   free (opkg->options);
208   free (opkg->args);
209   free (opkg->conf);
210   free (opkg);
211 }
212
213 int
214 opkg_re_read_config_files (opkg_t *opkg)
215 {
216   args_t *a;
217   opkg_conf_t *c;
218
219   opkg_assert (opkg != NULL);
220
221   a = opkg->args;
222   c = opkg->conf;
223
224   /* Unfortunatly, the easiest way to re-read the config files right now is to
225    * throw away opkg->conf and start again */
226
227   /* copy the settings we need to keep */
228   a->autoremove = c->autoremove;
229   a->force_depends = c->force_depends;
230   a->force_defaults = c->force_defaults;
231   a->force_overwrite = c->force_overwrite;
232   a->force_downgrade = c->force_downgrade;
233   a->force_reinstall = c->force_reinstall;
234   a->force_removal_of_dependent_packages = c->force_removal_of_dependent_packages;
235   a->force_removal_of_essential_packages = c->force_removal_of_essential_packages;
236   a->nodeps = c->nodeps;
237   a->noaction = c->noaction;
238   a->query_all = c->query_all;
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, NULL);
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_DEPENDENCIES_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     if (err)
460     {
461       pkg_vec_free (deps);
462       opkg_package_free (pdata.package);
463       return OPKG_DOWNLOAD_FAILED;
464     }
465
466   }
467   pkg_vec_free (deps);
468
469   /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
470   all = pkg_vec_alloc ();
471   pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
472   for (i = 0; i < all->len; i++)
473   {
474     all->pkgs[i]->parent->dependencies_checked = 0;
475   }
476   pkg_vec_free (all);
477
478
479   /* 75% of "install" progress is for downloading */
480   opkg_package_free (pdata.package);
481   pdata.package = old_pkg_to_new (new);
482   pdata.action = OPKG_INSTALL;
483   progress (pdata, 75);
484
485   /* unpack the package */
486   err = opkg_install_pkg(opkg->conf, new, 0);
487
488   if (err)
489   {
490     opkg_package_free (pdata.package);
491     switch (err)
492     {
493       case OPKG_INSTALL_ERR_NOT_TRUSTED: return OPKG_GPG_ERROR;
494       case OPKG_INSTALL_ERR_DOWNLOAD: return OPKG_DOWNLOAD_FAILED;
495       case OPKG_INSTALL_ERR_DEPENDENCIES:
496       case OPKG_INSTALL_ERR_CONFLICTS: return OPKG_DEPENDENCIES_FAILED;
497       case OPKG_INSTALL_ERR_ALREADY_INSTALLED: return OPKG_PACKAGE_ALREADY_INSTALLED;
498       case OPKG_INSTALL_ERR_SIGNATURE: return OPKG_GPG_ERROR;
499       case OPKG_INSTALL_ERR_MD5: return OPKG_MD5_ERROR;
500       default: return OPKG_UNKNOWN_ERROR;
501     }
502   }
503
504   progress (pdata, 75);
505
506   /* run configure scripts, etc. */
507   err = opkg_configure_packages (opkg->conf, NULL);
508   if (err)
509   {
510     opkg_package_free (pdata.package);
511     return OPKG_UNKNOWN_ERROR;
512   }
513
514   /* write out status files and file lists */
515   opkg_conf_write_status_files (opkg->conf);
516   pkg_write_changed_filelists (opkg->conf);
517
518   progress (pdata, 100);
519   opkg_package_free (pdata.package);
520   return 0;
521 }
522
523 int
524 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
525 {
526   int err;
527   pkg_t *pkg = NULL;
528   pkg_t *pkg_to_remove;
529   opkg_progress_data_t pdata;
530
531   opkg_assert (opkg != NULL);
532   opkg_assert (package_name != NULL);
533
534   pkg_info_preinstall_check (opkg->conf);
535
536   pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
537
538   if (pkg == NULL)
539   {
540     /* XXX: Error: Package not installed. */
541     return OPKG_PACKAGE_NOT_INSTALLED;
542   }
543
544   pdata.action = OPKG_REMOVE;
545   pdata.package = old_pkg_to_new (pkg);
546   progress (pdata, 0);
547
548
549   if (pkg->state_status == SS_NOT_INSTALLED)
550   {
551     /* XXX:  Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
552     return OPKG_PACKAGE_NOT_INSTALLED;
553   }
554   progress (pdata, 25);
555
556   if (opkg->conf->restrict_to_default_dest)
557   {
558     pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
559                                                            pkg->name,
560                                                            opkg->conf->default_dest);
561   }
562   else
563   {
564     pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
565   }
566
567
568   progress (pdata, 75);
569
570   err = opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
571
572   /* write out status files and file lists */
573   opkg_conf_write_status_files (opkg->conf);
574   pkg_write_changed_filelists (opkg->conf);
575
576
577   progress (pdata, 100);
578   opkg_package_free (pdata.package);
579   return (err) ? OPKG_UNKNOWN_ERROR : OPKG_NO_ERROR;
580 }
581
582 int
583 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
584 {
585   int err;
586   pkg_t *pkg;
587   opkg_progress_data_t pdata;
588
589
590
591   opkg_assert (opkg != NULL);
592   opkg_assert (package_name != NULL);
593
594   pkg_info_preinstall_check (opkg->conf);
595
596   if (opkg->conf->restrict_to_default_dest)
597   {
598     pkg = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
599                                                  package_name,
600                                                  opkg->conf->default_dest);
601     if (pkg == NULL)
602     {
603       /* XXX: Error: Package not installed in default_dest */
604       return OPKG_PACKAGE_NOT_INSTALLED;
605     }
606   }
607   else
608   {
609     pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash,
610                                             package_name);
611   }
612
613   if (!pkg)
614   {
615     /* XXX: Error: Package not installed */
616     return OPKG_PACKAGE_NOT_INSTALLED;
617   }
618
619   pdata.action = OPKG_INSTALL;
620   pdata.package = old_pkg_to_new (pkg);
621   progress (pdata, 0);
622
623   err = opkg_upgrade_pkg (opkg->conf, pkg);
624   /* opkg_upgrade_pkg returns the error codes of opkg_install_pkg */
625   if (err)
626   {
627     switch (err)
628     {
629       case OPKG_INSTALL_ERR_NOT_TRUSTED: return OPKG_GPG_ERROR;
630       case OPKG_INSTALL_ERR_DOWNLOAD: return OPKG_DOWNLOAD_FAILED;
631       case OPKG_INSTALL_ERR_DEPENDENCIES:
632       case OPKG_INSTALL_ERR_CONFLICTS: return OPKG_DEPENDENCIES_FAILED;
633       case OPKG_INSTALL_ERR_ALREADY_INSTALLED: return OPKG_PACKAGE_ALREADY_INSTALLED;
634       case OPKG_INSTALL_ERR_SIGNATURE: return OPKG_GPG_ERROR;
635       case OPKG_INSTALL_ERR_MD5: return OPKG_MD5_ERROR;
636       default: return OPKG_UNKNOWN_ERROR;
637     }
638   }
639   progress (pdata, 75);
640
641   err = opkg_configure_packages (opkg->conf, NULL);
642   if (err)
643     return OPKG_UNKNOWN_ERROR;
644   progress (pdata, 100);
645   opkg_package_free (pdata.package);
646   return 0;
647 }
648
649 int
650 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
651 {
652   pkg_vec_t *installed;
653   int err = 0;
654   int i;
655   pkg_t *pkg;
656   opkg_progress_data_t pdata;
657
658   pdata.action = OPKG_INSTALL;
659   pdata.package = NULL;
660
661   opkg_assert (opkg != NULL);
662   progress (pdata, 0);
663
664   installed = pkg_vec_alloc ();
665   pkg_info_preinstall_check (opkg->conf);
666
667   pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed);
668   for (i = 0; i < installed->len; i++)
669   {
670     pkg = installed->pkgs[i];
671
672     pdata.package = old_pkg_to_new (pkg);
673     progress (pdata, 99 * i / installed->len);
674     opkg_package_free (pdata.package);
675
676     err += opkg_upgrade_pkg (opkg->conf, pkg);
677   }
678   pkg_vec_free (installed);
679
680   if (err)
681     return 1;
682
683   err = opkg_configure_packages (opkg->conf, NULL);
684   if (err)
685     return 1;
686
687   pdata.package = NULL;
688   progress (pdata, 100);
689   return 0;
690 }
691
692 int
693 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
694 {
695   char *tmp;
696   int err, result = 0;
697   char *lists_dir;
698   pkg_src_list_elt_t *iter;
699   pkg_src_t *src;
700   int sources_list_count, sources_done;
701   opkg_progress_data_t pdata;
702
703   opkg_assert (opkg != NULL);
704
705   pdata.action = OPKG_DOWNLOAD;
706   pdata.package = NULL;
707   progress (pdata, 0);
708
709   sprintf_alloc (&lists_dir, "%s",
710                  (opkg->conf->restrict_to_default_dest)
711                  ? opkg->conf->default_dest->lists_dir
712                  : opkg->conf->lists_dir);
713
714   if (!file_is_dir (lists_dir))
715   {
716     if (file_exists (lists_dir))
717     {
718       /* XXX: Error: file exists but is not a directory */
719       free (lists_dir);
720       return 1;
721     }
722
723     err = file_mkdir_hier (lists_dir, 0755);
724     if (err)
725     {
726       /* XXX: Error: failed to create directory */
727       free (lists_dir);
728       return 1;
729     }
730   }
731
732   tmp = strdup ("/tmp/opkg.XXXXXX");
733
734   if (mkdtemp (tmp) == NULL)
735   {
736     /* XXX: Error: could not create temporary file name */
737     free (lists_dir);
738     free (tmp);
739     return 1;
740   }
741
742   /* count the number of sources so we can give some progress updates */
743   sources_list_count = 0;
744   sources_done = 0;
745   iter = opkg->conf->pkg_src_list.head;
746   while (iter)
747   {
748     sources_list_count++;
749     iter = iter->next;
750   }
751
752   for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
753   {
754     char *url, *list_file_name = NULL;
755
756     src = iter->data;
757
758     if (src->extra_data)  /* debian style? */
759       sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
760                      src->gzip ? "Packages.gz" : "Packages");
761     else
762       sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
763
764     sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
765     if (src->gzip)
766     {
767       FILE *in, *out;
768       struct _curl_cb_data cb_data;
769       char *tmp_file_name = NULL;
770
771       sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
772
773       /* XXX: Note: downloading url */
774
775       cb_data.cb = progress_callback;
776       cb_data.progress_data = &pdata;
777       cb_data.opkg = opkg;
778       cb_data.user_data = user_data;
779       cb_data.start_range = 100 * sources_done / sources_list_count;
780       cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
781
782       err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
783
784       if (err == 0)
785       {
786         /* XXX: Note: Inflating downloaded file */
787         in = fopen (tmp_file_name, "r");
788         out = fopen (list_file_name, "w");
789         if (in && out)
790           unzip (in, out);
791         else
792           err = 1;
793         if (in)
794           fclose (in);
795         if (out)
796           fclose (out);
797         unlink (tmp_file_name);
798       }
799       free (tmp_file_name);
800     }
801     else
802       err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
803
804     if (err)
805     {
806       /* XXX: Error: download error */
807       result = OPKG_DOWNLOAD_FAILED;
808     }
809     free (url);
810
811 #ifdef HAVE_GPGME
812     char *sig_file_name;
813     /* download detached signitures to verify the package lists */
814     /* get the url for the sig file */
815     if (src->extra_data)  /* debian style? */
816       sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
817                      "Packages.sig");
818     else
819       sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
820
821     /* create filename for signature */
822     sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, src->name);
823
824     /* make sure there is no existing signature file */
825     unlink (sig_file_name);
826
827     err = opkg_download (opkg->conf, url, sig_file_name, NULL, NULL);
828     if (err)
829     {
830       /* XXX: Warning: Download failed */
831     }
832     else
833     {
834       int err;
835       err = opkg_verify_file (opkg->conf, list_file_name, sig_file_name);
836       if (err == 0)
837       {
838         /* XXX: Notice: Signature check passed */
839       }
840       else
841       {
842         /* XXX: Warning: Signature check failed */
843       }
844     }
845     free (sig_file_name);
846     free (list_file_name);
847     free (url);
848 #else
849     /* XXX: Note: Signiture check for %s skipped because GPG support was not
850      * enabled in this build
851      */
852 #endif
853
854     sources_done++;
855     progress (pdata, 100 * sources_done / sources_list_count);
856   }
857
858   rmdir (tmp);
859   free (tmp);
860   free (lists_dir);
861
862   /* Now re-read the package lists to update package hash tables. */
863   opkg_re_read_config_files (opkg);
864
865   return result;
866 }
867
868
869 int
870 opkg_list_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 *pkg;
883     opkg_package_t *package;
884
885     pkg = all->pkgs[i];
886
887     package = old_pkg_to_new (pkg);
888     callback (opkg, package, user_data);
889   }
890
891   pkg_vec_free (all);
892
893   return 0;
894 }
895
896 int
897 opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
898 {
899   pkg_vec_t *all;
900   int i;
901
902   opkg_assert (opkg);
903   opkg_assert (callback);
904
905   /* ensure all data is valid */
906   pkg_info_preinstall_check (opkg->conf);
907
908   all = pkg_vec_alloc ();
909   pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
910   for (i = 0; i < all->len; i++)
911   {
912     pkg_t *old, *new;
913     int cmp;
914     opkg_package_t *package;
915
916     old = all->pkgs[i];
917     
918     if (old->state_status != SS_INSTALLED)
919       continue;
920
921     new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, old->name, NULL);
922     if (new == NULL) {
923       /* XXX: Notice: Assuming locally install package is up to date */
924       continue;
925     }
926           
927     cmp = pkg_compare_versions(old, new);
928
929     if (cmp < 0)
930     {
931       package = old_pkg_to_new (new);
932       callback (opkg, package, user_data);
933     }
934   }
935
936   pkg_vec_free (all);
937
938   return 0;
939 }
940
941 opkg_package_t*
942 opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *arch, const char *repo)
943 {
944   pkg_vec_t *all;
945   opkg_package_t *package = NULL;
946   int i;
947 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
948
949   opkg_assert (opkg);
950
951   all = pkg_vec_alloc ();
952   pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
953   for (i = 0; i < all->len; i++)
954   {
955     pkg_t *pkg;
956     char *pkgv;
957
958     pkg = all->pkgs[i];
959
960     /* check name */
961     if (sstrcmp (pkg->name, name))
962       continue;
963     
964     /* check version */
965     pkgv = pkg_version_str_alloc (pkg);
966     if (sstrcmp (pkgv, ver))
967     {
968       free (pkgv);
969       continue;
970     }
971     free (pkgv);
972
973     /* check architecture */
974     if (arch)
975     {
976       if (sstrcmp (pkg->architecture, arch))
977         continue;
978     }
979
980     /* check repository */
981     if (repo)
982     {
983       if (sstrcmp (pkg->src->name, repo))
984           continue;
985     }
986
987     /* match found */
988     package = old_pkg_to_new (pkg);
989     break;
990   }
991
992   pkg_vec_free (all);
993
994   return package;
995 }