Propagate errors upwards.
[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, 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 sprintf_alloc(&tmp, "%s/update-XXXXXX", opkg->conf->tmp_dir);
770 if (mkdtemp (tmp) == NULL) {
771 /* XXX: Error: could not create temporary file name */
772 free (lists_dir);
773 free (tmp);
774 return 1;
775 }
776
777 /* count the number of sources so we can give some progress updates */
778 sources_list_count = 0;
779 sources_done = 0;
780 list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
781 {
782 sources_list_count++;
783 }
784
785 list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
786 {
787 char *url, *list_file_name = NULL;
788
789 src = (pkg_src_t *)iter->data;
790
791 if (src->extra_data) /* debian style? */
792 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
793 src->gzip ? "Packages.gz" : "Packages");
794 else
795 sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
796
797 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
798 if (src->gzip)
799 {
800 FILE *in, *out;
801 struct _curl_cb_data cb_data;
802 char *tmp_file_name = NULL;
803
804 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
805
806 /* XXX: Note: downloading url */
807
808 cb_data.cb = progress_callback;
809 cb_data.progress_data = &pdata;
810 cb_data.opkg = opkg;
811 cb_data.user_data = user_data;
812 cb_data.start_range = 100 * sources_done / sources_list_count;
813 cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
814
815 err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
816
817 if (err == 0)
818 {
819 /* XXX: Note: Inflating downloaded file */
820 in = fopen (tmp_file_name, "r");
821 out = fopen (list_file_name, "w");
822 if (in && out)
823 unzip (in, out);
824 else
825 err = 1;
826 if (in)
827 fclose (in);
828 if (out)
829 fclose (out);
830 unlink (tmp_file_name);
831 }
832 free (tmp_file_name);
833 }
834 else
835 err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
836
837 if (err)
838 {
839 /* XXX: Error: download error */
840 result = OPKG_DOWNLOAD_FAILED;
841 }
842 free (url);
843
844 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
845 if ( opkg->conf->check_signature ) {
846 char *sig_file_name;
847 /* download detached signitures to verify the package lists */
848 /* get the url for the sig file */
849 if (src->extra_data) /* debian style? */
850 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
851 "Packages.sig");
852 else
853 sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
854
855 /* create filename for signature */
856 sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, src->name);
857
858 /* make sure there is no existing signature file */
859 unlink (sig_file_name);
860
861 err = opkg_download (opkg->conf, url, sig_file_name, NULL, NULL);
862 if (err)
863 {
864 /* XXX: Warning: Download failed */
865 }
866 else
867 {
868 int err;
869 err = opkg_verify_file (opkg->conf, list_file_name, sig_file_name);
870 if (err == 0)
871 {
872 /* XXX: Notice: Signature check passed */
873 }
874 else
875 {
876 /* XXX: Warning: Signature check failed */
877 }
878 }
879 free (sig_file_name);
880 free (list_file_name);
881 free (url);
882 }
883 #else
884 /* XXX: Note: Signature check for %s skipped because GPG support was not
885 * enabled in this build
886 */
887 #endif
888
889 sources_done++;
890 progress (pdata, 100 * sources_done / sources_list_count);
891 }
892
893 rmdir (tmp);
894 free (tmp);
895 free (lists_dir);
896
897 /* Now re-read the package lists to update package hash tables. */
898 opkg_re_read_config_files (opkg);
899
900 return result;
901 }
902
903
904 int
905 opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
906 {
907 pkg_vec_t *all;
908 int i;
909
910 opkg_assert (opkg);
911 opkg_assert (callback);
912
913 all = pkg_vec_alloc ();
914 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
915 for (i = 0; i < all->len; i++)
916 {
917 pkg_t *pkg;
918 opkg_package_t *package;
919
920 pkg = all->pkgs[i];
921
922 package = pkg_t_to_opkg_package_t (pkg);
923 callback (opkg, package, user_data);
924 opkg_package_free (package);
925 }
926
927 pkg_vec_free (all);
928
929 return 0;
930 }
931
932 int
933 opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
934 {
935 struct active_list *head;
936 struct active_list *node;
937 pkg_t *old=NULL, *new = NULL;
938 static opkg_package_t* package=NULL;
939
940
941 opkg_assert (opkg);
942 opkg_assert (callback);
943
944 /* ensure all data is valid */
945 pkg_info_preinstall_check (opkg->conf);
946
947 head = prepare_upgrade_list(opkg->conf);
948 for (node=active_list_next(head, head); node; active_list_next(head,node)) {
949 old = list_entry(node, pkg_t, list);
950 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, old->name, NULL);
951 package = pkg_t_to_opkg_package_t (new);
952 callback (opkg, package, user_data);
953 opkg_package_free (package);
954 }
955 active_list_head_delete(head);
956 return 0;
957 }
958
959 opkg_package_t*
960 opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *arch, const char *repo)
961 {
962 pkg_vec_t *all;
963 opkg_package_t *package = NULL;
964 int i;
965 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
966
967 opkg_assert (opkg);
968
969 all = pkg_vec_alloc ();
970 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
971 for (i = 0; i < all->len; i++)
972 {
973 pkg_t *pkg;
974 char *pkgv;
975
976 pkg = all->pkgs[i];
977
978 /* check name */
979 if (sstrcmp (pkg->name, name))
980 continue;
981
982 /* check version */
983 pkgv = pkg_version_str_alloc (pkg);
984 if (sstrcmp (pkgv, ver))
985 {
986 free (pkgv);
987 continue;
988 }
989 free (pkgv);
990
991 /* check architecture */
992 if (arch)
993 {
994 if (sstrcmp (pkg->architecture, arch))
995 continue;
996 }
997
998 /* check repository */
999 if (repo)
1000 {
1001 if (sstrcmp (pkg->src->name, repo))
1002 continue;
1003 }
1004
1005 /* match found */
1006 package = pkg_t_to_opkg_package_t (pkg);
1007 break;
1008 }
1009
1010 pkg_vec_free (all);
1011
1012 return package;
1013 }
1014
1015 #ifdef HAVE_CURL
1016 #include <curl/curl.h>
1017 #endif
1018 /**
1019 * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status.
1020 * @param opkg The opkg_t
1021 * @return return how many repositories cannot access. 0 means all okay.
1022 */
1023 int opkg_repository_accessibility_check(opkg_t *opkg)
1024 {
1025 pkg_src_list_elt_t *iter;
1026 str_list_elt_t *iter1;
1027 str_list_t *src;
1028 int repositories=0;
1029 int ret=0;
1030 int err;
1031 char *repo_ptr;
1032 char *stmp;
1033 opkg_assert(opkg != NULL);
1034
1035 src = str_list_alloc();
1036
1037 list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
1038 {
1039 if (strstr(((pkg_src_t *)iter->data)->value, "://") &&
1040 index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/'))
1041 stmp = xstrndup(((pkg_src_t *)iter->data)->value,
1042 (index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/') - ((pkg_src_t *)iter->data)->value)*sizeof(char));
1043
1044 else
1045 stmp = xstrdup(((pkg_src_t *)iter->data)->value);
1046
1047 for (iter1 = str_list_first(src); iter1; iter1 = str_list_next(src, iter1))
1048 {
1049 if (strstr(iter1->data, stmp))
1050 break;
1051 }
1052 if (iter1)
1053 continue;
1054
1055 sprintf_alloc(&repo_ptr, "%s/index.html",stmp);
1056 free(stmp);
1057
1058 str_list_append(src, repo_ptr);
1059 free(repo_ptr);
1060 repositories++;
1061 }
1062 while (repositories > 0)
1063 {
1064 iter1 = str_list_pop(src);
1065 repositories--;
1066
1067 err = opkg_download(opkg->conf, iter1->data, "/dev/null", NULL, NULL);
1068 #ifdef HAVE_CURL
1069 if (!(err == CURLE_OK ||
1070 err == CURLE_HTTP_RETURNED_ERROR ||
1071 err == CURLE_FILE_COULDNT_READ_FILE ||
1072 err == CURLE_REMOTE_FILE_NOT_FOUND ||
1073 err == CURLE_TFTP_NOTFOUND
1074 )) {
1075 #else
1076 if (!(err == 0)) {
1077 #endif
1078 ret++;
1079 }
1080 str_list_elt_deinit(iter1);
1081 }
1082 free(src);
1083 return ret;
1084 }