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