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