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