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