Remove unused parameter from pkg_hash_fetch_best_installation_candidate().
[project/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);
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 return OPKG_UNKNOWN_ERROR;
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 = pkg_t_to_opkg_package_t (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 = pkg_t_to_opkg_package_t (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 opkg_package_free (pdata.package);
644 return OPKG_UNKNOWN_ERROR;
645 }
646 progress (pdata, 75);
647
648 err = opkg_configure_packages (opkg->conf, NULL);
649 if (err) {
650 opkg_package_free (pdata.package);
651 return OPKG_UNKNOWN_ERROR;
652 }
653
654 /* write out status files and file lists */
655 opkg_conf_write_status_files (opkg->conf);
656 pkg_write_changed_filelists (opkg->conf);
657
658 progress (pdata, 100);
659 opkg_package_free (pdata.package);
660 return 0;
661 }
662
663 int
664 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
665 {
666 pkg_vec_t *installed;
667 int err = 0;
668 int i;
669 pkg_t *pkg;
670 opkg_progress_data_t pdata;
671
672 pdata.action = OPKG_INSTALL;
673 pdata.package = NULL;
674
675 opkg_assert (opkg != NULL);
676 progress (pdata, 0);
677
678 installed = pkg_vec_alloc ();
679 pkg_info_preinstall_check (opkg->conf);
680
681 pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed);
682 for (i = 0; i < installed->len; i++)
683 {
684 pkg = installed->pkgs[i];
685
686 pdata.package = pkg_t_to_opkg_package_t (pkg);
687 progress (pdata, 99 * i / installed->len);
688 opkg_package_free (pdata.package);
689
690 err += opkg_upgrade_pkg (opkg->conf, pkg);
691 }
692 pkg_vec_free (installed);
693
694 if (err)
695 return 1;
696
697 err = opkg_configure_packages (opkg->conf, NULL);
698 if (err)
699 return 1;
700
701 pdata.package = NULL;
702 progress (pdata, 100);
703 return 0;
704 }
705
706 int
707 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
708 {
709 char *tmp;
710 int err, result = 0;
711 char *lists_dir;
712 pkg_src_list_elt_t *iter;
713 pkg_src_t *src;
714 int sources_list_count, sources_done;
715 opkg_progress_data_t pdata;
716
717 opkg_assert (opkg != NULL);
718
719 pdata.action = OPKG_DOWNLOAD;
720 pdata.package = NULL;
721 progress (pdata, 0);
722
723 sprintf_alloc (&lists_dir, "%s",
724 (opkg->conf->restrict_to_default_dest)
725 ? opkg->conf->default_dest->lists_dir
726 : opkg->conf->lists_dir);
727
728 if (!file_is_dir (lists_dir))
729 {
730 if (file_exists (lists_dir))
731 {
732 /* XXX: Error: file exists but is not a directory */
733 free (lists_dir);
734 return 1;
735 }
736
737 err = file_mkdir_hier (lists_dir, 0755);
738 if (err)
739 {
740 /* XXX: Error: failed to create directory */
741 free (lists_dir);
742 return 1;
743 }
744 }
745
746 sprintf_alloc(&tmp, "%s/update-XXXXXX", opkg->conf->tmp_dir);
747 if (mkdtemp (tmp) == NULL) {
748 /* XXX: Error: could not create temporary file name */
749 free (lists_dir);
750 free (tmp);
751 return 1;
752 }
753
754 /* count the number of sources so we can give some progress updates */
755 sources_list_count = 0;
756 sources_done = 0;
757 list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
758 {
759 sources_list_count++;
760 }
761
762 list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
763 {
764 char *url, *list_file_name = NULL;
765
766 src = (pkg_src_t *)iter->data;
767
768 if (src->extra_data) /* debian style? */
769 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
770 src->gzip ? "Packages.gz" : "Packages");
771 else
772 sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
773
774 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
775 if (src->gzip)
776 {
777 FILE *in, *out;
778 struct _curl_cb_data cb_data;
779 char *tmp_file_name = NULL;
780
781 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
782
783 /* XXX: Note: downloading url */
784
785 cb_data.cb = progress_callback;
786 cb_data.progress_data = &pdata;
787 cb_data.opkg = opkg;
788 cb_data.user_data = user_data;
789 cb_data.start_range = 100 * sources_done / sources_list_count;
790 cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
791
792 err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
793
794 if (err == 0)
795 {
796 /* XXX: Note: Inflating downloaded file */
797 in = fopen (tmp_file_name, "r");
798 out = fopen (list_file_name, "w");
799 if (in && out)
800 unzip (in, out);
801 else
802 err = 1;
803 if (in)
804 fclose (in);
805 if (out)
806 fclose (out);
807 unlink (tmp_file_name);
808 }
809 free (tmp_file_name);
810 }
811 else
812 err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
813
814 if (err)
815 {
816 /* XXX: Error: download error */
817 result = OPKG_DOWNLOAD_FAILED;
818 }
819 free (url);
820
821 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
822 if ( opkg->conf->check_signature ) {
823 char *sig_file_name;
824 /* download detached signitures to verify the package lists */
825 /* get the url for the sig file */
826 if (src->extra_data) /* debian style? */
827 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
828 "Packages.sig");
829 else
830 sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
831
832 /* create filename for signature */
833 sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, src->name);
834
835 /* make sure there is no existing signature file */
836 unlink (sig_file_name);
837
838 err = opkg_download (opkg->conf, url, sig_file_name, NULL, NULL);
839 if (err)
840 {
841 /* XXX: Warning: Download failed */
842 }
843 else
844 {
845 int err;
846 err = opkg_verify_file (opkg->conf, list_file_name, sig_file_name);
847 if (err == 0)
848 {
849 /* XXX: Notice: Signature check passed */
850 }
851 else
852 {
853 /* XXX: Warning: Signature check failed */
854 }
855 }
856 free (sig_file_name);
857 free (list_file_name);
858 free (url);
859 }
860 #else
861 /* XXX: Note: Signature check for %s skipped because GPG support was not
862 * enabled in this build
863 */
864 #endif
865
866 sources_done++;
867 progress (pdata, 100 * sources_done / sources_list_count);
868 }
869
870 rmdir (tmp);
871 free (tmp);
872 free (lists_dir);
873
874 /* Now re-read the package lists to update package hash tables. */
875 opkg_re_read_config_files (opkg);
876
877 return result;
878 }
879
880
881 int
882 opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
883 {
884 pkg_vec_t *all;
885 int i;
886
887 opkg_assert (opkg);
888 opkg_assert (callback);
889
890 all = pkg_vec_alloc ();
891 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
892 for (i = 0; i < all->len; i++)
893 {
894 pkg_t *pkg;
895 opkg_package_t *package;
896
897 pkg = all->pkgs[i];
898
899 package = pkg_t_to_opkg_package_t (pkg);
900 callback (opkg, package, user_data);
901 opkg_package_free (package);
902 }
903
904 pkg_vec_free (all);
905
906 return 0;
907 }
908
909 int
910 opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
911 {
912 struct active_list *head;
913 struct active_list *node;
914 pkg_t *old=NULL, *new = NULL;
915 static opkg_package_t* package=NULL;
916
917
918 opkg_assert (opkg);
919 opkg_assert (callback);
920
921 /* ensure all data is valid */
922 pkg_info_preinstall_check (opkg->conf);
923
924 head = prepare_upgrade_list(opkg->conf);
925 for (node=active_list_next(head, head); node; active_list_next(head,node)) {
926 old = list_entry(node, pkg_t, list);
927 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, old->name);
928 if (new == NULL)
929 continue;
930 package = pkg_t_to_opkg_package_t (new);
931 callback (opkg, package, user_data);
932 opkg_package_free (package);
933 }
934 active_list_head_delete(head);
935 return 0;
936 }
937
938 opkg_package_t*
939 opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *arch, const char *repo)
940 {
941 pkg_vec_t *all;
942 opkg_package_t *package = NULL;
943 int i;
944 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
945
946 opkg_assert (opkg);
947
948 all = pkg_vec_alloc ();
949 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
950 for (i = 0; i < all->len; i++)
951 {
952 pkg_t *pkg;
953 char *pkgv;
954
955 pkg = all->pkgs[i];
956
957 /* check name */
958 if (sstrcmp (pkg->name, name))
959 continue;
960
961 /* check version */
962 pkgv = pkg_version_str_alloc (pkg);
963 if (sstrcmp (pkgv, ver))
964 {
965 free (pkgv);
966 continue;
967 }
968 free (pkgv);
969
970 /* check architecture */
971 if (arch)
972 {
973 if (sstrcmp (pkg->architecture, arch))
974 continue;
975 }
976
977 /* check repository */
978 if (repo)
979 {
980 if (sstrcmp (pkg->src->name, repo))
981 continue;
982 }
983
984 /* match found */
985 package = pkg_t_to_opkg_package_t (pkg);
986 break;
987 }
988
989 pkg_vec_free (all);
990
991 return package;
992 }
993
994 #ifdef HAVE_CURL
995 #include <curl/curl.h>
996 #endif
997 /**
998 * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status.
999 * @param opkg The opkg_t
1000 * @return return how many repositories cannot access. 0 means all okay.
1001 */
1002 int opkg_repository_accessibility_check(opkg_t *opkg)
1003 {
1004 pkg_src_list_elt_t *iter;
1005 str_list_elt_t *iter1;
1006 str_list_t *src;
1007 int repositories=0;
1008 int ret=0;
1009 int err;
1010 char *repo_ptr;
1011 char *stmp;
1012 opkg_assert(opkg != NULL);
1013
1014 src = str_list_alloc();
1015
1016 list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
1017 {
1018 if (strstr(((pkg_src_t *)iter->data)->value, "://") &&
1019 index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/'))
1020 stmp = xstrndup(((pkg_src_t *)iter->data)->value,
1021 (index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/') - ((pkg_src_t *)iter->data)->value)*sizeof(char));
1022
1023 else
1024 stmp = xstrdup(((pkg_src_t *)iter->data)->value);
1025
1026 for (iter1 = str_list_first(src); iter1; iter1 = str_list_next(src, iter1))
1027 {
1028 if (strstr(iter1->data, stmp))
1029 break;
1030 }
1031 if (iter1)
1032 continue;
1033
1034 sprintf_alloc(&repo_ptr, "%s/index.html",stmp);
1035 free(stmp);
1036
1037 str_list_append(src, repo_ptr);
1038 free(repo_ptr);
1039 repositories++;
1040 }
1041 while (repositories > 0)
1042 {
1043 iter1 = str_list_pop(src);
1044 repositories--;
1045
1046 err = opkg_download(opkg->conf, iter1->data, "/dev/null", NULL, NULL);
1047 #ifdef HAVE_CURL
1048 if (!(err == CURLE_OK ||
1049 err == CURLE_HTTP_RETURNED_ERROR ||
1050 err == CURLE_FILE_COULDNT_READ_FILE ||
1051 err == CURLE_REMOTE_FILE_NOT_FOUND ||
1052 err == CURLE_TFTP_NOTFOUND
1053 )) {
1054 #else
1055 if (!(err == 0)) {
1056 #endif
1057 ret++;
1058 }
1059 str_list_elt_deinit(iter1);
1060 }
1061 free(src);
1062 return ret;
1063 }