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