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