d994bd19888c98aa6e95fbcb1d29b94bad17d1bc
[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 <stdio.h>
19 #include <unistd.h>
20 #include <fnmatch.h>
21
22 #include "opkg.h"
23 #include "opkg_conf.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 #define opkg_assert(expr) if (!(expr)) { \
37 printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
38 __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
39
40 #define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (&d, user_data);
41
42 /** Private Functions ***/
43
44 static int opkg_configure_packages(char *pkg_name)
45 {
46 pkg_vec_t *all;
47 int i;
48 pkg_t *pkg;
49 int r, err = 0;
50
51 all = pkg_vec_alloc();
52 pkg_hash_fetch_available(all);
53
54 for (i = 0; i < all->len; i++) {
55 pkg = all->pkgs[i];
56
57 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
58 continue;
59
60 if (pkg->state_status == SS_UNPACKED) {
61 r = opkg_configure(pkg);
62 if (r == 0) {
63 pkg->state_status = SS_INSTALLED;
64 pkg->parent->state_status = SS_INSTALLED;
65 pkg->state_flag &= ~SF_PREFER;
66 } else {
67 if (!err)
68 err = r;
69 }
70 }
71 }
72
73 pkg_vec_free(all);
74 return err;
75 }
76
77 static struct opkg_conf saved_conf;
78 /*** Public API ***/
79
80 int opkg_new()
81 {
82 saved_conf = *conf;
83
84 if (opkg_conf_init())
85 goto err0;
86
87 if (opkg_conf_load())
88 goto err0;
89
90 if (pkg_hash_load_feeds(0))
91 goto err1;
92
93 if (pkg_hash_load_status_files())
94 goto err1;
95
96 return 0;
97
98 err1:
99 pkg_hash_deinit();
100 err0:
101 opkg_conf_deinit();
102 return -1;
103 }
104
105 void opkg_free(void)
106 {
107 opkg_conf_deinit();
108 }
109
110 int opkg_re_read_config_files(void)
111 {
112 opkg_free();
113 *conf = saved_conf;
114 return opkg_new();
115 }
116
117 void opkg_get_option(char *option, void **value)
118 {
119 int i = 0;
120 extern opkg_option_t options[];
121
122 /* look up the option
123 * TODO: this would be much better as a hash table
124 */
125 while (options[i].name) {
126 if (strcmp(options[i].name, option) != 0) {
127 i++;
128 continue;
129 }
130 }
131
132 /* get the option */
133 switch (options[i].type) {
134 case OPKG_OPT_TYPE_BOOL:
135 *((int *)value) = *((int *)options[i].value);
136 return;
137
138 case OPKG_OPT_TYPE_INT:
139 *((int *)value) = *((int *)options[i].value);
140 return;
141
142 case OPKG_OPT_TYPE_STRING:
143 *((char **)value) = xstrdup(options[i].value);
144 return;
145 }
146
147 }
148
149 void opkg_set_option(char *option, void *value)
150 {
151 int i = 0, found = 0;
152 extern opkg_option_t options[];
153
154 opkg_assert(option != NULL);
155 opkg_assert(value != NULL);
156
157 /* look up the option
158 * TODO: this would be much better as a hash table
159 */
160 while (options[i].name) {
161 if (strcmp(options[i].name, option) == 0) {
162 found = 1;
163 break;
164 }
165 i++;
166 }
167
168 if (!found) {
169 opkg_msg(ERROR, "Invalid option: %s\n", option);
170 return;
171 }
172
173 /* set the option */
174 switch (options[i].type) {
175 case OPKG_OPT_TYPE_BOOL:
176 if (*((int *)value) == 0)
177 *((int *)options[i].value) = 0;
178 else
179 *((int *)options[i].value) = 1;
180 return;
181
182 case OPKG_OPT_TYPE_INT:
183 *((int *)options[i].value) = *((int *)value);
184 return;
185
186 case OPKG_OPT_TYPE_STRING:
187 *((char **)options[i].value) = xstrdup(value);
188 return;
189 }
190
191 }
192
193 /**
194 * @brief libopkg API: Install package
195 * @param package_name The name of package in which is going to install
196 * @param progress_callback The callback function that report the status to caller.
197 */
198 int
199 opkg_install_package(const char *package_name,
200 opkg_progress_callback_t progress_callback,
201 void *user_data)
202 {
203 int err;
204 char *stripped_filename, *local_filename;
205 opkg_progress_data_t pdata;
206 pkg_t *old, *new;
207 pkg_vec_t *deps, *all;
208 int i;
209 char **unresolved = NULL;
210 const char *filename;
211
212 opkg_assert(package_name != NULL);
213
214 /* ... */
215 pkg_info_preinstall_check();
216
217 /* check to ensure package is not already installed */
218 old = pkg_hash_fetch_installed_by_name(package_name);
219 if (old) {
220 opkg_msg(ERROR, "Package %s is already installed\n",
221 package_name);
222 return -1;
223 }
224
225 new = pkg_hash_fetch_best_installation_candidate_by_name(package_name);
226 if (!new) {
227 opkg_msg(ERROR, "Couldn't find package %s\n", package_name);
228 return -1;
229 }
230
231 new->state_flag |= SF_USER;
232
233 pdata.action = -1;
234 pdata.pkg = new;
235
236 progress(pdata, 0);
237
238 /* find dependancies and download them */
239 deps = pkg_vec_alloc();
240 /* this function does not return the original package, so we insert it later */
241 pkg_hash_fetch_unsatisfied_dependencies(new, deps, &unresolved);
242 if (unresolved) {
243 char **tmp = unresolved;
244 opkg_msg(ERROR, "Couldn't satisfy the following dependencies"
245 " for %s:\n", package_name);
246 while (*tmp) {
247 opkg_msg(ERROR, "\t%s", *tmp);
248 free(*tmp);
249 tmp++;
250 }
251 free(unresolved);
252 pkg_vec_free(deps);
253 opkg_message(ERROR, "\n");
254 return -1;
255 }
256
257 /* insert the package we are installing so that we download it */
258 pkg_vec_insert(deps, new);
259
260 /* download package and dependencies */
261 for (i = 0; i < deps->len; i++) {
262 pkg_t *pkg;
263 char *url;
264
265 pkg = deps->pkgs[i];
266 if (pkg_get_string(pkg, PKG_LOCAL_FILENAME))
267 continue;
268
269 pdata.pkg = pkg;
270 pdata.action = OPKG_DOWNLOAD;
271
272 if (pkg->src == NULL) {
273 opkg_msg(ERROR, "Package %s not available from any "
274 "configured src\n", package_name);
275 return -1;
276 }
277
278 filename = pkg_get_string(pkg, PKG_FILENAME);
279
280 sprintf_alloc(&url, "%s/%s", pkg->src->value, filename);
281
282 /* Get the filename part, without any directory */
283 stripped_filename = strrchr(filename, '/');
284 if (!stripped_filename)
285 stripped_filename = (char *)filename;
286
287 sprintf_alloc(&local_filename, "%s/%s", conf->tmp_dir,
288 stripped_filename);
289
290 pkg_set_string(pkg, PKG_LOCAL_FILENAME, local_filename);
291
292 err = opkg_download(url, local_filename, 0);
293 free(url);
294
295 if (err) {
296 pkg_vec_free(deps);
297 return -1;
298 }
299
300 }
301 pkg_vec_free(deps);
302
303 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
304 all = pkg_vec_alloc();
305 pkg_hash_fetch_available(all);
306 for (i = 0; i < all->len; i++) {
307 all->pkgs[i]->parent->dependencies_checked = 0;
308 }
309 pkg_vec_free(all);
310
311 /* 75% of "install" progress is for downloading */
312 pdata.pkg = new;
313 pdata.action = OPKG_INSTALL;
314 progress(pdata, 75);
315
316 /* unpack the package */
317 err = opkg_install_pkg(new, 0);
318
319 if (err) {
320 return -1;
321 }
322
323 progress(pdata, 75);
324
325 /* run configure scripts, etc. */
326 err = opkg_configure_packages(NULL);
327 if (err) {
328 return -1;
329 }
330
331 /* write out status files and file lists */
332 opkg_conf_write_status_files();
333 pkg_write_changed_filelists();
334
335 progress(pdata, 100);
336 return 0;
337 }
338
339 int
340 opkg_remove_package(const char *package_name,
341 opkg_progress_callback_t progress_callback, void *user_data)
342 {
343 int err;
344 pkg_t *pkg = NULL;
345 pkg_t *pkg_to_remove;
346 opkg_progress_data_t pdata;
347
348 opkg_assert(package_name != NULL);
349
350 pkg_info_preinstall_check();
351
352 pkg = pkg_hash_fetch_installed_by_name(package_name);
353
354 if (pkg == NULL || pkg->state_status == SS_NOT_INSTALLED) {
355 opkg_msg(ERROR, "Package %s not installed\n", package_name);
356 return -1;
357 }
358
359 pdata.action = OPKG_REMOVE;
360 pdata.pkg = pkg;
361 progress(pdata, 0);
362
363 if (conf->restrict_to_default_dest) {
364 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name,
365 conf->
366 default_dest);
367 } else {
368 pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name);
369 }
370
371 progress(pdata, 75);
372
373 err = opkg_remove_pkg(pkg_to_remove, 0);
374
375 /* write out status files and file lists */
376 opkg_conf_write_status_files();
377 pkg_write_changed_filelists();
378
379 progress(pdata, 100);
380 return (err) ? -1 : 0;
381 }
382
383 int
384 opkg_upgrade_package(const char *package_name,
385 opkg_progress_callback_t progress_callback,
386 void *user_data)
387 {
388 int err;
389 pkg_t *pkg;
390 opkg_progress_data_t pdata;
391
392 opkg_assert(package_name != NULL);
393
394 pkg_info_preinstall_check();
395
396 if (conf->restrict_to_default_dest) {
397 pkg = pkg_hash_fetch_installed_by_name_dest(package_name,
398 conf->default_dest);
399 } else {
400 pkg = pkg_hash_fetch_installed_by_name(package_name);
401 }
402
403 if (!pkg) {
404 opkg_msg(ERROR, "Package %s not installed\n", package_name);
405 return -1;
406 }
407
408 pdata.action = OPKG_INSTALL;
409 pdata.pkg = pkg;
410 progress(pdata, 0);
411
412 err = opkg_upgrade_pkg(pkg);
413 if (err) {
414 return -1;
415 }
416 progress(pdata, 75);
417
418 err = opkg_configure_packages(NULL);
419 if (err) {
420 return -1;
421 }
422
423 /* write out status files and file lists */
424 opkg_conf_write_status_files();
425 pkg_write_changed_filelists();
426
427 progress(pdata, 100);
428 return 0;
429 }
430
431 int
432 opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
433 {
434 pkg_vec_t *installed;
435 int err = 0;
436 int i;
437 pkg_t *pkg;
438 opkg_progress_data_t pdata;
439
440 pdata.action = OPKG_INSTALL;
441 pdata.pkg = NULL;
442
443 progress(pdata, 0);
444
445 installed = pkg_vec_alloc();
446 pkg_info_preinstall_check();
447
448 pkg_hash_fetch_all_installed(installed);
449 for (i = 0; i < installed->len; i++) {
450 pkg = installed->pkgs[i];
451
452 pdata.pkg = pkg;
453 progress(pdata, 99 * i / installed->len);
454
455 err += opkg_upgrade_pkg(pkg);
456 }
457 pkg_vec_free(installed);
458
459 if (err)
460 return 1;
461
462 err = opkg_configure_packages(NULL);
463 if (err)
464 return 1;
465
466 /* write out status files and file lists */
467 opkg_conf_write_status_files();
468 pkg_write_changed_filelists();
469
470 pdata.pkg = NULL;
471 progress(pdata, 100);
472 return 0;
473 }
474
475 int
476 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
477 void *user_data)
478 {
479 char *tmp;
480 int err, result = 0;
481 char *lists_dir;
482 pkg_src_list_elt_t *iter;
483 pkg_src_t *src;
484 int sources_list_count, sources_done;
485 opkg_progress_data_t pdata;
486
487 pdata.action = OPKG_DOWNLOAD;
488 pdata.pkg = NULL;
489 progress(pdata, 0);
490
491 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
492 ? conf->default_dest->lists_dir : conf->lists_dir);
493
494 if (!file_is_dir(lists_dir)) {
495 if (file_exists(lists_dir)) {
496 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
497 free(lists_dir);
498 return 1;
499 }
500
501 err = file_mkdir_hier(lists_dir, 0755);
502 if (err) {
503 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
504 lists_dir);
505 free(lists_dir);
506 return 1;
507 }
508 }
509
510 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
511 if (mkdtemp(tmp) == NULL) {
512 opkg_perror(ERROR, "Coundn't create temporary directory %s",
513 tmp);
514 free(lists_dir);
515 free(tmp);
516 return 1;
517 }
518
519 /* count the number of sources so we can give some progress updates */
520 sources_list_count = 0;
521 sources_done = 0;
522 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
523 sources_list_count++;
524 }
525
526 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
527 char *url, *list_file_name = NULL;
528
529 src = (pkg_src_t *) iter->data;
530
531 if (src->extra_data) /* debian style? */
532 sprintf_alloc(&url, "%s/%s/%s", src->value,
533 src->extra_data,
534 src->gzip ? "Packages.gz" : "Packages");
535 else
536 sprintf_alloc(&url, "%s/%s", src->value,
537 src->gzip ? "Packages.gz" : "Packages");
538
539 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
540
541 if (opkg_download(url, list_file_name, 0)) {
542 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
543 result = -1;
544 }
545 free(url);
546
547 #if defined(HAVE_USIGN)
548 if (conf->check_signature) {
549 char *sig_file_name;
550 /* download detached signitures to verify the package lists */
551 /* get the url for the sig file */
552 if (src->extra_data) /* debian style? */
553 sprintf_alloc(&url, "%s/%s/%s", src->value,
554 src->extra_data, "Packages.sig");
555 else
556 sprintf_alloc(&url, "%s/%s", src->value,
557 "Packages.sig");
558
559 /* create filename for signature */
560 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
561 src->name);
562
563 /* make sure there is no existing signature file */
564 unlink(sig_file_name);
565
566 err = opkg_download(url, sig_file_name, 0);
567 if (err) {
568 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
569 } else {
570 int err;
571 err = opkg_verify_file(list_file_name,
572 sig_file_name);
573 if (err == 0) {
574 opkg_msg(INFO, "Signature check "
575 "passed for %s",
576 list_file_name);
577 } else {
578 opkg_msg(ERROR, "Signature check "
579 "failed for %s",
580 list_file_name);
581 }
582 }
583 free(sig_file_name);
584 free(url);
585 }
586 #else
587 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
588 " has not been enabled in this build\n",
589 list_file_name);
590 #endif
591 free(list_file_name);
592
593 sources_done++;
594 progress(pdata, 100 * sources_done / sources_list_count);
595 }
596
597 rmdir(tmp);
598 free(tmp);
599 free(lists_dir);
600
601 /* Now re-read the package lists to update package hash tables. */
602 opkg_re_read_config_files();
603
604 return result;
605 }
606
607 static int pkg_compare_names_and_version(const void *a0, const void *b0)
608 {
609 const pkg_t *a = *(const pkg_t **)a0;
610 const pkg_t *b = *(const pkg_t **)b0;
611 int ret;
612
613 ret = strcmp(a->name, b->name);
614
615 if (ret == 0)
616 ret = pkg_compare_versions(a, b);
617
618 return ret;
619 }
620
621 int opkg_list_packages(opkg_package_callback_t callback, void *user_data)
622 {
623 pkg_vec_t *all;
624 int i;
625
626 opkg_assert(callback);
627
628 all = pkg_vec_alloc();
629 pkg_hash_fetch_available(all);
630
631 pkg_vec_sort(all, pkg_compare_names_and_version);
632
633 for (i = 0; i < all->len; i++) {
634 pkg_t *pkg;
635
636 pkg = all->pkgs[i];
637
638 callback(pkg, user_data);
639 }
640
641 pkg_vec_free(all);
642
643 return 0;
644 }
645
646 int
647 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
648 {
649 struct active_list *head;
650 struct active_list *node;
651 pkg_t *old = NULL, *new = NULL;
652
653 opkg_assert(callback);
654
655 /* ensure all data is valid */
656 pkg_info_preinstall_check();
657
658 head = prepare_upgrade_list();
659 for (node = active_list_next(head, head); node;
660 node = active_list_next(head, node)) {
661 old = node->pkg;
662 new =
663 pkg_hash_fetch_best_installation_candidate_by_name(old->
664 name);
665 if (new == NULL)
666 continue;
667 callback(new, user_data);
668 }
669 active_list_head_delete(head);
670 return 0;
671 }
672
673 pkg_t *opkg_find_package(const char *name, const char *ver, const char *arch,
674 const char *repo)
675 {
676 int pkg_found = 0;
677 pkg_t *pkg = NULL;
678 pkg_vec_t *all;
679 int i;
680 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
681
682 all = pkg_vec_alloc();
683 pkg_hash_fetch_available(all);
684 for (i = 0; i < all->len; i++) {
685 char *pkgv;
686
687 pkg = all->pkgs[i];
688
689 /* check name */
690 if (sstrcmp(pkg->name, name))
691 continue;
692
693 /* check version */
694 pkgv = pkg_version_str_alloc(pkg);
695 if (sstrcmp(pkgv, ver)) {
696 free(pkgv);
697 continue;
698 }
699 free(pkgv);
700
701 /* check architecture */
702 if (arch) {
703 if (sstrcmp(pkg_get_architecture(pkg), arch))
704 continue;
705 }
706
707 /* check repository */
708 if (repo) {
709 if (sstrcmp(pkg->src->name, repo))
710 continue;
711 }
712
713 /* match found */
714 pkg_found = 1;
715 break;
716 }
717
718 pkg_vec_free(all);
719
720 return pkg_found ? pkg : NULL;
721 }
722
723 /**
724 * @brief Check the accessibility of repositories.
725 * @return return how many repositories cannot access. 0 means all okay.
726 */
727 int opkg_repository_accessibility_check(void)
728 {
729 pkg_src_list_elt_t *iter;
730 str_list_elt_t *iter1;
731 str_list_t *src;
732 int repositories = 0;
733 int ret = 0;
734 char *repo_ptr;
735 char *stmp;
736 char *host, *end;
737
738 src = str_list_alloc();
739
740 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
741 host = strstr(((pkg_src_t *) iter->data)->value, "://") + 3;
742 end = index(host, '/');
743 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
744 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
745 end -
746 ((pkg_src_t *) iter->data)->value);
747 else
748 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
749
750 for (iter1 = str_list_first(src); iter1;
751 iter1 = str_list_next(src, iter1)) {
752 if (strstr(iter1->data, stmp))
753 break;
754 }
755 if (iter1)
756 continue;
757
758 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
759 free(stmp);
760
761 str_list_append(src, repo_ptr);
762 free(repo_ptr);
763 repositories++;
764 }
765
766 while (repositories > 0) {
767 iter1 = str_list_pop(src);
768 repositories--;
769
770 if (opkg_download(iter1->data, "/dev/null", 0))
771 ret++;
772 str_list_elt_deinit(iter1);
773 }
774
775 free(src);
776
777 return ret;
778 }