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>
41 opkg_option_t *options;
44 #define opkg_assert(expr) if (!(expr)) { \
45 printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
46 __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
48 #define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (opkg, &d, user_data);
49 #define SSTRCMP(x,y) (x && y) ? strcmp (x, y) : 0
51 /** Private Functions ***/
53 static opkg_package_t*
54 old_pkg_to_new (pkg_t *old)
58 new = opkg_package_new ();
60 #define sstrdup(x) (x) ? strdup (x) : NULL;
62 new->name = sstrdup (old->name);
63 new->version = pkg_version_str_alloc (old);
64 new->architecture = sstrdup (old->architecture);
66 new->repository = sstrdup (old->src->name);
67 new->description = sstrdup (old->description);
68 new->tags = sstrdup (old->tags);
69 new->url = sstrdup (old->url);
71 new->size = (old->size) ? atoi (old->size) : 0;
72 new->installed = (old->state_status == SS_INSTALLED);
78 opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
85 all = pkg_vec_alloc ();
86 pkg_hash_fetch_available (&conf->pkg_hash, all);
88 for (i = 0; i < all->len; i++)
92 if (pkg_name && fnmatch (pkg_name, pkg->name, 0))
95 if (pkg->state_status == SS_UNPACKED)
97 r = opkg_configure (conf, pkg);
100 pkg->state_status = SS_INSTALLED;
101 pkg->parent->state_status = SS_INSTALLED;
102 pkg->state_flag &= ~SF_PREFER;
118 opkg_progress_callback_t cb;
119 opkg_progress_data_t *progress_data;
127 curl_progress_cb (struct _curl_cb_data *cb_data,
128 double t, /* dltotal */
129 double d, /* dlnow */
133 int p = (t) ? d * 100 / t : 0;
134 static int prev = -1;
137 /* prevent the same value being sent twice (can occur due to rounding) */
145 progress = cb_data->start_range + (d / t * ((cb_data->finish_range - cb_data->start_range)));
146 cb_data->progress_data->percentage = progress;
148 (cb_data->cb)(cb_data->opkg,
149 cb_data->progress_data,
164 p = malloc (sizeof (opkg_package_t));
165 memset (p, 0, sizeof (opkg_package_t));
171 opkg_package_free (opkg_package_t *p)
175 free (p->architecture);
176 free (p->description);
179 free (p->repository);
190 opkg = malloc (sizeof (opkg_t));
192 opkg->args = malloc (sizeof (args_t));
193 err = args_init (opkg->args);
201 opkg->conf = malloc (sizeof (opkg_conf_t));
202 err = opkg_conf_init (opkg->conf, opkg->args);
211 opkg_init_options_array (opkg->conf, &opkg->options);
216 opkg_free (opkg_t *opkg)
218 opkg_assert (opkg != NULL);
220 opkg_conf_deinit (opkg->conf);
221 args_deinit (opkg->args);
222 free (opkg->options);
229 opkg_re_read_config_files (opkg_t *opkg)
234 opkg_assert (opkg != NULL);
239 /* Unfortunatly, the easiest way to re-read the config files right now is to
240 * throw away opkg->conf and start again */
242 /* copy the settings we need to keep */
243 a->autoremove = c->autoremove;
244 a->force_depends = c->force_depends;
245 a->force_defaults = c->force_defaults;
246 a->force_overwrite = c->force_overwrite;
247 a->force_downgrade = c->force_downgrade;
248 a->force_reinstall = c->force_reinstall;
249 a->force_removal_of_dependent_packages = c->force_removal_of_dependent_packages;
250 a->force_removal_of_essential_packages = c->force_removal_of_essential_packages;
251 a->nodeps = c->nodeps;
252 a->noaction = c->noaction;
253 a->query_all = c->query_all;
254 a->verbosity = c->verbosity;
258 if (a->offline_root) free (a->offline_root);
259 a->offline_root = strdup (c->offline_root);
262 if (c->offline_root_pre_script_cmd)
264 if (a->offline_root_pre_script_cmd) free (a->offline_root_pre_script_cmd);
265 a->offline_root_pre_script_cmd = strdup (c->offline_root_pre_script_cmd);
268 if (c->offline_root_post_script_cmd)
270 if (a->offline_root_post_script_cmd) free (a->offline_root_post_script_cmd);
271 a->offline_root_post_script_cmd = strdup (c->offline_root_post_script_cmd);
274 /* throw away old opkg_conf and start again */
275 opkg_conf_deinit (opkg->conf);
276 opkg_conf_init (opkg->conf, opkg->args);
278 free (opkg->options);
279 opkg_init_options_array (opkg->conf, &opkg->options);
285 opkg_get_option (opkg_t *opkg, char *option, void **value)
288 opkg_option_t *options;
290 opkg_assert (opkg != NULL);
291 opkg_assert (option != NULL);
292 opkg_assert (value != NULL);
294 options = opkg->options;
296 /* look up the option
297 * TODO: this would be much better as a hash table
299 while (options[i].name)
301 if (strcmp (options[i].name, option) != 0)
309 switch (options[i].type)
311 case OPKG_OPT_TYPE_BOOL:
312 *((int *) value) = *((int *) options[i].value);
315 case OPKG_OPT_TYPE_INT:
316 *((int *) value) = *((int *) options[i].value);
319 case OPKG_OPT_TYPE_STRING:
320 *((char **)value) = strdup (options[i].value);
327 opkg_set_option (opkg_t *opkg, char *option, void *value)
329 int i = 0, found = 0;
330 opkg_option_t *options;
332 opkg_assert (opkg != NULL);
333 opkg_assert (option != NULL);
334 opkg_assert (value != NULL);
336 options = opkg->options;
338 /* look up the option
339 * TODO: this would be much better as a hash table
341 while (options[i].name)
343 if (strcmp (options[i].name, option) == 0)
353 /* XXX: Warning: Option not found */
358 switch (options[i].type)
360 case OPKG_OPT_TYPE_BOOL:
361 if (*((int *) value) == 0)
362 *((int *)options[i].value) = 0;
364 *((int *)options[i].value) = 1;
367 case OPKG_OPT_TYPE_INT:
368 *((int *) options[i].value) = *((int *) value);
371 case OPKG_OPT_TYPE_STRING:
372 *((char **)options[i].value) = strdup (value);
379 opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
382 char *stripped_filename;
383 opkg_progress_data_t pdata;
385 pkg_vec_t *deps, *all;
387 char **unresolved = NULL;
389 opkg_assert (opkg != NULL);
390 opkg_assert (package_name != NULL);
393 pkg_info_preinstall_check (opkg->conf);
396 /* check to ensure package is not already installed */
397 old = pkg_hash_fetch_installed_by_name(&opkg->conf->pkg_hash, package_name);
400 /* XXX: Error: Package is already installed. */
401 return OPKG_PACKAGE_ALREADY_INSTALLED;
404 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, package_name, NULL);
407 /* XXX: Error: Could not find package to install */
408 return OPKG_PACKAGE_NOT_FOUND;
411 new->state_flag |= SF_USER;
413 pdata.action = OPKG_INSTALL;
414 pdata.package = old_pkg_to_new (new);
418 /* find dependancies and download them */
419 deps = pkg_vec_alloc ();
420 /* this function does not return the original package, so we insert it later */
421 ndepends = pkg_hash_fetch_unsatisfied_dependencies (opkg->conf, new, deps, &unresolved);
424 /* XXX: Error: Could not satisfy dependencies */
426 return OPKG_DEPENDENCIES_FAILED;
429 /* insert the package we are installing so that we download it */
430 pkg_vec_insert (deps, new);
432 /* download package and dependancies */
433 for (i = 0; i < deps->len; i++)
436 struct _curl_cb_data cb_data;
440 if (pkg->local_filename)
443 opkg_package_free (pdata.package);
444 pdata.package = old_pkg_to_new (pkg);
445 pdata.action = OPKG_DOWNLOAD;
447 if (pkg->src == NULL)
449 /* XXX: Error: Package not available from any configured src */
450 return OPKG_PACKAGE_NOT_AVAILABLE;
453 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
455 /* Get the filename part, without any directory */
456 stripped_filename = strrchr(pkg->filename, '/');
457 if ( ! stripped_filename )
458 stripped_filename = pkg->filename;
460 sprintf_alloc(&pkg->local_filename, "%s/%s", opkg->conf->tmp_dir, stripped_filename);
462 cb_data.cb = progress_callback;
463 cb_data.progress_data = &pdata;
465 cb_data.user_data = user_data;
466 /* 75% of "install" progress is for downloading */
467 cb_data.start_range = 75 * i / deps->len;
468 cb_data.finish_range = 75 * (i + 1) / deps->len;
470 err = opkg_download(opkg->conf, url, pkg->local_filename,
471 (curl_progress_func) curl_progress_cb, &cb_data);
477 opkg_package_free (pdata.package);
478 return OPKG_DOWNLOAD_FAILED;
484 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
485 all = pkg_vec_alloc ();
486 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
487 for (i = 0; i < all->len; i++)
489 all->pkgs[i]->parent->dependencies_checked = 0;
494 /* 75% of "install" progress is for downloading */
495 opkg_package_free (pdata.package);
496 pdata.package = old_pkg_to_new (new);
497 pdata.action = OPKG_INSTALL;
498 progress (pdata, 75);
500 /* unpack the package */
501 err = opkg_install_pkg(opkg->conf, new, 0);
505 opkg_package_free (pdata.package);
508 case OPKG_INSTALL_ERR_NOT_TRUSTED: return OPKG_GPG_ERROR;
509 case OPKG_INSTALL_ERR_DOWNLOAD: return OPKG_DOWNLOAD_FAILED;
510 case OPKG_INSTALL_ERR_DEPENDENCIES:
511 case OPKG_INSTALL_ERR_CONFLICTS: return OPKG_DEPENDENCIES_FAILED;
512 case OPKG_INSTALL_ERR_ALREADY_INSTALLED: return OPKG_PACKAGE_ALREADY_INSTALLED;
513 case OPKG_INSTALL_ERR_SIGNATURE: return OPKG_GPG_ERROR;
514 case OPKG_INSTALL_ERR_MD5: return OPKG_MD5_ERROR;
515 default: return OPKG_UNKNOWN_ERROR;
519 progress (pdata, 75);
521 /* run configure scripts, etc. */
522 err = opkg_configure_packages (opkg->conf, NULL);
525 opkg_package_free (pdata.package);
526 return OPKG_UNKNOWN_ERROR;
529 /* write out status files and file lists */
530 opkg_conf_write_status_files (opkg->conf);
531 pkg_write_changed_filelists (opkg->conf);
533 progress (pdata, 100);
534 opkg_package_free (pdata.package);
539 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
543 pkg_t *pkg_to_remove;
544 opkg_progress_data_t pdata;
546 opkg_assert (opkg != NULL);
547 opkg_assert (package_name != NULL);
549 pkg_info_preinstall_check (opkg->conf);
551 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
555 /* XXX: Error: Package not installed. */
556 return OPKG_PACKAGE_NOT_INSTALLED;
559 pdata.action = OPKG_REMOVE;
560 pdata.package = old_pkg_to_new (pkg);
564 if (pkg->state_status == SS_NOT_INSTALLED)
566 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
567 return OPKG_PACKAGE_NOT_INSTALLED;
569 progress (pdata, 25);
571 if (opkg->conf->restrict_to_default_dest)
573 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
575 opkg->conf->default_dest);
579 pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
583 progress (pdata, 75);
585 err = opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
587 /* write out status files and file lists */
588 opkg_conf_write_status_files (opkg->conf);
589 pkg_write_changed_filelists (opkg->conf);
592 progress (pdata, 100);
593 opkg_package_free (pdata.package);
594 return (err) ? OPKG_UNKNOWN_ERROR : OPKG_NO_ERROR;
598 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
602 opkg_progress_data_t pdata;
606 opkg_assert (opkg != NULL);
607 opkg_assert (package_name != NULL);
609 pkg_info_preinstall_check (opkg->conf);
611 if (opkg->conf->restrict_to_default_dest)
613 pkg = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
615 opkg->conf->default_dest);
618 /* XXX: Error: Package not installed in default_dest */
619 return OPKG_PACKAGE_NOT_INSTALLED;
624 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash,
630 /* XXX: Error: Package not installed */
631 return OPKG_PACKAGE_NOT_INSTALLED;
634 pdata.action = OPKG_INSTALL;
635 pdata.package = old_pkg_to_new (pkg);
638 err = opkg_upgrade_pkg (opkg->conf, pkg);
639 /* opkg_upgrade_pkg returns the error codes of opkg_install_pkg */
644 case OPKG_INSTALL_ERR_NOT_TRUSTED: return OPKG_GPG_ERROR;
645 case OPKG_INSTALL_ERR_DOWNLOAD: return OPKG_DOWNLOAD_FAILED;
646 case OPKG_INSTALL_ERR_DEPENDENCIES:
647 case OPKG_INSTALL_ERR_CONFLICTS: return OPKG_DEPENDENCIES_FAILED;
648 case OPKG_INSTALL_ERR_ALREADY_INSTALLED: return OPKG_PACKAGE_ALREADY_INSTALLED;
649 case OPKG_INSTALL_ERR_SIGNATURE: return OPKG_GPG_ERROR;
650 case OPKG_INSTALL_ERR_MD5: return OPKG_MD5_ERROR;
651 default: return OPKG_UNKNOWN_ERROR;
654 progress (pdata, 75);
656 err = opkg_configure_packages (opkg->conf, NULL);
658 return OPKG_UNKNOWN_ERROR;
660 /* write out status files and file lists */
661 opkg_conf_write_status_files (opkg->conf);
662 pkg_write_changed_filelists (opkg->conf);
664 progress (pdata, 100);
665 opkg_package_free (pdata.package);
670 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
672 pkg_vec_t *installed;
676 opkg_progress_data_t pdata;
678 pdata.action = OPKG_INSTALL;
679 pdata.package = NULL;
681 opkg_assert (opkg != NULL);
684 installed = pkg_vec_alloc ();
685 pkg_info_preinstall_check (opkg->conf);
687 pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed);
688 for (i = 0; i < installed->len; i++)
690 pkg = installed->pkgs[i];
692 pdata.package = old_pkg_to_new (pkg);
693 progress (pdata, 99 * i / installed->len);
694 opkg_package_free (pdata.package);
696 err += opkg_upgrade_pkg (opkg->conf, pkg);
698 pkg_vec_free (installed);
703 err = opkg_configure_packages (opkg->conf, NULL);
707 pdata.package = NULL;
708 progress (pdata, 100);
713 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
718 pkg_src_list_elt_t *iter;
720 int sources_list_count, sources_done;
721 opkg_progress_data_t pdata;
723 opkg_assert (opkg != NULL);
725 pdata.action = OPKG_DOWNLOAD;
726 pdata.package = NULL;
729 sprintf_alloc (&lists_dir, "%s",
730 (opkg->conf->restrict_to_default_dest)
731 ? opkg->conf->default_dest->lists_dir
732 : opkg->conf->lists_dir);
734 if (!file_is_dir (lists_dir))
736 if (file_exists (lists_dir))
738 /* XXX: Error: file exists but is not a directory */
743 err = file_mkdir_hier (lists_dir, 0755);
746 /* XXX: Error: failed to create directory */
752 tmp = strdup ("/tmp/opkg.XXXXXX");
754 if (mkdtemp (tmp) == NULL)
756 /* XXX: Error: could not create temporary file name */
762 /* count the number of sources so we can give some progress updates */
763 sources_list_count = 0;
765 iter = opkg->conf->pkg_src_list.head;
768 sources_list_count++;
772 for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
774 char *url, *list_file_name = NULL;
778 if (src->extra_data) /* debian style? */
779 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
780 src->gzip ? "Packages.gz" : "Packages");
782 sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
784 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
788 struct _curl_cb_data cb_data;
789 char *tmp_file_name = NULL;
791 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
793 /* XXX: Note: downloading url */
795 cb_data.cb = progress_callback;
796 cb_data.progress_data = &pdata;
798 cb_data.user_data = user_data;
799 cb_data.start_range = 100 * sources_done / sources_list_count;
800 cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
802 err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
806 /* XXX: Note: Inflating downloaded file */
807 in = fopen (tmp_file_name, "r");
808 out = fopen (list_file_name, "w");
817 unlink (tmp_file_name);
819 free (tmp_file_name);
822 err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
826 /* XXX: Error: download error */
827 result = OPKG_DOWNLOAD_FAILED;
833 /* download detached signitures to verify the package lists */
834 /* get the url for the sig file */
835 if (src->extra_data) /* debian style? */
836 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
839 sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
841 /* create filename for signature */
842 sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, src->name);
844 /* make sure there is no existing signature file */
845 unlink (sig_file_name);
847 err = opkg_download (opkg->conf, url, sig_file_name, NULL, NULL);
850 /* XXX: Warning: Download failed */
855 err = opkg_verify_file (opkg->conf, list_file_name, sig_file_name);
858 /* XXX: Notice: Signature check passed */
862 /* XXX: Warning: Signature check failed */
865 free (sig_file_name);
866 free (list_file_name);
869 /* XXX: Note: Signiture check for %s skipped because GPG support was not
870 * enabled in this build
875 progress (pdata, 100 * sources_done / sources_list_count);
882 /* Now re-read the package lists to update package hash tables. */
883 opkg_re_read_config_files (opkg);
890 opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
896 opkg_assert (callback);
898 all = pkg_vec_alloc ();
899 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
900 for (i = 0; i < all->len; i++)
903 opkg_package_t *package;
907 package = old_pkg_to_new (pkg);
908 callback (opkg, package, user_data);
917 opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
923 opkg_assert (callback);
925 /* ensure all data is valid */
926 pkg_info_preinstall_check (opkg->conf);
928 all = pkg_vec_alloc ();
929 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
930 for (i = 0; i < all->len; i++)
934 opkg_package_t *package;
938 if (old->state_status != SS_INSTALLED)
941 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, old->name, NULL);
943 /* XXX: Notice: Assuming locally install package is up to date */
947 cmp = pkg_compare_versions(old, new);
951 package = old_pkg_to_new (new);
952 callback (opkg, package, user_data);
962 opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *arch, const char *repo)
965 opkg_package_t *package = NULL;
967 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
971 all = pkg_vec_alloc ();
972 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
973 for (i = 0; i < all->len; i++)
981 if (sstrcmp (pkg->name, name))
985 pkgv = pkg_version_str_alloc (pkg);
986 if (sstrcmp (pkgv, ver))
993 /* check architecture */
996 if (sstrcmp (pkg->architecture, arch))
1000 /* check repository */
1003 if (sstrcmp (pkg->src->name, repo))
1008 package = old_pkg_to_new (pkg);
1017 #include <curl/curl.h>
1019 * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status.
1020 * @param opkg The opkg_t
1021 * @return return how many repositories cannot access. 0 means all okay.
1023 int opkg_repository_accessibility_check(opkg_t *opkg)
1025 pkg_src_list_elt_t *iter;
1026 str_list_elt_t *iter1;
1033 opkg_assert(opkg != NULL);
1035 src = str_list_alloc();
1037 for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
1039 if (strstr(iter->data->value, "://") &&
1040 index(strstr(iter->data->value, "://") + 3, '/'))
1041 stmp = strndup(iter->data->value,
1042 (index(strstr(iter->data->value, "://") + 3, '/') - iter->data->value)*sizeof(char));
1045 stmp = strdup(iter->data->value);
1047 for (iter1 = src->head; iter1; iter1 = iter1->next)
1049 if (strstr(iter1->data, stmp))
1055 sprintf_alloc(&repo_ptr, "%s/index.html",stmp);
1058 str_list_append(src, repo_ptr);
1061 while (repositories > 0)
1063 iter1 = str_list_pop(src);
1066 err = opkg_download(opkg->conf, iter1->data, "/dev/null", NULL, NULL);
1067 if (!(err == CURLE_OK ||
1068 err == CURLE_HTTP_RETURNED_ERROR ||
1069 err == CURLE_FILE_COULDNT_READ_FILE ||
1070 err == CURLE_REMOTE_FILE_NOT_FOUND ||
1071 err == CURLE_TFTP_NOTFOUND
1075 str_list_elt_deinit(iter1);