Example #1
0
File: hfile.c Project: pipul/lab
hfile_t *hfile_load(const int8_t *filename)
{
	int32_t fd;
	hfile_t *fp;
	meta_t *o;
	htail_t *ht;
	hinfo_t *hi;
	
	if (!filename)
		return(NULL);
	if ((fp = hfile_new()) == NULL)
		return(NULL);
	memcpy(fp->name,filename,strlen(filename));
	if ((fd = open(fp->name,O_RDONLY)) < 0)
		__ERROR_LOG(H_ERROR);

	fp->trailer = htail_load(fd,-sizeof(htail_t),sizeof(htail_t));
	if (fp->trailer == NULL)
		__ERROR_LOG(H_ERROR);
	else
		ht = fp->trailer;

	fp->fileinfo = hinfo_load(fd,ht->infooffset,ht->infosize);
	if (fp->fileinfo == NULL)
		__ERROR_LOG(H_ERROR);
	else
		hi = fp->fileinfo;

	fp->metas = index_load(fd,ht->indexoffset,ht->indexsize);
	if (fp->metas == NULL)
		__ERROR_LOG(H_ERROR);
	
	fp->bloom = bloom_load(fd,ht->bloomoffset,ht->bloomsize);
	if (fp->bloom == NULL)
		__ERROR_LOG(H_ERROR);

	fp->blocks = malloc(hi->blockcount*sizeof(block_t *));
	if (fp->blocks == NULL)
		__ERROR_LOG(H_ERROR);
	memset(fp->blocks,0,hi->blockcount*sizeof(block_t *));

	close(fd);
	return(fp);

H_ERROR:
	close(fd);
	hfile_destroy(fp);
	return(NULL);
}
Example #2
0
BLOOM *bloom_create_from_file(uint32_t size, double error_rate)
{
	BLOOM *bloom = bloom_load(size, error_rate); 
	return bloom;
}
Example #3
0
int main (int argc, char *argv[]) {
	int count;
	unsigned long maxitems=0;
	int c;
	int index;
	FILE *fp;
	unsigned long items;
	char line[MAX_LINE_SIZE];
	char pline[MAX_LINE_SIZE];
	char unhex[MAX_LINE_SIZE];
	char *toprocess;
	int size;
	int found=0;	

	/* safe defaults */
	opt_errorrate=0.01;
	opt_bloomfile=NULL;

	/* load config */
	loadconfig();

  while ((c = getopt (argc, argv, "huicp:svde:b:f:")) != -1)
    switch (c)
      {
      case 'h':
	displayhelp();
	exit(0);
	break;
      case 'u':
	opt_unhex = 1;
	break;
      case 'i':
	opt_ignorecase = 1;
	break;
      case 'c':
        opt_init = 1;
        break;
      case 'p':
	opt_progressitems = atoi(optarg);
	break;
      case 'e':
	opt_errorrate = atof(optarg);
	break;
      case 'b':
        opt_bloomfile = optarg;
        break;
      case 'f':
	opt_readfromfile = optarg;
	break;
      case 's':
        opt_search = 1;
        break;
		case 'v':
		opt_verbose++;
		break;

		case 'd':
		opt_debug++;
		break;
      case '?':
        if (optopt == 'b')
          fprintf (stderr, "Option -%c requires an argument.\n", optopt);
        else if (isprint (optopt))
          fprintf (stderr, "Unknown option `-%c'.\n", optopt);
        else
          fprintf (stderr,
                   "Unknown option character `\\x%x'.\n",
                   optopt);
        return 1;
      default:
        abort ();
      }

	if (opt_debug) {
	  printf ("opt_init = %d, opt_search = %d, opt_bloomfile = %s\n",
		  opt_init, opt_search, opt_bloomfile);

	for (count = 1; count < argc; count++) {
		printf("argv[%d] = %s\n", count, argv[count]);
	}

		for (index = optind; index < argc; index++)
		  printf ("Non-option argument %s\n", argv[index]);
	}

	if (opt_init) {
		for (index = optind; index < argc; index++) {
			if (opt_verbose) fprintf(stderr,"[i] Counting lines for %s\n", argv[index]);
			fp=fopen(argv[index],"r");
			if (fp==NULL) {
				fprintf(stderr,"Error opening %s\n",argv[index]);
				break;	
			}
			items=getlinecount(fp);
			if (opt_verbose) fprintf(stderr,"[i] %s have %lu lines/items\n",argv[index],items);
			maxitems=maxitems+items;
			fclose(fp);
		}
		if (opt_verbose) fprintf(stderr,"[i] Maximum number of items: %lu\n",maxitems);
		bloom_init(&bloom, maxitems, opt_errorrate);

		items=0;
		for (index = optind; index < argc; index++) {
			if (opt_verbose) fprintf(stderr,"[i] Processing %s\n", argv[index]);
			fp=fopen(argv[index],"r");
			if (fp==NULL) {
				fprintf(stderr,"Error opening %s\n",argv[index]);
				break;	
			}
			/* read line by line */
			while (fgets (line, sizeof(line), fp)) {
				toprocess=line;
				size=strlen(line);
				if (line[size-1]=='\n') line[--size]='\0';
				if (line[size-1]=='\r') line[--size]='\0';
				if (opt_debug) fprintf(stderr,"Line (%d): %s \n",size,line);
				if (opt_verbose && (items++ % opt_progressitems==0)) fprintf(stderr,"\r[i] Line %lu of %lu", items, maxitems);

				if (opt_ignorecase) {
					toprocess=str2upper(toprocess,pline);
				}
				if (opt_unhex) {
					size=hexstr2char(toprocess,unhex,MAX_LINE_SIZE);
					toprocess=unhex;
				} 
				bloom_add(&bloom, toprocess, size);
			}
			if (opt_verbose) fprintf(stderr,"\n[i] Done for %s!\n",argv[index]);
			fclose(fp);
		}

		if (opt_bloomfile==NULL) {
			fprintf(stderr,"No bloom file specified for init. Not saving.\n");
		} else {
			if (opt_verbose) fprintf(stderr,"[i] Saving to %s\n",opt_bloomfile);
			bloom_save(&bloom,opt_bloomfile);
			/* if (opt_verbose) bloom_print(&bloom); */
		}
	}

	if (opt_search || (!opt_init)) {
		if (opt_bloomfile==NULL) {
			fprintf(stderr,"No bloom file specified.\n");
		} else {
			if (opt_verbose) fprintf(stderr,"[i] Opening bloom file: %s\n", opt_bloomfile);
			if (bloom_load(&bloom, opt_bloomfile)) {
				fprintf(stderr,"[i] Error loading bloom file: %s\n", opt_bloomfile);
				return (1);
			}
		}

		if (opt_verbose) fprintf(stderr,"[i] Searching patterns\n");

		for (index = optind; index < argc; index++) {
			toprocess=argv[index];
			if (opt_verbose) fprintf(stderr,"[i] Processing %s\n", toprocess);
			if (searchpattern(toprocess)) {
				fprintf(stdout,"%s found\n", argv[index]);
			} else {
				fprintf(stdout,"%s not found\n", argv[index]);
			}
		}

		if (opt_readfromfile!=NULL) {
			if (opt_verbose) fprintf(stderr,"[v] Reading from file %s\n",opt_readfromfile);
			if (strcmp(opt_readfromfile,"-")==0) {
				fprintf (stderr,"[i] Reading from standard input. Specify pattern separated by new line.\n");
				fp=stdin;
			} else {
				fp=fopen(opt_readfromfile,"r");
			}
			if (fp==NULL) {
				fprintf(stderr,"[!] Error opening file: %s\n",opt_readfromfile);
				exit(1);
			}
			while (fgets (line, sizeof(line), fp)) {
				toprocess=line;
				size=strlen(line);
				if (line[size-1]=='\n') line[--size]='\0';
				if (line[size-1]=='\r') line[--size]='\0';
				if (opt_debug) fprintf(stderr,"[d] Line in pattern (%d): %s \n",size,line);
				if (opt_verbose) fprintf(stderr,"[v] Processing from file %s\n", toprocess);
				if (searchpattern(toprocess)) {
					fprintf(stdout,"%s found\n", toprocess);
				} else {
					fprintf(stdout,"%s not found\n", toprocess);
				}
			}
			if (fp!=stdin) fclose (fp);
		}
	}
}