Fix some memory leaks.
[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 case OPKG_INSTALL_ERR_SHA256: return OPKG_SHA256_ERROR;
535 default: return OPKG_UNKNOWN_ERROR;
536 }
537 }
538
539 progress (pdata, 75);
540
541 /* run configure scripts, etc. */
542 err = opkg_configure_packages (opkg->conf, NULL);
543 if (err)
544 {
545 opkg_package_free (pdata.package);
546 return OPKG_UNKNOWN_ERROR;
547 }
548
549 /* write out status files and file lists */
550 opkg_conf_write_status_files (opkg->conf);
551 pkg_write_changed_filelists (opkg->conf);
552
553 progress (pdata, 100);
554 opkg_package_free (pdata.package);
555 return 0;
556 }
557
558 int
559 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
560 {
561 int err;
562 pkg_t *pkg = NULL;
563 pkg_t *pkg_to_remove;
564 opkg_progress_data_t pdata;
565
566 opkg_assert (opkg != NULL);
567 opkg_assert (package_name != NULL);
568
569 pkg_info_preinstall_check (opkg->conf);
570
571 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
572
573 if (pkg == NULL)
574 {
575 /* XXX: Error: Package not installed. */
576 return OPKG_PACKAGE_NOT_INSTALLED;
577 }
578
579 pdata.action = OPKG_REMOVE;
580 pdata.package = pkg_t_to_opkg_package_t (pkg);
581 progress (pdata, 0);
582
583
584 if (pkg->state_status == SS_NOT_INSTALLED)
585 {
586 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
587 opkg_package_free (pdata.package);
588 return OPKG_PACKAGE_NOT_INSTALLED;
589 }
590 progress (pdata, 25);
591
592 if (opkg->conf->restrict_to_default_dest)
593 {
594 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
595 pkg->name,
596 opkg->conf->default_dest);
597 }
598 else
599 {
600 pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
601 }
602
603
604 progress (pdata, 75);
605
606 err = opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
607
608 /* write out status files and file lists */
609 opkg_conf_write_status_files (opkg->conf);
610 pkg_write_changed_filelists (opkg->conf);
611
612
613 progress (pdata, 100);
614 opkg_package_free (pdata.package);
615 return (err) ? OPKG_UNKNOWN_ERROR : OPKG_NO_ERROR;
616 }
617
618 int
619 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
620 {
621 int err;
622 pkg_t *pkg;
623 opkg_progress_data_t pdata;
624
625
626
627 opkg_assert (opkg != NULL);
628 opkg_assert (package_name != NULL);
629
630 pkg_info_preinstall_check (opkg->conf);
631
632 if (opkg->conf->restrict_to_default_dest)
633 {
634 pkg = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
635 package_name,
636 opkg->conf->default_dest);
637 if (pkg == NULL)
638 {
639 /* XXX: Error: Package not installed in default_dest */
640 return OPKG_PACKAGE_NOT_INSTALLED;
641 }
642 }
643 else
644 {
645 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash,
646 package_name);
647 }
648
649 if (!pkg)
650 {
651 /* XXX: Error: Package not installed */
652 return OPKG_PACKAGE_NOT_INSTALLED;
653 }
654
655 pdata.action = OPKG_INSTALL;
656 pdata.package = pkg_t_to_opkg_package_t (pkg);
657 progress (pdata, 0);
658
659 err = opkg_upgrade_pkg (opkg->conf, pkg);
660 /* opkg_upgrade_pkg returns the error codes of opkg_install_pkg */
661 if (err)
662 {
663
664 opkg_package_free (pdata.package);
665 switch (err)
666 {
667 case OPKG_INSTALL_ERR_NOT_TRUSTED: return OPKG_GPG_ERROR;
668 case OPKG_INSTALL_ERR_DOWNLOAD: return OPKG_DOWNLOAD_FAILED;
669 case OPKG_INSTALL_ERR_DEPENDENCIES:
670 case OPKG_INSTALL_ERR_CONFLICTS: return OPKG_DEPENDENCIES_FAILED;
671 case OPKG_INSTALL_ERR_ALREADY_INSTALLED: return OPKG_PACKAGE_ALREADY_INSTALLED;
672 case OPKG_INSTALL_ERR_SIGNATURE: return OPKG_GPG_ERROR;
673 case OPKG_INSTALL_ERR_MD5: return OPKG_MD5_ERROR;
674 case OPKG_INSTALL_ERR_SHA256: return OPKG_SHA256_ERROR;
675 default: return OPKG_UNKNOWN_ERROR;
676 }
677 }
678 progress (pdata, 75);
679
680 err = opkg_configure_packages (opkg->conf, NULL);
681 if (err) {
682 opkg_package_free (pdata.package);
683 return OPKG_UNKNOWN_ERROR;
684 }
685
686 /* write out status files and file lists */
687 opkg_conf_write_status_files (opkg->conf);
688 pkg_write_changed_filelists (opkg->conf);
689
690 progress (pdata, 100);
691 opkg_package_free (pdata.package);
692 return 0;
693 }
694
695 int
696 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
697 {
698 pkg_vec_t *installed;
699 int err = 0;
700 int i;
701 pkg_t *pkg;
702 opkg_progress_data_t pdata;
703
704 pdata.action = OPKG_INSTALL;
705 pdata.package = NULL;
706
707 opkg_assert (opkg != NULL);
708 progress (pdata, 0);
709
710 installed = pkg_vec_alloc ();
711 pkg_info_preinstall_check (opkg->conf);
712
713 pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed);
714 for (i = 0; i < installed->len; i++)
715 {
716 pkg = installed->pkgs[i];
717
718 pdata.package = pkg_t_to_opkg_package_t (pkg);
719 progress (pdata, 99 * i / installed->len);
720 opkg_package_free (pdata.package);
721
722 err += opkg_upgrade_pkg (opkg->conf, pkg);
723 }
724 pkg_vec_free (installed);
725
726 if (err)
727 return 1;
728
729 err = opkg_configure_packages (opkg->conf, NULL);
730 if (err)
731 return 1;
732
733 pdata.package = NULL;
734 progress (pdata, 100);
735 return 0;
736 }
737
738 int
739 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
740 {
741 char *tmp;
742 int err, result = 0;
743 char *lists_dir;
744 pkg_src_list_elt_t *iter;
745 pkg_src_t *src;
746 int sources_list_count, sources_done;
747 opkg_progress_data_t pdata;
748
749 opkg_assert (opkg != NULL);
750
751 pdata.action = OPKG_DOWNLOAD;
752 pdata.package = NULL;
753 progress (pdata, 0);
754
755 sprintf_alloc (&lists_dir, "%s",
756 (opkg->conf->restrict_to_default_dest)
757 ? opkg->conf->default_dest->lists_dir
758 : opkg->conf->lists_dir);
759
760 if (!file_is_dir (lists_dir))
761 {
762 if (file_exists (lists_dir))
763 {
764 /* XXX: Error: file exists but is not a directory */
765 free (lists_dir);
766 return 1;
767 }
768
769 err = file_mkdir_hier (lists_dir, 0755);
770 if (err)
771 {
772 /* XXX: Error: failed to create directory */
773 free (lists_dir);
774 return 1;
775 }
776 }
777
778 tmp = strdup ("/tmp/opkg.XXXXXX");
779
780 if (mkdtemp (tmp) == NULL)
781 {
782 /* XXX: Error: could not create temporary file name */
783 free (lists_dir);
784 free (tmp);
785 return 1;
786 }
787
788 /* count the number of sources so we can give some progress updates */
789 sources_list_count = 0;
790 sources_done = 0;
791 list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
792 {
793 sources_list_count++;
794 }
795
796 list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
797 {
798 char *url, *list_file_name = NULL;
799
800 src = (pkg_src_t *)iter->data;
801
802 if (src->extra_data) /* debian style? */
803 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
804 src->gzip ? "Packages.gz" : "Packages");
805 else
806 sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
807
808 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
809 if (src->gzip)
810 {
811 FILE *in, *out;
812 struct _curl_cb_data cb_data;
813 char *tmp_file_name = NULL;
814
815 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
816
817 /* XXX: Note: downloading url */
818
819 cb_data.cb = progress_callback;
820 cb_data.progress_data = &pdata;
821 cb_data.opkg = opkg;
822 cb_data.user_data = user_data;
823 cb_data.start_range = 100 * sources_done / sources_list_count;
824 cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
825
826 err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
827
828 if (err == 0)
829 {
830 /* XXX: Note: Inflating downloaded file */
831 in = fopen (tmp_file_name, "r");
832 out = fopen (list_file_name, "w");
833 if (in && out)
834 unzip (in, out);
835 else
836 err = 1;
837 if (in)
838 fclose (in);
839 if (out)
840 fclose (out);
841 unlink (tmp_file_name);
842 }
843 free (tmp_file_name);
844 }
845 else
846 err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
847
848 if (err)
849 {
850 /* XXX: Error: download error */
851 result = OPKG_DOWNLOAD_FAILED;
852 }
853 free (url);
854
855 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
856 if ( opkg->conf->check_signature ) {
857 char *sig_file_name;
858 /* download detached signitures to verify the package lists */
859 /* get the url for the sig file */
860 if (src->extra_data) /* debian style? */
861 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
862 "Packages.sig");
863 else
864 sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
865
866 /* create filename for signature */
867 sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, src->name);
868
869 /* make sure there is no existing signature file */
870 unlink (sig_file_name);
871
872 err = opkg_download (opkg->conf, url, sig_file_name, NULL, NULL);
873 if (err)
874 {
875 /* XXX: Warning: Download failed */
876 }
877 else
878 {
879 int err;
880 err = opkg_verify_file (opkg->conf, list_file_name, sig_file_name);
881 if (err == 0)
882 {
883 /* XXX: Notice: Signature check passed */
884 }
885 else
886 {
887 /* XXX: Warning: Signature check failed */
888 }
889 }
890 free (sig_file_name);
891 free (list_file_name);
892 free (url);
893 }
894 #else
895 /* XXX: Note: Signature check for %s skipped because GPG support was not
896 * enabled in this build
897 */
898 #endif
899
900 sources_done++;
901 progress (pdata, 100 * sources_done / sources_list_count);
902 }
903
904 rmdir (tmp);
905 free (tmp);
906 free (lists_dir);
907
908 /* Now re-read the package lists to update package hash tables. */
909 opkg_re_read_config_files (opkg);
910
911 return result;
912 }
913
914
915 int
916 opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
917 {
918 pkg_vec_t *all;
919 int i;
920
921 opkg_assert (opkg);
922 opkg_assert (callback);
923
924 all = pkg_vec_alloc ();
925 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
926 for (i = 0; i < all->len; i++)
927 {
928 pkg_t *pkg;
929 opkg_package_t *package;
930
931 pkg = all->pkgs[i];
932
933 package = pkg_t_to_opkg_package_t (pkg);
934 callback (opkg, package, user_data);
935 opkg_package_free (package);
936 }
937
938 pkg_vec_free (all);
939
940 return 0;
941 }
942
943 int
944 opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
945 {
946 struct active_list *head;
947 struct active_list *node;
948 pkg_t *old=NULL, *new = NULL;
949 static opkg_package_t* package=NULL;
950
951
952 opkg_assert (opkg);
953 opkg_assert (callback);
954
955 /* ensure all data is valid */
956 pkg_info_preinstall_check (opkg->conf);
957
958 head = prepare_upgrade_list(opkg->conf);
959 for (node=active_list_next(head, head); node; active_list_next(head,node)) {
960 old = list_entry(node, pkg_t, list);
961 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, old->name, NULL);
962 package = pkg_t_to_opkg_package_t (new);
963 callback (opkg, package, user_data);
964 opkg_package_free (package);
965 }
966 active_list_head_delete(head);
967 return 0;
968 }
969
970 opkg_package_t*
971 opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *arch, const char *repo)
972 {
973 pkg_vec_t *all;
974 opkg_package_t *package = NULL;
975 int i;
976 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
977
978 opkg_assert (opkg);
979
980 all = pkg_vec_alloc ();
981 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
982 for (i = 0; i < all->len; i++)
983 {
984 pkg_t *pkg;
985 char *pkgv;
986
987 pkg = all->pkgs[i];
988
989 /* check name */
990 if (sstrcmp (pkg->name, name))
991 continue;
992
993 /* check version */
994 pkgv = pkg_version_str_alloc (pkg);
995 if (sstrcmp (pkgv, ver))
996 {
997 free (pkgv);
998 continue;
999 }
1000 free (pkgv);
1001
1002 /* check architecture */
1003 if (arch)
1004 {
1005 if (sstrcmp (pkg->architecture, arch))
1006 continue;
1007 }
1008
1009 /* check repository */
1010 if (repo)
1011 {
1012 if (sstrcmp (pkg->src->name, repo))
1013 continue;
1014 }
1015
1016 /* match found */
1017 package = pkg_t_to_opkg_package_t (pkg);
1018 break;
1019 }
1020
1021 pkg_vec_free (all);
1022
1023 return package;
1024 }
1025
1026 #ifdef HAVE_CURL
1027 #include <curl/curl.h>
1028 #endif
1029 /**
1030 * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status.
1031 * @param opkg The opkg_t
1032 * @return return how many repositories cannot access. 0 means all okay.
1033 */
1034 int opkg_repository_accessibility_check(opkg_t *opkg)
1035 {
1036 pkg_src_list_elt_t *iter;
1037 str_list_elt_t *iter1;
1038 str_list_t *src;
1039 int repositories=0;
1040 int ret=0;
1041 int err;
1042 char *repo_ptr;
1043 char *stmp;
1044 opkg_assert(opkg != NULL);
1045
1046 src = str_list_alloc();
1047
1048 list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
1049 {
1050 if (strstr(((pkg_src_t *)iter->data)->value, "://") &&
1051 index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/'))
1052 stmp = strndup(((pkg_src_t *)iter->data)->value,
1053 (index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/') - ((pkg_src_t *)iter->data)->value)*sizeof(char));
1054
1055 else
1056 stmp = strdup(((pkg_src_t *)iter->data)->value);
1057
1058 for (iter1 = str_list_first(src); iter1; iter1 = str_list_next(src, iter1))
1059 {
1060 if (strstr(iter1->data, stmp))
1061 break;
1062 }
1063 if (iter1)
1064 continue;
1065
1066 sprintf_alloc(&repo_ptr, "%s/index.html",stmp);
1067 free(stmp);
1068
1069 str_list_append(src, repo_ptr);
1070 free(repo_ptr);
1071 repositories++;
1072 }
1073 while (repositories > 0)
1074 {
1075 iter1 = str_list_pop(src);
1076 repositories--;
1077
1078 err = opkg_download(opkg->conf, iter1->data, "/dev/null", NULL, NULL);
1079 #ifdef HAVE_CURL
1080 if (!(err == CURLE_OK ||
1081 err == CURLE_HTTP_RETURNED_ERROR ||
1082 err == CURLE_FILE_COULDNT_READ_FILE ||
1083 err == CURLE_REMOTE_FILE_NOT_FOUND ||
1084 err == CURLE_TFTP_NOTFOUND
1085 )) {
1086 #else
1087 if (!(err == 0)) {
1088 #endif
1089 ret++;
1090 }
1091 str_list_elt_deinit(iter1);
1092 }
1093 free(src);
1094 return ret;
1095 }