fabd520c781cef19afbf6cf183741818d3e59f5f
[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   /* throw away old opkg_conf and start again */
277   opkg_conf_deinit (opkg->conf);
278   opkg_conf_init (opkg->conf, opkg->args);
279
280   free (opkg->options);
281   opkg_init_options_array (opkg->conf, &opkg->options);
282
283   return 0;
284 }
285
286 void
287 opkg_get_option (opkg_t *opkg, char *option, void **value)
288 {
289   int i = 0;
290   opkg_option_t *options;
291
292   opkg_assert (opkg != NULL);
293   opkg_assert (option != NULL);
294   opkg_assert (value != NULL);
295
296   options = opkg->options;
297
298   /* look up the option
299    * TODO: this would be much better as a hash table
300    */
301   while (options[i].name)
302   {
303     if (strcmp (options[i].name, option) != 0)
304     {
305       i++;
306       continue;
307     }
308   }
309
310   /* get the option */
311   switch (options[i].type)
312   {
313   case OPKG_OPT_TYPE_BOOL:
314     *((int *) value) = *((int *) options[i].value);
315     return;
316
317   case OPKG_OPT_TYPE_INT:
318     *((int *) value) = *((int *) options[i].value);
319     return;
320
321   case OPKG_OPT_TYPE_STRING:
322     *((char **)value) = strdup (options[i].value);
323     return;
324   }
325
326 }
327
328 void
329 opkg_set_option (opkg_t *opkg, char *option, void *value)
330 {
331   int i = 0, found = 0;
332   opkg_option_t *options;
333
334   opkg_assert (opkg != NULL);
335   opkg_assert (option != NULL);
336   opkg_assert (value != NULL);
337
338   options = opkg->options;
339
340   /* look up the option
341    * TODO: this would be much better as a hash table
342    */
343   while (options[i].name)
344   {
345     if (strcmp (options[i].name, option) == 0)
346     {
347       found = 1;
348       break;
349     }
350     i++;
351   }
352
353   if (!found)
354   {
355     /* XXX: Warning: Option not found */
356     return;
357   }
358
359   /* set the option */
360   switch (options[i].type)
361   {
362   case OPKG_OPT_TYPE_BOOL:
363     if (*((int *) value) == 0)
364       *((int *)options[i].value) = 0;
365     else
366       *((int *)options[i].value) = 1;
367     return;
368
369   case OPKG_OPT_TYPE_INT:
370     *((int *) options[i].value) = *((int *) value);
371     return;
372
373   case OPKG_OPT_TYPE_STRING:
374     *((char **)options[i].value) = strdup (value);
375     return;
376   }
377
378 }
379
380 int
381 opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
382 {
383   int err;
384   char *stripped_filename;
385   opkg_progress_data_t pdata;
386   pkg_t *old, *new;
387   pkg_vec_t *deps, *all;
388   int i, ndepends;
389   char **unresolved = NULL;
390
391   opkg_assert (opkg != NULL);
392   opkg_assert (package_name != NULL);
393
394   /* ... */
395   pkg_info_preinstall_check (opkg->conf);
396
397
398   /* check to ensure package is not already installed */
399   old = pkg_hash_fetch_installed_by_name(&opkg->conf->pkg_hash, package_name);
400   if (old)
401   {
402     /* XXX: Error: Package is already installed. */
403     return OPKG_PACKAGE_ALREADY_INSTALLED;
404   }
405
406   new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, package_name, NULL);
407   if (!new)
408   {
409     /* XXX: Error: Could not find package to install */
410     return OPKG_PACKAGE_NOT_FOUND;
411   }
412
413   new->state_flag |= SF_USER;
414
415   pdata.action = OPKG_INSTALL;
416   pdata.package = old_pkg_to_new (new);
417
418   progress (pdata, 0);
419
420   /* find dependancies and download them */
421   deps = pkg_vec_alloc ();
422   /* this function does not return the original package, so we insert it later */
423   ndepends = pkg_hash_fetch_unsatisfied_dependencies (opkg->conf, new, deps, &unresolved);
424   if (unresolved)
425   {
426     /* XXX: Error: Could not satisfy dependencies */
427     pkg_vec_free (deps);
428     return OPKG_DEPENDENCIES_FAILED;
429   }
430
431   /* insert the package we are installing so that we download it */
432   pkg_vec_insert (deps, new);
433
434   /* download package and dependancies */
435   for (i = 0; i < deps->len; i++)
436   {
437     pkg_t *pkg;
438     struct _curl_cb_data cb_data;
439     char *url;
440
441     pkg = deps->pkgs[i];
442     if (pkg->local_filename)
443       continue;
444
445     opkg_package_free (pdata.package);
446     pdata.package = old_pkg_to_new (pkg);
447     pdata.action = OPKG_DOWNLOAD;
448
449     if (pkg->src == NULL)
450     {
451       /* XXX: Error: Package not available from any configured src */
452       return OPKG_PACKAGE_NOT_AVAILABLE;
453     }
454
455     sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
456
457     /* Get the filename part, without any directory */
458     stripped_filename = strrchr(pkg->filename, '/');
459     if ( ! stripped_filename )
460         stripped_filename = pkg->filename;
461
462     sprintf_alloc(&pkg->local_filename, "%s/%s", opkg->conf->tmp_dir, stripped_filename);
463
464     cb_data.cb = progress_callback;
465     cb_data.progress_data = &pdata;
466     cb_data.opkg = opkg;
467     cb_data.user_data = user_data;
468     /* 75% of "install" progress is for downloading */
469     cb_data.start_range = 75 * i / deps->len;
470     cb_data.finish_range = 75 * (i + 1) / deps->len;
471
472     err = opkg_download(opkg->conf, url, pkg->local_filename,
473               (curl_progress_func) curl_progress_cb, &cb_data);
474     free(url);
475
476     if (err)
477     {
478       pkg_vec_free (deps);
479       opkg_package_free (pdata.package);
480       return OPKG_DOWNLOAD_FAILED;
481     }
482
483   }
484   pkg_vec_free (deps);
485
486   /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
487   all = pkg_vec_alloc ();
488   pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
489   for (i = 0; i < all->len; i++)
490   {
491     all->pkgs[i]->parent->dependencies_checked = 0;
492   }
493   pkg_vec_free (all);
494
495
496   /* 75% of "install" progress is for downloading */
497   opkg_package_free (pdata.package);
498   pdata.package = old_pkg_to_new (new);
499   pdata.action = OPKG_INSTALL;
500   progress (pdata, 75);
501
502   /* unpack the package */
503   err = opkg_install_pkg(opkg->conf, new, 0);
504
505   if (err)
506   {
507     opkg_package_free (pdata.package);
508     switch (err)
509     {
510       case OPKG_INSTALL_ERR_NOT_TRUSTED: return OPKG_GPG_ERROR;
511       case OPKG_INSTALL_ERR_DOWNLOAD: return OPKG_DOWNLOAD_FAILED;
512       case OPKG_INSTALL_ERR_DEPENDENCIES:
513       case OPKG_INSTALL_ERR_CONFLICTS: return OPKG_DEPENDENCIES_FAILED;
514       case OPKG_INSTALL_ERR_ALREADY_INSTALLED: return OPKG_PACKAGE_ALREADY_INSTALLED;
515       case OPKG_INSTALL_ERR_SIGNATURE: return OPKG_GPG_ERROR;
516       case OPKG_INSTALL_ERR_MD5: return OPKG_MD5_ERROR;
517       default: return OPKG_UNKNOWN_ERROR;
518     }
519   }
520
521   progress (pdata, 75);
522
523   /* run configure scripts, etc. */
524   err = opkg_configure_packages (opkg->conf, NULL);
525   if (err)
526   {
527     opkg_package_free (pdata.package);
528     return OPKG_UNKNOWN_ERROR;
529   }
530
531   /* write out status files and file lists */
532   opkg_conf_write_status_files (opkg->conf);
533   pkg_write_changed_filelists (opkg->conf);
534
535   progress (pdata, 100);
536   opkg_package_free (pdata.package);
537   return 0;
538 }
539
540 int
541 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
542 {
543   int err;
544   pkg_t *pkg = NULL;
545   pkg_t *pkg_to_remove;
546   opkg_progress_data_t pdata;
547
548   opkg_assert (opkg != NULL);
549   opkg_assert (package_name != NULL);
550
551   pkg_info_preinstall_check (opkg->conf);
552
553   pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
554
555   if (pkg == NULL)
556   {
557     /* XXX: Error: Package not installed. */
558     return OPKG_PACKAGE_NOT_INSTALLED;
559   }
560
561   pdata.action = OPKG_REMOVE;
562   pdata.package = old_pkg_to_new (pkg);
563   progress (pdata, 0);
564
565
566   if (pkg->state_status == SS_NOT_INSTALLED)
567   {
568     /* XXX:  Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
569     opkg_package_free (pdata.package);
570     return OPKG_PACKAGE_NOT_INSTALLED;
571   }
572   progress (pdata, 25);
573
574   if (opkg->conf->restrict_to_default_dest)
575   {
576     pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
577                                                            pkg->name,
578                                                            opkg->conf->default_dest);
579   }
580   else
581   {
582     pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
583   }
584
585
586   progress (pdata, 75);
587
588   err = opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
589
590   /* write out status files and file lists */
591   opkg_conf_write_status_files (opkg->conf);
592   pkg_write_changed_filelists (opkg->conf);
593
594
595   progress (pdata, 100);
596   opkg_package_free (pdata.package);
597   return (err) ? OPKG_UNKNOWN_ERROR : OPKG_NO_ERROR;
598 }
599
600 int
601 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
602 {
603   int err;
604   pkg_t *pkg;
605   opkg_progress_data_t pdata;
606
607
608
609   opkg_assert (opkg != NULL);
610   opkg_assert (package_name != NULL);
611
612   pkg_info_preinstall_check (opkg->conf);
613
614   if (opkg->conf->restrict_to_default_dest)
615   {
616     pkg = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
617                                                  package_name,
618                                                  opkg->conf->default_dest);
619     if (pkg == NULL)
620     {
621       /* XXX: Error: Package not installed in default_dest */
622       return OPKG_PACKAGE_NOT_INSTALLED;
623     }
624   }
625   else
626   {
627     pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash,
628                                             package_name);
629   }
630
631   if (!pkg)
632   {
633     /* XXX: Error: Package not installed */
634     return OPKG_PACKAGE_NOT_INSTALLED;
635   }
636
637   pdata.action = OPKG_INSTALL;
638   pdata.package = old_pkg_to_new (pkg);
639   progress (pdata, 0);
640
641   err = opkg_upgrade_pkg (opkg->conf, pkg);
642   /* opkg_upgrade_pkg returns the error codes of opkg_install_pkg */
643   if (err)
644   {
645
646     opkg_package_free (pdata.package);
647     switch (err)
648     {
649       case OPKG_INSTALL_ERR_NOT_TRUSTED: return OPKG_GPG_ERROR;
650       case OPKG_INSTALL_ERR_DOWNLOAD: return OPKG_DOWNLOAD_FAILED;
651       case OPKG_INSTALL_ERR_DEPENDENCIES:
652       case OPKG_INSTALL_ERR_CONFLICTS: return OPKG_DEPENDENCIES_FAILED;
653       case OPKG_INSTALL_ERR_ALREADY_INSTALLED: return OPKG_PACKAGE_ALREADY_INSTALLED;
654       case OPKG_INSTALL_ERR_SIGNATURE: return OPKG_GPG_ERROR;
655       case OPKG_INSTALL_ERR_MD5: return OPKG_MD5_ERROR;
656       default: return OPKG_UNKNOWN_ERROR;
657     }
658   }
659   progress (pdata, 75);
660
661   err = opkg_configure_packages (opkg->conf, NULL);
662   if (err) {
663     opkg_package_free (pdata.package);  
664     return OPKG_UNKNOWN_ERROR;
665   }
666
667   /* write out status files and file lists */
668   opkg_conf_write_status_files (opkg->conf);
669   pkg_write_changed_filelists (opkg->conf);
670
671   progress (pdata, 100);
672   opkg_package_free (pdata.package);
673   return 0;
674 }
675
676 int
677 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
678 {
679   pkg_vec_t *installed;
680   int err = 0;
681   int i;
682   pkg_t *pkg;
683   opkg_progress_data_t pdata;
684
685   pdata.action = OPKG_INSTALL;
686   pdata.package = NULL;
687
688   opkg_assert (opkg != NULL);
689   progress (pdata, 0);
690
691   installed = pkg_vec_alloc ();
692   pkg_info_preinstall_check (opkg->conf);
693
694   pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed);
695   for (i = 0; i < installed->len; i++)
696   {
697     pkg = installed->pkgs[i];
698
699     pdata.package = old_pkg_to_new (pkg);
700     progress (pdata, 99 * i / installed->len);
701     opkg_package_free (pdata.package);
702
703     err += opkg_upgrade_pkg (opkg->conf, pkg);
704   }
705   pkg_vec_free (installed);
706
707   if (err)
708     return 1;
709
710   err = opkg_configure_packages (opkg->conf, NULL);
711   if (err)
712     return 1;
713
714   pdata.package = NULL;
715   progress (pdata, 100);
716   return 0;
717 }
718
719 int
720 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
721 {
722   char *tmp;
723   int err, result = 0;
724   char *lists_dir;
725   pkg_src_list_elt_t *iter;
726   pkg_src_t *src;
727   int sources_list_count, sources_done;
728   opkg_progress_data_t pdata;
729
730   opkg_assert (opkg != NULL);
731
732   pdata.action = OPKG_DOWNLOAD;
733   pdata.package = NULL;
734   progress (pdata, 0);
735
736   sprintf_alloc (&lists_dir, "%s",
737                  (opkg->conf->restrict_to_default_dest)
738                  ? opkg->conf->default_dest->lists_dir
739                  : opkg->conf->lists_dir);
740
741   if (!file_is_dir (lists_dir))
742   {
743     if (file_exists (lists_dir))
744     {
745       /* XXX: Error: file exists but is not a directory */
746       free (lists_dir);
747       return 1;
748     }
749
750     err = file_mkdir_hier (lists_dir, 0755);
751     if (err)
752     {
753       /* XXX: Error: failed to create directory */
754       free (lists_dir);
755       return 1;
756     }
757   }
758
759   tmp = strdup ("/tmp/opkg.XXXXXX");
760
761   if (mkdtemp (tmp) == NULL)
762   {
763     /* XXX: Error: could not create temporary file name */
764     free (lists_dir);
765     free (tmp);
766     return 1;
767   }
768
769   /* count the number of sources so we can give some progress updates */
770   sources_list_count = 0;
771   sources_done = 0;
772   iter = opkg->conf->pkg_src_list.head;
773   while (iter)
774   {
775     sources_list_count++;
776     iter = iter->next;
777   }
778
779   for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
780   {
781     char *url, *list_file_name = NULL;
782
783     src = iter->data;
784
785     if (src->extra_data)  /* debian style? */
786       sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
787                      src->gzip ? "Packages.gz" : "Packages");
788     else
789       sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
790
791     sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
792     if (src->gzip)
793     {
794       FILE *in, *out;
795       struct _curl_cb_data cb_data;
796       char *tmp_file_name = NULL;
797
798       sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
799
800       /* XXX: Note: downloading url */
801
802       cb_data.cb = progress_callback;
803       cb_data.progress_data = &pdata;
804       cb_data.opkg = opkg;
805       cb_data.user_data = user_data;
806       cb_data.start_range = 100 * sources_done / sources_list_count;
807       cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
808
809       err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
810
811       if (err == 0)
812       {
813         /* XXX: Note: Inflating downloaded file */
814         in = fopen (tmp_file_name, "r");
815         out = fopen (list_file_name, "w");
816         if (in && out)
817           unzip (in, out);
818         else
819           err = 1;
820         if (in)
821           fclose (in);
822         if (out)
823           fclose (out);
824         unlink (tmp_file_name);
825       }
826       free (tmp_file_name);
827     }
828     else
829       err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
830
831     if (err)
832     {
833       /* XXX: Error: download error */
834       result = OPKG_DOWNLOAD_FAILED;
835     }
836     free (url);
837
838 #ifdef HAVE_GPGME
839     char *sig_file_name;
840     /* download detached signitures to verify the package lists */
841     /* get the url for the sig file */
842     if (src->extra_data)  /* debian style? */
843       sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
844                      "Packages.sig");
845     else
846       sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
847
848     /* create filename for signature */
849     sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, src->name);
850
851     /* make sure there is no existing signature file */
852     unlink (sig_file_name);
853
854     err = opkg_download (opkg->conf, url, sig_file_name, NULL, NULL);
855     if (err)
856     {
857       /* XXX: Warning: Download failed */
858     }
859     else
860     {
861       int err;
862       err = opkg_verify_file (opkg->conf, list_file_name, sig_file_name);
863       if (err == 0)
864       {
865         /* XXX: Notice: Signature check passed */
866       }
867       else
868       {
869         /* XXX: Warning: Signature check failed */
870       }
871     }
872     free (sig_file_name);
873     free (list_file_name);
874     free (url);
875 #else
876     /* XXX: Note: Signiture check for %s skipped because GPG support was not
877      * enabled in this build
878      */
879 #endif
880
881     sources_done++;
882     progress (pdata, 100 * sources_done / sources_list_count);
883   }
884
885   rmdir (tmp);
886   free (tmp);
887   free (lists_dir);
888
889   /* Now re-read the package lists to update package hash tables. */
890   opkg_re_read_config_files (opkg);
891
892   return result;
893 }
894
895
896 int
897 opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
898 {
899   pkg_vec_t *all;
900   int i;
901
902   opkg_assert (opkg);
903   opkg_assert (callback);
904
905   all = pkg_vec_alloc ();
906   pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
907   for (i = 0; i < all->len; i++)
908   {
909     pkg_t *pkg;
910     opkg_package_t *package;
911
912     pkg = all->pkgs[i];
913
914     package = old_pkg_to_new (pkg);
915     callback (opkg, package, user_data);
916     opkg_package_free (package);
917   }
918
919   pkg_vec_free (all);
920
921   return 0;
922 }
923
924 int
925 opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
926 {
927   pkg_vec_t *all;
928   int i;
929
930   opkg_assert (opkg);
931   opkg_assert (callback);
932
933   /* ensure all data is valid */
934   pkg_info_preinstall_check (opkg->conf);
935
936   all = pkg_vec_alloc ();
937   pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
938   for (i = 0; i < all->len; i++)
939   {
940     pkg_t *old, *new;
941     int cmp;
942     opkg_package_t *package;
943
944     old = all->pkgs[i];
945     
946     if (old->state_status != SS_INSTALLED)
947       continue;
948
949     new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, old->name, NULL);
950     if (new == NULL) {
951       /* XXX: Notice: Assuming locally install package is up to date */
952       continue;
953     }
954           
955     cmp = pkg_compare_versions(old, new);
956
957     if (cmp < 0)
958     {
959       package = old_pkg_to_new (new);
960       callback (opkg, package, user_data);
961       opkg_package_free (package);
962     }
963   }
964
965   pkg_vec_free (all);
966
967   return 0;
968 }
969
970 opkg_package_t*
971 opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *arch, const char *repo)
972 {
973   pkg_vec_t *all;
974   opkg_package_t *package = NULL;
975   int i;
976 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
977
978   opkg_assert (opkg);
979
980   all = pkg_vec_alloc ();
981   pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
982   for (i = 0; i < all->len; i++)
983   {
984     pkg_t *pkg;
985     char *pkgv;
986
987     pkg = all->pkgs[i];
988
989     /* check name */
990     if (sstrcmp (pkg->name, name))
991       continue;
992     
993     /* check version */
994     pkgv = pkg_version_str_alloc (pkg);
995     if (sstrcmp (pkgv, ver))
996     {
997       free (pkgv);
998       continue;
999     }
1000     free (pkgv);
1001
1002     /* check architecture */
1003     if (arch)
1004     {
1005       if (sstrcmp (pkg->architecture, arch))
1006         continue;
1007     }
1008
1009     /* check repository */
1010     if (repo)
1011     {
1012       if (sstrcmp (pkg->src->name, repo))
1013           continue;
1014     }
1015
1016     /* match found */
1017     package = old_pkg_to_new (pkg);
1018     break;
1019   }
1020
1021   pkg_vec_free (all);
1022
1023   return package;
1024 }
1025
1026 #include <curl/curl.h>
1027 /**
1028  * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status. 
1029  * @param opkg The opkg_t
1030  * @return return how many repositories cannot access. 0 means all okay. 
1031  */ 
1032 int opkg_repository_accessibility_check(opkg_t *opkg) 
1033 {
1034   pkg_src_list_elt_t *iter;
1035   str_list_elt_t *iter1;
1036   str_list_t *src;
1037   int repositories=0;
1038   int ret=0;
1039   int err;
1040   char *repo_ptr;
1041   char *stmp;
1042   opkg_assert(opkg != NULL);
1043
1044   src = str_list_alloc();
1045
1046   for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next) 
1047   {
1048     if (strstr(iter->data->value, "://") && 
1049                     index(strstr(iter->data->value, "://") + 3, '/')) 
1050       stmp = strndup(iter->data->value, 
1051                       (index(strstr(iter->data->value, "://") + 3, '/') - iter->data->value)*sizeof(char));
1052
1053     else
1054       stmp = strdup(iter->data->value);
1055
1056     for (iter1 = src->head; iter1; iter1 = iter1->next)
1057     {
1058       if (strstr(iter1->data, stmp)) 
1059         break;
1060     }
1061     if (iter1)
1062       continue;
1063
1064     sprintf_alloc(&repo_ptr, "%s/index.html",stmp);
1065     free(stmp);
1066
1067     str_list_append(src, repo_ptr);
1068     repositories++;
1069   }
1070   while (repositories > 0) 
1071   {
1072     iter1 = str_list_pop(src);
1073     repositories--;
1074
1075     err = opkg_download(opkg->conf, iter1->data, "/dev/null", NULL, NULL);
1076     if (!(err == CURLE_OK || 
1077                 err == CURLE_HTTP_RETURNED_ERROR || 
1078                 err == CURLE_FILE_COULDNT_READ_FILE ||
1079                 err == CURLE_REMOTE_FILE_NOT_FOUND || 
1080                 err == CURLE_TFTP_NOTFOUND
1081                 )) {
1082             ret++;
1083     }
1084     str_list_elt_deinit(iter1);
1085     free(iter1);
1086   }
1087   free(src);
1088   return ret;
1089 }