ipkg_cmd.c
来自「this is the pkg installer for linux」· C语言 代码 · 共 1,433 行 · 第 1/3 页
C
1,433 行
* Now scan through package names and install */ for (i=0; i < argc; i++) { arg = argv[i]; ipkg_message(conf, IPKG_DEBUG2, "Debug install_cmd: %s \n",arg ); err = ipkg_prepare_url_for_install(conf, arg, &argv[i]); if (err != EINVAL && err != 0) return err; } pkg_info_preinstall_check(conf); for (i=0; i < argc; i++) { arg = argv[i]; if (conf->multiple_providers) err = ipkg_install_multi_by_name(conf, arg); else{ err = ipkg_install_by_name(conf, arg); } if (err == IPKG_PKG_HAS_NO_CANDIDATE) { ipkg_message(conf, IPKG_ERROR, "Cannot find package %s.\n" "Check the spelling or perhaps run 'ipkg update'\n", arg); } } /* recheck to verify that all dependences are satisfied */ if (0) ipkg_satisfy_all_dependences(conf); ipkg_configure_packages(conf, NULL); write_status_files_if_changed(conf); return err;}static int ipkg_upgrade_cmd(ipkg_conf_t *conf, int argc, char **argv){ int i; pkg_t *pkg; int err; global_conf = conf; signal(SIGINT, sigint_handler); if (argc) { for (i=0; i < argc; i++) { char *arg = argv[i]; err = ipkg_prepare_url_for_install(conf, arg, &arg); if (err != EINVAL && err != 0) return err; } pkg_info_preinstall_check(conf); for (i=0; i < argc; i++) { char *arg = argv[i]; if (conf->restrict_to_default_dest) { pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash, argv[i], conf->default_dest); if (pkg == NULL) { ipkg_message(conf, IPKG_NOTICE, "Package %s not installed in %s\n", argv[i], conf->default_dest->name); continue; } } else { pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]); } if (pkg) ipkg_upgrade_pkg(conf, pkg); else { ipkg_install_by_name(conf, arg); } } } else { pkg_vec_t *installed = pkg_vec_alloc(); pkg_info_preinstall_check(conf); pkg_hash_fetch_all_installed(&conf->pkg_hash, installed); for (i = 0; i < installed->len; i++) { pkg = installed->pkgs[i]; ipkg_upgrade_pkg(conf, pkg); } pkg_vec_free(installed); } /* recheck to verify that all dependences are satisfied */ if (0) ipkg_satisfy_all_dependences(conf); ipkg_configure_packages(conf, NULL); write_status_files_if_changed(conf); return 0;}static int ipkg_download_cmd(ipkg_conf_t *conf, int argc, char **argv){ int i, err; char *arg; pkg_t *pkg; pkg_info_preinstall_check(conf); for (i = 0; i < argc; i++) { arg = argv[i]; pkg = pkg_hash_fetch_best_installation_candidate_by_name(conf, arg); if (pkg == NULL) { ipkg_message(conf, IPKG_ERROR, "Cannot find package %s.\n" "Check the spelling or perhaps run 'ipkg update'\n", arg); continue; } err = ipkg_download_pkg(conf, pkg, "."); if (err) { ipkg_message(conf, IPKG_ERROR, "Failed to download %s\n", pkg->name); } else { ipkg_message(conf, IPKG_NOTICE, "Downloaded %s as %s\n", pkg->name, pkg->local_filename); } } return 0;}static int ipkg_list_cmd(ipkg_conf_t *conf, int argc, char **argv){ int i ; pkg_vec_t *available; pkg_t *pkg; char desc_short[IPKG_LIST_DESCRIPTION_LENGTH]; char *newline; char *pkg_name = NULL; char *version_str; if (argc > 0) { pkg_name = argv[0]; } available = pkg_vec_alloc(); pkg_hash_fetch_available(&conf->pkg_hash, available); for (i=0; i < available->len; i++) { pkg = available->pkgs[i]; /* if we have package name or pattern and pkg does not match, then skip it */ if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) continue; if (pkg->description) { strncpy(desc_short, pkg->description, IPKG_LIST_DESCRIPTION_LENGTH); } else { desc_short[0] = '\0'; } desc_short[IPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0'; newline = strchr(desc_short, '\n'); if (newline) { *newline = '\0'; }#ifndef IPKG_LIB printf("%s - %s\n", pkg->name, desc_short);#else if (ipkg_cb_list) { version_str = pkg_version_str_alloc(pkg); ipkg_cb_list(pkg->name,desc_short, version_str, pkg->state_status, p_userdata); free(version_str); }#endif } pkg_vec_free(available); return 0;}static int ipkg_list_installed_cmd(ipkg_conf_t *conf, int argc, char **argv){ int i ; pkg_vec_t *available; pkg_t *pkg; char desc_short[IPKG_LIST_DESCRIPTION_LENGTH]; char *newline; char *pkg_name = NULL; char *version_str; if (argc > 0) { pkg_name = argv[0]; } available = pkg_vec_alloc(); pkg_hash_fetch_all_installed(&conf->pkg_hash, available); for (i=0; i < available->len; i++) { pkg = available->pkgs[i]; /* if we have package name or pattern and pkg does not match, then skip it */ if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) continue; if (pkg->description) { strncpy(desc_short, pkg->description, IPKG_LIST_DESCRIPTION_LENGTH); } else { desc_short[0] = '\0'; } desc_short[IPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0'; newline = strchr(desc_short, '\n'); if (newline) { *newline = '\0'; }#ifndef IPKG_LIB printf("%s - %s\n", pkg->name, desc_short);#else if (ipkg_cb_list) { version_str = pkg_version_str_alloc(pkg); ipkg_cb_list(pkg->name,desc_short, version_str, pkg->state_status, p_userdata); free(version_str); }#endif } return 0;}static int ipkg_info_status_cmd(ipkg_conf_t *conf, int argc, char **argv, int installed_only){ int i; pkg_vec_t *available; pkg_t *pkg; char *pkg_name = NULL; char **pkg_fields = NULL; int n_fields = 0; char *buff ; // = (char *)malloc(1); if (argc > 0) { pkg_name = argv[0]; } if (argc > 1) { pkg_fields = &argv[1]; n_fields = argc - 1; } available = pkg_vec_alloc(); if (installed_only) pkg_hash_fetch_all_installed(&conf->pkg_hash, available); else pkg_hash_fetch_available(&conf->pkg_hash, available); for (i=0; i < available->len; i++) { pkg = available->pkgs[i]; if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) { continue; }#ifndef IPKG_LIB if (n_fields) { for (j = 0; j < n_fields; j++) pkg_print_field(pkg, stdout, pkg_fields[j]); } else { pkg_print_info(pkg, stdout); }#else buff = pkg_formatted_info(pkg); if ( buff ) { if (ipkg_cb_status) ipkg_cb_status(pkg->name, pkg->state_status, buff, p_userdata);/* We should not forget that actually the pointer is allocated. We need to free it :) ( Thanks florian for seeing the error )*/ free(buff); }#endif if (conf->verbosity > 1) { conffile_list_elt_t *iter; for (iter = pkg->conffiles.head; iter; iter = iter->next) { conffile_t *cf = iter->data; int modified = conffile_has_been_modified(conf, cf); ipkg_message(conf, IPKG_NOTICE, "conffile=%s md5sum=%s modified=%d\n", cf->name, cf->value, modified); } } }#ifndef IPKG_LIB if (buff) free(buff);#endif pkg_vec_free(available); return 0;}static int ipkg_info_cmd(ipkg_conf_t *conf, int argc, char **argv){ return ipkg_info_status_cmd(conf, argc, argv, 0);}static int ipkg_status_cmd(ipkg_conf_t *conf, int argc, char **argv){ return ipkg_info_status_cmd(conf, argc, argv, 1);}static int ipkg_configure_cmd(ipkg_conf_t *conf, int argc, char **argv){ int err; if (argc > 0) { char *pkg_name = NULL; pkg_name = argv[0]; err = ipkg_configure_packages (conf, pkg_name); } else { err = ipkg_configure_packages (conf, NULL); } write_status_files_if_changed(conf); return err;}static int ipkg_install_pending_cmd(ipkg_conf_t *conf, int argc, char **argv){ int i, err; char *globpattern; glob_t globbuf; sprintf_alloc(&globpattern, "%s/*" IPKG_PKG_EXTENSION, conf->pending_dir); err = glob(globpattern, 0, NULL, &globbuf); free(globpattern); if (err) { return 0; } ipkg_message(conf, IPKG_NOTICE, "The following packages in %s will now be installed.\n", conf->pending_dir); for (i = 0; i < globbuf.gl_pathc; i++) { ipkg_message(conf, IPKG_NOTICE, "%s%s", i == 0 ? "" : " ", globbuf.gl_pathv[i]); } ipkg_message(conf, IPKG_NOTICE, "\n"); for (i = 0; i < globbuf.gl_pathc; i++) { err = ipkg_install_from_file(conf, globbuf.gl_pathv[i]); if (err == 0) { err = unlink(globbuf.gl_pathv[i]); if (err) { ipkg_message(conf, IPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n", __FUNCTION__, globbuf.gl_pathv[i], strerror(err)); return err; } } } globfree(&globbuf); return err;}static int ipkg_remove_cmd(ipkg_conf_t *conf, int argc, char **argv){ int i,a,done; pkg_t *pkg; pkg_t *pkg_to_remove; pkg_vec_t *available; char *pkg_name = NULL; global_conf = conf; signal(SIGINT, sigint_handler);// ENH: Add the "no pkg removed" just in case. done = 0; available = pkg_vec_alloc(); pkg_info_preinstall_check(conf); if ( argc > 0 ) { pkg_hash_fetch_all_installed(&conf->pkg_hash, available); for (i=0; i < argc; i++) { pkg_name = malloc(strlen(argv[i])+2); strcpy(pkg_name,argv[i]); for (a=0; a < available->len; a++) { pkg = available->pkgs[a]; if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) { continue; } if (conf->restrict_to_default_dest) { pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash, pkg->name, conf->default_dest); } else { pkg_to_remove = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name ); } if (pkg == NULL) { ipkg_message(conf, IPKG_ERROR, "Package %s is not installed.\n", pkg->name); continue; } if (pkg->state_status == SS_NOT_INSTALLED) { // Added the control, so every already removed package could be skipped ipkg_message(conf, IPKG_ERROR, "Package seems to be %s not installed (STATUS = NOT_INSTALLED).\n", pkg->name); continue; } ipkg_remove_pkg(conf, pkg_to_remove,0); done = 1; } free (pkg_name); } pkg_vec_free(available); } else { pkg_vec_t *installed_pkgs = pkg_vec_alloc(); int i; int flagged_pkg_count = 0; int removed; pkg_hash_fetch_all_installed(&conf->pkg_hash, installed_pkgs); for (i = 0; i < installed_pkgs->len; i++) { pkg_t *pkg = installed_pkgs->pkgs[i]; if (pkg->state_flag & SF_USER) { flagged_pkg_count++; } else { if (!pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL)) ipkg_message(conf, IPKG_NOTICE, "Non-user leaf package: %s\n", pkg->name); } } if (!flagged_pkg_count) { ipkg_message(conf, IPKG_NOTICE, "No packages flagged as installed by user, \n" "so refusing to uninstall unflagged non-leaf packages\n"); return 0; } /* find packages not flagged SF_USER (i.e., installed to * satisfy a dependence) and not having any dependents, and * remove them */ do { removed = 0; for (i = 0; i < installed_pkgs->len; i++) { pkg_t *pkg = installed_pkgs->pkgs[i]; if (!(pkg->state_flag & SF_USER) && !pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL)) { removed++; ipkg_message(conf, IPKG_NOTICE, "Removing non-user leaf package %s\n"); ipkg_remove_pkg(conf, pkg,0); done = 1; } } } while (removed); pkg_vec_free(installed_pkgs); } if ( done == 0 ) ipkg_message(conf, IPKG_NOTICE, "No packages removed.\n"); write_status_files_if_changed(conf); return 0;}static int ipkg_purge_cmd(ipkg_conf_t *conf, int argc, char **argv){ int i; pkg_t *pkg; global_conf = conf; signal(SIGINT, sigint_handler); pkg_info_preinstall_check(conf); for (i=0; i < argc; i++) { if (conf->restrict_to_default_dest) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?