ipkg_conf.c

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

C
718
字号
		    hash_entry_t *e = &hash->entries[j];		    if (e->next)			 n_conflicts++;		    while (e && e->key) {			 len++;			 e = e->next;		    }		    if (len > c) 			 c = len;	       }	       ipkg_message(conf, IPKG_DEBUG, "hash_table[%s] n_buckets=%d n_elements=%d max_conflicts=%d n_conflicts=%d\n", 			    hash->name, hash->n_entries, hash->n_elements, c, n_conflicts);	       hash_table_deinit(hash);	  }     }}static int ipkg_conf_set_default_dest(ipkg_conf_t *conf,				      const char *default_dest_name){     pkg_dest_list_elt_t *iter;     pkg_dest_t *dest;     for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {	  dest = iter->data;	  if (strcmp(dest->name, default_dest_name) == 0) {	       conf->default_dest = dest;	       conf->restrict_to_default_dest = 1;	       return 0;	  }     }     fprintf(stderr, "ERROR: Unknown dest name: `%s'\n", default_dest_name);     return 1;}static int set_and_load_pkg_src_list(ipkg_conf_t *conf, pkg_src_list_t *pkg_src_list){     pkg_src_list_elt_t *iter;     pkg_src_t *src;     char *list_file;     for (iter = pkg_src_list->head; iter; iter = iter->next) {          src = iter->data;	  if (src == NULL) {	       continue;	  }	  sprintf_alloc(&list_file, "%s/%s", 			  conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir, 			  src->name);	  if (file_exists(list_file)) {	       pkg_hash_add_from_file(conf, list_file, src, NULL, 0);	  }	  free(list_file);     }     return 0;}static int set_and_load_pkg_dest_list(ipkg_conf_t *conf, nv_pair_list_t *nv_pair_list, char *lists_dir ){     nv_pair_list_elt_t *iter;     nv_pair_t *nv_pair;     pkg_dest_t *dest;     char *root_dir;     for (iter = nv_pair_list->head; iter; iter = iter->next) {	  nv_pair = iter->data;	  if (conf->offline_root) {	       sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value);	  } else {	       root_dir = strdup(nv_pair->value);	  }	  dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, lists_dir);	  free(root_dir);	  if (dest == NULL) {	       continue;	  }	  if (conf->default_dest == NULL) {	       conf->default_dest = dest;	  }	  if (file_exists(dest->status_file_name)) {	       pkg_hash_add_from_file(conf, dest->status_file_name,				      NULL, dest, 1);	  }     }     return 0;}static int ipkg_conf_parse_file(ipkg_conf_t *conf, const char *filename,				pkg_src_list_t *pkg_src_list,				nv_pair_list_t *tmp_dest_nv_pair_list,				char **lists_dir){     int err;     ipkg_option_t * options;     FILE *file = fopen(filename, "r");     regex_t valid_line_re, comment_re;#define regmatch_size 12     regmatch_t regmatch[regmatch_size];     if (ipkg_init_options_array(conf, &options)<0)        return ENOMEM;     if (file == NULL) {	  fprintf(stderr, "%s: failed to open %s: %s\n",		  __FUNCTION__, filename, strerror(errno));	  free(options);	  return errno;     }     ipkg_message(conf, IPKG_NOTICE, "loading conf file %s\n", filename);     err = xregcomp(&comment_re, 		    "^[[:space:]]*(#.*|[[:space:]]*)$",		    REG_EXTENDED);     if (err) {	  free(options);	  return err;     }     err = xregcomp(&valid_line_re, "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))([[:space:]]+([^[:space:]]+))?[[:space:]]*$", REG_EXTENDED);     if (err) {	  free(options);	  return err;     }     while(1) {	  int line_num = 0;	  char *line;	  char *type, *name, *value, *extra;	  line = file_read_line_alloc(file);	  line_num++;	  if (line == NULL) {	       break;	  }	  str_chomp(line);	  if (regexec(&comment_re, line, 0, 0, 0) == 0) {	       goto NEXT_LINE;	  }	  if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) {	       str_chomp(line);	       fprintf(stderr, "%s:%d: Ignoring invalid line: `%s'\n",		       filename, line_num, line);	       goto NEXT_LINE;	  }	  /* This has to be so ugly to deal with optional quotation marks */	  if (regmatch[2].rm_so > 0) {	       type = strndup(line + regmatch[2].rm_so,			      regmatch[2].rm_eo - regmatch[2].rm_so);	  } else {	       type = strndup(line + regmatch[3].rm_so,			      regmatch[3].rm_eo - regmatch[3].rm_so);	  }	  if (regmatch[5].rm_so > 0) {	       name = strndup(line + regmatch[5].rm_so,			      regmatch[5].rm_eo - regmatch[5].rm_so);	  } else {	       name = strndup(line + regmatch[6].rm_so,			      regmatch[6].rm_eo - regmatch[6].rm_so);	  }	  if (regmatch[8].rm_so > 0) {	       value = strndup(line + regmatch[8].rm_so,			       regmatch[8].rm_eo - regmatch[8].rm_so);	  } else {	       value = strndup(line + regmatch[9].rm_so,			       regmatch[9].rm_eo - regmatch[9].rm_so);	  }	  extra = NULL;	  if (regmatch[11].rm_so > 0) {	       extra = strndup (line + regmatch[11].rm_so,				regmatch[11].rm_eo - regmatch[11].rm_so);	  }	  /* We use the tmp_dest_nv_pair_list below instead of	     conf->pkg_dest_list because we might encounter an	     offline_root option later and that would invalidate the	     directories we would have computed in	     pkg_dest_list_init. (We do a similar thing with	     tmp_src_nv_pair_list for sake of symmetry.) */	  if (strcmp(type, "option") == 0) {	       ipkg_conf_set_option(options, name, value);	  } else if (strcmp(type, "src") == 0) {	       if (!nv_pair_list_find(pkg_src_list, name)) {		    pkg_src_list_append (pkg_src_list, name, value, extra, 0);	       } else {		    ipkg_message(conf, IPKG_ERROR, "ERROR: duplicate src declaration.  Skipping:\n\t src %s %s\n",				 name, value);	       }	  } else if (strcmp(type, "src/gz") == 0) {	       if (!nv_pair_list_find(pkg_src_list, name)) {		    pkg_src_list_append (pkg_src_list, name, value, extra, 1);	       } else {		    ipkg_message(conf, IPKG_ERROR, "ERROR: duplicate src declaration.  Skipping:\n\t src %s %s\n",				 name, value);	       }	  } else if (strcmp(type, "dest") == 0) {	       nv_pair_list_append(tmp_dest_nv_pair_list, name, value);	  } else if (strcmp(type, "lists_dir") == 0) {	       *lists_dir = realloc(*lists_dir,strlen(value)+1);               if (*lists_dir == NULL) {		    ipkg_message(conf, IPKG_ERROR, "ERROR: Not enough memory\n");	            free(options);	            return EINVAL;               }               sprintf (*lists_dir,"%s",value);	  } else if (strcmp(type, "arch") == 0) {	       ipkg_message(conf, IPKG_INFO, "supported arch %s priority (%s)\n", name, value);	       if (!value) {		    ipkg_message(conf, IPKG_NOTICE, "defaulting architecture %s priority to 10\n", name);		    value = strdup("10");	       }	       nv_pair_list_append(&conf->arch_list, strdup(name), strdup(value));	  } else {	       fprintf(stderr, "WARNING: Ignoring unknown configuration "		       "parameter: %s %s %s\n", type, name, value);	       free(options);	       return EINVAL;	  }	  free(type);	  free(name);	  free(value);	  if (extra)	       free (extra);     NEXT_LINE:	  free(line);     }     free(options);     regfree(&comment_re);     regfree(&valid_line_re);     fclose(file);     return 0;}static int ipkg_conf_set_option(const ipkg_option_t *options,				const char *name, const char *value){     int i = 0;     while (options[i].name) {	  if (strcmp(options[i].name, name) == 0) {	       switch (options[i].type) {	       case IPKG_OPT_TYPE_BOOL:		    *((int *)options[i].value) = 1;		    return 0;	       case IPKG_OPT_TYPE_INT:		    if (value) {			 *((int *)options[i].value) = atoi(value);			 return 0;		    } else {			 printf("%s: Option %s need an argument\n",				__FUNCTION__, name);			 return EINVAL;		    }		    	       case IPKG_OPT_TYPE_STRING:		    if (value) {			 *((char **)options[i].value) = strdup(value);			 return 0;		    } else {			 printf("%s: Option %s need an argument\n",				__FUNCTION__, name);			 return EINVAL;		    }	       }	  }	  i++;     }         fprintf(stderr, "%s: Unrecognized option: %s=%s\n",	     __FUNCTION__, name, value);     return EINVAL;}int ipkg_conf_write_status_files(ipkg_conf_t *conf){     pkg_dest_list_elt_t *iter;     pkg_dest_t *dest;     pkg_vec_t *all;     pkg_t *pkg;     register int i;     int err;     if (conf->noaction)	  return 0;     for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {	  dest = iter->data;	  dest->status_file = fopen(dest->status_file_tmp_name, "w");	  if (dest->status_file == NULL) {	       fprintf(stderr, "%s: Can't open status file: %s for writing: %s\n",		       __FUNCTION__, dest->status_file_name, strerror(errno));	  }     }     all = pkg_vec_alloc();     pkg_hash_fetch_available(&conf->pkg_hash, all);     for(i = 0; i < all->len; i++) {	  pkg = all->pkgs[i];	  /* We don't need most uninstalled packages in the status file */	  if (pkg->state_status == SS_NOT_INSTALLED	      && (pkg->state_want == SW_UNKNOWN		  || pkg->state_want == SW_DEINSTALL		  || pkg->state_want == SW_PURGE)) {	       continue;	  }	  if (!pkg) {	    fprintf(stderr, "Null package\n");	  }	  if (pkg->dest == NULL) {	       fprintf(stderr, "%s: ERROR: Can't write status for "		       "package %s since it has a NULL dest\n",		       __FUNCTION__, pkg->name);	       continue;	  }	  if (pkg->dest->status_file) {	       pkg_print_status(pkg, pkg->dest->status_file);	  }     }     pkg_vec_free(all);     for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {	  dest = iter->data;	  if (dest->status_file) {	       err = ferror(dest->status_file);	       fclose(dest->status_file);	       dest->status_file = NULL;	       if (!err) {		    file_move(dest->status_file_tmp_name, dest->status_file_name);	       } else {		    fprintf(stderr, "%s: ERROR: An error has occurred writing %s, "			    "retaining old %s\n", __FUNCTION__, 			    dest->status_file_tmp_name, dest->status_file_name);	       }	  }     }     return 0;}char *root_filename_alloc(ipkg_conf_t *conf, char *filename){     char *root_filename;     sprintf_alloc(&root_filename, "%s%s", (conf->offline_root ? conf->offline_root : ""), filename);     return root_filename;}

⌨️ 快捷键说明

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