Cleanup error_list stuff a bit more.
[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 /**
54  * Clone a pkg_t 
55  */ 
56 static opkg_package_t*
57 pkg_t_to_opkg_package_t (pkg_t *old)
58 {
59   opkg_package_t *new;
60
61   new = opkg_package_new ();
62
63 #define sstrdup(x) (x) ? strdup (x) : NULL;
64
65   new->name = sstrdup (old->name);
66   new->version = pkg_version_str_alloc (old);
67   new->architecture = sstrdup (old->architecture);
68   if (old->src)
69     new->repository = sstrdup (old->src->name);
70   new->description = sstrdup (old->description);
71   new->tags = sstrdup (old->tags);
72   new->url = sstrdup (old->url);
73
74 #undef sstrdup
75
76   new->size = (old->size) ? atoi (old->size) : 0;
77   new->installed = (old->state_status == SS_INSTALLED);
78
79   return new;
80 }
81
82 static int
83 opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
84 {
85   pkg_vec_t *all;
86   int i;
87   pkg_t *pkg;
88   int r, err = 0;
89
90   all = pkg_vec_alloc ();
91   pkg_hash_fetch_available (&conf->pkg_hash, all);
92
93   for (i = 0; i < all->len; i++)
94   {
95     pkg = all->pkgs[i];
96
97     if (pkg_name && fnmatch (pkg_name, pkg->name, 0))
98       continue;
99
100     if (pkg->state_status == SS_UNPACKED)
101     {
102       r = opkg_configure (conf, pkg);
103       if (r == 0)
104       {
105         pkg->state_status = SS_INSTALLED;
106         pkg->parent->state_status = SS_INSTALLED;
107         pkg->state_flag &= ~SF_PREFER;
108       }
109       else
110       {
111         if (!err)
112           err = r;
113       }
114     }
115   }
116
117   pkg_vec_free (all);
118   return err;
119 }
120
121 struct _curl_cb_data
122 {
123   opkg_progress_callback_t cb;
124   opkg_progress_data_t *progress_data;
125   opkg_t *opkg;
126   void *user_data;
127   int start_range;
128   int finish_range;
129 };
130
131 int
132 curl_progress_cb (struct _curl_cb_data *cb_data,
133                   double t,   /* dltotal */
134                   double d,   /* dlnow */
135                   double ultotal,
136                   double ulnow)
137 {
138   int p = (t) ? d * 100 / t : 0;
139   static int prev = -1;
140   int progress = 0;
141
142   /* prevent the same value being sent twice (can occur due to rounding) */
143   if (p == prev)
144     return 0;
145   prev = p;
146
147   if (t < 1)
148     return 0;
149
150   progress = cb_data->start_range + (d / t * ((cb_data->finish_range - cb_data->start_range)));
151   cb_data->progress_data->percentage = progress;
152
153   (cb_data->cb)(cb_data->opkg,
154                 cb_data->progress_data,
155                 cb_data->user_data);
156
157   return 0;
158 }
159
160
161 /*** Public API ***/
162
163 opkg_package_t *
164 opkg_package_new ()
165 {
166
167   opkg_package_t *p;
168
169   p = calloc (1, sizeof (opkg_package_t));
170
171   return p;
172 }
173
174 void
175 opkg_package_free (opkg_package_t *p)
176 {
177   free (p->name);
178   free (p->version);
179   free (p->architecture);
180   free (p->description);
181   free (p->tags);
182   free (p->url);
183   free (p->repository);
184
185   free (p);
186 }
187
188 opkg_t *
189 opkg_new ()
190 {
191   opkg_t *opkg;
192   int err;
193
194   opkg = calloc (1, sizeof (opkg_t));
195
196   opkg->args = calloc (1, sizeof (args_t));
197   err = args_init (opkg->args);
198   if (err)
199   {
200     free (opkg->args);
201     free (opkg);
202     return NULL;
203   }
204
205   opkg->conf = calloc (1, sizeof (opkg_conf_t));
206   err = opkg_conf_init (opkg->conf, opkg->args);
207   if (err)
208   {
209     free (opkg->conf);
210     free (opkg->args);
211     free (opkg);
212     return NULL;
213   }
214
215   opkg_init_options_array (opkg->conf, &opkg->options);
216   return opkg;
217 }
218
219 void
220 opkg_free (opkg_t *opkg)
221 {
222   opkg_assert (opkg != NULL);
223
224   opkg_conf_deinit (opkg->conf);
225   args_deinit (opkg->args);
226   free (opkg->options);
227   free (opkg->args);
228   free (opkg->conf);
229   free (opkg);
230 }
231
232 int
233 opkg_re_read_config_files (opkg_t *opkg)
234 {
235   args_t *a;
236   opkg_conf_t *c;
237
238   opkg_assert (opkg != NULL);
239
240   a = opkg->args;
241   c = opkg->conf;
242
243   /* Unfortunatly, the easiest way to re-read the config files right now is to
244    * throw away opkg->conf and start again */
245
246   /* copy the settings we need to keep */
247   a->autoremove = c->autoremove;
248   a->force_depends = c->force_depends;
249   a->force_defaults = c->force_defaults;
250   a->force_maintainer = c->force_maintainer;
251   a->force_overwrite = c->force_overwrite;
252   a->force_downgrade = c->force_downgrade;
253   a->force_reinstall = c->force_reinstall;
254   a->force_removal_of_dependent_packages = c->force_removal_of_dependent_packages;
255   a->force_removal_of_essential_packages = c->force_removal_of_essential_packages;
256   a->nodeps = c->nodeps;
257   a->noaction = c->noaction;
258   a->query_all = c->query_all;
259   a->verbosity = c->verbosity;
260
261   if (c->offline_root)
262   {
263     if (a->offline_root) free (a->offline_root);
264     a->offline_root = strdup (c->offline_root);
265   }
266
267   if (c->offline_root_pre_script_cmd)
268   {
269     if (a->offline_root_pre_script_cmd) free (a->offline_root_pre_script_cmd);
270     a->offline_root_pre_script_cmd = strdup (c->offline_root_pre_script_cmd);
271   }
272
273   if (c->offline_root_post_script_cmd)
274   {
275     if (a->offline_root_post_script_cmd) free (a->offline_root_post_script_cmd);
276     a->offline_root_post_script_cmd = strdup (c->offline_root_post_script_cmd);
277   }
278
279   if (c->cache) {
280     if (a->cache)
281         free (a->cache);
282     a->cache = strdup(c->cache);
283   }
284
285   /* throw away old opkg_conf and start again */
286   opkg_conf_deinit (opkg->conf);
287   opkg_conf_init (opkg->conf, opkg->args);
288
289   free (opkg->options);
290   opkg_init_options_array (opkg->conf, &opkg->options);
291
292   return 0;
293 }
294
295 void
296 opkg_get_option (opkg_t *opkg, char *option, void **value)
297 {
298   int i = 0;
299   opkg_option_t *options;
300
301   opkg_assert (opkg != NULL);
302   opkg_assert (option != NULL);
303   opkg_assert (value != NULL);
304
305   options = opkg->options;
306
307   /* look up the option
308    * TODO: this would be much better as a hash table
309    */
310   while (options[i].name)
311   {
312     if (strcmp (options[i].name, option) != 0)
313     {
314       i++;
315       continue;
316     }
317   }
318
319   /* get the option */
320   switch (options[i].type)
321   {
322   case OPKG_OPT_TYPE_BOOL:
323     *((int *) value) = *((int *) options[i].value);
324     return;
325
326   case OPKG_OPT_TYPE_INT:
327     *((int *) value) = *((int *) options[i].value);
328     return;
329
330   case OPKG_OPT_TYPE_STRING:
331     *((char **)value) = strdup (options[i].value);
332     return;
333   }
334
335 }
336
337 void
338 opkg_set_option (opkg_t *opkg, char *option, void *value)
339 {
340   int i = 0, found = 0;
341   opkg_option_t *options;
342
343   opkg_assert (opkg != NULL);
344   opkg_assert (option != NULL);
345   opkg_assert (value != NULL);
346
347   options = opkg->options;
348
349   /* look up the option
350    * TODO: this would be much better as a hash table
351    */
352   while (options[i].name)
353   {
354     if (strcmp (options[i].name, option) == 0)
355     {
356       found = 1;
357       break;
358     }
359     i++;
360   }
361
362   if (!found)
363   {
364     /* XXX: Warning: Option not found */
365     return;
366   }
367
368   /* set the option */
369   switch (options[i].type)
370   {
371   case OPKG_OPT_TYPE_BOOL:
372     if (*((int *) value) == 0)
373       *((int *)options[i].value) = 0;
374     else
375       *((int *)options[i].value) = 1;
376     return;
377
378   case OPKG_OPT_TYPE_INT:
379     *((int *) options[i].value) = *((int *) value);
380     return;
381
382   case OPKG_OPT_TYPE_STRING:
383     *((char **)options[i].value) = strdup (value);
384     return;
385   }
386
387 }
388
389 /**
390  * @brief libopkg API: Install package
391  * @param opkg Then opkg handler
392  * @param package_name The name of package in which is going to install
393  * @param progress_callback The callback function that report the status to caller. 
394  */ 
395 int
396 opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
397 {
398   int err;
399   char *stripped_filename;
400   opkg_progress_data_t pdata;
401   pkg_t *old, *new;
402   pkg_vec_t *deps, *all;
403   int i, ndepends;
404   char **unresolved = NULL;
405
406   opkg_assert (opkg != NULL);
407   opkg_assert (package_name != NULL);
408
409   /* ... */
410   pkg_info_preinstall_check (opkg->conf);
411
412
413   /* check to ensure package is not already installed */
414   old = pkg_hash_fetch_installed_by_name(&opkg->conf->pkg_hash, package_name);
415   if (old)
416   {
417     /* XXX: Error: Package is already installed. */
418     return OPKG_PACKAGE_ALREADY_INSTALLED;
419   }
420
421   new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, package_name, NULL);
422   if (!new)
423   {
424     /* XXX: Error: Could not find package to install */
425     return OPKG_PACKAGE_NOT_FOUND;
426   }
427
428   new->state_flag |= SF_USER;
429
430   pdata.action = OPKG_INSTALL;
431   pdata.package = pkg_t_to_opkg_package_t (new);
432
433   progress (pdata, 0);
434
435   /* find dependancies and download them */
436   deps = pkg_vec_alloc ();
437   /* this function does not return the original package, so we insert it later */
438   ndepends = pkg_hash_fetch_unsatisfied_dependencies (opkg->conf, new, deps, &unresolved);
439   if (unresolved)
440   {
441     /* XXX: Error: Could not satisfy dependencies */
442     pkg_vec_free (deps);
443     return OPKG_DEPENDENCIES_FAILED;
444   }
445
446   /* insert the package we are installing so that we download it */
447   pkg_vec_insert (deps, new);
448
449   /* download package and dependancies */
450   for (i = 0; i < deps->len; i++)
451   {
452     pkg_t *pkg;
453     struct _curl_cb_data cb_data;
454     char *url;
455
456     pkg = deps->pkgs[i];
457     if (pkg->local_filename)
458       continue;
459
460     opkg_package_free (pdata.package);
461     pdata.package = pkg_t_to_opkg_package_t (pkg);
462     pdata.action = OPKG_DOWNLOAD;
463
464     if (pkg->src == NULL)
465     {
466       /* XXX: Error: Package not available from any configured src */
467       return OPKG_PACKAGE_NOT_AVAILABLE;
468     }
469
470     sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
471
472     /* Get the filename part, without any directory */
473     stripped_filename = strrchr(pkg->filename, '/');
474     if ( ! stripped_filename )
475         stripped_filename = pkg->filename;
476
477     sprintf_alloc(&pkg->local_filename, "%s/%s", opkg->conf->tmp_dir, stripped_filename);
478
479     cb_data.cb = progress_callback;
480     cb_data.progress_data = &pdata;
481     cb_data.opkg = opkg;
482     cb_data.user_data = user_data;
483     /* 75% of "install" progress is for downloading */
484     cb_data.start_range = 75 * i / deps->len;
485     cb_data.finish_range = 75 * (i + 1) / deps->len;
486
487     err = opkg_download(opkg->conf, url, pkg->local_filename,
488               (curl_progress_func) curl_progress_cb, &cb_data);
489     free(url);
490
491     if (err)
492     {
493       pkg_vec_free (deps);
494       opkg_package_free (pdata.package);
495       return OPKG_DOWNLOAD_FAILED;
496     }
497
498   }
499   pkg_vec_free (deps);
500
501   /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
502   all = pkg_vec_alloc ();
503   pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
504   for (i = 0; i < all->len; i++)
505   {
506     all->pkgs[i]->parent->dependencies_checked = 0;
507   }
508   pkg_vec_free (all);
509
510
511   /* 75% of "install" progress is for downloading */
512   opkg_package_free (pdata.package);
513   pdata.package = pkg_t_to_opkg_package_t (new);
514   pdata.action = OPKG_INSTALL;
515   progress (pdata, 75);
516
517   /* unpack the package */
518   err = opkg_install_pkg(opkg->conf, new, 0);
519
520   if (err)
521   {
522     opkg_package_free (pdata.package);
523     switch (err)
524     {
525       case OPKG_INSTALL_ERR_NOT_TRUSTED: return OPKG_GPG_ERROR;
526       case OPKG_INSTALL_ERR_DOWNLOAD: return OPKG_DOWNLOAD_FAILED;
527       case OPKG_INSTALL_ERR_DEPENDENCIES:
528       case OPKG_INSTALL_ERR_CONFLICTS: return OPKG_DEPENDENCIES_FAILED;
529       case OPKG_INSTALL_ERR_ALREADY_INSTALLED: return OPKG_PACKAGE_ALREADY_INSTALLED;
530       case OPKG_INSTALL_ERR_SIGNATURE: return OPKG_GPG_ERROR;
531       case OPKG_INSTALL_ERR_MD5: return OPKG_MD5_ERROR;
532       case OPKG_INSTALL_ERR_SHA256: return OPKG_SHA256_ERROR;
533       default: return OPKG_UNKNOWN_ERROR;
534     }
535   }
536
537   progress (pdata, 75);
538
539   /* run configure scripts, etc. */
540   err = opkg_configure_packages (opkg->conf, NULL);
541   if (err)
542   {
543     opkg_package_free (pdata.package);
544     return OPKG_UNKNOWN_ERROR;
545   }
546
547   /* write out status files and file lists */
548   opkg_conf_write_status_files (opkg->conf);
549   pkg_write_changed_filelists (opkg->conf);
550
551   progress (pdata, 100);
552   opkg_package_free (pdata.package);
553   return 0;
554 }
555
556 int
557 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
558 {
559   int err;
560   pkg_t *pkg = NULL;
561   pkg_t *pkg_to_remove;
562   opkg_progress_data_t pdata;
563
564   opkg_assert (opkg != NULL);
565   opkg_assert (package_name != NULL);
566
567   pkg_info_preinstall_check (opkg->conf);
568
569   pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
570
571   if (pkg == NULL)
572   {
573     /* XXX: Error: Package not installed. */
574     return OPKG_PACKAGE_NOT_INSTALLED;
575   }
576
577   pdata.action = OPKG_REMOVE;
578   pdata.package = pkg_t_to_opkg_package_t (pkg);
579   progress (pdata, 0);
580
581
582   if (pkg->state_status == SS_NOT_INSTALLED)
583   {
584     /* XXX:  Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
585     opkg_package_free (pdata.package);
586     return OPKG_PACKAGE_NOT_INSTALLED;
587   }
588   progress (pdata, 25);
589
590   if (opkg->conf->restrict_to_default_dest)
591   {
592     pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
593                                                            pkg->name,
594                                                            opkg->conf->default_dest);
595   }
596   else
597   {
598     pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
599   }
600
601
602   progress (pdata, 75);
603
604   err = opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
605
606   /* write out status files and file lists */
607   opkg_conf_write_status_files (opkg->conf);
608   pkg_write_changed_filelists (opkg->conf);
609
610
611   progress (pdata, 100);
612   opkg_package_free (pdata.package);
613   return (err) ? OPKG_UNKNOWN_ERROR : OPKG_NO_ERROR;
614 }
615
616 int
617 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
618 {
619   int err;
620   pkg_t *pkg;
621   opkg_progress_data_t pdata;
622
623
624
625   opkg_assert (opkg != NULL);
626   opkg_assert (package_name != NULL);
627
628   pkg_info_preinstall_check (opkg->conf);
629
630   if (opkg->conf->restrict_to_default_dest)
631   {
632     pkg = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
633                                                  package_name,
634                                                  opkg->conf->default_dest);
635     if (pkg == NULL)
636     {
637       /* XXX: Error: Package not installed in default_dest */
638       return OPKG_PACKAGE_NOT_INSTALLED;
639     }
640   }
641   else
642   {
643     pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash,
644                                             package_name);
645   }
646
647   if (!pkg)
648   {
649     /* XXX: Error: Package not installed */
650     return OPKG_PACKAGE_NOT_INSTALLED;
651   }
652
653   pdata.action = OPKG_INSTALL;
654   pdata.package = pkg_t_to_opkg_package_t (pkg);
655   progress (pdata, 0);
656
657   err = opkg_upgrade_pkg (opkg->conf, pkg);
658   /* opkg_upgrade_pkg returns the error codes of opkg_install_pkg */
659   if (err)
660   {
661
662     opkg_package_free (pdata.package);
663     switch (err)
664     {
665       case OPKG_INSTALL_ERR_NOT_TRUSTED: return OPKG_GPG_ERROR;
666       case OPKG_INSTALL_ERR_DOWNLOAD: return OPKG_DOWNLOAD_FAILED;
667       case OPKG_INSTALL_ERR_DEPENDENCIES:
668       case OPKG_INSTALL_ERR_CONFLICTS: return OPKG_DEPENDENCIES_FAILED;
669       case OPKG_INSTALL_ERR_ALREADY_INSTALLED: return OPKG_PACKAGE_ALREADY_INSTALLED;
670       case OPKG_INSTALL_ERR_SIGNATURE: return OPKG_GPG_ERROR;
671       case OPKG_INSTALL_ERR_MD5: return OPKG_MD5_ERROR;
672       case OPKG_INSTALL_ERR_SHA256: return OPKG_SHA256_ERROR;
673       default: return OPKG_UNKNOWN_ERROR;
674     }
675   }
676   progress (pdata, 75);
677
678   err = opkg_configure_packages (opkg->conf, NULL);
679   if (err) {
680     opkg_package_free (pdata.package);  
681     return OPKG_UNKNOWN_ERROR;
682   }
683
684   /* write out status files and file lists */
685   opkg_conf_write_status_files (opkg->conf);
686   pkg_write_changed_filelists (opkg->conf);
687
688   progress (pdata, 100);
689   opkg_package_free (pdata.package);
690   return 0;
691 }
692
693 int
694 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
695 {
696   pkg_vec_t *installed;
697   int err = 0;
698   int i;
699   pkg_t *pkg;
700   opkg_progress_data_t pdata;
701
702   pdata.action = OPKG_INSTALL;
703   pdata.package = NULL;
704
705   opkg_assert (opkg != NULL);
706   progress (pdata, 0);
707
708   installed = pkg_vec_alloc ();
709   pkg_info_preinstall_check (opkg->conf);
710
711   pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed);
712   for (i = 0; i < installed->len; i++)
713   {
714     pkg = installed->pkgs[i];
715
716     pdata.package = pkg_t_to_opkg_package_t (pkg);
717     progress (pdata, 99 * i / installed->len);
718     opkg_package_free (pdata.package);
719
720     err += opkg_upgrade_pkg (opkg->conf, pkg);
721   }
722   pkg_vec_free (installed);
723
724   if (err)
725     return 1;
726
727   err = opkg_configure_packages (opkg->conf, NULL);
728   if (err)
729     return 1;
730
731   pdata.package = NULL;
732   progress (pdata, 100);
733   return 0;
734 }
735
736 int
737 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
738 {
739   char *tmp;
740   int err, result = 0;
741   char *lists_dir;
742   pkg_src_list_elt_t *iter;
743   pkg_src_t *src;
744   int sources_list_count, sources_done;
745   opkg_progress_data_t pdata;
746
747   opkg_assert (opkg != NULL);
748
749   pdata.action = OPKG_DOWNLOAD;
750   pdata.package = NULL;
751   progress (pdata, 0);
752
753   sprintf_alloc (&lists_dir, "%s",
754                  (opkg->conf->restrict_to_default_dest)
755                  ? opkg->conf->default_dest->lists_dir
756                  : opkg->conf->lists_dir);
757
758   if (!file_is_dir (lists_dir))
759   {
760     if (file_exists (lists_dir))
761     {
762       /* XXX: Error: file exists but is not a directory */
763       free (lists_dir);
764       return 1;
765     }
766
767     err = file_mkdir_hier (lists_dir, 0755);
768     if (err)
769     {
770       /* XXX: Error: failed to create directory */
771       free (lists_dir);
772       return 1;
773     }
774   }
775
776   tmp = strdup ("/tmp/opkg.XXXXXX");
777
778   if (mkdtemp (tmp) == NULL)
779   {
780     /* XXX: Error: could not create temporary file name */
781     free (lists_dir);
782     free (tmp);
783     return 1;
784   }
785
786   /* count the number of sources so we can give some progress updates */
787   sources_list_count = 0;
788   sources_done = 0;
789   list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
790   {
791     sources_list_count++;
792   }
793
794   list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
795   {
796     char *url, *list_file_name = NULL;
797
798     src = (pkg_src_t *)iter->data;
799
800     if (src->extra_data)  /* debian style? */
801       sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
802                      src->gzip ? "Packages.gz" : "Packages");
803     else
804       sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
805
806     sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
807     if (src->gzip)
808     {
809       FILE *in, *out;
810       struct _curl_cb_data cb_data;
811       char *tmp_file_name = NULL;
812
813       sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
814
815       /* XXX: Note: downloading url */
816
817       cb_data.cb = progress_callback;
818       cb_data.progress_data = &pdata;
819       cb_data.opkg = opkg;
820       cb_data.user_data = user_data;
821       cb_data.start_range = 100 * sources_done / sources_list_count;
822       cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
823
824       err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
825
826       if (err == 0)
827       {
828         /* XXX: Note: Inflating downloaded file */
829         in = fopen (tmp_file_name, "r");
830         out = fopen (list_file_name, "w");
831         if (in && out)
832           unzip (in, out);
833         else
834           err = 1;
835         if (in)
836           fclose (in);
837         if (out)
838           fclose (out);
839         unlink (tmp_file_name);
840       }
841       free (tmp_file_name);
842     }
843     else
844       err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
845
846     if (err)
847     {
848       /* XXX: Error: download error */
849       result = OPKG_DOWNLOAD_FAILED;
850     }
851     free (url);
852
853 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
854     if ( opkg->conf->check_signature ) {
855         char *sig_file_name;
856         /* download detached signitures to verify the package lists */
857         /* get the url for the sig file */
858         if (src->extra_data)  /* debian style? */
859             sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
860                     "Packages.sig");
861         else
862             sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
863
864         /* create filename for signature */
865         sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, src->name);
866
867         /* make sure there is no existing signature file */
868         unlink (sig_file_name);
869
870         err = opkg_download (opkg->conf, url, sig_file_name, NULL, NULL);
871         if (err)
872         {
873             /* XXX: Warning: Download failed */
874         }
875         else
876         {
877             int err;
878             err = opkg_verify_file (opkg->conf, list_file_name, sig_file_name);
879             if (err == 0)
880             {
881                 /* XXX: Notice: Signature check passed */
882             }
883             else
884             {
885                 /* XXX: Warning: Signature check failed */
886             }
887         }
888         free (sig_file_name);
889         free (list_file_name);
890         free (url);
891     }
892 #else
893     /* XXX: Note: Signature check for %s skipped because GPG support was not
894      * enabled in this build
895      */
896 #endif
897
898     sources_done++;
899     progress (pdata, 100 * sources_done / sources_list_count);
900   }
901
902   rmdir (tmp);
903   free (tmp);
904   free (lists_dir);
905
906   /* Now re-read the package lists to update package hash tables. */
907   opkg_re_read_config_files (opkg);
908
909   return result;
910 }
911
912
913 int
914 opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
915 {
916   pkg_vec_t *all;
917   int i;
918
919   opkg_assert (opkg);
920   opkg_assert (callback);
921
922   all = pkg_vec_alloc ();
923   pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
924   for (i = 0; i < all->len; i++)
925   {
926     pkg_t *pkg;
927     opkg_package_t *package;
928
929     pkg = all->pkgs[i];
930
931     package = pkg_t_to_opkg_package_t (pkg);
932     callback (opkg, package, user_data);
933     opkg_package_free (package);
934   }
935
936   pkg_vec_free (all);
937
938   return 0;
939 }
940
941 int
942 opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
943 {
944     struct active_list *head;
945     struct active_list *node;
946     pkg_t *old=NULL, *new = NULL;
947     static opkg_package_t* package=NULL;
948
949
950     opkg_assert (opkg);
951     opkg_assert (callback);
952
953     /* ensure all data is valid */
954     pkg_info_preinstall_check (opkg->conf);
955
956     head  =  prepare_upgrade_list(opkg->conf);
957     for (node=active_list_next(head, head); node; active_list_next(head,node)) {
958         old = list_entry(node, pkg_t, list);
959         new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, old->name, NULL);
960         package = pkg_t_to_opkg_package_t (new);
961         callback (opkg, package, user_data);
962         opkg_package_free (package);
963     }
964     active_list_head_delete(head);
965     return 0;
966 }
967
968 opkg_package_t*
969 opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *arch, const char *repo)
970 {
971   pkg_vec_t *all;
972   opkg_package_t *package = NULL;
973   int i;
974 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
975
976   opkg_assert (opkg);
977
978   all = pkg_vec_alloc ();
979   pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
980   for (i = 0; i < all->len; i++)
981   {
982     pkg_t *pkg;
983     char *pkgv;
984
985     pkg = all->pkgs[i];
986
987     /* check name */
988     if (sstrcmp (pkg->name, name))
989       continue;
990     
991     /* check version */
992     pkgv = pkg_version_str_alloc (pkg);
993     if (sstrcmp (pkgv, ver))
994     {
995       free (pkgv);
996       continue;
997     }
998     free (pkgv);
999
1000     /* check architecture */
1001     if (arch)
1002     {
1003       if (sstrcmp (pkg->architecture, arch))
1004         continue;
1005     }
1006
1007     /* check repository */
1008     if (repo)
1009     {
1010       if (sstrcmp (pkg->src->name, repo))
1011           continue;
1012     }
1013
1014     /* match found */
1015     package = pkg_t_to_opkg_package_t (pkg);
1016     break;
1017   }
1018
1019   pkg_vec_free (all);
1020
1021   return package;
1022 }
1023
1024 #ifdef HAVE_CURL
1025 #include <curl/curl.h>
1026 #endif
1027 /**
1028  * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status. 
1029  * @param opkg The opkg_t
1030  * @return return how many repositories cannot access. 0 means all okay. 
1031  */ 
1032 int opkg_repository_accessibility_check(opkg_t *opkg) 
1033 {
1034   pkg_src_list_elt_t *iter;
1035   str_list_elt_t *iter1;
1036   str_list_t *src;
1037   int repositories=0;
1038   int ret=0;
1039   int err;
1040   char *repo_ptr;
1041   char *stmp;
1042   opkg_assert(opkg != NULL);
1043
1044   src = str_list_alloc();
1045
1046   list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
1047   {
1048     if (strstr(((pkg_src_t *)iter->data)->value, "://") && 
1049                     index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/')) 
1050       stmp = strndup(((pkg_src_t *)iter->data)->value, 
1051                       (index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/') - ((pkg_src_t *)iter->data)->value)*sizeof(char));
1052
1053     else
1054       stmp = strdup(((pkg_src_t *)iter->data)->value);
1055
1056     for (iter1 = str_list_first(src); iter1; iter1 = str_list_next(src, iter1))
1057     {
1058       if (strstr(iter1->data, stmp)) 
1059         break;
1060     }
1061     if (iter1)
1062       continue;
1063
1064     sprintf_alloc(&repo_ptr, "%s/index.html",stmp);
1065     free(stmp);
1066
1067     str_list_append(src, repo_ptr);
1068     free(repo_ptr);
1069     repositories++;
1070   }
1071   while (repositories > 0) 
1072   {
1073     iter1 = str_list_pop(src);
1074     repositories--;
1075
1076     err = opkg_download(opkg->conf, iter1->data, "/dev/null", NULL, NULL);
1077 #ifdef HAVE_CURL
1078     if (!(err == CURLE_OK || 
1079                 err == CURLE_HTTP_RETURNED_ERROR || 
1080                 err == CURLE_FILE_COULDNT_READ_FILE ||
1081                 err == CURLE_REMOTE_FILE_NOT_FOUND || 
1082                 err == CURLE_TFTP_NOTFOUND
1083                 )) {
1084 #else
1085     if (!(err == 0)) {
1086 #endif
1087             ret++;
1088     }
1089     str_list_elt_deinit(iter1);
1090   }
1091   free(src);
1092   return ret;
1093 }