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