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