1 /* opkg.c - the opkg package management system
3 Thomas Wood <thomas@openedhand.com>
5 Copyright (C) 2008 OpenMoko Inc
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.
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.
22 #include "opkg_conf.h"
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 #include "opkg_error.h"
32 #include "sprintf_alloc.h"
33 #include "file_util.h"
35 #include <libbb/libbb.h>
37 struct errlist* error_list;
43 opkg_option_t *options;
46 #define opkg_assert(expr) if (!(expr)) { \
47 printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
48 __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
50 #define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (opkg, &d, user_data);
51 #define SSTRCMP(x,y) (x && y) ? strcmp (x, y) : 0
53 /** Private Functions ***/
58 static opkg_package_t*
59 pkg_clone (pkg_t *old)
63 new = opkg_package_new ();
65 #define sstrdup(x) (x) ? strdup (x) : NULL;
67 new->name = sstrdup (old->name);
68 new->version = pkg_version_str_alloc (old);
69 new->architecture = sstrdup (old->architecture);
71 new->repository = sstrdup (old->src->name);
72 new->description = sstrdup (old->description);
73 new->tags = sstrdup (old->tags);
74 new->url = sstrdup (old->url);
78 new->size = (old->size) ? atoi (old->size) : 0;
79 new->installed = (old->state_status == SS_INSTALLED);
85 opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
92 all = pkg_vec_alloc ();
93 pkg_hash_fetch_available (&conf->pkg_hash, all);
95 for (i = 0; i < all->len; i++)
99 if (pkg_name && fnmatch (pkg_name, pkg->name, 0))
102 if (pkg->state_status == SS_UNPACKED)
104 r = opkg_configure (conf, pkg);
107 pkg->state_status = SS_INSTALLED;
108 pkg->parent->state_status = SS_INSTALLED;
109 pkg->state_flag &= ~SF_PREFER;
125 opkg_progress_callback_t cb;
126 opkg_progress_data_t *progress_data;
134 curl_progress_cb (struct _curl_cb_data *cb_data,
135 double t, /* dltotal */
136 double d, /* dlnow */
140 int p = (t) ? d * 100 / t : 0;
141 static int prev = -1;
144 /* prevent the same value being sent twice (can occur due to rounding) */
152 progress = cb_data->start_range + (d / t * ((cb_data->finish_range - cb_data->start_range)));
153 cb_data->progress_data->percentage = progress;
155 (cb_data->cb)(cb_data->opkg,
156 cb_data->progress_data,
171 p = calloc (1, sizeof (opkg_package_t));
177 opkg_package_free (opkg_package_t *p)
181 free (p->architecture);
182 free (p->description);
185 free (p->repository);
196 opkg = calloc (1, sizeof (opkg_t));
198 opkg->args = calloc (1, sizeof (args_t));
199 err = args_init (opkg->args);
207 opkg->conf = calloc (1, sizeof (opkg_conf_t));
208 err = opkg_conf_init (opkg->conf, opkg->args);
217 opkg_init_options_array (opkg->conf, &opkg->options);
222 opkg_free (opkg_t *opkg)
224 opkg_assert (opkg != NULL);
226 opkg_conf_deinit (opkg->conf);
227 args_deinit (opkg->args);
228 free (opkg->options);
235 opkg_re_read_config_files (opkg_t *opkg)
240 opkg_assert (opkg != NULL);
245 /* Unfortunatly, the easiest way to re-read the config files right now is to
246 * throw away opkg->conf and start again */
248 /* copy the settings we need to keep */
249 a->autoremove = c->autoremove;
250 a->force_depends = c->force_depends;
251 a->force_defaults = c->force_defaults;
252 a->force_overwrite = c->force_overwrite;
253 a->force_downgrade = c->force_downgrade;
254 a->force_reinstall = c->force_reinstall;
255 a->force_removal_of_dependent_packages = c->force_removal_of_dependent_packages;
256 a->force_removal_of_essential_packages = c->force_removal_of_essential_packages;
257 a->nodeps = c->nodeps;
258 a->noaction = c->noaction;
259 a->query_all = c->query_all;
260 a->verbosity = c->verbosity;
264 if (a->offline_root) free (a->offline_root);
265 a->offline_root = strdup (c->offline_root);
268 if (c->offline_root_pre_script_cmd)
270 if (a->offline_root_pre_script_cmd) free (a->offline_root_pre_script_cmd);
271 a->offline_root_pre_script_cmd = strdup (c->offline_root_pre_script_cmd);
274 if (c->offline_root_post_script_cmd)
276 if (a->offline_root_post_script_cmd) free (a->offline_root_post_script_cmd);
277 a->offline_root_post_script_cmd = strdup (c->offline_root_post_script_cmd);
283 a->cache = strdup(c->cache);
286 /* throw away old opkg_conf and start again */
287 opkg_conf_deinit (opkg->conf);
288 opkg_conf_init (opkg->conf, opkg->args);
290 free (opkg->options);
291 opkg_init_options_array (opkg->conf, &opkg->options);
297 opkg_get_option (opkg_t *opkg, char *option, void **value)
300 opkg_option_t *options;
302 opkg_assert (opkg != NULL);
303 opkg_assert (option != NULL);
304 opkg_assert (value != NULL);
306 options = opkg->options;
308 /* look up the option
309 * TODO: this would be much better as a hash table
311 while (options[i].name)
313 if (strcmp (options[i].name, option) != 0)
321 switch (options[i].type)
323 case OPKG_OPT_TYPE_BOOL:
324 *((int *) value) = *((int *) options[i].value);
327 case OPKG_OPT_TYPE_INT:
328 *((int *) value) = *((int *) options[i].value);
331 case OPKG_OPT_TYPE_STRING:
332 *((char **)value) = strdup (options[i].value);
339 opkg_set_option (opkg_t *opkg, char *option, void *value)
341 int i = 0, found = 0;
342 opkg_option_t *options;
344 opkg_assert (opkg != NULL);
345 opkg_assert (option != NULL);
346 opkg_assert (value != NULL);
348 options = opkg->options;
350 /* look up the option
351 * TODO: this would be much better as a hash table
353 while (options[i].name)
355 if (strcmp (options[i].name, option) == 0)
365 /* XXX: Warning: Option not found */
370 switch (options[i].type)
372 case OPKG_OPT_TYPE_BOOL:
373 if (*((int *) value) == 0)
374 *((int *)options[i].value) = 0;
376 *((int *)options[i].value) = 1;
379 case OPKG_OPT_TYPE_INT:
380 *((int *) options[i].value) = *((int *) value);
383 case OPKG_OPT_TYPE_STRING:
384 *((char **)options[i].value) = strdup (value);
391 opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
394 char *stripped_filename;
395 opkg_progress_data_t pdata;
397 pkg_vec_t *deps, *all;
399 char **unresolved = NULL;
401 opkg_assert (opkg != NULL);
402 opkg_assert (package_name != NULL);
405 pkg_info_preinstall_check (opkg->conf);
408 /* check to ensure package is not already installed */
409 old = pkg_hash_fetch_installed_by_name(&opkg->conf->pkg_hash, package_name);
412 /* XXX: Error: Package is already installed. */
413 return OPKG_PACKAGE_ALREADY_INSTALLED;
416 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, package_name, NULL);
419 /* XXX: Error: Could not find package to install */
420 return OPKG_PACKAGE_NOT_FOUND;
423 new->state_flag |= SF_USER;
425 pdata.action = OPKG_INSTALL;
426 pdata.package = pkg_clone (new);
430 /* find dependancies and download them */
431 deps = pkg_vec_alloc ();
432 /* this function does not return the original package, so we insert it later */
433 ndepends = pkg_hash_fetch_unsatisfied_dependencies (opkg->conf, new, deps, &unresolved);
436 /* XXX: Error: Could not satisfy dependencies */
438 return OPKG_DEPENDENCIES_FAILED;
441 /* insert the package we are installing so that we download it */
442 pkg_vec_insert (deps, new);
444 /* download package and dependancies */
445 for (i = 0; i < deps->len; i++)
448 struct _curl_cb_data cb_data;
452 if (pkg->local_filename)
455 opkg_package_free (pdata.package);
456 pdata.package = pkg_clone (pkg);
457 pdata.action = OPKG_DOWNLOAD;
459 if (pkg->src == NULL)
461 /* XXX: Error: Package not available from any configured src */
462 return OPKG_PACKAGE_NOT_AVAILABLE;
465 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
467 /* Get the filename part, without any directory */
468 stripped_filename = strrchr(pkg->filename, '/');
469 if ( ! stripped_filename )
470 stripped_filename = pkg->filename;
472 sprintf_alloc(&pkg->local_filename, "%s/%s", opkg->conf->tmp_dir, stripped_filename);
474 cb_data.cb = progress_callback;
475 cb_data.progress_data = &pdata;
477 cb_data.user_data = user_data;
478 /* 75% of "install" progress is for downloading */
479 cb_data.start_range = 75 * i / deps->len;
480 cb_data.finish_range = 75 * (i + 1) / deps->len;
482 err = opkg_download(opkg->conf, url, pkg->local_filename,
483 (curl_progress_func) curl_progress_cb, &cb_data);
489 opkg_package_free (pdata.package);
490 return OPKG_DOWNLOAD_FAILED;
496 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
497 all = pkg_vec_alloc ();
498 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
499 for (i = 0; i < all->len; i++)
501 all->pkgs[i]->parent->dependencies_checked = 0;
506 /* 75% of "install" progress is for downloading */
507 opkg_package_free (pdata.package);
508 pdata.package = pkg_clone (new);
509 pdata.action = OPKG_INSTALL;
510 progress (pdata, 75);
512 /* unpack the package */
513 err = opkg_install_pkg(opkg->conf, new, 0);
517 opkg_package_free (pdata.package);
520 case OPKG_INSTALL_ERR_NOT_TRUSTED: return OPKG_GPG_ERROR;
521 case OPKG_INSTALL_ERR_DOWNLOAD: return OPKG_DOWNLOAD_FAILED;
522 case OPKG_INSTALL_ERR_DEPENDENCIES:
523 case OPKG_INSTALL_ERR_CONFLICTS: return OPKG_DEPENDENCIES_FAILED;
524 case OPKG_INSTALL_ERR_ALREADY_INSTALLED: return OPKG_PACKAGE_ALREADY_INSTALLED;
525 case OPKG_INSTALL_ERR_SIGNATURE: return OPKG_GPG_ERROR;
526 case OPKG_INSTALL_ERR_MD5: return OPKG_MD5_ERROR;
527 default: return OPKG_UNKNOWN_ERROR;
531 progress (pdata, 75);
533 /* run configure scripts, etc. */
534 err = opkg_configure_packages (opkg->conf, NULL);
537 opkg_package_free (pdata.package);
538 return OPKG_UNKNOWN_ERROR;
541 /* write out status files and file lists */
542 opkg_conf_write_status_files (opkg->conf);
543 pkg_write_changed_filelists (opkg->conf);
545 progress (pdata, 100);
546 opkg_package_free (pdata.package);
551 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
555 pkg_t *pkg_to_remove;
556 opkg_progress_data_t pdata;
558 opkg_assert (opkg != NULL);
559 opkg_assert (package_name != NULL);
561 pkg_info_preinstall_check (opkg->conf);
563 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
567 /* XXX: Error: Package not installed. */
568 return OPKG_PACKAGE_NOT_INSTALLED;
571 pdata.action = OPKG_REMOVE;
572 pdata.package = pkg_clone (pkg);
576 if (pkg->state_status == SS_NOT_INSTALLED)
578 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
579 opkg_package_free (pdata.package);
580 return OPKG_PACKAGE_NOT_INSTALLED;
582 progress (pdata, 25);
584 if (opkg->conf->restrict_to_default_dest)
586 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
588 opkg->conf->default_dest);
592 pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
596 progress (pdata, 75);
598 err = opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
600 /* write out status files and file lists */
601 opkg_conf_write_status_files (opkg->conf);
602 pkg_write_changed_filelists (opkg->conf);
605 progress (pdata, 100);
606 opkg_package_free (pdata.package);
607 return (err) ? OPKG_UNKNOWN_ERROR : OPKG_NO_ERROR;
611 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
615 opkg_progress_data_t pdata;
619 opkg_assert (opkg != NULL);
620 opkg_assert (package_name != NULL);
622 pkg_info_preinstall_check (opkg->conf);
624 if (opkg->conf->restrict_to_default_dest)
626 pkg = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
628 opkg->conf->default_dest);
631 /* XXX: Error: Package not installed in default_dest */
632 return OPKG_PACKAGE_NOT_INSTALLED;
637 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash,
643 /* XXX: Error: Package not installed */
644 return OPKG_PACKAGE_NOT_INSTALLED;
647 pdata.action = OPKG_INSTALL;
648 pdata.package = pkg_clone (pkg);
651 err = opkg_upgrade_pkg (opkg->conf, pkg);
652 /* opkg_upgrade_pkg returns the error codes of opkg_install_pkg */
656 opkg_package_free (pdata.package);
659 case OPKG_INSTALL_ERR_NOT_TRUSTED: return OPKG_GPG_ERROR;
660 case OPKG_INSTALL_ERR_DOWNLOAD: return OPKG_DOWNLOAD_FAILED;
661 case OPKG_INSTALL_ERR_DEPENDENCIES:
662 case OPKG_INSTALL_ERR_CONFLICTS: return OPKG_DEPENDENCIES_FAILED;
663 case OPKG_INSTALL_ERR_ALREADY_INSTALLED: return OPKG_PACKAGE_ALREADY_INSTALLED;
664 case OPKG_INSTALL_ERR_SIGNATURE: return OPKG_GPG_ERROR;
665 case OPKG_INSTALL_ERR_MD5: return OPKG_MD5_ERROR;
666 default: return OPKG_UNKNOWN_ERROR;
669 progress (pdata, 75);
671 err = opkg_configure_packages (opkg->conf, NULL);
673 opkg_package_free (pdata.package);
674 return OPKG_UNKNOWN_ERROR;
677 /* write out status files and file lists */
678 opkg_conf_write_status_files (opkg->conf);
679 pkg_write_changed_filelists (opkg->conf);
681 progress (pdata, 100);
682 opkg_package_free (pdata.package);
687 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
689 pkg_vec_t *installed;
693 opkg_progress_data_t pdata;
695 pdata.action = OPKG_INSTALL;
696 pdata.package = NULL;
698 opkg_assert (opkg != NULL);
701 installed = pkg_vec_alloc ();
702 pkg_info_preinstall_check (opkg->conf);
704 pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed);
705 for (i = 0; i < installed->len; i++)
707 pkg = installed->pkgs[i];
709 pdata.package = pkg_clone (pkg);
710 progress (pdata, 99 * i / installed->len);
711 opkg_package_free (pdata.package);
713 err += opkg_upgrade_pkg (opkg->conf, pkg);
715 pkg_vec_free (installed);
720 err = opkg_configure_packages (opkg->conf, NULL);
724 pdata.package = NULL;
725 progress (pdata, 100);
730 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
735 pkg_src_list_elt_t *iter;
737 int sources_list_count, sources_done;
738 opkg_progress_data_t pdata;
740 opkg_assert (opkg != NULL);
742 pdata.action = OPKG_DOWNLOAD;
743 pdata.package = NULL;
746 sprintf_alloc (&lists_dir, "%s",
747 (opkg->conf->restrict_to_default_dest)
748 ? opkg->conf->default_dest->lists_dir
749 : opkg->conf->lists_dir);
751 if (!file_is_dir (lists_dir))
753 if (file_exists (lists_dir))
755 /* XXX: Error: file exists but is not a directory */
760 err = file_mkdir_hier (lists_dir, 0755);
763 /* XXX: Error: failed to create directory */
769 tmp = strdup ("/tmp/opkg.XXXXXX");
771 if (mkdtemp (tmp) == NULL)
773 /* XXX: Error: could not create temporary file name */
779 /* count the number of sources so we can give some progress updates */
780 sources_list_count = 0;
782 iter = opkg->conf->pkg_src_list.head;
785 sources_list_count++;
789 for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
791 char *url, *list_file_name = NULL;
795 if (src->extra_data) /* debian style? */
796 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
797 src->gzip ? "Packages.gz" : "Packages");
799 sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
801 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
805 struct _curl_cb_data cb_data;
806 char *tmp_file_name = NULL;
808 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
810 /* XXX: Note: downloading url */
812 cb_data.cb = progress_callback;
813 cb_data.progress_data = &pdata;
815 cb_data.user_data = user_data;
816 cb_data.start_range = 100 * sources_done / sources_list_count;
817 cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
819 err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
823 /* XXX: Note: Inflating downloaded file */
824 in = fopen (tmp_file_name, "r");
825 out = fopen (list_file_name, "w");
834 unlink (tmp_file_name);
836 free (tmp_file_name);
839 err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
843 /* XXX: Error: download error */
844 result = OPKG_DOWNLOAD_FAILED;
850 /* download detached signitures to verify the package lists */
851 /* get the url for the sig file */
852 if (src->extra_data) /* debian style? */
853 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
856 sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
858 /* create filename for signature */
859 sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, src->name);
861 /* make sure there is no existing signature file */
862 unlink (sig_file_name);
864 err = opkg_download (opkg->conf, url, sig_file_name, NULL, NULL);
867 /* XXX: Warning: Download failed */
872 err = opkg_verify_file (opkg->conf, list_file_name, sig_file_name);
875 /* XXX: Notice: Signature check passed */
879 /* XXX: Warning: Signature check failed */
882 free (sig_file_name);
883 free (list_file_name);
886 /* XXX: Note: Signature check for %s skipped because GPG support was not
887 * enabled in this build
892 progress (pdata, 100 * sources_done / sources_list_count);
899 /* Now re-read the package lists to update package hash tables. */
900 opkg_re_read_config_files (opkg);
907 opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
913 opkg_assert (callback);
915 all = pkg_vec_alloc ();
916 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
917 for (i = 0; i < all->len; i++)
920 opkg_package_t *package;
924 package = pkg_clone (pkg);
925 callback (opkg, package, user_data);
926 opkg_package_free (package);
935 opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
941 opkg_assert (callback);
943 /* ensure all data is valid */
944 pkg_info_preinstall_check (opkg->conf);
946 all = opkg_upgrade_all_list_get (opkg->conf);
947 for (i = 0; i < all->len; i++)
950 opkg_package_t *package;
954 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, old->name, NULL);
956 package = pkg_clone (new);
957 callback (opkg, package, user_data);
958 opkg_package_free (package);
967 opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *arch, const char *repo)
970 opkg_package_t *package = NULL;
972 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
976 all = pkg_vec_alloc ();
977 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
978 for (i = 0; i < all->len; i++)
986 if (sstrcmp (pkg->name, name))
990 pkgv = pkg_version_str_alloc (pkg);
991 if (sstrcmp (pkgv, ver))
998 /* check architecture */
1001 if (sstrcmp (pkg->architecture, arch))
1005 /* check repository */
1008 if (sstrcmp (pkg->src->name, repo))
1013 package = pkg_clone (pkg);
1022 #include <curl/curl.h>
1024 * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status.
1025 * @param opkg The opkg_t
1026 * @return return how many repositories cannot access. 0 means all okay.
1028 int opkg_repository_accessibility_check(opkg_t *opkg)
1030 pkg_src_list_elt_t *iter;
1031 str_list_elt_t *iter1;
1038 opkg_assert(opkg != NULL);
1040 src = str_list_alloc();
1042 for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
1044 if (strstr(iter->data->value, "://") &&
1045 index(strstr(iter->data->value, "://") + 3, '/'))
1046 stmp = strndup(iter->data->value,
1047 (index(strstr(iter->data->value, "://") + 3, '/') - iter->data->value)*sizeof(char));
1050 stmp = strdup(iter->data->value);
1052 for (iter1 = src->head; iter1; iter1 = iter1->next)
1054 if (strstr(iter1->data, stmp))
1060 sprintf_alloc(&repo_ptr, "%s/index.html",stmp);
1063 str_list_append(src, repo_ptr);
1066 while (repositories > 0)
1068 iter1 = str_list_pop(src);
1071 err = opkg_download(opkg->conf, iter1->data, "/dev/null", NULL, NULL);
1072 if (!(err == CURLE_OK ||
1073 err == CURLE_HTTP_RETURNED_ERROR ||
1074 err == CURLE_FILE_COULDNT_READ_FILE ||
1075 err == CURLE_REMOTE_FILE_NOT_FOUND ||
1076 err == CURLE_TFTP_NOTFOUND
1080 str_list_elt_deinit(iter1);