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