atexit() isn't really appropriate for a library.
[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 #ifdef HAVE_CURL
215   opkg_curl_cleanup();
216 #endif
217   opkg_conf_deinit (opkg->conf);
218   args_deinit (opkg->args);
219   free (opkg->options);
220   free (opkg->args);
221   free (opkg->conf);
222   free (opkg);
223 }
224
225 int
226 opkg_re_read_config_files (opkg_t *opkg)
227 {
228   args_t *a;
229   opkg_conf_t *c;
230
231   opkg_assert (opkg != NULL);
232
233   a = opkg->args;
234   c = opkg->conf;
235
236   /* Unfortunatly, the easiest way to re-read the config files right now is to
237    * throw away opkg->conf and start again */
238
239   /* copy the settings we need to keep */
240   a->autoremove = c->autoremove;
241   a->force_depends = c->force_depends;
242   a->force_defaults = c->force_defaults;
243   a->force_maintainer = c->force_maintainer;
244   a->force_overwrite = c->force_overwrite;
245   a->force_downgrade = c->force_downgrade;
246   a->force_reinstall = c->force_reinstall;
247   a->force_removal_of_dependent_packages = c->force_removal_of_dependent_packages;
248   a->force_removal_of_essential_packages = c->force_removal_of_essential_packages;
249   a->nodeps = c->nodeps;
250   a->noaction = c->noaction;
251   a->query_all = c->query_all;
252   a->verbosity = c->verbosity;
253
254   if (c->offline_root)
255   {
256     if (a->offline_root) free (a->offline_root);
257     a->offline_root = xstrdup(c->offline_root);
258   }
259
260   if (c->offline_root_pre_script_cmd)
261   {
262     if (a->offline_root_pre_script_cmd) free (a->offline_root_pre_script_cmd);
263     a->offline_root_pre_script_cmd = xstrdup(c->offline_root_pre_script_cmd);
264   }
265
266   if (c->offline_root_post_script_cmd)
267   {
268     if (a->offline_root_post_script_cmd) free (a->offline_root_post_script_cmd);
269     a->offline_root_post_script_cmd = xstrdup(c->offline_root_post_script_cmd);
270   }
271
272   if (c->cache) {
273     if (a->cache)
274         free (a->cache);
275     a->cache = xstrdup(c->cache);
276   }
277
278   /* throw away old opkg_conf and start again */
279   opkg_conf_deinit (opkg->conf);
280   opkg_conf_init (opkg->conf, opkg->args);
281
282   free (opkg->options);
283   opkg_init_options_array (opkg->conf, &opkg->options);
284
285   return 0;
286 }
287
288 void
289 opkg_get_option (opkg_t *opkg, char *option, void **value)
290 {
291   int i = 0;
292   opkg_option_t *options;
293
294   opkg_assert (opkg != NULL);
295   opkg_assert (option != NULL);
296   opkg_assert (value != NULL);
297
298   options = opkg->options;
299
300   /* look up the option
301    * TODO: this would be much better as a hash table
302    */
303   while (options[i].name)
304   {
305     if (strcmp (options[i].name, option) != 0)
306     {
307       i++;
308       continue;
309     }
310   }
311
312   /* get the option */
313   switch (options[i].type)
314   {
315   case OPKG_OPT_TYPE_BOOL:
316     *((int *) value) = *((int *) options[i].value);
317     return;
318
319   case OPKG_OPT_TYPE_INT:
320     *((int *) value) = *((int *) options[i].value);
321     return;
322
323   case OPKG_OPT_TYPE_STRING:
324     *((char **)value) = xstrdup(options[i].value);
325     return;
326   }
327
328 }
329
330 void
331 opkg_set_option (opkg_t *opkg, char *option, void *value)
332 {
333   int i = 0, found = 0;
334   opkg_option_t *options;
335
336   opkg_assert (opkg != NULL);
337   opkg_assert (option != NULL);
338   opkg_assert (value != NULL);
339
340   options = opkg->options;
341
342   /* look up the option
343    * TODO: this would be much better as a hash table
344    */
345   while (options[i].name)
346   {
347     if (strcmp (options[i].name, option) == 0)
348     {
349       found = 1;
350       break;
351     }
352     i++;
353   }
354
355   if (!found)
356   {
357     /* XXX: Warning: Option not found */
358     return;
359   }
360
361   /* set the option */
362   switch (options[i].type)
363   {
364   case OPKG_OPT_TYPE_BOOL:
365     if (*((int *) value) == 0)
366       *((int *)options[i].value) = 0;
367     else
368       *((int *)options[i].value) = 1;
369     return;
370
371   case OPKG_OPT_TYPE_INT:
372     *((int *) options[i].value) = *((int *) value);
373     return;
374
375   case OPKG_OPT_TYPE_STRING:
376     *((char **)options[i].value) = xstrdup(value);
377     return;
378   }
379
380 }
381
382 /**
383  * @brief libopkg API: Install package
384  * @param opkg Then opkg handler
385  * @param package_name The name of package in which is going to install
386  * @param progress_callback The callback function that report the status to caller. 
387  */ 
388 int
389 opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
390 {
391   int err;
392   char *stripped_filename;
393   opkg_progress_data_t pdata;
394   pkg_t *old, *new;
395   pkg_vec_t *deps, *all;
396   int i, ndepends;
397   char **unresolved = NULL;
398
399   opkg_assert (opkg != NULL);
400   opkg_assert (package_name != NULL);
401
402   /* ... */
403   pkg_info_preinstall_check (opkg->conf);
404
405
406   /* check to ensure package is not already installed */
407   old = pkg_hash_fetch_installed_by_name(&opkg->conf->pkg_hash, package_name);
408   if (old)
409   {
410     /* XXX: Error: Package is already installed. */
411     return OPKG_PACKAGE_ALREADY_INSTALLED;
412   }
413
414   new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, package_name, NULL);
415   if (!new)
416   {
417     /* XXX: Error: Could not find package to install */
418     return OPKG_PACKAGE_NOT_FOUND;
419   }
420
421   new->state_flag |= SF_USER;
422
423   pdata.action = OPKG_INSTALL;
424   pdata.package = pkg_t_to_opkg_package_t (new);
425
426   progress (pdata, 0);
427
428   /* find dependancies and download them */
429   deps = pkg_vec_alloc ();
430   /* this function does not return the original package, so we insert it later */
431   ndepends = pkg_hash_fetch_unsatisfied_dependencies (opkg->conf, new, deps, &unresolved);
432   if (unresolved)
433   {
434     /* XXX: Error: Could not satisfy dependencies */
435     pkg_vec_free (deps);
436     return OPKG_DEPENDENCIES_FAILED;
437   }
438
439   /* insert the package we are installing so that we download it */
440   pkg_vec_insert (deps, new);
441
442   /* download package and dependancies */
443   for (i = 0; i < deps->len; i++)
444   {
445     pkg_t *pkg;
446     struct _curl_cb_data cb_data;
447     char *url;
448
449     pkg = deps->pkgs[i];
450     if (pkg->local_filename)
451       continue;
452
453     opkg_package_free (pdata.package);
454     pdata.package = pkg_t_to_opkg_package_t (pkg);
455     pdata.action = OPKG_DOWNLOAD;
456
457     if (pkg->src == NULL)
458     {
459       /* XXX: Error: Package not available from any configured src */
460       return OPKG_PACKAGE_NOT_AVAILABLE;
461     }
462
463     sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
464
465     /* Get the filename part, without any directory */
466     stripped_filename = strrchr(pkg->filename, '/');
467     if ( ! stripped_filename )
468         stripped_filename = pkg->filename;
469
470     sprintf_alloc(&pkg->local_filename, "%s/%s", opkg->conf->tmp_dir, stripped_filename);
471
472     cb_data.cb = progress_callback;
473     cb_data.progress_data = &pdata;
474     cb_data.opkg = opkg;
475     cb_data.user_data = user_data;
476     /* 75% of "install" progress is for downloading */
477     cb_data.start_range = 75 * i / deps->len;
478     cb_data.finish_range = 75 * (i + 1) / deps->len;
479
480     err = opkg_download(opkg->conf, url, pkg->local_filename,
481               (curl_progress_func) curl_progress_cb, &cb_data);
482     free(url);
483
484     if (err)
485     {
486       pkg_vec_free (deps);
487       opkg_package_free (pdata.package);
488       return OPKG_DOWNLOAD_FAILED;
489     }
490
491   }
492   pkg_vec_free (deps);
493
494   /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
495   all = pkg_vec_alloc ();
496   pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
497   for (i = 0; i < all->len; i++)
498   {
499     all->pkgs[i]->parent->dependencies_checked = 0;
500   }
501   pkg_vec_free (all);
502
503
504   /* 75% of "install" progress is for downloading */
505   opkg_package_free (pdata.package);
506   pdata.package = pkg_t_to_opkg_package_t (new);
507   pdata.action = OPKG_INSTALL;
508   progress (pdata, 75);
509
510   /* unpack the package */
511   err = opkg_install_pkg(opkg->conf, new, 0);
512
513   if (err)
514   {
515     opkg_package_free (pdata.package);
516     switch (err)
517     {
518       case OPKG_INSTALL_ERR_NOT_TRUSTED: return OPKG_GPG_ERROR;
519       case OPKG_INSTALL_ERR_DOWNLOAD: return OPKG_DOWNLOAD_FAILED;
520       case OPKG_INSTALL_ERR_DEPENDENCIES:
521       case OPKG_INSTALL_ERR_CONFLICTS: return OPKG_DEPENDENCIES_FAILED;
522       case OPKG_INSTALL_ERR_ALREADY_INSTALLED: return OPKG_PACKAGE_ALREADY_INSTALLED;
523       case OPKG_INSTALL_ERR_SIGNATURE: return OPKG_GPG_ERROR;
524       case OPKG_INSTALL_ERR_MD5: return OPKG_MD5_ERROR;
525       case OPKG_INSTALL_ERR_SHA256: return OPKG_SHA256_ERROR;
526       default: return OPKG_UNKNOWN_ERROR;
527     }
528   }
529
530   progress (pdata, 75);
531
532   /* run configure scripts, etc. */
533   err = opkg_configure_packages (opkg->conf, NULL);
534   if (err)
535   {
536     opkg_package_free (pdata.package);
537     return OPKG_UNKNOWN_ERROR;
538   }
539
540   /* write out status files and file lists */
541   opkg_conf_write_status_files (opkg->conf);
542   pkg_write_changed_filelists (opkg->conf);
543
544   progress (pdata, 100);
545   opkg_package_free (pdata.package);
546   return 0;
547 }
548
549 int
550 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
551 {
552   int err;
553   pkg_t *pkg = NULL;
554   pkg_t *pkg_to_remove;
555   opkg_progress_data_t pdata;
556
557   opkg_assert (opkg != NULL);
558   opkg_assert (package_name != NULL);
559
560   pkg_info_preinstall_check (opkg->conf);
561
562   pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
563
564   if (pkg == NULL)
565   {
566     /* XXX: Error: Package not installed. */
567     return OPKG_PACKAGE_NOT_INSTALLED;
568   }
569
570   pdata.action = OPKG_REMOVE;
571   pdata.package = pkg_t_to_opkg_package_t (pkg);
572   progress (pdata, 0);
573
574
575   if (pkg->state_status == SS_NOT_INSTALLED)
576   {
577     /* XXX:  Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
578     opkg_package_free (pdata.package);
579     return OPKG_PACKAGE_NOT_INSTALLED;
580   }
581   progress (pdata, 25);
582
583   if (opkg->conf->restrict_to_default_dest)
584   {
585     pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
586                                                            pkg->name,
587                                                            opkg->conf->default_dest);
588   }
589   else
590   {
591     pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
592   }
593
594
595   progress (pdata, 75);
596
597   err = opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
598
599   /* write out status files and file lists */
600   opkg_conf_write_status_files (opkg->conf);
601   pkg_write_changed_filelists (opkg->conf);
602
603
604   progress (pdata, 100);
605   opkg_package_free (pdata.package);
606   return (err) ? OPKG_UNKNOWN_ERROR : OPKG_NO_ERROR;
607 }
608
609 int
610 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
611 {
612   int err;
613   pkg_t *pkg;
614   opkg_progress_data_t pdata;
615
616
617
618   opkg_assert (opkg != NULL);
619   opkg_assert (package_name != NULL);
620
621   pkg_info_preinstall_check (opkg->conf);
622
623   if (opkg->conf->restrict_to_default_dest)
624   {
625     pkg = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
626                                                  package_name,
627                                                  opkg->conf->default_dest);
628     if (pkg == NULL)
629     {
630       /* XXX: Error: Package not installed in default_dest */
631       return OPKG_PACKAGE_NOT_INSTALLED;
632     }
633   }
634   else
635   {
636     pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash,
637                                             package_name);
638   }
639
640   if (!pkg)
641   {
642     /* XXX: Error: Package not installed */
643     return OPKG_PACKAGE_NOT_INSTALLED;
644   }
645
646   pdata.action = OPKG_INSTALL;
647   pdata.package = pkg_t_to_opkg_package_t (pkg);
648   progress (pdata, 0);
649
650   err = opkg_upgrade_pkg (opkg->conf, pkg);
651   /* opkg_upgrade_pkg returns the error codes of opkg_install_pkg */
652   if (err)
653   {
654
655     opkg_package_free (pdata.package);
656     switch (err)
657     {
658       case OPKG_INSTALL_ERR_NOT_TRUSTED: return OPKG_GPG_ERROR;
659       case OPKG_INSTALL_ERR_DOWNLOAD: return OPKG_DOWNLOAD_FAILED;
660       case OPKG_INSTALL_ERR_DEPENDENCIES:
661       case OPKG_INSTALL_ERR_CONFLICTS: return OPKG_DEPENDENCIES_FAILED;
662       case OPKG_INSTALL_ERR_ALREADY_INSTALLED: return OPKG_PACKAGE_ALREADY_INSTALLED;
663       case OPKG_INSTALL_ERR_SIGNATURE: return OPKG_GPG_ERROR;
664       case OPKG_INSTALL_ERR_MD5: return OPKG_MD5_ERROR;
665       case OPKG_INSTALL_ERR_SHA256: return OPKG_SHA256_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_t_to_opkg_package_t (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 = xstrdup("/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   list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
783   {
784     sources_list_count++;
785   }
786
787   list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
788   {
789     char *url, *list_file_name = NULL;
790
791     src = (pkg_src_t *)iter->data;
792
793     if (src->extra_data)  /* debian style? */
794       sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
795                      src->gzip ? "Packages.gz" : "Packages");
796     else
797       sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
798
799     sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
800     if (src->gzip)
801     {
802       FILE *in, *out;
803       struct _curl_cb_data cb_data;
804       char *tmp_file_name = NULL;
805
806       sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
807
808       /* XXX: Note: downloading url */
809
810       cb_data.cb = progress_callback;
811       cb_data.progress_data = &pdata;
812       cb_data.opkg = opkg;
813       cb_data.user_data = user_data;
814       cb_data.start_range = 100 * sources_done / sources_list_count;
815       cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
816
817       err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
818
819       if (err == 0)
820       {
821         /* XXX: Note: Inflating downloaded file */
822         in = fopen (tmp_file_name, "r");
823         out = fopen (list_file_name, "w");
824         if (in && out)
825           unzip (in, out);
826         else
827           err = 1;
828         if (in)
829           fclose (in);
830         if (out)
831           fclose (out);
832         unlink (tmp_file_name);
833       }
834       free (tmp_file_name);
835     }
836     else
837       err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
838
839     if (err)
840     {
841       /* XXX: Error: download error */
842       result = OPKG_DOWNLOAD_FAILED;
843     }
844     free (url);
845
846 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
847     if ( opkg->conf->check_signature ) {
848         char *sig_file_name;
849         /* download detached signitures to verify the package lists */
850         /* get the url for the sig file */
851         if (src->extra_data)  /* debian style? */
852             sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
853                     "Packages.sig");
854         else
855             sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
856
857         /* create filename for signature */
858         sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, src->name);
859
860         /* make sure there is no existing signature file */
861         unlink (sig_file_name);
862
863         err = opkg_download (opkg->conf, url, sig_file_name, NULL, NULL);
864         if (err)
865         {
866             /* XXX: Warning: Download failed */
867         }
868         else
869         {
870             int err;
871             err = opkg_verify_file (opkg->conf, list_file_name, sig_file_name);
872             if (err == 0)
873             {
874                 /* XXX: Notice: Signature check passed */
875             }
876             else
877             {
878                 /* XXX: Warning: Signature check failed */
879             }
880         }
881         free (sig_file_name);
882         free (list_file_name);
883         free (url);
884     }
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_t_to_opkg_package_t (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     struct active_list *head;
938     struct active_list *node;
939     pkg_t *old=NULL, *new = NULL;
940     static opkg_package_t* package=NULL;
941
942
943     opkg_assert (opkg);
944     opkg_assert (callback);
945
946     /* ensure all data is valid */
947     pkg_info_preinstall_check (opkg->conf);
948
949     head  =  prepare_upgrade_list(opkg->conf);
950     for (node=active_list_next(head, head); node; active_list_next(head,node)) {
951         old = list_entry(node, pkg_t, list);
952         new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, old->name, NULL);
953         package = pkg_t_to_opkg_package_t (new);
954         callback (opkg, package, user_data);
955         opkg_package_free (package);
956     }
957     active_list_head_delete(head);
958     return 0;
959 }
960
961 opkg_package_t*
962 opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *arch, const char *repo)
963 {
964   pkg_vec_t *all;
965   opkg_package_t *package = NULL;
966   int i;
967 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
968
969   opkg_assert (opkg);
970
971   all = pkg_vec_alloc ();
972   pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
973   for (i = 0; i < all->len; i++)
974   {
975     pkg_t *pkg;
976     char *pkgv;
977
978     pkg = all->pkgs[i];
979
980     /* check name */
981     if (sstrcmp (pkg->name, name))
982       continue;
983     
984     /* check version */
985     pkgv = pkg_version_str_alloc (pkg);
986     if (sstrcmp (pkgv, ver))
987     {
988       free (pkgv);
989       continue;
990     }
991     free (pkgv);
992
993     /* check architecture */
994     if (arch)
995     {
996       if (sstrcmp (pkg->architecture, arch))
997         continue;
998     }
999
1000     /* check repository */
1001     if (repo)
1002     {
1003       if (sstrcmp (pkg->src->name, repo))
1004           continue;
1005     }
1006
1007     /* match found */
1008     package = pkg_t_to_opkg_package_t (pkg);
1009     break;
1010   }
1011
1012   pkg_vec_free (all);
1013
1014   return package;
1015 }
1016
1017 #ifdef HAVE_CURL
1018 #include <curl/curl.h>
1019 #endif
1020 /**
1021  * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status. 
1022  * @param opkg The opkg_t
1023  * @return return how many repositories cannot access. 0 means all okay. 
1024  */ 
1025 int opkg_repository_accessibility_check(opkg_t *opkg) 
1026 {
1027   pkg_src_list_elt_t *iter;
1028   str_list_elt_t *iter1;
1029   str_list_t *src;
1030   int repositories=0;
1031   int ret=0;
1032   int err;
1033   char *repo_ptr;
1034   char *stmp;
1035   opkg_assert(opkg != NULL);
1036
1037   src = str_list_alloc();
1038
1039   list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
1040   {
1041     if (strstr(((pkg_src_t *)iter->data)->value, "://") && 
1042                     index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/')) 
1043       stmp = xstrndup(((pkg_src_t *)iter->data)->value, 
1044                       (index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/') - ((pkg_src_t *)iter->data)->value)*sizeof(char));
1045
1046     else
1047       stmp = xstrdup(((pkg_src_t *)iter->data)->value);
1048
1049     for (iter1 = str_list_first(src); iter1; iter1 = str_list_next(src, iter1))
1050     {
1051       if (strstr(iter1->data, stmp)) 
1052         break;
1053     }
1054     if (iter1)
1055       continue;
1056
1057     sprintf_alloc(&repo_ptr, "%s/index.html",stmp);
1058     free(stmp);
1059
1060     str_list_append(src, repo_ptr);
1061     free(repo_ptr);
1062     repositories++;
1063   }
1064   while (repositories > 0) 
1065   {
1066     iter1 = str_list_pop(src);
1067     repositories--;
1068
1069     err = opkg_download(opkg->conf, iter1->data, "/dev/null", NULL, NULL);
1070 #ifdef HAVE_CURL
1071     if (!(err == CURLE_OK || 
1072                 err == CURLE_HTTP_RETURNED_ERROR || 
1073                 err == CURLE_FILE_COULDNT_READ_FILE ||
1074                 err == CURLE_REMOTE_FILE_NOT_FOUND || 
1075                 err == CURLE_TFTP_NOTFOUND
1076                 )) {
1077 #else
1078     if (!(err == 0)) {
1079 #endif
1080             ret++;
1081     }
1082     str_list_elt_deinit(iter1);
1083   }
1084   free(src);
1085   return ret;
1086 }