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