4320ab985dffaaef28a26069994a5a2b13211cb9
[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
31 #include "sprintf_alloc.h"
32 #include "file_util.h"
33
34 #include <libbb/libbb.h>
35
36 struct _opkg_t
37 {
38 args_t *args;
39 opkg_conf_t *conf;
40 opkg_option_t *options;
41 };
42
43 #define opkg_assert(expr) if (!(expr)) { \
44 printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
45 __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
46
47 #define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (opkg, &d, user_data);
48 #define SSTRCMP(x,y) (x && y) ? strcmp (x, y) : 0
49
50 /** Private Functions ***/
51
52 static opkg_package_t*
53 old_pkg_to_new (pkg_t *old)
54 {
55 opkg_package_t *new;
56
57 new = opkg_package_new ();
58
59 #define sstrdup(x) (x) ? strdup (x) : NULL;
60
61 new->name = sstrdup (old->name);
62 new->version = pkg_version_str_alloc (old);
63 new->architecture = sstrdup (old->architecture);
64 if (old->src)
65 new->repository = sstrdup (old->src->name);
66 new->description = sstrdup (old->description);
67 new->tags = sstrdup (old->tags);
68 new->url = sstrdup (old->url);
69
70 new->size = (old->size) ? atoi (old->size) : 0;
71 new->installed = (old->state_status == SS_INSTALLED);
72
73 return new;
74 }
75
76 static int
77 opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
78 {
79 pkg_vec_t *all;
80 int i;
81 pkg_t *pkg;
82 int r, err = 0;
83
84 all = pkg_vec_alloc ();
85 pkg_hash_fetch_available (&conf->pkg_hash, all);
86
87 for (i = 0; i < all->len; i++)
88 {
89 pkg = all->pkgs[i];
90
91 if (pkg_name && fnmatch (pkg_name, pkg->name, 0))
92 continue;
93
94 if (pkg->state_status == SS_UNPACKED)
95 {
96 r = opkg_configure (conf, pkg);
97 if (r == 0)
98 {
99 pkg->state_status = SS_INSTALLED;
100 pkg->parent->state_status = SS_INSTALLED;
101 pkg->state_flag &= ~SF_PREFER;
102 }
103 else
104 {
105 if (!err)
106 err = r;
107 }
108 }
109 }
110
111 pkg_vec_free (all);
112 return err;
113 }
114
115 struct _curl_cb_data
116 {
117 opkg_progress_callback_t cb;
118 opkg_progress_data_t *progress_data;
119 opkg_t *opkg;
120 void *user_data;
121 int start_range;
122 int finish_range;
123 };
124
125 int
126 curl_progress_cb (struct _curl_cb_data *cb_data,
127 double t, /* dltotal */
128 double d, /* dlnow */
129 double ultotal,
130 double ulnow)
131 {
132 int p = (t) ? d * 100 / t : 0;
133 static int prev = -1;
134 int progress = 0;
135
136 /* prevent the same value being sent twice (can occur due to rounding) */
137 if (p == prev)
138 return 0;
139 prev = p;
140
141 if (t < 1)
142 return 0;
143
144 progress = cb_data->start_range + (d / t * ((cb_data->finish_range - cb_data->start_range)));
145 cb_data->progress_data->percentage = progress;
146
147 (cb_data->cb)(cb_data->opkg,
148 cb_data->progress_data,
149 cb_data->user_data);
150
151 return 0;
152 }
153
154
155 /*** Public API ***/
156
157 opkg_package_t *
158 opkg_package_new ()
159 {
160
161 opkg_package_t *p;
162
163 p = malloc (sizeof (opkg_package_t));
164 memset (p, 0, sizeof (opkg_package_t));
165
166 return p;
167 }
168
169 void
170 opkg_package_free (opkg_package_t *p)
171 {
172 free (p->name);
173 free (p->version);
174 free (p->architecture);
175 free (p->description);
176 free (p->tags);
177 free (p->url);
178
179 free (p);
180 }
181
182 opkg_t *
183 opkg_new ()
184 {
185 opkg_t *opkg;
186 opkg = malloc (sizeof (opkg_t));
187
188 opkg->args = malloc (sizeof (args_t));
189 args_init (opkg->args);
190
191 opkg->conf = malloc (sizeof (opkg_conf_t));
192 opkg_conf_init (opkg->conf, opkg->args);
193
194 opkg_init_options_array (opkg->conf, &opkg->options);
195 return opkg;
196 }
197
198 void
199 opkg_free (opkg_t *opkg)
200 {
201 opkg_assert (opkg != NULL);
202
203 opkg_conf_deinit (opkg->conf);
204 args_deinit (opkg->args);
205 }
206
207 int
208 opkg_re_read_config_files (opkg_t *opkg)
209 {
210 args_t *a;
211 opkg_conf_t *c;
212
213 opkg_assert (opkg != NULL);
214
215 a = opkg->args;
216 c = opkg->conf;
217
218 /* Unfortunatly, the easiest way to re-read the config files right now is to
219 * throw away opkg->conf and start again */
220
221 /* copy the settings we need to keep */
222 a->autoremove = c->autoremove;
223 a->force_depends = c->force_depends;
224 a->force_defaults = c->force_defaults;
225 a->force_overwrite = c->force_overwrite;
226 a->force_downgrade = c->force_downgrade;
227 a->force_reinstall = c->force_reinstall;
228 a->force_removal_of_dependent_packages = c->force_removal_of_dependent_packages;
229 a->force_removal_of_essential_packages = c->force_removal_of_essential_packages;
230 a->nodeps = c->nodeps;
231 a->noaction = c->noaction;
232 a->query_all = c->query_all;
233 a->multiple_providers = c->multiple_providers;
234 a->verbosity = c->verbosity;
235
236 if (c->offline_root)
237 {
238 if (a->offline_root) free (a->offline_root);
239 a->offline_root = strdup (c->offline_root);
240 }
241
242 if (c->offline_root_pre_script_cmd)
243 {
244 if (a->offline_root_pre_script_cmd) free (a->offline_root_pre_script_cmd);
245 a->offline_root_pre_script_cmd = strdup (c->offline_root_pre_script_cmd);
246 }
247
248 if (c->offline_root_post_script_cmd)
249 {
250 if (a->offline_root_post_script_cmd) free (a->offline_root_post_script_cmd);
251 a->offline_root_post_script_cmd = strdup (c->offline_root_post_script_cmd);
252 }
253
254 /* throw away old opkg_conf and start again */
255 opkg_conf_deinit (opkg->conf);
256 opkg_conf_init (opkg->conf, opkg->args);
257
258 free (opkg->options);
259 opkg_init_options_array (opkg->conf, &opkg->options);
260
261 return 0;
262 }
263
264 void
265 opkg_get_option (opkg_t *opkg, char *option, void **value)
266 {
267 int i = 0;
268 opkg_option_t *options;
269
270 opkg_assert (opkg != NULL);
271 opkg_assert (option != NULL);
272 opkg_assert (value != NULL);
273
274 options = opkg->options;
275
276 /* look up the option
277 * TODO: this would be much better as a hash table
278 */
279 while (options[i].name)
280 {
281 if (strcmp (options[i].name, option) != 0)
282 {
283 i++;
284 continue;
285 }
286 }
287
288 /* get the option */
289 switch (options[i].type)
290 {
291 case OPKG_OPT_TYPE_BOOL:
292 *((int *) value) = *((int *) options[i].value);
293 return;
294
295 case OPKG_OPT_TYPE_INT:
296 *((int *) value) = *((int *) options[i].value);
297 return;
298
299 case OPKG_OPT_TYPE_STRING:
300 *((char **)value) = strdup (options[i].value);
301 return;
302 }
303
304 }
305
306 void
307 opkg_set_option (opkg_t *opkg, char *option, void *value)
308 {
309 int i = 0, found = 0;
310 opkg_option_t *options;
311
312 opkg_assert (opkg != NULL);
313 opkg_assert (option != NULL);
314 opkg_assert (value != NULL);
315
316 options = opkg->options;
317
318 /* look up the option
319 * TODO: this would be much better as a hash table
320 */
321 while (options[i].name)
322 {
323 if (strcmp (options[i].name, option) == 0)
324 {
325 found = 1;
326 break;
327 }
328 i++;
329 }
330
331 if (!found)
332 {
333 /* XXX: Warning: Option not found */
334 return;
335 }
336
337 /* set the option */
338 switch (options[i].type)
339 {
340 case OPKG_OPT_TYPE_BOOL:
341 if (*((int *) value) == 0)
342 *((int *)options[i].value) = 0;
343 else
344 *((int *)options[i].value) = 1;
345 return;
346
347 case OPKG_OPT_TYPE_INT:
348 *((int *) options[i].value) = *((int *) value);
349 return;
350
351 case OPKG_OPT_TYPE_STRING:
352 *((char **)options[i].value) = strdup (value);
353 return;
354 }
355
356 }
357
358 int
359 opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
360 {
361 int err;
362 char *package_id = NULL;
363 opkg_progress_data_t pdata;
364 pkg_t *new;
365
366 opkg_assert (opkg != NULL);
367 opkg_assert (package_name != NULL);
368
369 /* ... */
370 pkg_info_preinstall_check (opkg->conf);
371
372 new = pkg_hash_fetch_best_installation_candidate_by_name (opkg->conf, package_name);
373
374 if (!new)
375 {
376 /* XXX: Error: Could not find package to install */
377 return 1;
378 }
379 pdata.action = OPKG_INSTALL;
380 pdata.package = old_pkg_to_new (new);
381
382 progress (pdata, 0);
383
384 /* download the package */
385 opkg_prepare_url_for_install (opkg->conf, package_name, &package_id);
386
387 progress (pdata, 50);
388
389 if (!package_id)
390 package_id = strdup (package_name);
391
392 /* unpack the package */
393 if (opkg->conf->multiple_providers)
394 {
395 err = opkg_install_multi_by_name (opkg->conf, package_id);
396 }
397 else
398 {
399 err = opkg_install_by_name (opkg->conf, package_id);
400 }
401
402 if (err)
403 return err;
404
405 progress (pdata, 75);
406
407 /* run configure scripts, etc. */
408 err = opkg_configure_packages (opkg->conf, NULL);
409 if (err)
410 return err;
411
412 /* write out status files and file lists */
413 opkg_conf_write_status_files (opkg->conf);
414 pkg_write_changed_filelists (opkg->conf);
415
416 progress (pdata, 100);
417 opkg_package_free (pdata.package);
418 return 0;
419 }
420
421 int
422 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
423 {
424 pkg_t *pkg = NULL;
425 pkg_t *pkg_to_remove;
426 opkg_progress_data_t pdata;
427
428 opkg_assert (opkg != NULL);
429 opkg_assert (package_name != NULL);
430
431
432
433 pkg_info_preinstall_check (opkg->conf);
434
435
436 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
437
438 if (pkg == NULL)
439 {
440 /* XXX: Error: Package not installed. */
441 return 1;
442 }
443
444 pdata.action = OPKG_REMOVE;
445 pdata.package = old_pkg_to_new (pkg);
446 progress (pdata, 0);
447
448
449 if (pkg->state_status == SS_NOT_INSTALLED)
450 {
451 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
452 return 1;
453 }
454 progress (pdata, 25);
455
456 if (opkg->conf->restrict_to_default_dest)
457 {
458 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
459 pkg->name,
460 opkg->conf->default_dest);
461 }
462 else
463 {
464 pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
465 }
466
467
468 progress (pdata, 75);
469
470 opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
471
472 /* write out status files and file lists */
473 opkg_conf_write_status_files (opkg->conf);
474 pkg_write_changed_filelists (opkg->conf);
475
476
477 progress (pdata, 100);
478 opkg_package_free (pdata.package);
479 return 0;
480 }
481
482 int
483 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
484 {
485 pkg_t *pkg;
486 opkg_progress_data_t pdata;
487
488
489
490 opkg_assert (opkg != NULL);
491 opkg_assert (package_name != NULL);
492
493 pkg_info_preinstall_check (opkg->conf);
494
495 if (opkg->conf->restrict_to_default_dest)
496 {
497 pkg = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
498 package_name,
499 opkg->conf->default_dest);
500 if (pkg == NULL)
501 {
502 /* XXX: Error: Package not installed in default_dest */
503 return 1;
504 }
505 }
506 else
507 {
508 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash,
509 package_name);
510 }
511
512 if (!pkg)
513 {
514 /* XXX: Error: Package not installed */
515 return 1;
516 }
517
518 pdata.action = OPKG_INSTALL;
519 pdata.package = old_pkg_to_new (pkg);
520 progress (pdata, 0);
521
522 opkg_upgrade_pkg (opkg->conf, pkg);
523 progress (pdata, 75);
524
525 opkg_configure_packages (opkg->conf, NULL);
526 progress (pdata, 100);
527 opkg_package_free (pdata.package);
528 return 0;
529 }
530
531 int
532 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
533 {
534 pkg_vec_t *installed;
535 int err = 0;
536 int i;
537 pkg_t *pkg;
538 opkg_progress_data_t pdata;
539
540 pdata.action = OPKG_INSTALL;
541 pdata.package = NULL;
542
543 opkg_assert (opkg != NULL);
544 progress (pdata, 0);
545
546 installed = pkg_vec_alloc ();
547 pkg_info_preinstall_check (opkg->conf);
548
549 pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed);
550 for (i = 0; i < installed->len; i++)
551 {
552 pkg = installed->pkgs[i];
553
554 pdata.package = old_pkg_to_new (pkg);
555 progress (pdata, 99 * i / installed->len);
556 opkg_package_free (pdata.package);
557
558 err += opkg_upgrade_pkg (opkg->conf, pkg);
559 }
560 pkg_vec_free (installed);
561
562 if (err)
563 return 1;
564
565 err = opkg_configure_packages (opkg->conf, NULL);
566 if (err)
567 return 1;
568
569 pdata.package = NULL;
570 progress (pdata, 100);
571 return 0;
572 }
573
574 int
575 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
576 {
577 char *tmp;
578 int err;
579 char *lists_dir;
580 pkg_src_list_elt_t *iter;
581 pkg_src_t *src;
582 int sources_list_count, sources_done;
583 opkg_progress_data_t pdata;
584
585 opkg_assert (opkg != NULL);
586
587 pdata.action = OPKG_DOWNLOAD;
588 pdata.package = NULL;
589 progress (pdata, 0);
590
591 sprintf_alloc (&lists_dir, "%s",
592 (opkg->conf->restrict_to_default_dest)
593 ? opkg->conf->default_dest->lists_dir
594 : opkg->conf->lists_dir);
595
596 if (!file_is_dir (lists_dir))
597 {
598 if (file_exists (lists_dir))
599 {
600 /* XXX: Error: file exists but is not a directory */
601 free (lists_dir);
602 return 1;
603 }
604
605 err = file_mkdir_hier (lists_dir, 0755);
606 if (err)
607 {
608 /* XXX: Error: failed to create directory */
609 free (lists_dir);
610 return 1;
611 }
612 }
613
614 tmp = strdup ("/tmp/opkg.XXXXXX");
615
616 if (mkdtemp (tmp) == NULL)
617 {
618 /* XXX: Error: could not create temporary file name */
619 free (lists_dir);
620 free (tmp);
621 return 1;
622 }
623
624 /* cout the number of sources so we can give some progress updates */
625 sources_list_count = 0;
626 sources_done = 0;
627 iter = opkg->conf->pkg_src_list.head;
628 while (iter)
629 {
630 sources_list_count++;
631 iter = iter->next;
632 }
633
634 for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
635 {
636 char *url, *list_file_name;
637
638 src = iter->data;
639
640 if (src->extra_data) /* debian style? */
641 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
642 src->gzip ? "Packages.gz" : "Packages");
643 else
644 sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
645
646 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
647 if (src->gzip)
648 {
649 char *tmp_file_name;
650 FILE *in, *out;
651 struct _curl_cb_data cb_data;
652
653 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
654
655 /* XXX: Note: downloading url */
656
657 cb_data.cb = progress_callback;
658 cb_data.progress_data = &pdata;
659 cb_data.opkg = opkg;
660 cb_data.user_data = user_data;
661 cb_data.start_range = 100 * sources_done / sources_list_count;
662 cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
663
664 err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
665
666 if (err == 0)
667 {
668 /* XXX: Note: Inflating downloaded file */
669 in = fopen (tmp_file_name, "r");
670 out = fopen (list_file_name, "w");
671 if (in && out)
672 unzip (in, out);
673 else
674 err = 1;
675 if (in)
676 fclose (in);
677 if (out)
678 fclose (out);
679 unlink (tmp_file_name);
680 }
681 }
682 else
683 err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
684
685 if (err)
686 {
687 /* XXX: Error: download error */
688 }
689 free (url);
690
691 #ifdef HAVE_GPGME
692 /* download detached signitures to verify the package lists */
693 /* get the url for the sig file */
694 if (src->extra_data) /* debian style? */
695 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
696 "Packages.sig");
697 else
698 sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
699
700 /* create temporary file for it */
701 char *tmp_file_name;
702
703 sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
704
705 err = opkg_download (opkg->conf, url, tmp_file_name, NULL, NULL);
706 if (err)
707 {
708 /* XXX: Warning: Download failed */
709 }
710 else
711 {
712 int err;
713 err = opkg_verify_file (opkg->conf, list_file_name, tmp_file_name);
714 if (err == 0)
715 {
716 /* XXX: Notice: Signature check passed */
717 }
718 else
719 {
720 /* XXX: Warning: Signature check failed */
721 }
722 }
723 unlink (tmp_file_name);
724 free (tmp_file_name);
725 free (url);
726 #else
727 /* XXX: Note: Signiture check for %s skipped because GPG support was not
728 * enabled in this build
729 */
730 #endif
731 free (list_file_name);
732
733 sources_done++;
734 progress (pdata, 100 * sources_done / sources_list_count);
735 }
736
737 rmdir (tmp);
738 free (tmp);
739 free (lists_dir);
740
741 return 0;
742 }
743
744
745 int
746 opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
747 {
748 pkg_vec_t *all;
749 int i;
750
751 opkg_assert (opkg);
752 opkg_assert (callback);
753
754 all = pkg_vec_alloc ();
755 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
756 for (i = 0; i < all->len; i++)
757 {
758 pkg_t *pkg;
759 opkg_package_t *package;
760
761 pkg = all->pkgs[i];
762
763 package = old_pkg_to_new (pkg);
764 callback (opkg, package, user_data);
765 }
766
767 pkg_vec_free (all);
768
769 return 0;
770 }
771
772 int
773 opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
774 {
775 pkg_vec_t *all;
776 int i;
777
778 opkg_assert (opkg);
779 opkg_assert (callback);
780
781 all = pkg_vec_alloc ();
782 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
783 for (i = 0; i < all->len; i++)
784 {
785 pkg_t *old, *new;
786 int cmp;
787 opkg_package_t *package;
788
789 old = all->pkgs[i];
790
791 if (old->state_status != SS_INSTALLED)
792 continue;
793
794 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, old->name);
795 if (new == NULL) {
796 /* XXX: Notice: Assuming locally install package is up to date */
797 continue;
798 }
799
800 cmp = pkg_compare_versions(old, new);
801
802 if (cmp < 0)
803 {
804 package = old_pkg_to_new (new);
805 callback (opkg, package, user_data);
806 }
807 }
808
809 pkg_vec_free (all);
810
811 return 0;
812 }
813
814 opkg_package_t*
815 opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *arch, const char *repo)
816 {
817 pkg_vec_t *all;
818 opkg_package_t *package = NULL;
819 int i;
820 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
821
822 opkg_assert (opkg);
823
824 all = pkg_vec_alloc ();
825 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
826 for (i = 0; i < all->len; i++)
827 {
828 pkg_t *pkg;
829 char *pkgv;
830
831 pkg = all->pkgs[i];
832
833 /* check name */
834 if (sstrcmp (pkg->name, name))
835 continue;
836
837 /* check version */
838 pkgv = pkg_version_str_alloc (pkg);
839 if (sstrcmp (pkgv, ver))
840 {
841 free (pkgv);
842 continue;
843 }
844 free (pkgv);
845
846 /* check architecture */
847 if (arch)
848 {
849 if (sstrcmp (pkg->architecture, arch))
850 continue;
851 }
852
853 /* check repository */
854 if (repo)
855 {
856 if (sstrcmp (pkg->src->name, repo))
857 continue;
858 }
859
860 /* match found */
861 package = old_pkg_to_new (pkg);
862 break;
863 }
864
865 pkg_vec_free (all);
866
867 return package;
868 }