ipkg_cmd.c

来自「this is the pkg installer for linux」· C语言 代码 · 共 1,433 行 · 第 1/3 页

C
1,433
字号
	       pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,							   argv[i],							   conf->default_dest);	  } else {	       pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);	  }	  if (pkg == NULL) {	       ipkg_message(conf, IPKG_ERROR,			    "Package %s is not installed.\n", argv[i]);	       continue;	  }	  ipkg_purge_pkg(conf, pkg);     }     write_status_files_if_changed(conf);     return 0;}static int ipkg_flag_cmd(ipkg_conf_t *conf, int argc, char **argv){     int i;     pkg_t *pkg;     const char *flags = argv[0];         global_conf = conf;     signal(SIGINT, sigint_handler);     for (i=1; i < argc; i++) {	  if (conf->restrict_to_default_dest) {	       pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,							   argv[i],							   conf->default_dest);	  } else {	       pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);	  }	  if (pkg == NULL) {	       ipkg_message(conf, IPKG_ERROR,			    "Package %s is not installed.\n", argv[i]);	       continue;	  }          if (( strcmp(flags,"hold")==0)||( strcmp(flags,"noprune")==0)||              ( strcmp(flags,"user")==0)||( strcmp(flags,"ok")==0)) {	      pkg->state_flag = pkg_state_flag_from_str(flags);          }/* pb_ asked this feature 03292004 *//* Actually I will use only this two, but this is an open for various status */          if (( strcmp(flags,"installed")==0)||( strcmp(flags,"unpacked")==0)){	      pkg->state_status = pkg_state_status_from_str(flags);          }	  ipkg_state_changed++;	  ipkg_message(conf, IPKG_NOTICE,		       "Setting flags for package %s to %s\n",		       pkg->name, flags);     }     write_status_files_if_changed(conf);     return 0;}static int ipkg_files_cmd(ipkg_conf_t *conf, int argc, char **argv){     pkg_t *pkg;     str_list_t *installed_files;     str_list_elt_t *iter;     char *pkg_version;     size_t buff_len = 8192;     size_t used_len;     char *buff ;     buff = (char *)malloc(buff_len);     if ( buff == NULL ) {        fprintf( stderr,"%s: Unable to allocate memory \n",__FUNCTION__);        return ENOMEM;     }      if (argc < 1) {	  return EINVAL;     }     pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,					    argv[0]);     if (pkg == NULL) {	  ipkg_message(conf, IPKG_ERROR,		       "Package %s not installed.\n", argv[0]);	  return 0;     }     installed_files = pkg_get_installed_files(pkg);     pkg_version = pkg_version_str_alloc(pkg);#ifndef IPKG_LIB     printf("Package %s (%s) is installed on %s and has the following files:\n",	    pkg->name, pkg_version, pkg->dest->name);     for (iter = installed_files->head; iter; iter = iter->next) {	  puts(iter->data);     }#else     if (buff) {     try_again:	  used_len = snprintf(buff, buff_len, "Package %s (%s) is installed on %s and has the following files:\n",			      pkg->name, pkg_version, pkg->dest->name) + 1;	  if (used_len > buff_len) {	       buff_len *= 2;	       buff = realloc (buff, buff_len);	       goto try_again;	  }	  for (iter = installed_files->head; iter; iter = iter->next) {	       used_len += strlen (iter->data) + 1;	       while (buff_len <= used_len) {		    buff_len *= 2;		    buff = realloc (buff, buff_len);	       }	       strncat(buff, iter->data, buff_len);	       strncat(buff, "\n", buff_len);	  } 	  if (ipkg_cb_list) ipkg_cb_list(pkg->name,					 buff,					 pkg_version_str_alloc(pkg),					 pkg->state_status,					 p_userdata);	  free(buff);     }#endif     free(pkg_version);     pkg_free_installed_files(pkg);     return 0;}static int ipkg_depends_cmd(ipkg_conf_t *conf, int argc, char **argv){     if (argc > 0) {	  pkg_vec_t *available_pkgs = pkg_vec_alloc();	  const char *rel_str = "depends on";	  int i;     	  pkg_info_preinstall_check(conf);	  if (conf->query_all)	       pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);	  else	       pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);	  for (i = 0; i < argc; i++) {	       const char *target = argv[i];	       int j;	       ipkg_message(conf, IPKG_ERROR, "target=%s\n", target);	       for (j = 0; j < available_pkgs->len; j++) {		    pkg_t *pkg = available_pkgs->pkgs[j];		    if (fnmatch(target, pkg->name, 0) == 0) {			 int k;			 int count = pkg->depends_count + pkg->pre_depends_count;			 ipkg_message(conf, IPKG_ERROR, "What %s (arch=%s) %s\n",				      target, pkg->architecture, rel_str);			 for (k = 0; k < count; k++) {			      compound_depend_t *cdepend = &pkg->depends[k];			      int l;			      for (l = 0; l < cdepend->possibility_count; l++) {				   depend_t *possibility = cdepend->possibilities[l];				   ipkg_message(conf, IPKG_ERROR, "    %s", possibility->pkg->name);				   if (conf->verbosity > 0) {					// char *ver = abstract_pkg_version_str_alloc(possibility->pkg); 					ipkg_message(conf, IPKG_NOTICE, " %s", possibility->version);					if (possibility->version) {					     char *typestr = NULL;					     switch (possibility->constraint) {					     case NONE: typestr = "none"; break;					     case EARLIER: typestr = "<"; break;					     case EARLIER_EQUAL: typestr = "<="; break;					     case EQUAL: typestr = "="; break;					     case LATER_EQUAL: typestr = ">="; break;					     case LATER: typestr = ">"; break;					     }					     ipkg_message(conf, IPKG_NOTICE, " (%s %s)", typestr, possibility->version);					}					// free(ver);				   }				   ipkg_message(conf, IPKG_ERROR, "\n");			      }			 }		    }	       }	  }	  pkg_vec_free(available_pkgs);     }     return 0;}enum what_field_type {  WHATDEPENDS,  WHATCONFLICTS,  WHATPROVIDES,  WHATREPLACES,  WHATRECOMMENDS,  WHATSUGGESTS};static int ipkg_what_depends_conflicts_cmd(ipkg_conf_t *conf, enum what_field_type what_field_type, int recursive, int argc, char **argv){     if (argc > 0) {	  pkg_vec_t *available_pkgs = pkg_vec_alloc();	  const char *rel_str = NULL;	  int i;	  int changed;	  switch (what_field_type) {	  case WHATDEPENDS: rel_str = "depends on"; break;	  case WHATCONFLICTS: rel_str = "conflicts with"; break;	  case WHATSUGGESTS: rel_str = "suggests"; break;	  case WHATRECOMMENDS: rel_str = "recommends"; break;	  case WHATPROVIDES: rel_str = "provides"; break;	  case WHATREPLACES: rel_str = "replaces"; break;	  }     	  if (conf->query_all)	       pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);	  else	       pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);	  /* mark the root set */	  pkg_vec_clear_marks(available_pkgs);	  ipkg_message(conf, IPKG_NOTICE, "Root set:\n");	  for (i = 0; i < argc; i++) {	       const char *dependee_pattern = argv[i];	       pkg_vec_mark_if_matches(available_pkgs, dependee_pattern);	  }	  for (i = 0; i < available_pkgs->len; i++) {	       pkg_t *pkg = available_pkgs->pkgs[i];	       if (pkg->state_flag & SF_MARKED) {		    /* mark the parent (abstract) package */		    pkg_mark_provides(pkg);		    ipkg_message(conf, IPKG_NOTICE, "  %s\n", pkg->name);	       }	  }	  ipkg_message(conf, IPKG_NOTICE, "What %s root set\n", rel_str);	  do {	       int j;	       changed = 0;	       for (j = 0; j < available_pkgs->len; j++) {		    pkg_t *pkg = available_pkgs->pkgs[j];		    int k;		    int count = ((what_field_type == WHATCONFLICTS)				 ? pkg->conflicts_count				 : pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count);		    /* skip this package if it is already marked */		    if (pkg->parent->state_flag & SF_MARKED) {			 continue;		    }		    for (k = 0; k < count; k++) {			 compound_depend_t *cdepend = 			      (what_field_type == WHATCONFLICTS) ? &pkg->conflicts[k] : &pkg->depends[k];			 int l;			 for (l = 0; l < cdepend->possibility_count; l++) {			      depend_t *possibility = cdepend->possibilities[l];			      if (possibility->pkg->state_flag & SF_MARKED) {				   /* mark the depending package so we won't visit it again */				   pkg->state_flag |= SF_MARKED;				   pkg_mark_provides(pkg);				   changed++;				   ipkg_message(conf, IPKG_NOTICE, "    %s", pkg->name);				   if (conf->verbosity > 0) {					char *ver = pkg_version_str_alloc(pkg); 					ipkg_message(conf, IPKG_NOTICE, " %s", ver);					ipkg_message(conf, IPKG_NOTICE, "\t%s %s", rel_str, possibility->pkg->name);					if (possibility->version) {					     char *typestr = NULL;					     switch (possibility->constraint) {					     case NONE: typestr = "none"; break;					     case EARLIER: typestr = "<"; break;					     case EARLIER_EQUAL: typestr = "<="; break;					     case EQUAL: typestr = "="; break;					     case LATER_EQUAL: typestr = ">="; break;					     case LATER: typestr = ">"; break;					     }					     ipkg_message(conf, IPKG_NOTICE, " (%s %s)", typestr, possibility->version);					}					free(ver);					if (!pkg_dependence_satisfiable(conf, possibility))					     ipkg_message(conf, IPKG_NOTICE, " unsatisfiable");				   }				   ipkg_message(conf, IPKG_NOTICE, "\n");				   goto next_package;			      }			 }		    }	       next_package:		    ;	       }	  } while (changed && recursive);	  pkg_vec_free(available_pkgs);     }     return 0;}int pkg_mark_provides(pkg_t *pkg){     int provides_count = pkg->provides_count;     abstract_pkg_t **provides = pkg->provides;     int i;     pkg->parent->state_flag |= SF_MARKED;     for (i = 0; i < provides_count; i++) {	  provides[i]->state_flag |= SF_MARKED;     }     return 0;}static int ipkg_whatdepends_recursively_cmd(ipkg_conf_t *conf, int argc, char **argv){     return ipkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 1, argc, argv);}static int ipkg_whatdepends_cmd(ipkg_conf_t *conf, int argc, char **argv){     return ipkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 0, argc, argv);}static int ipkg_whatsuggests_cmd(ipkg_conf_t *conf, int argc, char **argv){     return ipkg_what_depends_conflicts_cmd(conf, WHATSUGGESTS, 0, argc, argv);}static int ipkg_whatrecommends_cmd(ipkg_conf_t *conf, int argc, char **argv){     return ipkg_what_depends_conflicts_cmd(conf, WHATRECOMMENDS, 0, argc, argv);}static int ipkg_whatconflicts_cmd(ipkg_conf_t *conf, int argc, char **argv){     return ipkg_what_depends_conflicts_cmd(conf, WHATCONFLICTS, 0, argc, argv);}static int ipkg_what_provides_replaces_cmd(ipkg_conf_t *conf, enum what_field_type what_field_type, int argc, char **argv){     if (argc > 0) {	  pkg_vec_t *available_pkgs = pkg_vec_alloc();	  const char *rel_str = (what_field_type == WHATPROVIDES ? "provides" : "replaces");	  int i;     	  pkg_info_preinstall_check(conf);	  if (conf->query_all)	       pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);	  else	       pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);	  for (i = 0; i < argc; i++) {	       const char *target = argv[i];	       int j;	       ipkg_message(conf, IPKG_ERROR, "What %s %s\n",			    rel_str, target);	       for (j = 0; j < available_pkgs->len; j++) {		    pkg_t *pkg = available_pkgs->pkgs[j];		    int k;		    int count = (what_field_type == WHATPROVIDES) ? pkg->provides_count : pkg->replaces_count;		    for (k = 0; k < count; k++) {			 abstract_pkg_t *apkg = 			      ((what_field_type == WHATPROVIDES) 			       ? pkg->provides[k]			       : pkg->replaces[k]);			 if (fnmatch(target, apkg->name, 0) == 0) {			      ipkg_message(conf, IPKG_ERROR, "    %s", pkg->name);			      if (strcmp(target, apkg->name) != 0)				   ipkg_message(conf, IPKG_ERROR, "\t%s %s\n", rel_str, apkg->name);			      ipkg_message(conf, IPKG_ERROR, "\n");			 }		    }	       }	  }	  pkg_vec_free(available_pkgs);     }     return 0;}static int ipkg_whatprovides_cmd(ipkg_conf_t *conf, int argc, char **argv){     return ipkg_what_provides_replaces_cmd(conf, WHATPROVIDES, argc, argv);}static int ipkg_whatreplaces_cmd(ipkg_conf_t *conf, int argc, char **argv){     return ipkg_what_provides_replaces_cmd(conf, WHATREPLACES, argc, argv);}static int ipkg_search_cmd(ipkg_conf_t *conf, int argc, char **argv){     int i;     pkg_vec_t *installed;     pkg_t *pkg;     str_list_t *installed_files;     str_list_elt_t *iter;     char *installed_file;     if (argc < 1) {	  return EINVAL;     }      installed = pkg_vec_alloc();     pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);     for (i=0; i < installed->len; i++) {	  pkg = installed->pkgs[i];	  installed_files = pkg_get_installed_files(pkg);	  for (iter = installed_files->head; iter; iter = iter->next) {	       installed_file = iter->data;	       if (fnmatch(argv[0], installed_file, 0)==0)  {#ifndef IPKG_LIB		    printf("%s: %s\n", pkg->name, installed_file);#else			if (ipkg_cb_list) ipkg_cb_list(pkg->name, 						       installed_file, 			                               pkg_version_str_alloc(pkg), 			                               pkg->state_status, p_userdata);#endif			   	       }			  }	  pkg_free_installed_files(pkg);     }     /* XXX: CLEANUP: It's not obvious from the name of	pkg_hash_fetch_all_installed that we need to call	pkg_vec_free to avoid a memory leak. */     pkg_vec_free(installed);     return 0;}static int ipkg_compare_versions_cmd(ipkg_conf_t *conf, int argc, char **argv){     if (argc == 3) {	  /* this is a bit gross */	  struct pkg p1, p2;	  parseVersion(&p1, argv[0]); 	  parseVersion(&p2, argv[2]); 	  return pkg_version_satisfied(&p1, &p2, argv[1]);     } else {	  ipkg_message(conf, IPKG_ERROR,		       "ipkg compare_versions <v1> <op> <v2>\n"		       "<op> is one of <= >= << >> =\n");	  return -1;     }}#ifndef HOST_CPU_STR#define HOST_CPU_STR__(X) #X#define HOST_CPU_STR_(X) HOST_CPU_STR__(X)#define HOST_CPU_STR HOST_CPU_STR_(HOST_CPU_FOO)#endifstatic int ipkg_print_architecture_cmd(ipkg_conf_t *conf, int argc, char **argv){     nv_pair_list_elt_t *l;     l = conf->arch_list.head;     while (l) {	  nv_pair_t *nv = l->data;	  printf("arch %s %s\n", nv->name, nv->value);	  l = l->next;     }     return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?