1 /* ipkg_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 "ipkg_conf.h"
32 #include "ipkg_message.h"
35 #include "pkg_parse.h"
36 #include "sprintf_alloc.h"
38 #include "file_util.h"
40 #include "libbb/libbb.h"
45 #include "ipkg_download.h"
46 #include "ipkg_install.h"
47 #include "ipkg_upgrade.h"
48 #include "ipkg_remove.h"
49 #include "ipkg_configure.h"
50 #include "ipkg_message.h"
54 static void *p_userdata = NULL;
57 static int ipkg_update_cmd(ipkg_conf_t *conf, int argc, char **argv);
58 static int ipkg_upgrade_cmd(ipkg_conf_t *conf, int argc, char **argv);
59 static int ipkg_list_cmd(ipkg_conf_t *conf, int argc, char **argv);
60 static int ipkg_info_cmd(ipkg_conf_t *conf, int argc, char **argv);
61 static int ipkg_status_cmd(ipkg_conf_t *conf, int argc, char **argv);
62 static int ipkg_install_pending_cmd(ipkg_conf_t *conf, int argc, char **argv);
63 static int ipkg_install_cmd(ipkg_conf_t *conf, int argc, char **argv);
64 static int ipkg_list_installed_cmd(ipkg_conf_t *conf, int argc, char **argv);
65 static int ipkg_remove_cmd(ipkg_conf_t *conf, int argc, char **argv);
66 static int ipkg_purge_cmd(ipkg_conf_t *conf, int argc, char **argv);
67 static int ipkg_flag_cmd(ipkg_conf_t *conf, int argc, char **argv);
68 static int ipkg_files_cmd(ipkg_conf_t *conf, int argc, char **argv);
69 static int ipkg_search_cmd(ipkg_conf_t *conf, int argc, char **argv);
70 static int ipkg_download_cmd(ipkg_conf_t *conf, int argc, char **argv);
71 static int ipkg_depends_cmd(ipkg_conf_t *conf, int argc, char **argv);
72 static int ipkg_whatdepends_cmd(ipkg_conf_t *conf, int argc, char **argv);
73 static int ipkg_whatdepends_recursively_cmd(ipkg_conf_t *conf, int argc, char **argv);
74 static int ipkg_whatsuggests_cmd(ipkg_conf_t *conf, int argc, char **argv);
75 static int ipkg_whatrecommends_cmd(ipkg_conf_t *conf, int argc, char **argv);
76 static int ipkg_whatprovides_cmd(ipkg_conf_t *conf, int argc, char **argv);
77 static int ipkg_whatconflicts_cmd(ipkg_conf_t *conf, int argc, char **argv);
78 static int ipkg_whatreplaces_cmd(ipkg_conf_t *conf, int argc, char **argv);
79 static int ipkg_compare_versions_cmd(ipkg_conf_t *conf, int argc, char **argv);
80 static int ipkg_print_architecture_cmd(ipkg_conf_t *conf, int argc, char **argv);
81 static int ipkg_configure_cmd(ipkg_conf_t *conf, int argc, char **argv);
83 /* XXX: CLEANUP: The usage strings should be incorporated into this
84 array for easier maintenance */
85 static ipkg_cmd_t cmds[] = {
86 {"update", 0, (ipkg_cmd_fun_t)ipkg_update_cmd},
87 {"upgrade", 0, (ipkg_cmd_fun_t)ipkg_upgrade_cmd},
88 {"list", 0, (ipkg_cmd_fun_t)ipkg_list_cmd},
89 {"list_installed", 0, (ipkg_cmd_fun_t)ipkg_list_installed_cmd},
90 {"info", 0, (ipkg_cmd_fun_t)ipkg_info_cmd},
91 {"flag", 1, (ipkg_cmd_fun_t)ipkg_flag_cmd},
92 {"status", 0, (ipkg_cmd_fun_t)ipkg_status_cmd},
93 {"install_pending", 0, (ipkg_cmd_fun_t)ipkg_install_pending_cmd},
94 {"install", 1, (ipkg_cmd_fun_t)ipkg_install_cmd},
95 {"remove", 1, (ipkg_cmd_fun_t)ipkg_remove_cmd},
96 {"purge", 1, (ipkg_cmd_fun_t)ipkg_purge_cmd},
97 {"configure", 0, (ipkg_cmd_fun_t)ipkg_configure_cmd},
98 {"files", 1, (ipkg_cmd_fun_t)ipkg_files_cmd},
99 {"search", 1, (ipkg_cmd_fun_t)ipkg_search_cmd},
100 {"download", 1, (ipkg_cmd_fun_t)ipkg_download_cmd},
101 {"compare_versions", 1, (ipkg_cmd_fun_t)ipkg_compare_versions_cmd},
102 {"compare-versions", 1, (ipkg_cmd_fun_t)ipkg_compare_versions_cmd},
103 {"print-architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
104 {"print_architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
105 {"print-installation-architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
106 {"print_installation_architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
107 {"depends", 1, (ipkg_cmd_fun_t)ipkg_depends_cmd},
108 {"whatdepends", 1, (ipkg_cmd_fun_t)ipkg_whatdepends_cmd},
109 {"whatdependsrec", 1, (ipkg_cmd_fun_t)ipkg_whatdepends_recursively_cmd},
110 {"whatrecommends", 1, (ipkg_cmd_fun_t)ipkg_whatrecommends_cmd},
111 {"whatsuggests", 1, (ipkg_cmd_fun_t)ipkg_whatsuggests_cmd},
112 {"whatprovides", 1, (ipkg_cmd_fun_t)ipkg_whatprovides_cmd},
113 {"whatreplaces", 1, (ipkg_cmd_fun_t)ipkg_whatreplaces_cmd},
114 {"whatconflicts", 1, (ipkg_cmd_fun_t)ipkg_whatconflicts_cmd},
117 int ipkg_state_changed;
118 static void write_status_files_if_changed(ipkg_conf_t *conf)
120 if (ipkg_state_changed && !conf->noaction) {
121 ipkg_message(conf, IPKG_INFO,
122 " writing status file\n");
123 ipkg_conf_write_status_files(conf);
124 pkg_write_changed_filelists(conf);
126 ipkg_message(conf, IPKG_NOTICE, "Nothing to be done\n");
131 static int num_cmds = sizeof(cmds) / sizeof(ipkg_cmd_t);
133 ipkg_cmd_t *ipkg_cmd_find(const char *name)
138 for (i=0; i < num_cmds; i++) {
140 if (strcmp(name, cmd->name) == 0) {
149 int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc, const char **argv, void *userdata)
152 p_userdata = userdata;
155 result = (cmd->fun)(conf, argc, argv);
157 ipkg_message(conf, IPKG_NOTICE, "Successfully terminated.\n");
159 ipkg_message(conf, IPKG_NOTICE, "An error ocurred, return value: %d.\n", result);
163 reverse_error_list(&error_list);
165 ipkg_message(conf, IPKG_NOTICE, "Collected errors:\n");
166 /* Here we print the errors collected and free the list */
167 while (error_list != NULL) {
168 ipkg_message(conf, IPKG_NOTICE, "%s",error_list->errmsg);
169 error_list = error_list->next;
172 free_error_list(&error_list);
180 int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc, const char **argv)
182 return (cmd->fun)(conf, argc, argv);
186 static int ipkg_update_cmd(ipkg_conf_t *conf, int argc, char **argv)
191 pkg_src_list_elt_t *iter;
195 sprintf_alloc(&lists_dir, "%s", conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir);
197 if (! file_is_dir(lists_dir)) {
198 if (file_exists(lists_dir)) {
199 ipkg_message(conf, IPKG_ERROR,
200 "%s: ERROR: %s exists, but is not a directory\n",
201 __FUNCTION__, lists_dir);
205 err = file_mkdir_hier(lists_dir, 0755);
207 ipkg_message(conf, IPKG_ERROR,
208 "%s: ERROR: failed to make directory %s: %s\n",
209 __FUNCTION__, lists_dir, strerror(errno));
216 for (iter = conf->pkg_src_list.head; iter; iter = iter->next) {
217 char *url, *list_file_name;
221 if (src->extra_data) /* debian style? */
222 sprintf_alloc(&url, "%s/%s/%s", src->value, src->extra_data,
223 src->gzip ? "Packages.gz" : "Packages");
225 sprintf_alloc(&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
227 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
233 tmp = strdup ("/tmp/ipkg.XXXXXX");
235 if (mkdtemp (tmp) == NULL) {
241 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
242 err = ipkg_download(conf, url, tmp_file_name);
244 ipkg_message (conf, IPKG_NOTICE, "Inflating %s\n", url);
245 in = fopen (tmp_file_name, "r");
246 out = fopen (list_file_name, "w");
255 unlink (tmp_file_name);
260 err = ipkg_download(conf, url, list_file_name);
264 ipkg_message(conf, IPKG_NOTICE,
265 "Updated list of available packages in %s\n",
269 free(list_file_name);
273 #ifdef CONFIG_CLEAR_SW_INSTALL_FLAG
275 /* clear SW_INSTALL on any package where state is SS_NOT_INSTALLED.
276 * this is a hack to work around poor bookkeeping in old ipkg upgrade code
282 pkg_vec_t *available = pkg_vec_alloc();
283 pkg_hash_fetch_available(&conf->pkg_hash, available);
284 ipkg_message(conf, IPKG_DEBUG, "Clearing SW_INSTALL for SS_NOT_INSTALLED packages.\n");
285 for (i = 0; i < available->len; i++) {
286 pkg_t *pkg = available->pkgs[i];
287 if (pkg->state_want == SW_INSTALL && pkg->state_status == SS_NOT_INSTALLED) {
288 ipkg_message(conf, IPKG_DEBUG, "Clearing SW_INSTALL on package %s.\n", pkg->name);
289 pkg->state_want = SW_UNKNOWN;
293 pkg_vec_free(available);
295 write_status_files_if_changed(conf);
304 /* scan the args passed and cache the local filenames of the packages */
305 int ipkg_multiple_files_scan(ipkg_conf_t *conf, int argc, char **argv)
311 * First scan through package names/urls
312 * For any urls, download the packages and install in database.
313 * For any files, install package info in database.
315 for (i = 0; i < argc; i ++) {
316 char *filename = argv [i];
317 //char *tmp = basename (tmp);
318 //int tmplen = strlen (tmp);
320 //if (strcmp (tmp + (tmplen - strlen (IPKG_PKG_EXTENSION)), IPKG_PKG_EXTENSION) != 0)
322 //if (strcmp (tmp + (tmplen - strlen (DPKG_PKG_EXTENSION)), DPKG_PKG_EXTENSION) != 0)
325 ipkg_message(conf, IPKG_DEBUG2, "Debug mfs: %s \n",filename );
327 err = ipkg_prepare_url_for_install(conf, filename, &argv[i]);
334 struct ipkg_intercept
340 typedef struct ipkg_intercept *ipkg_intercept_t;
342 ipkg_intercept_t ipkg_prep_intercepts(ipkg_conf_t *conf)
344 ipkg_intercept_t ctx;
348 ctx = malloc (sizeof (*ctx));
349 ctx->oldpath = strdup (getenv ("PATH"));
351 sprintf_alloc (&newpath, "%s/ipkg/intercept:%s", DATADIR, ctx->oldpath);
352 setenv ("PATH", newpath, 1);
357 sprintf_alloc (&ctx->statedir, "/tmp/ipkg-intercept-%d-%d", getpid (), gen);
358 if (mkdir (ctx->statedir, 0770) < 0) {
359 if (errno == EEXIST) {
360 free (ctx->statedir);
364 perror (ctx->statedir);
367 setenv ("IPKG_INTERCEPT_DIR", ctx->statedir, 1);
371 int ipkg_finalize_intercepts(ipkg_intercept_t ctx)
377 setenv ("PATH", ctx->oldpath, 1);
380 dir = opendir (ctx->statedir);
383 while (de = readdir (dir), de != NULL) {
386 if (de->d_name[0] == '.')
389 sprintf_alloc (&path, "%s/%s", ctx->statedir, de->d_name);
390 if (access (path, X_OK) == 0) {
399 perror (ctx->statedir);
401 sprintf_alloc (&cmd, "rm -rf %s", ctx->statedir);
405 free (ctx->statedir);
411 int ipkg_configure_packages(ipkg_conf_t *conf, char *pkg_name)
419 ipkg_message(conf, IPKG_INFO,
420 "Configuring unpacked packages\n");
423 all = pkg_vec_alloc();
424 pkg_hash_fetch_available(&conf->pkg_hash, all);
426 ic = ipkg_prep_intercepts (conf);
428 for(i = 0; i < all->len; i++) {
431 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
434 if (pkg->state_status == SS_UNPACKED) {
435 ipkg_message(conf, IPKG_NOTICE,
436 "Configuring %s\n", pkg->name);
438 r = ipkg_configure(conf, pkg);
440 pkg->state_status = SS_INSTALLED;
441 pkg->parent->state_status = SS_INSTALLED;
442 pkg->state_flag &= ~SF_PREFER;
450 r = ipkg_finalize_intercepts (ic);
458 static ipkg_conf_t *global_conf;
460 static void sigint_handler(int sig)
462 signal(sig, SIG_DFL);
463 ipkg_message(NULL, IPKG_NOTICE,
464 "ipkg: interrupted. writing out status database\n");
465 write_status_files_if_changed(global_conf);
469 static int ipkg_install_cmd(ipkg_conf_t *conf, int argc, char **argv)
476 signal(SIGINT, sigint_handler);
479 * Now scan through package names and install
481 for (i=0; i < argc; i++) {
484 ipkg_message(conf, IPKG_DEBUG2, "Debug install_cmd: %s \n",arg );
485 err = ipkg_prepare_url_for_install(conf, arg, &argv[i]);
486 if (err != EINVAL && err != 0)
489 pkg_info_preinstall_check(conf);
491 for (i=0; i < argc; i++) {
493 if (conf->multiple_providers)
494 err = ipkg_install_multi_by_name(conf, arg);
496 err = ipkg_install_by_name(conf, arg);
498 if (err == IPKG_PKG_HAS_NO_CANDIDATE) {
499 ipkg_message(conf, IPKG_ERROR,
500 "Cannot find package %s.\n"
501 "Check the spelling or perhaps run 'ipkg update'\n",
506 /* recheck to verify that all dependences are satisfied */
507 if (0) ipkg_satisfy_all_dependences(conf);
509 ipkg_configure_packages(conf, NULL);
511 write_status_files_if_changed(conf);
516 static int ipkg_upgrade_cmd(ipkg_conf_t *conf, int argc, char **argv)
523 signal(SIGINT, sigint_handler);
526 for (i=0; i < argc; i++) {
529 err = ipkg_prepare_url_for_install(conf, arg, &arg);
530 if (err != EINVAL && err != 0)
533 pkg_info_preinstall_check(conf);
535 for (i=0; i < argc; i++) {
537 if (conf->restrict_to_default_dest) {
538 pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
542 ipkg_message(conf, IPKG_NOTICE,
543 "Package %s not installed in %s\n",
544 argv[i], conf->default_dest->name);
548 pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
552 ipkg_upgrade_pkg(conf, pkg);
554 ipkg_install_by_name(conf, arg);
558 pkg_vec_t *installed = pkg_vec_alloc();
560 pkg_info_preinstall_check(conf);
562 pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
563 for (i = 0; i < installed->len; i++) {
564 pkg = installed->pkgs[i];
565 ipkg_upgrade_pkg(conf, pkg);
567 pkg_vec_free(installed);
570 /* recheck to verify that all dependences are satisfied */
571 if (0) ipkg_satisfy_all_dependences(conf);
573 ipkg_configure_packages(conf, NULL);
575 write_status_files_if_changed(conf);
580 static int ipkg_download_cmd(ipkg_conf_t *conf, int argc, char **argv)
586 pkg_info_preinstall_check(conf);
587 for (i = 0; i < argc; i++) {
590 pkg = pkg_hash_fetch_best_installation_candidate_by_name(conf, arg);
592 ipkg_message(conf, IPKG_ERROR,
593 "Cannot find package %s.\n"
594 "Check the spelling or perhaps run 'ipkg update'\n",
599 err = ipkg_download_pkg(conf, pkg, ".");
602 ipkg_message(conf, IPKG_ERROR,
603 "Failed to download %s\n", pkg->name);
605 ipkg_message(conf, IPKG_NOTICE,
606 "Downloaded %s as %s\n",
607 pkg->name, pkg->local_filename);
615 static int ipkg_list_cmd(ipkg_conf_t *conf, int argc, char **argv)
618 pkg_vec_t *available;
620 char desc_short[IPKG_LIST_DESCRIPTION_LENGTH];
622 char *pkg_name = NULL;
628 available = pkg_vec_alloc();
629 pkg_hash_fetch_available(&conf->pkg_hash, available);
630 for (i=0; i < available->len; i++) {
631 pkg = available->pkgs[i];
632 /* if we have package name or pattern and pkg does not match, then skip it */
633 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
635 if (pkg->description) {
636 strncpy(desc_short, pkg->description, IPKG_LIST_DESCRIPTION_LENGTH);
638 desc_short[0] = '\0';
640 desc_short[IPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
641 newline = strchr(desc_short, '\n');
646 printf("%s - %s\n", pkg->name, desc_short);
649 version_str = pkg_version_str_alloc(pkg);
650 ipkg_cb_list(pkg->name,desc_short,
658 pkg_vec_free(available);
664 static int ipkg_list_installed_cmd(ipkg_conf_t *conf, int argc, char **argv)
667 pkg_vec_t *available;
669 char desc_short[IPKG_LIST_DESCRIPTION_LENGTH];
671 char *pkg_name = NULL;
677 available = pkg_vec_alloc();
678 pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
679 for (i=0; i < available->len; i++) {
680 pkg = available->pkgs[i];
681 /* if we have package name or pattern and pkg does not match, then skip it */
682 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
684 if (pkg->description) {
685 strncpy(desc_short, pkg->description, IPKG_LIST_DESCRIPTION_LENGTH);
687 desc_short[0] = '\0';
689 desc_short[IPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
690 newline = strchr(desc_short, '\n');
695 printf("%s - %s\n", pkg->name, desc_short);
698 version_str = pkg_version_str_alloc(pkg);
699 ipkg_cb_list(pkg->name,desc_short,
711 static int ipkg_info_status_cmd(ipkg_conf_t *conf, int argc, char **argv, int installed_only)
714 pkg_vec_t *available;
716 char *pkg_name = NULL;
717 char **pkg_fields = NULL;
719 char *buff ; // = (char *)malloc(1);
725 pkg_fields = &argv[1];
729 available = pkg_vec_alloc();
731 pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
733 pkg_hash_fetch_available(&conf->pkg_hash, available);
734 for (i=0; i < available->len; i++) {
735 pkg = available->pkgs[i];
736 if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
741 for (j = 0; j < n_fields; j++)
742 pkg_print_field(pkg, stdout, pkg_fields[j]);
744 pkg_print_info(pkg, stdout);
748 buff = pkg_formatted_info(pkg);
750 if (ipkg_cb_status) ipkg_cb_status(pkg->name,
755 We should not forget that actually the pointer is allocated.
756 We need to free it :) ( Thanks florian for seeing the error )
761 if (conf->verbosity > 1) {
762 conffile_list_elt_t *iter;
763 for (iter = pkg->conffiles.head; iter; iter = iter->next) {
764 conffile_t *cf = iter->data;
765 int modified = conffile_has_been_modified(conf, cf);
766 ipkg_message(conf, IPKG_NOTICE, "conffile=%s md5sum=%s modified=%d\n",
767 cf->name, cf->value, modified);
775 pkg_vec_free(available);
780 static int ipkg_info_cmd(ipkg_conf_t *conf, int argc, char **argv)
782 return ipkg_info_status_cmd(conf, argc, argv, 0);
785 static int ipkg_status_cmd(ipkg_conf_t *conf, int argc, char **argv)
787 return ipkg_info_status_cmd(conf, argc, argv, 1);
790 static int ipkg_configure_cmd(ipkg_conf_t *conf, int argc, char **argv)
795 char *pkg_name = NULL;
799 err = ipkg_configure_packages (conf, pkg_name);
802 err = ipkg_configure_packages (conf, NULL);
805 write_status_files_if_changed(conf);
810 static int ipkg_install_pending_cmd(ipkg_conf_t *conf, int argc, char **argv)
816 sprintf_alloc(&globpattern, "%s/*" IPKG_PKG_EXTENSION, conf->pending_dir);
817 err = glob(globpattern, 0, NULL, &globbuf);
823 ipkg_message(conf, IPKG_NOTICE,
824 "The following packages in %s will now be installed.\n",
826 for (i = 0; i < globbuf.gl_pathc; i++) {
827 ipkg_message(conf, IPKG_NOTICE,
828 "%s%s", i == 0 ? "" : " ", globbuf.gl_pathv[i]);
830 ipkg_message(conf, IPKG_NOTICE, "\n");
831 for (i = 0; i < globbuf.gl_pathc; i++) {
832 err = ipkg_install_from_file(conf, globbuf.gl_pathv[i]);
834 err = unlink(globbuf.gl_pathv[i]);
836 ipkg_message(conf, IPKG_ERROR,
837 "%s: ERROR: failed to unlink %s: %s\n",
838 __FUNCTION__, globbuf.gl_pathv[i], strerror(err));
848 static int ipkg_remove_cmd(ipkg_conf_t *conf, int argc, char **argv)
852 pkg_t *pkg_to_remove;
853 pkg_vec_t *available;
854 char *pkg_name = NULL;
856 signal(SIGINT, sigint_handler);
858 // ENH: Add the "no pkg removed" just in case.
862 available = pkg_vec_alloc();
863 pkg_info_preinstall_check(conf);
865 pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
866 for (i=0; i < argc; i++) {
867 pkg_name = malloc(strlen(argv[i])+2);
868 strcpy(pkg_name,argv[i]);
869 for (a=0; a < available->len; a++) {
870 pkg = available->pkgs[a];
871 if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
874 if (conf->restrict_to_default_dest) {
875 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
879 pkg_to_remove = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name );
883 ipkg_message(conf, IPKG_ERROR, "Package %s is not installed.\n", pkg->name);
886 if (pkg->state_status == SS_NOT_INSTALLED) { // Added the control, so every already removed package could be skipped
887 ipkg_message(conf, IPKG_ERROR, "Package seems to be %s not installed (STATUS = NOT_INSTALLED).\n", pkg->name);
890 ipkg_remove_pkg(conf, pkg_to_remove,0);
895 pkg_vec_free(available);
897 pkg_vec_t *installed_pkgs = pkg_vec_alloc();
899 int flagged_pkg_count = 0;
902 pkg_hash_fetch_all_installed(&conf->pkg_hash, installed_pkgs);
904 for (i = 0; i < installed_pkgs->len; i++) {
905 pkg_t *pkg = installed_pkgs->pkgs[i];
906 if (pkg->state_flag & SF_USER) {
909 if (!pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL))
910 ipkg_message(conf, IPKG_NOTICE, "Non-user leaf package: %s\n", pkg->name);
913 if (!flagged_pkg_count) {
914 ipkg_message(conf, IPKG_NOTICE, "No packages flagged as installed by user, \n"
915 "so refusing to uninstall unflagged non-leaf packages\n");
919 /* find packages not flagged SF_USER (i.e., installed to
920 * satisfy a dependence) and not having any dependents, and
924 for (i = 0; i < installed_pkgs->len; i++) {
925 pkg_t *pkg = installed_pkgs->pkgs[i];
926 if (!(pkg->state_flag & SF_USER)
927 && !pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL)) {
929 ipkg_message(conf, IPKG_NOTICE, "Removing non-user leaf package %s\n");
930 ipkg_remove_pkg(conf, pkg,0);
935 pkg_vec_free(installed_pkgs);
939 ipkg_message(conf, IPKG_NOTICE, "No packages removed.\n");
941 write_status_files_if_changed(conf);
945 static int ipkg_purge_cmd(ipkg_conf_t *conf, int argc, char **argv)
951 signal(SIGINT, sigint_handler);
953 pkg_info_preinstall_check(conf);
955 for (i=0; i < argc; i++) {
956 if (conf->restrict_to_default_dest) {
957 pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
961 pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
965 ipkg_message(conf, IPKG_ERROR,
966 "Package %s is not installed.\n", argv[i]);
969 ipkg_purge_pkg(conf, pkg);
972 write_status_files_if_changed(conf);
976 static int ipkg_flag_cmd(ipkg_conf_t *conf, int argc, char **argv)
980 const char *flags = argv[0];
983 signal(SIGINT, sigint_handler);
985 for (i=1; i < argc; i++) {
986 if (conf->restrict_to_default_dest) {
987 pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
991 pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
995 ipkg_message(conf, IPKG_ERROR,
996 "Package %s is not installed.\n", argv[i]);
999 if (( strcmp(flags,"hold")==0)||( strcmp(flags,"noprune")==0)||
1000 ( strcmp(flags,"user")==0)||( strcmp(flags,"ok")==0)) {
1001 pkg->state_flag = pkg_state_flag_from_str(flags);
1003 /* pb_ asked this feature 03292004 */
1004 /* Actually I will use only this two, but this is an open for various status */
1005 if (( strcmp(flags,"installed")==0)||( strcmp(flags,"unpacked")==0)){
1006 pkg->state_status = pkg_state_status_from_str(flags);
1008 ipkg_state_changed++;
1009 ipkg_message(conf, IPKG_NOTICE,
1010 "Setting flags for package %s to %s\n",
1014 write_status_files_if_changed(conf);
1018 static int ipkg_files_cmd(ipkg_conf_t *conf, int argc, char **argv)
1021 str_list_t *installed_files;
1022 str_list_elt_t *iter;
1024 size_t buff_len = 8192;
1028 buff = (char *)malloc(buff_len);
1029 if ( buff == NULL ) {
1030 fprintf( stderr,"%s: Unable to allocate memory \n",__FUNCTION__);
1038 pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
1041 ipkg_message(conf, IPKG_ERROR,
1042 "Package %s not installed.\n", argv[0]);
1046 installed_files = pkg_get_installed_files(pkg);
1047 pkg_version = pkg_version_str_alloc(pkg);
1050 printf("Package %s (%s) is installed on %s and has the following files:\n",
1051 pkg->name, pkg_version, pkg->dest->name);
1052 for (iter = installed_files->head; iter; iter = iter->next) {
1058 used_len = snprintf(buff, buff_len, "Package %s (%s) is installed on %s and has the following files:\n",
1059 pkg->name, pkg_version, pkg->dest->name) + 1;
1060 if (used_len > buff_len) {
1062 buff = realloc (buff, buff_len);
1065 for (iter = installed_files->head; iter; iter = iter->next) {
1066 used_len += strlen (iter->data) + 1;
1067 while (buff_len <= used_len) {
1069 buff = realloc (buff, buff_len);
1071 strncat(buff, iter->data, buff_len);
1072 strncat(buff, "\n", buff_len);
1074 if (ipkg_cb_list) ipkg_cb_list(pkg->name,
1076 pkg_version_str_alloc(pkg),
1084 pkg_free_installed_files(pkg);
1089 static int ipkg_depends_cmd(ipkg_conf_t *conf, int argc, char **argv)
1093 pkg_vec_t *available_pkgs = pkg_vec_alloc();
1094 const char *rel_str = "depends on";
1097 pkg_info_preinstall_check(conf);
1099 if (conf->query_all)
1100 pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1102 pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1103 for (i = 0; i < argc; i++) {
1104 const char *target = argv[i];
1107 ipkg_message(conf, IPKG_ERROR, "target=%s\n", target);
1109 for (j = 0; j < available_pkgs->len; j++) {
1110 pkg_t *pkg = available_pkgs->pkgs[j];
1111 if (fnmatch(target, pkg->name, 0) == 0) {
1113 int count = pkg->depends_count + pkg->pre_depends_count;
1114 ipkg_message(conf, IPKG_ERROR, "What %s (arch=%s) %s\n",
1115 target, pkg->architecture, rel_str);
1116 for (k = 0; k < count; k++) {
1117 compound_depend_t *cdepend = &pkg->depends[k];
1119 for (l = 0; l < cdepend->possibility_count; l++) {
1120 depend_t *possibility = cdepend->possibilities[l];
1121 ipkg_message(conf, IPKG_ERROR, " %s", possibility->pkg->name);
1122 if (conf->verbosity > 0) {
1123 // char *ver = abstract_pkg_version_str_alloc(possibility->pkg);
1124 ipkg_message(conf, IPKG_NOTICE, " %s", possibility->version);
1125 if (possibility->version) {
1126 char *typestr = NULL;
1127 switch (possibility->constraint) {
1128 case NONE: typestr = "none"; break;
1129 case EARLIER: typestr = "<"; break;
1130 case EARLIER_EQUAL: typestr = "<="; break;
1131 case EQUAL: typestr = "="; break;
1132 case LATER_EQUAL: typestr = ">="; break;
1133 case LATER: typestr = ">"; break;
1135 ipkg_message(conf, IPKG_NOTICE, " (%s %s)", typestr, possibility->version);
1139 ipkg_message(conf, IPKG_ERROR, "\n");
1145 pkg_vec_free(available_pkgs);
1150 enum what_field_type {
1159 static int ipkg_what_depends_conflicts_cmd(ipkg_conf_t *conf, enum what_field_type what_field_type, int recursive, int argc, char **argv)
1163 pkg_vec_t *available_pkgs = pkg_vec_alloc();
1164 const char *rel_str = NULL;
1168 switch (what_field_type) {
1169 case WHATDEPENDS: rel_str = "depends on"; break;
1170 case WHATCONFLICTS: rel_str = "conflicts with"; break;
1171 case WHATSUGGESTS: rel_str = "suggests"; break;
1172 case WHATRECOMMENDS: rel_str = "recommends"; break;
1173 case WHATPROVIDES: rel_str = "provides"; break;
1174 case WHATREPLACES: rel_str = "replaces"; break;
1177 if (conf->query_all)
1178 pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1180 pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1182 /* mark the root set */
1183 pkg_vec_clear_marks(available_pkgs);
1184 ipkg_message(conf, IPKG_NOTICE, "Root set:\n");
1185 for (i = 0; i < argc; i++) {
1186 const char *dependee_pattern = argv[i];
1187 pkg_vec_mark_if_matches(available_pkgs, dependee_pattern);
1189 for (i = 0; i < available_pkgs->len; i++) {
1190 pkg_t *pkg = available_pkgs->pkgs[i];
1191 if (pkg->state_flag & SF_MARKED) {
1192 /* mark the parent (abstract) package */
1193 pkg_mark_provides(pkg);
1194 ipkg_message(conf, IPKG_NOTICE, " %s\n", pkg->name);
1198 ipkg_message(conf, IPKG_NOTICE, "What %s root set\n", rel_str);
1203 for (j = 0; j < available_pkgs->len; j++) {
1204 pkg_t *pkg = available_pkgs->pkgs[j];
1206 int count = ((what_field_type == WHATCONFLICTS)
1207 ? pkg->conflicts_count
1208 : pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count);
1209 /* skip this package if it is already marked */
1210 if (pkg->parent->state_flag & SF_MARKED) {
1213 for (k = 0; k < count; k++) {
1214 compound_depend_t *cdepend =
1215 (what_field_type == WHATCONFLICTS) ? &pkg->conflicts[k] : &pkg->depends[k];
1217 for (l = 0; l < cdepend->possibility_count; l++) {
1218 depend_t *possibility = cdepend->possibilities[l];
1219 if (possibility->pkg->state_flag & SF_MARKED) {
1220 /* mark the depending package so we won't visit it again */
1221 pkg->state_flag |= SF_MARKED;
1222 pkg_mark_provides(pkg);
1225 ipkg_message(conf, IPKG_NOTICE, " %s", pkg->name);
1226 if (conf->verbosity > 0) {
1227 char *ver = pkg_version_str_alloc(pkg);
1228 ipkg_message(conf, IPKG_NOTICE, " %s", ver);
1229 ipkg_message(conf, IPKG_NOTICE, "\t%s %s", rel_str, possibility->pkg->name);
1230 if (possibility->version) {
1231 char *typestr = NULL;
1232 switch (possibility->constraint) {
1233 case NONE: typestr = "none"; break;
1234 case EARLIER: typestr = "<"; break;
1235 case EARLIER_EQUAL: typestr = "<="; break;
1236 case EQUAL: typestr = "="; break;
1237 case LATER_EQUAL: typestr = ">="; break;
1238 case LATER: typestr = ">"; break;
1240 ipkg_message(conf, IPKG_NOTICE, " (%s %s)", typestr, possibility->version);
1243 if (!pkg_dependence_satisfiable(conf, possibility))
1244 ipkg_message(conf, IPKG_NOTICE, " unsatisfiable");
1246 ipkg_message(conf, IPKG_NOTICE, "\n");
1254 } while (changed && recursive);
1255 pkg_vec_free(available_pkgs);
1261 int pkg_mark_provides(pkg_t *pkg)
1263 int provides_count = pkg->provides_count;
1264 abstract_pkg_t **provides = pkg->provides;
1266 pkg->parent->state_flag |= SF_MARKED;
1267 for (i = 0; i < provides_count; i++) {
1268 provides[i]->state_flag |= SF_MARKED;
1273 static int ipkg_whatdepends_recursively_cmd(ipkg_conf_t *conf, int argc, char **argv)
1275 return ipkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 1, argc, argv);
1277 static int ipkg_whatdepends_cmd(ipkg_conf_t *conf, int argc, char **argv)
1279 return ipkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 0, argc, argv);
1282 static int ipkg_whatsuggests_cmd(ipkg_conf_t *conf, int argc, char **argv)
1284 return ipkg_what_depends_conflicts_cmd(conf, WHATSUGGESTS, 0, argc, argv);
1287 static int ipkg_whatrecommends_cmd(ipkg_conf_t *conf, int argc, char **argv)
1289 return ipkg_what_depends_conflicts_cmd(conf, WHATRECOMMENDS, 0, argc, argv);
1292 static int ipkg_whatconflicts_cmd(ipkg_conf_t *conf, int argc, char **argv)
1294 return ipkg_what_depends_conflicts_cmd(conf, WHATCONFLICTS, 0, argc, argv);
1297 static int ipkg_what_provides_replaces_cmd(ipkg_conf_t *conf, enum what_field_type what_field_type, int argc, char **argv)
1301 pkg_vec_t *available_pkgs = pkg_vec_alloc();
1302 const char *rel_str = (what_field_type == WHATPROVIDES ? "provides" : "replaces");
1305 pkg_info_preinstall_check(conf);
1307 if (conf->query_all)
1308 pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
1310 pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
1311 for (i = 0; i < argc; i++) {
1312 const char *target = argv[i];
1315 ipkg_message(conf, IPKG_ERROR, "What %s %s\n",
1317 for (j = 0; j < available_pkgs->len; j++) {
1318 pkg_t *pkg = available_pkgs->pkgs[j];
1320 int count = (what_field_type == WHATPROVIDES) ? pkg->provides_count : pkg->replaces_count;
1321 for (k = 0; k < count; k++) {
1322 abstract_pkg_t *apkg =
1323 ((what_field_type == WHATPROVIDES)
1325 : pkg->replaces[k]);
1326 if (fnmatch(target, apkg->name, 0) == 0) {
1327 ipkg_message(conf, IPKG_ERROR, " %s", pkg->name);
1328 if (strcmp(target, apkg->name) != 0)
1329 ipkg_message(conf, IPKG_ERROR, "\t%s %s\n", rel_str, apkg->name);
1330 ipkg_message(conf, IPKG_ERROR, "\n");
1335 pkg_vec_free(available_pkgs);
1340 static int ipkg_whatprovides_cmd(ipkg_conf_t *conf, int argc, char **argv)
1342 return ipkg_what_provides_replaces_cmd(conf, WHATPROVIDES, argc, argv);
1345 static int ipkg_whatreplaces_cmd(ipkg_conf_t *conf, int argc, char **argv)
1347 return ipkg_what_provides_replaces_cmd(conf, WHATREPLACES, argc, argv);
1350 static int ipkg_search_cmd(ipkg_conf_t *conf, int argc, char **argv)
1354 pkg_vec_t *installed;
1356 str_list_t *installed_files;
1357 str_list_elt_t *iter;
1358 char *installed_file;
1364 installed = pkg_vec_alloc();
1365 pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
1367 for (i=0; i < installed->len; i++) {
1368 pkg = installed->pkgs[i];
1370 installed_files = pkg_get_installed_files(pkg);
1372 for (iter = installed_files->head; iter; iter = iter->next) {
1373 installed_file = iter->data;
1374 if (fnmatch(argv[0], installed_file, 0)==0) {
1376 printf("%s: %s\n", pkg->name, installed_file);
1378 if (ipkg_cb_list) ipkg_cb_list(pkg->name,
1380 pkg_version_str_alloc(pkg),
1381 pkg->state_status, p_userdata);
1386 pkg_free_installed_files(pkg);
1389 /* XXX: CLEANUP: It's not obvious from the name of
1390 pkg_hash_fetch_all_installed that we need to call
1391 pkg_vec_free to avoid a memory leak. */
1392 pkg_vec_free(installed);
1397 static int ipkg_compare_versions_cmd(ipkg_conf_t *conf, int argc, char **argv)
1400 /* this is a bit gross */
1402 parseVersion(&p1, argv[0]);
1403 parseVersion(&p2, argv[2]);
1404 return pkg_version_satisfied(&p1, &p2, argv[1]);
1406 ipkg_message(conf, IPKG_ERROR,
1407 "ipkg compare_versions <v1> <op> <v2>\n"
1408 "<op> is one of <= >= << >> =\n");
1413 #ifndef HOST_CPU_STR
1414 #define HOST_CPU_STR__(X) #X
1415 #define HOST_CPU_STR_(X) HOST_CPU_STR__(X)
1416 #define HOST_CPU_STR HOST_CPU_STR_(HOST_CPU_FOO)
1419 static int ipkg_print_architecture_cmd(ipkg_conf_t *conf, int argc, char **argv)
1421 nv_pair_list_elt_t *l;
1423 l = conf->arch_list.head;
1425 nv_pair_t *nv = l->data;
1426 printf("arch %s %s\n", nv->name, nv->value);