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);
295 #ifdef CONFIG_CLEAR_SW_INSTALL_FLAG
297 /* clear SW_INSTALL on any package where state is SS_NOT_INSTALLED.
298 * this is a hack to work around poor bookkeeping in old opkg upgrade code
304 pkg_vec_t *available = pkg_vec_alloc();
305 pkg_hash_fetch_available(&conf->pkg_hash, available);
306 opkg_message(conf, OPKG_DEBUG, "Clearing SW_INSTALL for SS_NOT_INSTALLED packages.\n");
307 for (i = 0; i < available->len; i++) {
308 pkg_t *pkg = available->pkgs[i];
309 if (pkg->state_want == SW_INSTALL && pkg->state_status == SS_NOT_INSTALLED) {
310 opkg_message(conf, OPKG_DEBUG, "Clearing SW_INSTALL on package %s.\n", pkg->name);
311 pkg->state_want = SW_UNKNOWN;
315 pkg_vec_free(available);
317 write_status_files_if_changed(conf);
326 /* scan the args passed and cache the local filenames of the packages */
327 int opkg_multiple_files_scan(opkg_conf_t *conf, int argc, char **argv)
333 * First scan through package names/urls
334 * For any urls, download the packages and install in database.
335 * For any files, install package info in database.
337 for (i = 0; i < argc; i ++) {
338 char *filename = argv [i];
339 //char *tmp = basename (tmp);
340 //int tmplen = strlen (tmp);
342 //if (strcmp (tmp + (tmplen - strlen (OPKG_PKG_EXTENSION)), OPKG_PKG_EXTENSION) != 0)
344 //if (strcmp (tmp + (tmplen - strlen (DPKG_PKG_EXTENSION)), DPKG_PKG_EXTENSION) != 0)
347 opkg_message(conf, OPKG_DEBUG2, "Debug mfs: %s \n",filename );
349 err = opkg_prepare_url_for_install(conf, filename, &argv[i]);
356 struct opkg_intercept
362 typedef struct opkg_intercept *opkg_intercept_t;
364 opkg_intercept_t opkg_prep_intercepts(opkg_conf_t *conf)
366 opkg_intercept_t ctx;
370 ctx = malloc (sizeof (*ctx));
371 ctx->oldpath = strdup (getenv ("PATH"));
373 sprintf_alloc (&newpath, "%s/opkg/intercept:%s", DATADIR, ctx->oldpath);
374 setenv ("PATH", newpath, 1);
379 sprintf_alloc (&ctx->statedir, "/tmp/opkg-intercept-%d-%d", getpid (), gen);
380 if (mkdir (ctx->statedir, 0770) < 0) {
381 if (errno == EEXIST) {
382 free (ctx->statedir);
386 perror (ctx->statedir);
389 setenv ("OPKG_INTERCEPT_DIR", ctx->statedir, 1);
393 int opkg_finalize_intercepts(opkg_intercept_t ctx)
399 setenv ("PATH", ctx->oldpath, 1);
402 dir = opendir (ctx->statedir);
405 while (de = readdir (dir), de != NULL) {
408 if (de->d_name[0] == '.')
411 sprintf_alloc (&path, "%s/%s", ctx->statedir, de->d_name);
412 if (access (path, X_OK) == 0) {
421 perror (ctx->statedir);
423 sprintf_alloc (&cmd, "rm -rf %s", ctx->statedir);
427 free (ctx->statedir);
433 int opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
441 opkg_message(conf, OPKG_INFO,
442 "Configuring unpacked packages\n");
445 all = pkg_vec_alloc();
446 pkg_hash_fetch_available(&conf->pkg_hash, all);
448 ic = opkg_prep_intercepts (conf);
450 for(i = 0; i < all->len; i++) {
453 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
456 if (pkg->state_status == SS_UNPACKED) {
457 opkg_message(conf, OPKG_NOTICE,
458 "Configuring %s\n", pkg->name);
460 r = opkg_configure(conf, pkg);
462 pkg->state_status = SS_INSTALLED;
463 pkg->parent->state_status = SS_INSTALLED;
464 pkg->state_flag &= ~SF_PREFER;
472 r = opkg_finalize_intercepts (ic);
480 static opkg_conf_t *global_conf;
482 static void sigint_handler(int sig)
484 signal(sig, SIG_DFL);
485 opkg_message(NULL, OPKG_NOTICE,
486 "opkg: interrupted. writing out status database\n");
487 write_status_files_if_changed(global_conf);
491 static int opkg_install_cmd(opkg_conf_t *conf, int argc, char **argv)
498 signal(SIGINT, sigint_handler);
501 * Now scan through package names and install
503 for (i=0; i < argc; i++) {
506 opkg_message(conf, OPKG_DEBUG2, "Debug install_cmd: %s \n",arg );
507 err = opkg_prepare_url_for_install(conf, arg, &argv[i]);
508 if (err != EINVAL && err != 0)
511 pkg_info_preinstall_check(conf);
513 for (i=0; i < argc; i++) {
515 if (conf->multiple_providers)
516 err = opkg_install_multi_by_name(conf, arg);
518 err = opkg_install_by_name(conf, arg);
520 if (err == OPKG_PKG_HAS_NO_CANDIDATE) {
521 opkg_message(conf, OPKG_ERROR,
522 "Cannot find package %s.\n"
523 "Check the spelling or perhaps run 'opkg update'\n",
528 /* recheck to verify that all dependences are satisfied */
529 if (0) opkg_satisfy_all_dependences(conf);
531 opkg_configure_packages(conf, NULL);
533 write_status_files_if_changed(conf);
538 static int opkg_upgrade_cmd(opkg_conf_t *conf, int argc, char **argv)
545 signal(SIGINT, sigint_handler);
548 for (i=0; i < argc; i++) {
551 err = opkg_prepare_url_for_install(conf, arg, &arg);
552 if (err != EINVAL && err != 0)
555 pkg_info_preinstall_check(conf);
557 for (i=0; i < argc; i++) {
559 if (conf->restrict_to_default_dest) {
560 pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
564 opkg_message(conf, OPKG_NOTICE,
565 "Package %s not installed in %s\n",
566 argv[i], conf->default_dest->name);
570 pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
574 opkg_upgrade_pkg(conf, pkg);
576 opkg_install_by_name(conf, arg);
580 pkg_vec_t *installed = pkg_vec_alloc();
582 pkg_info_preinstall_check(conf);
584 pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
585 for (i = 0; i < installed->len; i++) {
586 pkg = installed->pkgs[i];
587 opkg_upgrade_pkg(conf, pkg);
589 pkg_vec_free(installed);
592 /* recheck to verify that all dependences are satisfied */
593 if (0) opkg_satisfy_all_dependences(conf);
595 opkg_configure_packages(conf, NULL);
597 write_status_files_if_changed(conf);
602 static int opkg_download_cmd(opkg_conf_t *conf, int argc, char **argv)
608 pkg_info_preinstall_check(conf);
609 for (i = 0; i < argc; i++) {
612 pkg = pkg_hash_fetch_best_installation_candidate_by_name(conf, arg);
614 opkg_message(conf, OPKG_ERROR,
615 "Cannot find package %s.\n"
616 "Check the spelling or perhaps run 'opkg update'\n",
621 err = opkg_download_pkg(conf, pkg, ".");
624 opkg_message(conf, OPKG_ERROR,
625 "Failed to download %s\n", pkg->name);
627 opkg_message(conf, OPKG_NOTICE,
628 "Downloaded %s as %s\n",
629 pkg->name, pkg->local_filename);
637 static int opkg_list_cmd(opkg_conf_t *conf, int argc, char **argv)
640 pkg_vec_t *available;
642 char desc_short[OPKG_LIST_DESCRIPTION_LENGTH];
644 char *pkg_name = NULL;
650 available = pkg_vec_alloc();
651 pkg_hash_fetch_available(&conf->pkg_hash, available);
652 for (i=0; i < available->len; i++) {
653 pkg = available->pkgs[i];
654 /* if we have package name or pattern and pkg does not match, then skip it */
655 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
657 if (pkg->description) {
658 strncpy(desc_short, pkg->description, OPKG_LIST_DESCRIPTION_LENGTH);
660 desc_short[0] = '\0';
662 desc_short[OPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
663 newline = strchr(desc_short, '\n');
668 version_str = pkg_version_str_alloc(pkg);
669 opkg_cb_list(pkg->name,desc_short,
676 pkg_vec_free(available);
682 static int opkg_list_installed_cmd(opkg_conf_t *conf, int argc, char **argv)
685 pkg_vec_t *available;
687 char desc_short[OPKG_LIST_DESCRIPTION_LENGTH];
689 char *pkg_name = NULL;
695 available = pkg_vec_alloc();
696 pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
697 for (i=0; i < available->len; i++) {
698 pkg = available->pkgs[i];
699 /* if we have package name or pattern and pkg does not match, then skip it */
700 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
702 if (pkg->description) {
703 strncpy(desc_short, pkg->description, OPKG_LIST_DESCRIPTION_LENGTH);
705 desc_short[0] = '\0';
707 desc_short[OPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
708 newline = strchr(desc_short, '\n');
713 version_str = pkg_version_str_alloc(pkg);
714 opkg_cb_list(pkg->name,desc_short,
725 static int opkg_info_status_cmd(opkg_conf_t *conf, int argc, char **argv, int installed_only)
728 pkg_vec_t *available;
730 char *pkg_name = NULL;
731 char **pkg_fields = NULL;
733 char *buff ; // = (char *)malloc(1);
739 pkg_fields = &argv[1];
743 available = pkg_vec_alloc();
745 pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
747 pkg_hash_fetch_available(&conf->pkg_hash, available);
748 for (i=0; i < available->len; i++) {
749 pkg = available->pkgs[i];
750 if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
754 buff = pkg_formatted_info(pkg);
756 if (opkg_cb_status) opkg_cb_status(pkg->name,
761 We should not forget that actually the pointer is allocated.
762 We need to free it :) ( Thanks florian for seeing the error )
766 if (conf->verbosity > 1) {
767 conffile_list_elt_t *iter;
768 for (iter = pkg->conffiles.head; iter; iter = iter->next) {
769 conffile_t *cf = iter->data;
770 int modified = conffile_has_been_modified(conf, cf);
771 opkg_message(conf, OPKG_NOTICE, "conffile=%s md5sum=%s modified=%d\n",
772 cf->name, cf->value, modified);
776 pkg_vec_free(available);
781 static int opkg_info_cmd(opkg_conf_t *conf, int argc, char **argv)
783 return opkg_info_status_cmd(conf, argc, argv, 0);
786 static int opkg_status_cmd(opkg_conf_t *conf, int argc, char **argv)
788 return opkg_info_status_cmd(conf, argc, argv, 1);
791 static int opkg_configure_cmd(opkg_conf_t *conf, int argc, char **argv)
796 char *pkg_name = NULL;
800 err = opkg_configure_packages (conf, pkg_name);
803 err = opkg_configure_packages (conf, NULL);
806 write_status_files_if_changed(conf);
811 static int opkg_install_pending_cmd(opkg_conf_t *conf, int argc, char **argv)
817 sprintf_alloc(&globpattern, "%s/*" OPKG_PKG_EXTENSION, conf->pending_dir);
818 err = glob(globpattern, 0, NULL, &globbuf);
824 opkg_message(conf, OPKG_NOTICE,
825 "The following packages in %s will now be installed.\n",
827 for (i = 0; i < globbuf.gl_pathc; i++) {
828 opkg_message(conf, OPKG_NOTICE,
829 "%s%s", i == 0 ? "" : " ", globbuf.gl_pathv[i]);
831 opkg_message(conf, OPKG_NOTICE, "\n");
832 for (i = 0; i < globbuf.gl_pathc; i++) {
833 err = opkg_install_from_file(conf, globbuf.gl_pathv[i]);
835 err = unlink(globbuf.gl_pathv[i]);
837 opkg_message(conf, OPKG_ERROR,
838 "%s: ERROR: failed to unlink %s: %s\n",
839 __FUNCTION__, globbuf.gl_pathv[i], strerror(err));
849 static int opkg_remove_cmd(opkg_conf_t *conf, int argc, char **argv)
853 pkg_t *pkg_to_remove;
854 pkg_vec_t *available;
855 char *pkg_name = NULL;
857 signal(SIGINT, sigint_handler);
859 // ENH: Add the "no pkg removed" just in case.
863 available = pkg_vec_alloc();
864 pkg_info_preinstall_check(conf);
866 pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
867 for (i=0; i < argc; i++) {
868 pkg_name = malloc(strlen(argv[i])+2);
869 strcpy(pkg_name,argv[i]);
870 for (a=0; a < available->len; a++) {
871 pkg = available->pkgs[a];
872 if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
875 if (conf->restrict_to_default_dest) {
876 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
880 pkg_to_remove = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name );
884 opkg_message(conf, OPKG_ERROR, "Package %s is not installed.\n", pkg->name);
887 if (pkg->state_status == SS_NOT_INSTALLED) { // Added the control, so every already removed package could be skipped
888 opkg_message(conf, OPKG_ERROR, "Package seems to be %s not installed (STATUS = NOT_INSTALLED).\n", pkg->name);
891 opkg_remove_pkg(conf, pkg_to_remove,0);
896 pkg_vec_free(available);
898 pkg_vec_t *installed_pkgs = pkg_vec_alloc();
900 int flagged_pkg_count = 0;
903 pkg_hash_fetch_all_installed(&conf->pkg_hash, installed_pkgs);
905 for (i = 0; i < installed_pkgs->len; i++) {
906 pkg_t *pkg = installed_pkgs->pkgs[i];
907 if (pkg->state_flag & SF_USER) {
910 if (!pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL))
911 opkg_message(conf, OPKG_NOTICE, "Non-user leaf package: %s\n", pkg->name);
914 if (!flagged_pkg_count) {
915 opkg_message(conf, OPKG_NOTICE, "No packages flagged as installed by user, \n"
916 "so refusing to uninstall unflagged non-leaf packages\n");
920 /* find packages not flagged SF_USER (i.e., installed to
921 * satisfy a dependence) and not having any dependents, and
925 for (i = 0; i < installed_pkgs->len; i++) {
926 pkg_t *pkg = installed_pkgs->pkgs[i];
927 if (!(pkg->state_flag & SF_USER)
928 && !pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL)) {
930 opkg_message(conf, OPKG_NOTICE, "Removing non-user leaf package %s\n");
931 opkg_remove_pkg(conf, pkg,0);
936 pkg_vec_free(installed_pkgs);
940 opkg_message(conf, OPKG_NOTICE, "No packages removed.\n");
942 write_status_files_if_changed(conf);
946 static int opkg_purge_cmd(opkg_conf_t *conf, int argc, char **argv)
952 signal(SIGINT, sigint_handler);
954 pkg_info_preinstall_check(conf);
956 for (i=0; i < argc; i++) {
957 if (conf->restrict_to_default_dest) {
958 pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
962 pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
966 opkg_message(conf, OPKG_ERROR,
967 "Package %s is not installed.\n", argv[i]);
970 opkg_purge_pkg(conf, pkg);
973 write_status_files_if_changed(conf);
977 static int opkg_flag_cmd(opkg_conf_t *conf, int argc, char **argv)
981 const char *flags = argv[0];
984 signal(SIGINT, sigint_handler);
986 for (i=1; i < argc; i++) {
987 if (conf->restrict_to_default_dest) {
988 pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
992 pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
996 opkg_message(conf, OPKG_ERROR,
997 "Package %s is not installed.\n", argv[i]);
1000 if (( strcmp(flags,"hold")==0)||( strcmp(flags,"noprune")==0)||
1001 ( strcmp(flags,"user")==0)||( strcmp(flags,"ok")==0)) {
1002 pkg->state_flag = pkg_state_flag_from_str(flags);
1004 /* pb_ asked this feature 03292004 */
1005 /* Actually I will use only this two, but this is an open for various status */
1006 if (( strcmp(flags,"installed")==0)||( strcmp(flags,"unpacked")==0)){
1007 pkg->state_status = pkg_state_status_from_str(flags);
1009 opkg_state_changed++;
1010 opkg_message(conf, OPKG_NOTICE,
1011 "Setting flags for package %s to %s\n",
1015 write_status_files_if_changed(conf);
1019 static int opkg_files_cmd(opkg_conf_t *conf, int argc, char **argv)
1022 str_list_t *installed_files;
1023 str_list_elt_t *iter;
1025 size_t buff_len = 8192;
1029 buff = (char *)malloc(buff_len);
1030 if ( buff == NULL ) {
1031 fprintf( stderr,"%s: Unable to allocate memory \n",__FUNCTION__);
1039 pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
1042 opkg_message(conf, OPKG_ERROR,
1043 "Package %s not installed.\n", argv[0]);
1047 installed_files = pkg_get_installed_files(pkg);
1048 pkg_version = pkg_version_str_alloc(pkg);
1052 used_len = snprintf(buff, buff_len, "Package %s (%s) is installed on %s and has the following files:\n",
1053 pkg->name, pkg_version, pkg->dest->name) + 1;
1054 if (used_len > buff_len) {
1056 buff = realloc (buff, buff_len);
1059 for (iter = installed_files->head; iter; iter = iter->next) {
1060 used_len += strlen (iter->data) + 1;
1061 while (buff_len <= used_len) {
1063 buff = realloc (buff, buff_len);
1065 strncat(buff, iter->data, buff_len);
1066 strncat(buff, "\n", buff_len);
1068 if (opkg_cb_list) opkg_cb_list(pkg->name,
1070 pkg_version_str_alloc(pkg),
1077 pkg_free_installed_files(pkg);
1082 static int opkg_depends_cmd(opkg_conf_t *conf, int argc, char **argv)
1086 pkg_vec_t *available_pkgs = pkg_vec_alloc();
1087 const char *rel_str = "depends on";
1090 pkg_info_preinstall_check(conf);
1092 if (conf->query_all)
1093 pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1095 pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1096 for (i = 0; i < argc; i++) {
1097 const char *target = argv[i];
1100 opkg_message(conf, OPKG_ERROR, "target=%s\n", target);
1102 for (j = 0; j < available_pkgs->len; j++) {
1103 pkg_t *pkg = available_pkgs->pkgs[j];
1104 if (fnmatch(target, pkg->name, 0) == 0) {
1106 int count = pkg->depends_count + pkg->pre_depends_count;
1107 opkg_message(conf, OPKG_ERROR, "What %s (arch=%s) %s\n",
1108 target, pkg->architecture, rel_str);
1109 for (k = 0; k < count; k++) {
1110 compound_depend_t *cdepend = &pkg->depends[k];
1112 for (l = 0; l < cdepend->possibility_count; l++) {
1113 depend_t *possibility = cdepend->possibilities[l];
1114 opkg_message(conf, OPKG_ERROR, " %s", possibility->pkg->name);
1115 if (conf->verbosity > 0) {
1116 // char *ver = abstract_pkg_version_str_alloc(possibility->pkg);
1117 opkg_message(conf, OPKG_NOTICE, " %s", possibility->version);
1118 if (possibility->version) {
1119 char *typestr = NULL;
1120 switch (possibility->constraint) {
1121 case NONE: typestr = "none"; break;
1122 case EARLIER: typestr = "<"; break;
1123 case EARLIER_EQUAL: typestr = "<="; break;
1124 case EQUAL: typestr = "="; break;
1125 case LATER_EQUAL: typestr = ">="; break;
1126 case LATER: typestr = ">"; break;
1128 opkg_message(conf, OPKG_NOTICE, " (%s %s)", typestr, possibility->version);
1132 opkg_message(conf, OPKG_ERROR, "\n");
1138 pkg_vec_free(available_pkgs);
1143 enum what_field_type {
1152 static int opkg_what_depends_conflicts_cmd(opkg_conf_t *conf, enum what_field_type what_field_type, int recursive, int argc, char **argv)
1156 pkg_vec_t *available_pkgs = pkg_vec_alloc();
1157 const char *rel_str = NULL;
1161 switch (what_field_type) {
1162 case WHATDEPENDS: rel_str = "depends on"; break;
1163 case WHATCONFLICTS: rel_str = "conflicts with"; break;
1164 case WHATSUGGESTS: rel_str = "suggests"; break;
1165 case WHATRECOMMENDS: rel_str = "recommends"; break;
1166 case WHATPROVIDES: rel_str = "provides"; break;
1167 case WHATREPLACES: rel_str = "replaces"; break;
1170 if (conf->query_all)
1171 pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1173 pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1175 /* mark the root set */
1176 pkg_vec_clear_marks(available_pkgs);
1177 opkg_message(conf, OPKG_NOTICE, "Root set:\n");
1178 for (i = 0; i < argc; i++) {
1179 const char *dependee_pattern = argv[i];
1180 pkg_vec_mark_if_matches(available_pkgs, dependee_pattern);
1182 for (i = 0; i < available_pkgs->len; i++) {
1183 pkg_t *pkg = available_pkgs->pkgs[i];
1184 if (pkg->state_flag & SF_MARKED) {
1185 /* mark the parent (abstract) package */
1186 pkg_mark_provides(pkg);
1187 opkg_message(conf, OPKG_NOTICE, " %s\n", pkg->name);
1191 opkg_message(conf, OPKG_NOTICE, "What %s root set\n", rel_str);
1196 for (j = 0; j < available_pkgs->len; j++) {
1197 pkg_t *pkg = available_pkgs->pkgs[j];
1199 int count = ((what_field_type == WHATCONFLICTS)
1200 ? pkg->conflicts_count
1201 : pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count);
1202 /* skip this package if it is already marked */
1203 if (pkg->parent->state_flag & SF_MARKED) {
1206 for (k = 0; k < count; k++) {
1207 compound_depend_t *cdepend =
1208 (what_field_type == WHATCONFLICTS) ? &pkg->conflicts[k] : &pkg->depends[k];
1210 for (l = 0; l < cdepend->possibility_count; l++) {
1211 depend_t *possibility = cdepend->possibilities[l];
1212 if (possibility->pkg->state_flag & SF_MARKED) {
1213 /* mark the depending package so we won't visit it again */
1214 pkg->state_flag |= SF_MARKED;
1215 pkg_mark_provides(pkg);
1218 opkg_message(conf, OPKG_NOTICE, " %s", pkg->name);
1219 if (conf->verbosity > 0) {
1220 char *ver = pkg_version_str_alloc(pkg);
1221 opkg_message(conf, OPKG_NOTICE, " %s", ver);
1222 opkg_message(conf, OPKG_NOTICE, "\t%s %s", rel_str, possibility->pkg->name);
1223 if (possibility->version) {
1224 char *typestr = NULL;
1225 switch (possibility->constraint) {
1226 case NONE: typestr = "none"; break;
1227 case EARLIER: typestr = "<"; break;
1228 case EARLIER_EQUAL: typestr = "<="; break;
1229 case EQUAL: typestr = "="; break;
1230 case LATER_EQUAL: typestr = ">="; break;
1231 case LATER: typestr = ">"; break;
1233 opkg_message(conf, OPKG_NOTICE, " (%s %s)", typestr, possibility->version);
1236 if (!pkg_dependence_satisfiable(conf, possibility))
1237 opkg_message(conf, OPKG_NOTICE, " unsatisfiable");
1239 opkg_message(conf, OPKG_NOTICE, "\n");
1247 } while (changed && recursive);
1248 pkg_vec_free(available_pkgs);
1254 int pkg_mark_provides(pkg_t *pkg)
1256 int provides_count = pkg->provides_count;
1257 abstract_pkg_t **provides = pkg->provides;
1259 pkg->parent->state_flag |= SF_MARKED;
1260 for (i = 0; i < provides_count; i++) {
1261 provides[i]->state_flag |= SF_MARKED;
1266 static int opkg_whatdepends_recursively_cmd(opkg_conf_t *conf, int argc, char **argv)
1268 return opkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 1, argc, argv);
1270 static int opkg_whatdepends_cmd(opkg_conf_t *conf, int argc, char **argv)
1272 return opkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 0, argc, argv);
1275 static int opkg_whatsuggests_cmd(opkg_conf_t *conf, int argc, char **argv)
1277 return opkg_what_depends_conflicts_cmd(conf, WHATSUGGESTS, 0, argc, argv);
1280 static int opkg_whatrecommends_cmd(opkg_conf_t *conf, int argc, char **argv)
1282 return opkg_what_depends_conflicts_cmd(conf, WHATRECOMMENDS, 0, argc, argv);
1285 static int opkg_whatconflicts_cmd(opkg_conf_t *conf, int argc, char **argv)
1287 return opkg_what_depends_conflicts_cmd(conf, WHATCONFLICTS, 0, argc, argv);
1290 static int opkg_what_provides_replaces_cmd(opkg_conf_t *conf, enum what_field_type what_field_type, int argc, char **argv)
1294 pkg_vec_t *available_pkgs = pkg_vec_alloc();
1295 const char *rel_str = (what_field_type == WHATPROVIDES ? "provides" : "replaces");
1298 pkg_info_preinstall_check(conf);
1300 if (conf->query_all)
1301 pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1303 pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1304 for (i = 0; i < argc; i++) {
1305 const char *target = argv[i];
1308 opkg_message(conf, OPKG_ERROR, "What %s %s\n",
1310 for (j = 0; j < available_pkgs->len; j++) {
1311 pkg_t *pkg = available_pkgs->pkgs[j];
1313 int count = (what_field_type == WHATPROVIDES) ? pkg->provides_count : pkg->replaces_count;
1314 for (k = 0; k < count; k++) {
1315 abstract_pkg_t *apkg =
1316 ((what_field_type == WHATPROVIDES)
1318 : pkg->replaces[k]);
1319 if (fnmatch(target, apkg->name, 0) == 0) {
1320 opkg_message(conf, OPKG_ERROR, " %s", pkg->name);
1321 if (strcmp(target, apkg->name) != 0)
1322 opkg_message(conf, OPKG_ERROR, "\t%s %s\n", rel_str, apkg->name);
1323 opkg_message(conf, OPKG_ERROR, "\n");
1328 pkg_vec_free(available_pkgs);
1333 static int opkg_whatprovides_cmd(opkg_conf_t *conf, int argc, char **argv)
1335 return opkg_what_provides_replaces_cmd(conf, WHATPROVIDES, argc, argv);
1338 static int opkg_whatreplaces_cmd(opkg_conf_t *conf, int argc, char **argv)
1340 return opkg_what_provides_replaces_cmd(conf, WHATREPLACES, argc, argv);
1343 static int opkg_search_cmd(opkg_conf_t *conf, int argc, char **argv)
1347 pkg_vec_t *installed;
1349 str_list_t *installed_files;
1350 str_list_elt_t *iter;
1351 char *installed_file;
1357 installed = pkg_vec_alloc();
1358 pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
1360 for (i=0; i < installed->len; i++) {
1361 pkg = installed->pkgs[i];
1363 installed_files = pkg_get_installed_files(pkg);
1365 for (iter = installed_files->head; iter; iter = iter->next) {
1366 installed_file = iter->data;
1367 if (fnmatch(argv[0], installed_file, 0)==0) {
1368 if (opkg_cb_list) opkg_cb_list(pkg->name,
1370 pkg_version_str_alloc(pkg),
1371 pkg->state_status, p_userdata);
1375 pkg_free_installed_files(pkg);
1378 /* XXX: CLEANUP: It's not obvious from the name of
1379 pkg_hash_fetch_all_installed that we need to call
1380 pkg_vec_free to avoid a memory leak. */
1381 pkg_vec_free(installed);
1386 static int opkg_compare_versions_cmd(opkg_conf_t *conf, int argc, char **argv)
1389 /* this is a bit gross */
1391 parseVersion(&p1, argv[0]);
1392 parseVersion(&p2, argv[2]);
1393 return pkg_version_satisfied(&p1, &p2, argv[1]);
1395 opkg_message(conf, OPKG_ERROR,
1396 "opkg compare_versions <v1> <op> <v2>\n"
1397 "<op> is one of <= >= << >> =\n");
1402 #ifndef HOST_CPU_STR
1403 #define HOST_CPU_STR__(X) #X
1404 #define HOST_CPU_STR_(X) HOST_CPU_STR__(X)
1405 #define HOST_CPU_STR HOST_CPU_STR_(HOST_CPU_FOO)
1408 static int opkg_print_architecture_cmd(opkg_conf_t *conf, int argc, char **argv)
1410 nv_pair_list_elt_t *l;
1412 l = conf->arch_list.head;
1414 nv_pair_t *nv = l->data;
1415 printf("arch %s %s\n", nv->name, nv->value);