1 /* opkg_cmd.c - the itsy package management system
5 Copyright (C) 2001 University of Southern California
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.
30 #include "opkg_conf.h"
32 #include "opkg_message.h"
35 #include "pkg_parse.h"
36 #include "sprintf_alloc.h"
38 #include "file_util.h"
40 #include "libbb/libbb.h"
45 #include "opkg_download.h"
46 #include "opkg_install.h"
47 #include "opkg_upgrade.h"
48 #include "opkg_remove.h"
49 #include "opkg_configure.h"
50 #include "opkg_message.h"
53 static void *p_userdata = NULL;
55 static int opkg_update_cmd(opkg_conf_t *conf, int argc, char **argv);
56 static int opkg_upgrade_cmd(opkg_conf_t *conf, int argc, char **argv);
57 static int opkg_list_cmd(opkg_conf_t *conf, int argc, char **argv);
58 static int opkg_info_cmd(opkg_conf_t *conf, int argc, char **argv);
59 static int opkg_status_cmd(opkg_conf_t *conf, int argc, char **argv);
60 static int opkg_install_pending_cmd(opkg_conf_t *conf, int argc, char **argv);
61 static int opkg_install_cmd(opkg_conf_t *conf, int argc, char **argv);
62 static int opkg_list_installed_cmd(opkg_conf_t *conf, int argc, char **argv);
63 static int opkg_remove_cmd(opkg_conf_t *conf, int argc, char **argv);
64 static int opkg_purge_cmd(opkg_conf_t *conf, int argc, char **argv);
65 static int opkg_flag_cmd(opkg_conf_t *conf, int argc, char **argv);
66 static int opkg_files_cmd(opkg_conf_t *conf, int argc, char **argv);
67 static int opkg_search_cmd(opkg_conf_t *conf, int argc, char **argv);
68 static int opkg_download_cmd(opkg_conf_t *conf, int argc, char **argv);
69 static int opkg_depends_cmd(opkg_conf_t *conf, int argc, char **argv);
70 static int opkg_whatdepends_cmd(opkg_conf_t *conf, int argc, char **argv);
71 static int opkg_whatdepends_recursively_cmd(opkg_conf_t *conf, int argc, char **argv);
72 static int opkg_whatsuggests_cmd(opkg_conf_t *conf, int argc, char **argv);
73 static int opkg_whatrecommends_cmd(opkg_conf_t *conf, int argc, char **argv);
74 static int opkg_whatprovides_cmd(opkg_conf_t *conf, int argc, char **argv);
75 static int opkg_whatconflicts_cmd(opkg_conf_t *conf, int argc, char **argv);
76 static int opkg_whatreplaces_cmd(opkg_conf_t *conf, int argc, char **argv);
77 static int opkg_compare_versions_cmd(opkg_conf_t *conf, int argc, char **argv);
78 static int opkg_print_architecture_cmd(opkg_conf_t *conf, int argc, char **argv);
79 static int opkg_configure_cmd(opkg_conf_t *conf, int argc, char **argv);
81 /* XXX: CLEANUP: The usage strings should be incorporated into this
82 array for easier maintenance */
83 static opkg_cmd_t cmds[] = {
84 {"update", 0, (opkg_cmd_fun_t)opkg_update_cmd},
85 {"upgrade", 0, (opkg_cmd_fun_t)opkg_upgrade_cmd},
86 {"list", 0, (opkg_cmd_fun_t)opkg_list_cmd},
87 {"list_installed", 0, (opkg_cmd_fun_t)opkg_list_installed_cmd},
88 {"info", 0, (opkg_cmd_fun_t)opkg_info_cmd},
89 {"flag", 1, (opkg_cmd_fun_t)opkg_flag_cmd},
90 {"status", 0, (opkg_cmd_fun_t)opkg_status_cmd},
91 {"install_pending", 0, (opkg_cmd_fun_t)opkg_install_pending_cmd},
92 {"install", 1, (opkg_cmd_fun_t)opkg_install_cmd},
93 {"remove", 1, (opkg_cmd_fun_t)opkg_remove_cmd},
94 {"purge", 1, (opkg_cmd_fun_t)opkg_purge_cmd},
95 {"configure", 0, (opkg_cmd_fun_t)opkg_configure_cmd},
96 {"files", 1, (opkg_cmd_fun_t)opkg_files_cmd},
97 {"search", 1, (opkg_cmd_fun_t)opkg_search_cmd},
98 {"download", 1, (opkg_cmd_fun_t)opkg_download_cmd},
99 {"compare_versions", 1, (opkg_cmd_fun_t)opkg_compare_versions_cmd},
100 {"compare-versions", 1, (opkg_cmd_fun_t)opkg_compare_versions_cmd},
101 {"print-architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd},
102 {"print_architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd},
103 {"print-installation-architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd},
104 {"print_installation_architecture", 0, (opkg_cmd_fun_t)opkg_print_architecture_cmd},
105 {"depends", 1, (opkg_cmd_fun_t)opkg_depends_cmd},
106 {"whatdepends", 1, (opkg_cmd_fun_t)opkg_whatdepends_cmd},
107 {"whatdependsrec", 1, (opkg_cmd_fun_t)opkg_whatdepends_recursively_cmd},
108 {"whatrecommends", 1, (opkg_cmd_fun_t)opkg_whatrecommends_cmd},
109 {"whatsuggests", 1, (opkg_cmd_fun_t)opkg_whatsuggests_cmd},
110 {"whatprovides", 1, (opkg_cmd_fun_t)opkg_whatprovides_cmd},
111 {"whatreplaces", 1, (opkg_cmd_fun_t)opkg_whatreplaces_cmd},
112 {"whatconflicts", 1, (opkg_cmd_fun_t)opkg_whatconflicts_cmd},
115 int opkg_state_changed;
116 static void write_status_files_if_changed(opkg_conf_t *conf)
118 if (opkg_state_changed && !conf->noaction) {
119 opkg_message(conf, OPKG_INFO,
120 " writing status file\n");
121 opkg_conf_write_status_files(conf);
122 pkg_write_changed_filelists(conf);
124 opkg_message(conf, OPKG_NOTICE, "Nothing to be done\n");
129 static int num_cmds = sizeof(cmds) / sizeof(opkg_cmd_t);
131 opkg_cmd_t *opkg_cmd_find(const char *name)
136 for (i=0; i < num_cmds; i++) {
138 if (strcmp(name, cmd->name) == 0) {
146 int opkg_cmd_exec(opkg_cmd_t *cmd, opkg_conf_t *conf, int argc, const char **argv, void *userdata)
149 p_userdata = userdata;
152 result = (cmd->fun)(conf, argc, argv);
155 opkg_message(conf, OPKG_NOTICE, "An error ocurred, return value: %d.\n", result);
159 reverse_error_list(&error_list);
161 opkg_message(conf, OPKG_NOTICE, "Collected errors:\n");
162 /* Here we print the errors collected and free the list */
163 while (error_list != NULL) {
164 opkg_message(conf, OPKG_NOTICE, "%s",error_list->errmsg);
165 error_list = error_list->next;
168 free_error_list(&error_list);
176 static int opkg_update_cmd(opkg_conf_t *conf, int argc, char **argv)
182 pkg_src_list_elt_t *iter;
186 sprintf_alloc(&lists_dir, "%s", conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir);
188 if (! file_is_dir(lists_dir)) {
189 if (file_exists(lists_dir)) {
190 opkg_message(conf, OPKG_ERROR,
191 "%s: ERROR: %s exists, but is not a directory\n",
192 __FUNCTION__, lists_dir);
196 err = file_mkdir_hier(lists_dir, 0755);
198 opkg_message(conf, OPKG_ERROR,
199 "%s: ERROR: failed to make directory %s: %s\n",
200 __FUNCTION__, lists_dir, strerror(errno));
209 tmp = strdup ("/tmp/opkg.XXXXXX");
211 if (mkdtemp (tmp) == NULL) {
217 for (iter = conf->pkg_src_list.head; iter; iter = iter->next) {
218 char *url, *list_file_name;
222 if (src->extra_data) /* debian style? */
223 sprintf_alloc(&url, "%s/%s/%s", src->value, src->extra_data,
224 src->gzip ? "Packages.gz" : "Packages");
226 sprintf_alloc(&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
228 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
233 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
234 err = opkg_download(conf, url, tmp_file_name);
236 opkg_message (conf, OPKG_NOTICE, "Inflating %s\n", url);
237 in = fopen (tmp_file_name, "r");
238 out = fopen (list_file_name, "w");
247 unlink (tmp_file_name);
250 err = opkg_download(conf, url, list_file_name);
254 opkg_message(conf, OPKG_NOTICE,
255 "Updated list of available packages in %s\n",
260 /* download detached signitures to verify the package lists */
261 /* get the url for the sig file */
262 if (src->extra_data) /* debian style? */
263 sprintf_alloc(&url, "%s/%s/%s", src->value, src->extra_data,
266 sprintf_alloc(&url, "%s/%s", src->value, "Packages.sig");
268 /* create temporary file for it */
271 sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
273 err = opkg_download(conf, url, tmp_file_name);
276 opkg_message (conf, OPKG_NOTICE, "Signature check failed\n");
279 err = opkg_verify_file (list_file_name, tmp_file_name);
281 opkg_message (conf, OPKG_NOTICE, "Signature check passed\n");
283 opkg_message (conf, OPKG_NOTICE, "Signature check failed\n");
285 unlink (tmp_file_name);
286 free (tmp_file_name);
289 free(list_file_name);
299 /* scan the args passed and cache the local filenames of the packages */
300 int opkg_multiple_files_scan(opkg_conf_t *conf, int argc, char **argv)
306 * First scan through package names/urls
307 * For any urls, download the packages and install in database.
308 * For any files, install package info in database.
310 for (i = 0; i < argc; i ++) {
311 char *filename = argv [i];
312 //char *tmp = basename (tmp);
313 //int tmplen = strlen (tmp);
315 //if (strcmp (tmp + (tmplen - strlen (OPKG_PKG_EXTENSION)), OPKG_PKG_EXTENSION) != 0)
317 //if (strcmp (tmp + (tmplen - strlen (DPKG_PKG_EXTENSION)), DPKG_PKG_EXTENSION) != 0)
320 opkg_message(conf, OPKG_DEBUG2, "Debug mfs: %s \n",filename );
322 err = opkg_prepare_url_for_install(conf, filename, &argv[i]);
329 struct opkg_intercept
335 typedef struct opkg_intercept *opkg_intercept_t;
337 opkg_intercept_t opkg_prep_intercepts(opkg_conf_t *conf)
339 opkg_intercept_t ctx;
343 ctx = malloc (sizeof (*ctx));
344 ctx->oldpath = strdup (getenv ("PATH"));
346 sprintf_alloc (&newpath, "%s/opkg/intercept:%s", DATADIR, ctx->oldpath);
347 setenv ("PATH", newpath, 1);
352 sprintf_alloc (&ctx->statedir, "/tmp/opkg-intercept-%d-%d", getpid (), gen);
353 if (mkdir (ctx->statedir, 0770) < 0) {
354 if (errno == EEXIST) {
355 free (ctx->statedir);
359 perror (ctx->statedir);
362 setenv ("OPKG_INTERCEPT_DIR", ctx->statedir, 1);
366 int opkg_finalize_intercepts(opkg_intercept_t ctx)
372 setenv ("PATH", ctx->oldpath, 1);
375 dir = opendir (ctx->statedir);
378 while (de = readdir (dir), de != NULL) {
381 if (de->d_name[0] == '.')
384 sprintf_alloc (&path, "%s/%s", ctx->statedir, de->d_name);
385 if (access (path, X_OK) == 0) {
394 perror (ctx->statedir);
396 sprintf_alloc (&cmd, "rm -rf %s", ctx->statedir);
400 free (ctx->statedir);
406 int opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
414 opkg_message(conf, OPKG_INFO,
415 "Configuring unpacked packages\n");
418 all = pkg_vec_alloc();
419 pkg_hash_fetch_available(&conf->pkg_hash, all);
421 ic = opkg_prep_intercepts (conf);
423 for(i = 0; i < all->len; i++) {
426 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
429 if (pkg->state_status == SS_UNPACKED) {
430 opkg_message(conf, OPKG_NOTICE,
431 "Configuring %s\n", pkg->name);
433 r = opkg_configure(conf, pkg);
435 pkg->state_status = SS_INSTALLED;
436 pkg->parent->state_status = SS_INSTALLED;
437 pkg->state_flag &= ~SF_PREFER;
445 r = opkg_finalize_intercepts (ic);
453 static opkg_conf_t *global_conf;
455 static void sigint_handler(int sig)
457 signal(sig, SIG_DFL);
458 opkg_message(NULL, OPKG_NOTICE,
459 "opkg: interrupted. writing out status database\n");
460 write_status_files_if_changed(global_conf);
464 static int opkg_install_cmd(opkg_conf_t *conf, int argc, char **argv)
471 signal(SIGINT, sigint_handler);
474 * Now scan through package names and install
476 for (i=0; i < argc; i++) {
479 opkg_message(conf, OPKG_DEBUG2, "Debug install_cmd: %s \n",arg );
480 err = opkg_prepare_url_for_install(conf, arg, &argv[i]);
481 if (err != EINVAL && err != 0)
484 pkg_info_preinstall_check(conf);
486 for (i=0; i < argc; i++) {
488 if (conf->multiple_providers)
489 err = opkg_install_multi_by_name(conf, arg);
491 err = opkg_install_by_name(conf, arg);
493 if (err == OPKG_PKG_HAS_NO_CANDIDATE) {
494 opkg_message(conf, OPKG_ERROR,
495 "Cannot find package %s.\n"
496 "Check the spelling or perhaps run 'opkg update'\n",
501 /* recheck to verify that all dependences are satisfied */
502 if (0) opkg_satisfy_all_dependences(conf);
504 opkg_configure_packages(conf, NULL);
506 write_status_files_if_changed(conf);
511 static int opkg_upgrade_cmd(opkg_conf_t *conf, int argc, char **argv)
518 signal(SIGINT, sigint_handler);
521 for (i=0; i < argc; i++) {
524 err = opkg_prepare_url_for_install(conf, arg, &arg);
525 if (err != EINVAL && err != 0)
528 pkg_info_preinstall_check(conf);
530 for (i=0; i < argc; i++) {
532 if (conf->restrict_to_default_dest) {
533 pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
537 opkg_message(conf, OPKG_NOTICE,
538 "Package %s not installed in %s\n",
539 argv[i], conf->default_dest->name);
543 pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
547 opkg_upgrade_pkg(conf, pkg);
549 opkg_install_by_name(conf, arg);
553 pkg_vec_t *installed = pkg_vec_alloc();
555 pkg_info_preinstall_check(conf);
557 pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
558 for (i = 0; i < installed->len; i++) {
559 pkg = installed->pkgs[i];
560 opkg_upgrade_pkg(conf, pkg);
562 pkg_vec_free(installed);
565 /* recheck to verify that all dependences are satisfied */
566 if (0) opkg_satisfy_all_dependences(conf);
568 opkg_configure_packages(conf, NULL);
570 write_status_files_if_changed(conf);
575 static int opkg_download_cmd(opkg_conf_t *conf, int argc, char **argv)
581 pkg_info_preinstall_check(conf);
582 for (i = 0; i < argc; i++) {
585 pkg = pkg_hash_fetch_best_installation_candidate_by_name(conf, arg);
587 opkg_message(conf, OPKG_ERROR,
588 "Cannot find package %s.\n"
589 "Check the spelling or perhaps run 'opkg update'\n",
594 err = opkg_download_pkg(conf, pkg, ".");
597 opkg_message(conf, OPKG_ERROR,
598 "Failed to download %s\n", pkg->name);
600 opkg_message(conf, OPKG_NOTICE,
601 "Downloaded %s as %s\n",
602 pkg->name, pkg->local_filename);
610 static int opkg_list_cmd(opkg_conf_t *conf, int argc, char **argv)
613 pkg_vec_t *available;
615 char desc_short[OPKG_LIST_DESCRIPTION_LENGTH];
617 char *pkg_name = NULL;
623 available = pkg_vec_alloc();
624 pkg_hash_fetch_available(&conf->pkg_hash, available);
625 for (i=0; i < available->len; i++) {
626 pkg = available->pkgs[i];
627 /* if we have package name or pattern and pkg does not match, then skip it */
628 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
630 if (pkg->description) {
631 strncpy(desc_short, pkg->description, OPKG_LIST_DESCRIPTION_LENGTH);
633 desc_short[0] = '\0';
635 desc_short[OPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
636 newline = strchr(desc_short, '\n');
641 version_str = pkg_version_str_alloc(pkg);
642 opkg_cb_list(pkg->name,desc_short,
649 pkg_vec_free(available);
655 static int opkg_list_installed_cmd(opkg_conf_t *conf, int argc, char **argv)
658 pkg_vec_t *available;
660 char desc_short[OPKG_LIST_DESCRIPTION_LENGTH];
662 char *pkg_name = NULL;
668 available = pkg_vec_alloc();
669 pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
670 for (i=0; i < available->len; i++) {
671 pkg = available->pkgs[i];
672 /* if we have package name or pattern and pkg does not match, then skip it */
673 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
675 if (pkg->description) {
676 strncpy(desc_short, pkg->description, OPKG_LIST_DESCRIPTION_LENGTH);
678 desc_short[0] = '\0';
680 desc_short[OPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
681 newline = strchr(desc_short, '\n');
686 version_str = pkg_version_str_alloc(pkg);
687 opkg_cb_list(pkg->name,desc_short,
698 static int opkg_info_status_cmd(opkg_conf_t *conf, int argc, char **argv, int installed_only)
701 pkg_vec_t *available;
703 char *pkg_name = NULL;
704 char **pkg_fields = NULL;
706 char *buff ; // = (char *)malloc(1);
712 pkg_fields = &argv[1];
716 available = pkg_vec_alloc();
718 pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
720 pkg_hash_fetch_available(&conf->pkg_hash, available);
721 for (i=0; i < available->len; i++) {
722 pkg = available->pkgs[i];
723 if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
727 buff = pkg_formatted_info(pkg);
729 if (opkg_cb_status) opkg_cb_status(pkg->name,
734 We should not forget that actually the pointer is allocated.
735 We need to free it :) ( Thanks florian for seeing the error )
739 if (conf->verbosity > 1) {
740 conffile_list_elt_t *iter;
741 for (iter = pkg->conffiles.head; iter; iter = iter->next) {
742 conffile_t *cf = iter->data;
743 int modified = conffile_has_been_modified(conf, cf);
744 opkg_message(conf, OPKG_NOTICE, "conffile=%s md5sum=%s modified=%d\n",
745 cf->name, cf->value, modified);
749 pkg_vec_free(available);
754 static int opkg_info_cmd(opkg_conf_t *conf, int argc, char **argv)
756 return opkg_info_status_cmd(conf, argc, argv, 0);
759 static int opkg_status_cmd(opkg_conf_t *conf, int argc, char **argv)
761 return opkg_info_status_cmd(conf, argc, argv, 1);
764 static int opkg_configure_cmd(opkg_conf_t *conf, int argc, char **argv)
769 char *pkg_name = NULL;
773 err = opkg_configure_packages (conf, pkg_name);
776 err = opkg_configure_packages (conf, NULL);
779 write_status_files_if_changed(conf);
784 static int opkg_install_pending_cmd(opkg_conf_t *conf, int argc, char **argv)
790 sprintf_alloc(&globpattern, "%s/*" OPKG_PKG_EXTENSION, conf->pending_dir);
791 err = glob(globpattern, 0, NULL, &globbuf);
797 opkg_message(conf, OPKG_NOTICE,
798 "The following packages in %s will now be installed.\n",
800 for (i = 0; i < globbuf.gl_pathc; i++) {
801 opkg_message(conf, OPKG_NOTICE,
802 "%s%s", i == 0 ? "" : " ", globbuf.gl_pathv[i]);
804 opkg_message(conf, OPKG_NOTICE, "\n");
805 for (i = 0; i < globbuf.gl_pathc; i++) {
806 err = opkg_install_from_file(conf, globbuf.gl_pathv[i]);
808 err = unlink(globbuf.gl_pathv[i]);
810 opkg_message(conf, OPKG_ERROR,
811 "%s: ERROR: failed to unlink %s: %s\n",
812 __FUNCTION__, globbuf.gl_pathv[i], strerror(err));
822 static int opkg_remove_cmd(opkg_conf_t *conf, int argc, char **argv)
826 pkg_t *pkg_to_remove;
827 pkg_vec_t *available;
828 char *pkg_name = NULL;
830 signal(SIGINT, sigint_handler);
832 // ENH: Add the "no pkg removed" just in case.
836 available = pkg_vec_alloc();
837 pkg_info_preinstall_check(conf);
839 pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
840 for (i=0; i < argc; i++) {
841 pkg_name = malloc(strlen(argv[i])+2);
842 strcpy(pkg_name,argv[i]);
843 for (a=0; a < available->len; a++) {
844 pkg = available->pkgs[a];
845 if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
848 if (conf->restrict_to_default_dest) {
849 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
853 pkg_to_remove = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name );
857 opkg_message(conf, OPKG_ERROR, "Package %s is not installed.\n", pkg->name);
860 if (pkg->state_status == SS_NOT_INSTALLED) { // Added the control, so every already removed package could be skipped
861 opkg_message(conf, OPKG_ERROR, "Package seems to be %s not installed (STATUS = NOT_INSTALLED).\n", pkg->name);
864 opkg_remove_pkg(conf, pkg_to_remove,0);
869 pkg_vec_free(available);
871 pkg_vec_t *installed_pkgs = pkg_vec_alloc();
873 int flagged_pkg_count = 0;
876 pkg_hash_fetch_all_installed(&conf->pkg_hash, installed_pkgs);
878 for (i = 0; i < installed_pkgs->len; i++) {
879 pkg_t *pkg = installed_pkgs->pkgs[i];
880 if (pkg->state_flag & SF_USER) {
883 if (!pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL))
884 opkg_message(conf, OPKG_NOTICE, "Non-user leaf package: %s\n", pkg->name);
887 if (!flagged_pkg_count) {
888 opkg_message(conf, OPKG_NOTICE, "No packages flagged as installed by user, \n"
889 "so refusing to uninstall unflagged non-leaf packages\n");
893 /* find packages not flagged SF_USER (i.e., installed to
894 * satisfy a dependence) and not having any dependents, and
898 for (i = 0; i < installed_pkgs->len; i++) {
899 pkg_t *pkg = installed_pkgs->pkgs[i];
900 if (!(pkg->state_flag & SF_USER)
901 && !pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL)) {
903 opkg_message(conf, OPKG_NOTICE, "Removing non-user leaf package %s\n");
904 opkg_remove_pkg(conf, pkg,0);
909 pkg_vec_free(installed_pkgs);
913 opkg_message(conf, OPKG_NOTICE, "No packages removed.\n");
915 write_status_files_if_changed(conf);
919 static int opkg_purge_cmd(opkg_conf_t *conf, int argc, char **argv)
925 signal(SIGINT, sigint_handler);
927 pkg_info_preinstall_check(conf);
929 for (i=0; i < argc; i++) {
930 if (conf->restrict_to_default_dest) {
931 pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
935 pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
939 opkg_message(conf, OPKG_ERROR,
940 "Package %s is not installed.\n", argv[i]);
943 opkg_purge_pkg(conf, pkg);
946 write_status_files_if_changed(conf);
950 static int opkg_flag_cmd(opkg_conf_t *conf, int argc, char **argv)
954 const char *flags = argv[0];
957 signal(SIGINT, sigint_handler);
959 for (i=1; i < argc; i++) {
960 if (conf->restrict_to_default_dest) {
961 pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
965 pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
969 opkg_message(conf, OPKG_ERROR,
970 "Package %s is not installed.\n", argv[i]);
973 if (( strcmp(flags,"hold")==0)||( strcmp(flags,"noprune")==0)||
974 ( strcmp(flags,"user")==0)||( strcmp(flags,"ok")==0)) {
975 pkg->state_flag = pkg_state_flag_from_str(flags);
977 /* pb_ asked this feature 03292004 */
978 /* Actually I will use only this two, but this is an open for various status */
979 if (( strcmp(flags,"installed")==0)||( strcmp(flags,"unpacked")==0)){
980 pkg->state_status = pkg_state_status_from_str(flags);
982 opkg_state_changed++;
983 opkg_message(conf, OPKG_NOTICE,
984 "Setting flags for package %s to %s\n",
988 write_status_files_if_changed(conf);
992 static int opkg_files_cmd(opkg_conf_t *conf, int argc, char **argv)
995 str_list_t *installed_files;
996 str_list_elt_t *iter;
998 size_t buff_len = 8192;
1002 buff = (char *)malloc(buff_len);
1003 if ( buff == NULL ) {
1004 fprintf( stderr,"%s: Unable to allocate memory \n",__FUNCTION__);
1012 pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
1015 opkg_message(conf, OPKG_ERROR,
1016 "Package %s not installed.\n", argv[0]);
1020 installed_files = pkg_get_installed_files(pkg);
1021 pkg_version = pkg_version_str_alloc(pkg);
1025 used_len = snprintf(buff, buff_len, "Package %s (%s) is installed on %s and has the following files:\n",
1026 pkg->name, pkg_version, pkg->dest->name) + 1;
1027 if (used_len > buff_len) {
1029 buff = realloc (buff, buff_len);
1032 for (iter = installed_files->head; iter; iter = iter->next) {
1033 used_len += strlen (iter->data) + 1;
1034 while (buff_len <= used_len) {
1036 buff = realloc (buff, buff_len);
1038 strncat(buff, iter->data, buff_len);
1039 strncat(buff, "\n", buff_len);
1041 if (opkg_cb_list) opkg_cb_list(pkg->name,
1043 pkg_version_str_alloc(pkg),
1050 pkg_free_installed_files(pkg);
1055 static int opkg_depends_cmd(opkg_conf_t *conf, int argc, char **argv)
1059 pkg_vec_t *available_pkgs = pkg_vec_alloc();
1060 const char *rel_str = "depends on";
1063 pkg_info_preinstall_check(conf);
1065 if (conf->query_all)
1066 pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1068 pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1069 for (i = 0; i < argc; i++) {
1070 const char *target = argv[i];
1073 opkg_message(conf, OPKG_ERROR, "target=%s\n", target);
1075 for (j = 0; j < available_pkgs->len; j++) {
1076 pkg_t *pkg = available_pkgs->pkgs[j];
1077 if (fnmatch(target, pkg->name, 0) == 0) {
1079 int count = pkg->depends_count + pkg->pre_depends_count;
1080 opkg_message(conf, OPKG_ERROR, "What %s (arch=%s) %s\n",
1081 target, pkg->architecture, rel_str);
1082 for (k = 0; k < count; k++) {
1083 compound_depend_t *cdepend = &pkg->depends[k];
1085 for (l = 0; l < cdepend->possibility_count; l++) {
1086 depend_t *possibility = cdepend->possibilities[l];
1087 opkg_message(conf, OPKG_ERROR, " %s", possibility->pkg->name);
1088 if (conf->verbosity > 0) {
1089 // char *ver = abstract_pkg_version_str_alloc(possibility->pkg);
1090 opkg_message(conf, OPKG_NOTICE, " %s", possibility->version);
1091 if (possibility->version) {
1092 char *typestr = NULL;
1093 switch (possibility->constraint) {
1094 case NONE: typestr = "none"; break;
1095 case EARLIER: typestr = "<"; break;
1096 case EARLIER_EQUAL: typestr = "<="; break;
1097 case EQUAL: typestr = "="; break;
1098 case LATER_EQUAL: typestr = ">="; break;
1099 case LATER: typestr = ">"; break;
1101 opkg_message(conf, OPKG_NOTICE, " (%s %s)", typestr, possibility->version);
1105 opkg_message(conf, OPKG_ERROR, "\n");
1111 pkg_vec_free(available_pkgs);
1116 enum what_field_type {
1125 static int opkg_what_depends_conflicts_cmd(opkg_conf_t *conf, enum what_field_type what_field_type, int recursive, int argc, char **argv)
1129 pkg_vec_t *available_pkgs = pkg_vec_alloc();
1130 const char *rel_str = NULL;
1134 switch (what_field_type) {
1135 case WHATDEPENDS: rel_str = "depends on"; break;
1136 case WHATCONFLICTS: rel_str = "conflicts with"; break;
1137 case WHATSUGGESTS: rel_str = "suggests"; break;
1138 case WHATRECOMMENDS: rel_str = "recommends"; break;
1139 case WHATPROVIDES: rel_str = "provides"; break;
1140 case WHATREPLACES: rel_str = "replaces"; break;
1143 if (conf->query_all)
1144 pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1146 pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1148 /* mark the root set */
1149 pkg_vec_clear_marks(available_pkgs);
1150 opkg_message(conf, OPKG_NOTICE, "Root set:\n");
1151 for (i = 0; i < argc; i++) {
1152 const char *dependee_pattern = argv[i];
1153 pkg_vec_mark_if_matches(available_pkgs, dependee_pattern);
1155 for (i = 0; i < available_pkgs->len; i++) {
1156 pkg_t *pkg = available_pkgs->pkgs[i];
1157 if (pkg->state_flag & SF_MARKED) {
1158 /* mark the parent (abstract) package */
1159 pkg_mark_provides(pkg);
1160 opkg_message(conf, OPKG_NOTICE, " %s\n", pkg->name);
1164 opkg_message(conf, OPKG_NOTICE, "What %s root set\n", rel_str);
1169 for (j = 0; j < available_pkgs->len; j++) {
1170 pkg_t *pkg = available_pkgs->pkgs[j];
1172 int count = ((what_field_type == WHATCONFLICTS)
1173 ? pkg->conflicts_count
1174 : pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count);
1175 /* skip this package if it is already marked */
1176 if (pkg->parent->state_flag & SF_MARKED) {
1179 for (k = 0; k < count; k++) {
1180 compound_depend_t *cdepend =
1181 (what_field_type == WHATCONFLICTS) ? &pkg->conflicts[k] : &pkg->depends[k];
1183 for (l = 0; l < cdepend->possibility_count; l++) {
1184 depend_t *possibility = cdepend->possibilities[l];
1185 if (possibility->pkg->state_flag & SF_MARKED) {
1186 /* mark the depending package so we won't visit it again */
1187 pkg->state_flag |= SF_MARKED;
1188 pkg_mark_provides(pkg);
1191 opkg_message(conf, OPKG_NOTICE, " %s", pkg->name);
1192 if (conf->verbosity > 0) {
1193 char *ver = pkg_version_str_alloc(pkg);
1194 opkg_message(conf, OPKG_NOTICE, " %s", ver);
1195 opkg_message(conf, OPKG_NOTICE, "\t%s %s", rel_str, possibility->pkg->name);
1196 if (possibility->version) {
1197 char *typestr = NULL;
1198 switch (possibility->constraint) {
1199 case NONE: typestr = "none"; break;
1200 case EARLIER: typestr = "<"; break;
1201 case EARLIER_EQUAL: typestr = "<="; break;
1202 case EQUAL: typestr = "="; break;
1203 case LATER_EQUAL: typestr = ">="; break;
1204 case LATER: typestr = ">"; break;
1206 opkg_message(conf, OPKG_NOTICE, " (%s %s)", typestr, possibility->version);
1209 if (!pkg_dependence_satisfiable(conf, possibility))
1210 opkg_message(conf, OPKG_NOTICE, " unsatisfiable");
1212 opkg_message(conf, OPKG_NOTICE, "\n");
1220 } while (changed && recursive);
1221 pkg_vec_free(available_pkgs);
1227 int pkg_mark_provides(pkg_t *pkg)
1229 int provides_count = pkg->provides_count;
1230 abstract_pkg_t **provides = pkg->provides;
1232 pkg->parent->state_flag |= SF_MARKED;
1233 for (i = 0; i < provides_count; i++) {
1234 provides[i]->state_flag |= SF_MARKED;
1239 static int opkg_whatdepends_recursively_cmd(opkg_conf_t *conf, int argc, char **argv)
1241 return opkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 1, argc, argv);
1243 static int opkg_whatdepends_cmd(opkg_conf_t *conf, int argc, char **argv)
1245 return opkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 0, argc, argv);
1248 static int opkg_whatsuggests_cmd(opkg_conf_t *conf, int argc, char **argv)
1250 return opkg_what_depends_conflicts_cmd(conf, WHATSUGGESTS, 0, argc, argv);
1253 static int opkg_whatrecommends_cmd(opkg_conf_t *conf, int argc, char **argv)
1255 return opkg_what_depends_conflicts_cmd(conf, WHATRECOMMENDS, 0, argc, argv);
1258 static int opkg_whatconflicts_cmd(opkg_conf_t *conf, int argc, char **argv)
1260 return opkg_what_depends_conflicts_cmd(conf, WHATCONFLICTS, 0, argc, argv);
1263 static int opkg_what_provides_replaces_cmd(opkg_conf_t *conf, enum what_field_type what_field_type, int argc, char **argv)
1267 pkg_vec_t *available_pkgs = pkg_vec_alloc();
1268 const char *rel_str = (what_field_type == WHATPROVIDES ? "provides" : "replaces");
1271 pkg_info_preinstall_check(conf);
1273 if (conf->query_all)
1274 pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1276 pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1277 for (i = 0; i < argc; i++) {
1278 const char *target = argv[i];
1281 opkg_message(conf, OPKG_ERROR, "What %s %s\n",
1283 for (j = 0; j < available_pkgs->len; j++) {
1284 pkg_t *pkg = available_pkgs->pkgs[j];
1286 int count = (what_field_type == WHATPROVIDES) ? pkg->provides_count : pkg->replaces_count;
1287 for (k = 0; k < count; k++) {
1288 abstract_pkg_t *apkg =
1289 ((what_field_type == WHATPROVIDES)
1291 : pkg->replaces[k]);
1292 if (fnmatch(target, apkg->name, 0) == 0) {
1293 opkg_message(conf, OPKG_ERROR, " %s", pkg->name);
1294 if (strcmp(target, apkg->name) != 0)
1295 opkg_message(conf, OPKG_ERROR, "\t%s %s\n", rel_str, apkg->name);
1296 opkg_message(conf, OPKG_ERROR, "\n");
1301 pkg_vec_free(available_pkgs);
1306 static int opkg_whatprovides_cmd(opkg_conf_t *conf, int argc, char **argv)
1308 return opkg_what_provides_replaces_cmd(conf, WHATPROVIDES, argc, argv);
1311 static int opkg_whatreplaces_cmd(opkg_conf_t *conf, int argc, char **argv)
1313 return opkg_what_provides_replaces_cmd(conf, WHATREPLACES, argc, argv);
1316 static int opkg_search_cmd(opkg_conf_t *conf, int argc, char **argv)
1320 pkg_vec_t *installed;
1322 str_list_t *installed_files;
1323 str_list_elt_t *iter;
1324 char *installed_file;
1330 installed = pkg_vec_alloc();
1331 pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
1333 for (i=0; i < installed->len; i++) {
1334 pkg = installed->pkgs[i];
1336 installed_files = pkg_get_installed_files(pkg);
1338 for (iter = installed_files->head; iter; iter = iter->next) {
1339 installed_file = iter->data;
1340 if (fnmatch(argv[0], installed_file, 0)==0) {
1341 if (opkg_cb_list) opkg_cb_list(pkg->name,
1343 pkg_version_str_alloc(pkg),
1344 pkg->state_status, p_userdata);
1348 pkg_free_installed_files(pkg);
1351 /* XXX: CLEANUP: It's not obvious from the name of
1352 pkg_hash_fetch_all_installed that we need to call
1353 pkg_vec_free to avoid a memory leak. */
1354 pkg_vec_free(installed);
1359 static int opkg_compare_versions_cmd(opkg_conf_t *conf, int argc, char **argv)
1362 /* this is a bit gross */
1364 parseVersion(&p1, argv[0]);
1365 parseVersion(&p2, argv[2]);
1366 return pkg_version_satisfied(&p1, &p2, argv[1]);
1368 opkg_message(conf, OPKG_ERROR,
1369 "opkg compare_versions <v1> <op> <v2>\n"
1370 "<op> is one of <= >= << >> =\n");
1375 #ifndef HOST_CPU_STR
1376 #define HOST_CPU_STR__(X) #X
1377 #define HOST_CPU_STR_(X) HOST_CPU_STR__(X)
1378 #define HOST_CPU_STR HOST_CPU_STR_(HOST_CPU_FOO)
1381 static int opkg_print_architecture_cmd(opkg_conf_t *conf, int argc, char **argv)
1383 nv_pair_list_elt_t *l;
1385 l = conf->arch_list.head;
1387 nv_pair_t *nv = l->data;
1388 printf("arch %s %s\n", nv->name, nv->value);