Example #1
0
static IndexWriter *create_iw(Store *store)
{
    FieldInfos *fis = create_fis();
    index_create(store, fis);
    fis_deref(fis);
    return iw_open(store, standard_analyzer_new(true), &default_config);
}
Example #2
0
File: ind.c Project: dustin/ferret
INLINE void ensure_writer_open(Index *self)
{
    if (!self->iw) {
        INDEX_CLOSE_READER(self);

        /* make sure the analzyer isn't deleted by the IndexWriter */
        REF(self->analyzer);
        self->iw = iw_open(self->store, self->analyzer, false);
        self->iw->config.use_compound_file = self->config.use_compound_file;
    }
}
Example #3
0
static void sort_multi_test_setup(Store *store1, Store *store2)
{
    int i;
    FieldInfos *fis = fis_new(STORE_YES, INDEX_YES, TERM_VECTOR_YES);
    IndexWriter *iw;
    SortTestData data[] = {            /* len mod */
        {"findall","a","6","0.01"},    /*  4   0  */
        {"findall","c","5","0.1"},     /*  3   3  */
        {"findall","e","2","0.001"},   /*  5   1  */
        {"findall","g","1","1.0"},     /*  3   3  */
        {"findall","i","3","0.0001"},  /*  6   2  */
        {"findall","", "4","10.0"},    /*  4   0  */
        {"findall","h","5","0.00001"}, /*  7   3  */
        {"findall","f","2","100.0"},   /*  5   1  */
        {"findall","d","3","1000.0"},  /*  6   2  */
        {"findall","b","4","0.000001"} /*  8   0  */
    };

    index_create(store1, fis);
    index_create(store2, fis);
    fis_deref(fis);

    iw = iw_open(store1, whitespace_analyzer_new(false), NULL);

    for (i = 0; i < NELEMS(data)/2; i++) {
        add_sort_test_data(&data[i], iw);
    }
    iw_close(iw);

    iw = iw_open(store2, whitespace_analyzer_new(false), NULL);

    for (i = NELEMS(data)/2; i < NELEMS(data); i++) {
        add_sort_test_data(&data[i], iw);
    }
    iw_close(iw);
}
Example #4
0
static void sort_test_setup(Store *store)
{
    int i;
    IndexWriter *iw;
    FieldInfos *fis = fis_new(STORE_YES, INDEX_YES, TERM_VECTOR_YES);
    index_create(store, fis);
    fis_deref(fis);

    iw = iw_open(store, whitespace_analyzer_new(false), NULL);

    for (i = 0; i < NELEMS(data); i++) {
        add_sort_test_data(&data[i], iw);
    }
    iw_close(iw);
}
Example #5
0
void prepare_filter_index(Store *store)
{
    int i;
    IndexWriter *iw;
    FieldInfos *fis = fis_new(STORE_YES, INDEX_YES, TERM_VECTOR_NO);

    num      = intern("num");
    date     = intern("date");
    flipflop = intern("flipflop");

    struct FilterData data[FILTER_DOCS_SIZE] = {
        {"0", "20040601", "on"},
        {"1", "20041001", "off"},
        {"2", "20051101", "on"},
        {"3", "20041201", "off"},
        {"4", "20051101", "on"},
        {"5", "20041201", "off"},
        {"6", "20050101", "on"},
        {"7", "20040701", "off"},
        {"8", "20050301", "on"},
        {"9", "20050401", "off"}
    };

    index_create(store, fis);
    fis_deref(fis);

    iw = iw_open(store, whitespace_analyzer_new(false), NULL);
    for (i = 0; i < FILTER_DOCS_SIZE; i++) {
        Document *doc = doc_new();
        doc->boost = (float)(i+1);
        doc_add_field(doc, df_add_data(df_new(num), data[i].num));
        doc_add_field(doc, df_add_data(df_new(date), data[i].date));
        doc_add_field(doc, df_add_data(df_new(flipflop), data[i].flipflop));
        iw_add_doc(iw, doc);
        doc_destroy(doc);
    }
    iw_close(iw);
    return;
}
Example #6
0
int main(int argc, char *argv[]) {
	struct ap_list apl;
	struct ap_info api;
	struct iw_dev dev;
	struct pollfd pfd[2];
	struct deauth_thread_args ta;
	struct timeval tv1, tv2;
	suseconds_t msec;
	pthread_t deauth_thread;
	pthread_mutex_t chan_mutex, list_mutex, cnc_mutex;
	pthread_cond_t chan_cond;
	channelset_t chans;
	int ret, sigfd, c, n, chan;
	char *ifname, *chans_str;
	sigset_t exit_sig;
	time_t tm;

	if (argc < 2) {
		print_usage(stderr);
		return EXIT_FAILURE;
	}

	/* arguments */
	ifname = argv[argc-1];
	chans_str = NULL;

	while((c = getopt(argc, argv, "c:lh")) != -1) {
		switch (c) {
		case 'c':
			chans_str = optarg;
			break;
		case 'l':
			return print_interfaces();
		case 'h':
			print_usage(stdout);
			return EXIT_SUCCESS;
		case '?':
		default:
			return EXIT_FAILURE;
		}
	}

	if (argv[optind] != ifname) {
		print_usage(stderr);
		return EXIT_FAILURE;
	}

	if (getuid()) {
		fprintf(stderr, "Not root?\n");
		return EXIT_FAILURE;
	}

	/* init channel set */
	if (chans_str == NULL) {
		channel_zero(&chans);
		for (n=1; n<=14; n++)
			channel_set(&chans, n);
	} else {
		if (parse_chans_str(chans_str, &chans) == -1) {
			fprintf(stderr, "Can not parse the channels\n");
			return EXIT_FAILURE;
		}
	}

	/* init access point list */
	init_ap_list(&apl);

	/* init signals */
	sigemptyset(&exit_sig);
	sigaddset(&exit_sig, SIGINT);
	sigaddset(&exit_sig, SIGTERM);

	if (sigprocmask(SIG_BLOCK, &exit_sig, NULL) < 0) {
		err_msg("sigprocmask");
		return EXIT_FAILURE;
	}

	sigfd = signalfd(-1, &exit_sig, 0);
	if (sigfd < 0) {
		err_msg("signalfd");
		return EXIT_FAILURE;
	}

	pfd[0].fd = sigfd;
	pfd[0].revents = 0;
	pfd[0].events = POLLIN;

	/* init device */
	iw_init_dev(&dev);
	strncpy(dev.ifname, ifname, sizeof(dev.ifname)-1);

	if (iw_open(&dev) < 0) {
		print_error();
		goto _errout_no_thread;
	}

	pfd[1].fd = dev.fd_in;
	pfd[1].revents = 0;
	pfd[1].events = POLLIN;

	/* set channel */
	n = 0;
	chan = 0;
	do {
		chan = (chan % CHANNEL_MAX) + 1;
		if (channel_isset(&chans, chan))
			ret = iw_set_channel(&dev, chan);
		else
			ret = -1;
		/* if fails try next channel */
	} while(++n < CHANNEL_MAX && ret < 0);
	if (ret < 0) {
		print_error();
		goto _errout_no_thread;
	}

	/* start deauth thread */
	ta.stop = 0;
	ta.apl = &apl;
	ta.dev = &dev;
	pthread_mutex_init(&chan_mutex, NULL);
	ta.chan_mutex = &chan_mutex;
	pthread_cond_init(&chan_cond, NULL);
	ta.chan_cond = &chan_cond;
	pthread_mutex_init(&list_mutex, NULL);
	ta.list_mutex = &list_mutex;
	ta.chan_changed = 1;
	pthread_mutex_init(&cnc_mutex, NULL);
	ta.cnc_mutex = &cnc_mutex;
	ta.chan_need_change = 0;

	if (pthread_create(&deauth_thread, NULL, deauth_thread_func, &ta) < 0) {
		err_msg("pthread_create");
		goto _errout_no_thread;
	}

	clear_scr();
	update_scr(&apl, &dev);
	tm = time(NULL);
	gettimeofday(&tv1, NULL);

	while (!ta.stop) {
		if (poll(pfd, 2, 0) < 0) {
			err_msg("poll");
			goto _errout;
		}

		if (pfd[0].revents & POLLIN) /* got SIGTERM or SIGINT */
			break;

		if (pfd[1].revents & POLLIN) {
			ret = read_ap_info(&dev, &api);
			if (ret < 0 && ret != ERRNODATA) { /* error */
				print_error();
				goto _errout;
			} else if (ret == 0 && channel_isset(&chans, api.chan)) { /* got infos */
				pthread_mutex_lock(&list_mutex);
				if (add_or_update_ap(&apl, &api) < 0) {
					pthread_mutex_unlock(&list_mutex);
					print_error();
					goto _errout;
				}
				pthread_mutex_unlock(&list_mutex);
			}
		}

		gettimeofday(&tv2, NULL);
		if (tv2.tv_usec > tv1.tv_usec)
			msec = tv2.tv_usec - tv1.tv_usec;
		else
			msec = tv1.tv_usec - tv2.tv_usec;

		/* update screen every 0.5 second */
		if (msec >= 500000) {
			pthread_mutex_lock(&list_mutex);
			update_scr(&apl, &dev);
			pthread_mutex_unlock(&list_mutex);
			gettimeofday(&tv1, NULL);
		}

		/* change channel at least every 1 second */
		if (time(NULL) - tm >= 1) {
			if (!ta.chan_changed) {
				pthread_mutex_lock(&cnc_mutex);
				ta.chan_need_change = 0;
				pthread_mutex_unlock(&cnc_mutex);
				pthread_mutex_lock(&chan_mutex);
				n = 0;
				do {
					chan = (chan % CHANNEL_MAX) + 1;
					if (channel_isset(&chans, chan))
						ret = iw_set_channel(&dev, chan);
					else
						ret = -1;
					/* if fails try next channel */
				} while(++n < CHANNEL_MAX && ret < 0);
				/* if all channels failed */
				if (ret < 0) {
					print_error();
					goto _errout;
				}
				tm = time(NULL);
				ta.chan_changed = 1;
				pthread_cond_signal(&chan_cond);
				pthread_mutex_unlock(&chan_mutex);
			} else {
				pthread_mutex_lock(&cnc_mutex);
				ta.chan_need_change = 1;
				pthread_mutex_unlock(&cnc_mutex);
			}
		}
	}

	/* we got an error from deauth thread */
	if (ta.stop == 2)
		goto _errout;

	printf("\nExiting..\n");
	ta.stop = 1;
	pthread_mutex_unlock(&list_mutex);
	pthread_cond_broadcast(&chan_cond);
	pthread_mutex_unlock(&chan_mutex);
	pthread_join(deauth_thread, NULL);
	iw_close(&dev);
	free_ap_list(&apl);
	return EXIT_SUCCESS;
_errout:
	ta.stop = 1;
	pthread_mutex_unlock(&list_mutex);
	pthread_cond_broadcast(&chan_cond);
	pthread_mutex_unlock(&chan_mutex);
	pthread_join(deauth_thread, NULL);
_errout_no_thread:
	iw_close(&dev);
	free_ap_list(&apl);
	return EXIT_FAILURE;
}
Example #7
0
File: writer.c Project: rwl/ferret
IndexWriter *
frjs_iw_init(bool create, bool create_if_missing, Store *store,
Analyzer *analyzer, FieldInfos *fis) {
	IndexWriter *iw = NULL;
	Config config = default_config;

//    rb_scan_args(argc, argv, "01", &roptions);
	/*if (argc > 0) {
	 Check_Type(roptions, T_HASH);

	 if ((rval = rb_hash_aref(roptions, sym_dir)) != Qnil) {
	 Check_Type(rval, T_DATA);
	 store = DATA_PTR(rval);
	 } else if ((rval = rb_hash_aref(roptions, sym_path)) != Qnil) {
	 StringValue(rval);
	 frb_create_dir(rval);
	 store = open_fs_store(rs2s(rval));
	 DEREF(store);
	 }

	 // Let ruby's garbage collector handle the closing of the store
	 // if (!close_dir) {
	 // close_dir = RTEST(rb_hash_aref(roptions, sym_close_dir));
	 // }

	 // use_compound_file defaults to true
	 config.use_compound_file =
	 (rb_hash_aref(roptions, sym_use_compound_file) == Qfalse)
	 ? false
	 : true;

	 if ((rval = rb_hash_aref(roptions, sym_analyzer)) != Qnil) {
	 analyzer = frb_get_cwrapped_analyzer(rval);
	 }

	 create = RTEST(rb_hash_aref(roptions, sym_create));
	 if ((rval = rb_hash_aref(roptions, sym_create_if_missing)) != Qnil) {
	 create_if_missing = RTEST(rval);
	 }
	 SET_INT_ATTR(chunk_size);
	 SET_INT_ATTR(max_buffer_memory);
	 SET_INT_ATTR(index_interval);
	 SET_INT_ATTR(skip_interval);
	 SET_INT_ATTR(merge_factor);
	 SET_INT_ATTR(max_buffered_docs);
	 SET_INT_ATTR(max_merge_docs);
	 SET_INT_ATTR(max_field_length);
	 }*/
	if (NULL == store) {
		store = open_ram_store();
		DEREF(store);
	}
	if (!create && create_if_missing && !store->exists(store, "segments")) {
		create = true;
	}
	if (create) {
		if (fis != NULL) {
			index_create(store, fis);
		} else {
			fis = fis_new(STORE_YES, INDEX_YES,
			TERM_VECTOR_WITH_POSITIONS_OFFSETS);
			index_create(store, fis);
			fis_deref(fis);
		}
	}

	iw = iw_open(store, analyzer, &config);
	return iw;
}