// Tests if an options with MyFamily fingerprints missing '$' normalises // them correctly and also ensure it also works with multiple fingerprints static void test_config_fix_my_family(void *arg) { char *err = NULL; const char *family = "$1111111111111111111111111111111111111111, " "1111111111111111111111111111111111111112, " "$1111111111111111111111111111111111111113"; or_options_t* options = options_new(); or_options_t* defaults = options_new(); (void) arg; options_init(options); options_init(defaults); options->MyFamily = tor_strdup(family); options_validate(NULL, options, defaults, 0, &err) ; if (err != NULL) { TT_FAIL(("options_validate failed: %s", err)); } test_streq(options->MyFamily, "$1111111111111111111111111111111111111111, " "$1111111111111111111111111111111111111112, " "$1111111111111111111111111111111111111113"); done: if (err != NULL) { tor_free(err); } or_options_free(options); or_options_free(defaults); }
CTEST(node_serial_test, leaf_empty) { int ret = 0; int fd = ness_os_open(BRT_FILE, O_RDWR | O_CREAT, 0777); struct block *b = block_new(); struct hdr *hdr = (struct hdr*)xcalloc(1, sizeof(*hdr)); struct options *opts = options_new(); /* * dummy brt leaf */ uint64_t nid = 3; struct node *dummy_leaf = leaf_alloc_empty(nid); leaf_alloc_bsm(dummy_leaf); ret = serialize_node_to_disk(fd, b, dummy_leaf, hdr); ASSERT_TRUE(ret > 0); //free node_free(dummy_leaf); struct node *dummy_leaf1; ret = deserialize_node_from_disk(fd, b, nid, &dummy_leaf1, 0); ASSERT_TRUE(ret > 0); ASSERT_EQUAL(0, basement_count(dummy_leaf1->u.l.le->bsm)); //free node_free(dummy_leaf1); ness_os_close(fd); block_free(b); xfree(hdr); options_free(opts); xcheck_all_free(); }
static options_t *get_configuration_object() { const char *filename = options_get_filename(); if (!filename) { return NULL; } return options_new(filename, CLIENT_OPTIONS); }
/** Main entry point for benchmark code: parse the command line, and run * some benchmarks. */ int main(int argc, const char **argv) { int i; int list=0, n_enabled=0; benchmark_t *b; char *errmsg; or_options_t *options; tor_threads_init(); for (i = 1; i < argc; ++i) { if (!strcmp(argv[i], "--list")) { list = 1; } else { benchmark_t *b = find_benchmark(argv[i]); ++n_enabled; if (b) { b->enabled = 1; } else { printf("No such benchmark as %s\n", argv[i]); } } } reset_perftime(); if (crypto_seed_rng() < 0) { printf("Couldn't seed RNG; exiting.\n"); return 1; } crypto_init_siphash_key(); options = options_new(); init_logging(1); options->command = CMD_RUN_UNITTESTS; options->DataDirectory = tor_strdup(""); options_init(options); if (set_options(options, &errmsg) < 0) { printf("Failed to set initial options: %s\n", errmsg); tor_free(errmsg); return 1; } for (b = benchmarks; b->name; ++b) { if (b->enabled || n_enabled == 0) { printf("===== %s =====\n", b->name); if (!list) b->fn(); } } return 0; }
void options_parse(int argc, char** argv) { if (argv_singleton != NULL) argv_singleton = NULL; options_t* o = options_new(); // Parse the basic options. o->num_args = argc; o->args = polymec_malloc(sizeof(char*) * o->num_args); int first_named_value = -1; for (int i = 0; i < argc; ++i) { if (string_contains(argv[i], "=") && (first_named_value == -1)) first_named_value = i; o->args[i] = string_dup(argv[i]); } // Now parse parameters. int i = (first_named_value == -1) ? 0 : first_named_value; while (i < argc) { // Parse a key=value pair. int pos = 0, length; char* token; if (string_next_token(argv[i], "=", &pos, &token, &length)) { // We found a key with an '=' after it. What's the value? char key[length+1]; strncpy(key, token, length); key[length] = '\0'; if (string_next_token(argv[i], "=", &pos, &token, &length)) { char value[length+1]; strncpy(value, token, length); value[length] = '\0'; options_set(o, key, value); } } // Next! ++i; } argv_singleton = o; }
CTEST(cursor, empty) { int ret; struct options *opts; struct status *status; struct cache *cache; struct tree *tree; /* create */ opts = options_new(); status = status_new(); cache = dbcache_new(opts); tree = tree_new(DBPATH0, opts, status, cache, 1); /* cursor */ struct cursor *cur; cur = cursor_new(tree); /* first */ tree_cursor_first(cur); ret = tree_cursor_valid(cur); ASSERT_EQUAL(0, ret); /* last */ tree_cursor_last(cur); ret = tree_cursor_valid(cur); ASSERT_EQUAL(0, ret); /* next */ tree_cursor_next(cur); ret = tree_cursor_valid(cur); ASSERT_EQUAL(0, ret); cursor_free(cur); /* free */ dbcache_free(cache); tree_free(tree); options_free(opts); status_free(status); xcheck_all_free(); }
CTEST(log, read) { uint64_t lsn = 112; struct options *opts = options_new(); struct logr *lgr = logr_open(opts, lsn); struct msg k; struct msg v; msgtype_t type; uint32_t tid; int i = 0; while (logr_read(lgr, &k, &v, &type, &tid) == NESS_OK) { i++; } ASSERT_EQUAL(R, i); logr_close(lgr); options_free(opts); }
CTEST(log, write) { int i; char kbuf[KEY_SIZE]; char vbuf[VAL_SIZE]; uint32_t tbn = 0U; uint64_t lsn = 112; struct options *opts = options_new(); struct logw *lgw = logw_open(opts, lsn); for (i = 0; i < R; i++) { memset(kbuf, 0 , KEY_SIZE); memset(vbuf, 0 , VAL_SIZE); snprintf(kbuf, KEY_SIZE, "key-%d", i); snprintf(vbuf, VAL_SIZE, "val-%d", i); struct msg k = {.data = kbuf, .size = KEY_SIZE}; struct msg v = {.data = vbuf, .size = VAL_SIZE}; logw_append(lgw, &k, &v, MSG_PUT, tbn); } uint32_t all_size; uint32_t one_size = 4 /* first length */ + 4 /* tid */ + 2 /* type */ + 4 /* key length */ + KEY_SIZE /* key */ + 4 /*val length */ + VAL_SIZE /* val */ + 4; /* crc */ all_size = R * one_size; ASSERT_EQUAL(all_size, lgw->size); logw_close(lgw); options_free(opts); }
static void test_router_get_my_family(void *arg) { (void)arg; or_options_t *options = options_new(); smartlist_t *sl = NULL; char *join = NULL; // Overwrite the result of router_get_my_identity_digest(). This // happens to be okay, but only for testing. set_server_identity_key_digest_testing( (const uint8_t*)"holeinthebottomofthe"); setup_capture_of_logs(LOG_WARN); // No family listed -- so there's no list. sl = get_my_declared_family(options); tt_ptr_op(sl, OP_EQ, NULL); expect_no_log_entry(); #define CLEAR() do { \ if (sl) { \ SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp)); \ smartlist_free(sl); \ } \ tor_free(join); \ mock_clean_saved_logs(); \ } while (0) // Add a single nice friendly hex member. This should be enough // to have our own ID added. tt_ptr_op(options->MyFamily, OP_EQ, NULL); config_line_append(&options->MyFamily, "MyFamily", "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); sl = get_my_declared_family(options); tt_ptr_op(sl, OP_NE, NULL); tt_int_op(smartlist_len(sl), OP_EQ, 2); join = smartlist_join_strings(sl, " ", 0, NULL); tt_str_op(join, OP_EQ, "$686F6C65696E746865626F74746F6D6F66746865 " "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); expect_no_log_entry(); CLEAR(); // Add a hex member with a ~. The ~ part should get removed. config_line_append(&options->MyFamily, "MyFamily", "$0123456789abcdef0123456789abcdef01234567~Muffin"); sl = get_my_declared_family(options); tt_ptr_op(sl, OP_NE, NULL); tt_int_op(smartlist_len(sl), OP_EQ, 3); join = smartlist_join_strings(sl, " ", 0, NULL); tt_str_op(join, OP_EQ, "$0123456789ABCDEF0123456789ABCDEF01234567 " "$686F6C65696E746865626F74746F6D6F66746865 " "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); expect_no_log_entry(); CLEAR(); // Nickname lookup will fail, so a nickname will appear verbatim. config_line_append(&options->MyFamily, "MyFamily", "BAGEL"); sl = get_my_declared_family(options); tt_ptr_op(sl, OP_NE, NULL); tt_int_op(smartlist_len(sl), OP_EQ, 4); join = smartlist_join_strings(sl, " ", 0, NULL); tt_str_op(join, OP_EQ, "$0123456789ABCDEF0123456789ABCDEF01234567 " "$686F6C65696E746865626F74746F6D6F66746865 " "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA " "bagel"); expect_single_log_msg_containing( "There is a router named \"BAGEL\" in my declared family, but " "I have no descriptor for it."); CLEAR(); // A bogus digest should fail entirely. config_line_append(&options->MyFamily, "MyFamily", "$painauchocolat"); sl = get_my_declared_family(options); tt_ptr_op(sl, OP_NE, NULL); tt_int_op(smartlist_len(sl), OP_EQ, 4); join = smartlist_join_strings(sl, " ", 0, NULL); tt_str_op(join, OP_EQ, "$0123456789ABCDEF0123456789ABCDEF01234567 " "$686F6C65696E746865626F74746F6D6F66746865 " "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA " "bagel"); // "BAGEL" is still there, but it won't make a warning, because we already // warned about it. expect_single_log_msg_containing( "There is a router named \"$painauchocolat\" in my declared " "family, but that isn't a legal digest or nickname. Skipping it."); CLEAR(); // Let's introduce a node we can look up by nickname memset(&fake_node, 0, sizeof(fake_node)); memcpy(fake_node.identity, "whydoyouasknonononon", DIGEST_LEN); MOCK(node_get_by_nickname, mock_node_get_by_nickname); config_line_append(&options->MyFamily, "MyFamily", "CRUmpeT"); sl = get_my_declared_family(options); tt_ptr_op(sl, OP_NE, NULL); tt_int_op(smartlist_len(sl), OP_EQ, 5); join = smartlist_join_strings(sl, " ", 0, NULL); tt_str_op(join, OP_EQ, "$0123456789ABCDEF0123456789ABCDEF01234567 " "$686F6C65696E746865626F74746F6D6F66746865 " "$776879646F796F7561736B6E6F6E6F6E6F6E6F6E " "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA " "bagel"); // "BAGEL" is still there, but it won't make a warning, because we already // warned about it. Some with "$painauchocolat". expect_single_log_msg_containing( "There is a router named \"CRUmpeT\" in my declared " "family, but it wasn't listed by digest. Please consider saying " "$776879646F796F7561736B6E6F6E6F6E6F6E6F6E instead, if that's " "what you meant."); CLEAR(); UNMOCK(node_get_by_nickname); // Try a singleton list containing only us: It should give us NULL. config_free_lines(options->MyFamily); config_line_append(&options->MyFamily, "MyFamily", "$686F6C65696E746865626F74746F6D6F66746865"); sl = get_my_declared_family(options); tt_ptr_op(sl, OP_EQ, NULL); expect_no_log_entry(); done: or_options_free(options); teardown_capture_of_logs(); CLEAR(); UNMOCK(node_get_by_nickname); #undef CLEAR }
CTEST(node_serial_test, leaf_2_record) { int ret = 0; int fd = ness_os_open(BRT_FILE, O_RDWR | O_CREAT, 0777); struct block *b = block_new(); struct hdr *hdr = (struct hdr*)xcalloc(1, sizeof(*hdr)); struct options *opts = options_new(); /* * dummy brt leaf */ uint64_t nid = 3; struct node *dummy_leaf = leaf_alloc_empty(nid); leaf_alloc_bsm(dummy_leaf); MSN msn = 0U; struct xids *xids = NULL; struct msg k, v; k.size = 6; k.data = "hello"; v.size = 6; v.data = "world"; basement_put(dummy_leaf->u.l.le->bsm, &k, &v, MSG_INSERT, msn, xids); struct msg k1, v1; k1.size = 6; k1.data = "hellx"; v1.size = 6; v1.data = "worlx"; basement_put(dummy_leaf->u.l.le->bsm, &k1, &v1, MSG_INSERT, msn, xids); ret = serialize_node_to_disk(fd, b, dummy_leaf, hdr); ASSERT_TRUE(ret > 0); //free node_free(dummy_leaf); struct node *dummy_leaf1; ret = deserialize_node_from_disk(fd, b, nid, &dummy_leaf1, 0); ASSERT_TRUE(ret > 0); ASSERT_EQUAL(2, basement_count(dummy_leaf1->u.l.le->bsm)); struct basement_iter iter; basement_iter_init(&iter, dummy_leaf1->u.l.le->bsm); basement_iter_seek(&iter, &k); ASSERT_EQUAL(1, iter.valid); ASSERT_STR("world", iter.val.data); basement_iter_seek(&iter, &k1); ASSERT_EQUAL(1, iter.valid); ASSERT_STR("worlx", iter.val.data); //free node_free(dummy_leaf1); ness_os_close(fd); block_free(b); xfree(hdr); options_free(opts); xcheck_all_free(); }
CTEST(node_serial_test, node_2th_part_empty) { int ret = 0; NID nid; uint32_t n_children = 3; int fd = ness_os_open(BRT_FILE, O_RDWR | O_CREAT, 0777); struct block *b = block_new(); struct hdr *hdr = (struct hdr*)xcalloc(1, sizeof(*hdr)); struct options *opts = options_new(); /* * serialize */ struct node *dummy_node; hdr->last_nid++; nid = hdr->last_nid; dummy_node = nonleaf_alloc_empty(nid, 1, n_children); nonleaf_alloc_buffer(dummy_node); struct msg p0; p0.size = 6; p0.data = "pivot0"; msgcpy(&dummy_node->u.n.pivots[0], &p0); struct msg p1; p1.size = 6; p1.data = "pivot1"; msgcpy(&dummy_node->u.n.pivots[1], &p1); MSN msn = 0U; struct xids *xids = NULL; struct msg k, v; k.size = 5; k.data = "hello"; v.size = 5; v.data = "world"; basement_put(dummy_node->u.n.parts[0].buffer, &k, &v, MSG_INSERT, msn, xids); hdr->method = NESS_QUICKLZ_METHOD; ret = serialize_node_to_disk(fd, b, dummy_node, hdr); ASSERT_TRUE(ret > 0); node_free(dummy_node); //deserialize int light = 0; struct node *dummy_node1; ret = deserialize_node_from_disk(fd, b, nid, &dummy_node1, light); ASSERT_TRUE(ret > 0); ASSERT_EQUAL(1, dummy_node1->height); ASSERT_EQUAL(3, dummy_node1->u.n.n_children); ASSERT_DATA((const unsigned char*)"pivot0", 6, (const unsigned char*)dummy_node1->u.n.pivots[0].data, dummy_node1->u.n.pivots[0].size); ASSERT_DATA((const unsigned char*)"pivot1", 6, (const unsigned char*)dummy_node1->u.n.pivots[1].data, dummy_node1->u.n.pivots[1].size); ASSERT_EQUAL(3, dummy_node1->u.n.n_children); if (!light) { int cmp; struct basement_iter iter; struct basement *bsm; bsm = dummy_node1->u.n.parts[0].buffer; basement_iter_init(&iter, bsm); int mb_c = basement_count(dummy_node1->u.n.parts[0].buffer); ASSERT_EQUAL(1, mb_c); basement_iter_seek(&iter, &k); ret = basement_iter_valid(&iter); ASSERT_EQUAL(1, ret); cmp = msg_key_compare(&k, &iter.key); ASSERT_EQUAL(0, cmp); cmp = msg_key_compare(&v, &iter.val); ASSERT_EQUAL(0, cmp); mb_c = basement_count(dummy_node1->u.n.parts[1].buffer); ASSERT_EQUAL(0, mb_c); } node_free(dummy_node1); ness_os_close(fd); block_free(b); xfree(hdr); options_free(opts); xcheck_all_free(); }
int main( int argc, char *argv[] ) { char *h1, *h2, *s, *q; unsigned i; unsigned n; struct options *o = options_new( ); struct fqreader *z; /* Get the command line options */ options_cmdline( o, argc, argv ); words = tokenset_new( ); word = realloc( word, sizeof ( char ) * ( 1 + o->word_size ) ); if ( o->optind == argc ) { /* read from stdin */ z = fqreader_new( NULL ); while ( fqreader_next( z, &h1, &h2, &s, &q ) ) { stru_toupper( s ); _update_table( o->word_size, s, o->check_initial ); } fqreader_free( z ); } else { /* read from input files */ int k; for ( k = o->optind; k < argc; k++ ) { z = fqreader_new( argv[k] ); if ( _IS_NULL( z ) ) { fprintf( stderr, "[ERROR] %s: Cannot open input file \"%s\"\n", _I_AM, argv[k] ); exit( 1 ); } while ( fqreader_next( z, &h1, &h2, &s, &q ) ) { stru_toupper( s ); _update_table( o->word_size, s, o->check_initial ); } fqreader_free( z ); } } /* Print the countlist */ n = tokenset_count( words ); for ( i = 0; i < n; i++ ) { if ( countlist[i].count < o->min_count ) continue; printf( "%s\t%d\n", tokenset_get_by_id( words, countlist[i].id ), countlist[i].count ); } options_free( o ); tokenset_free( words ); _FREE( word ); _FREE( countlist ); return 0; }
int main(int argc, char **argv) { int option_index = 0; if (argc < 2) { show_help(argv); return EXIT_FAILURE; } gru_status_t status = gru_status_new(); options_t *options = options_new(&status); if (!options) { fprintf(stderr, "Unable to create options object: %s", status.message); return EXIT_FAILURE; } set_options_object(options); gru_logger_set(gru_logger_timed_printer); while (1) { static struct option long_options[] = {{"log-level", required_argument, 0, 'l'}, {"log-dir", required_argument, 0, 'L'}, {"maestro-url", required_argument, 0, 'm'}, {"name", required_argument, 0, 'n'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; int c = getopt_long(argc, argv, "l:L:m:n:h", long_options, &option_index); if (c == -1) { break; } switch (c) { case 'l': options_set_log_level(options, optarg); break; case 'L': if (!options_set_logdir(options, optarg)) { fprintf(stderr, "Unable to allocate memory for setting the log directory\n"); goto err_exit; } break; case 'm': if (!options_set_maestro_uri(options, optarg, &status)) { fprintf(stderr, "%s\n", status.message); goto err_exit; } break; case 'n': if (!options_set_name(options, optarg)) { fprintf(stderr, "Unable to allocate memory for setting the node name\n"); goto err_exit; } break; case 'h': show_help(argv); options_destroy(&options); return EXIT_SUCCESS; default: printf("Invalid or missing option\n"); show_help(argv); options_destroy(&options); return EXIT_FAILURE; } } if (!options_get_log_dir()) { fprintf(stderr, "Log directory is mandatory for the sender daemon\n"); goto err_exit; } if (!gru_path_mkdirs(options_get_log_dir(), &status)) { fprintf(stderr, "Unable to create log directory: %s\n", status.message); goto err_exit; } if (!options_get_maestro_host()) { fprintf(stderr, "Maestro host is mandatory for the sender daemon\n"); goto err_exit; } int cret = init_controller(options_get_log_dir(), "mpt-sender-daemon"); if (cret == 0) { if (senderd_worker_start(options) != 0) { logger_t logger = gru_logger_get(); logger(GRU_ERROR, "Unable to start the sender worker"); goto err_exit; } #ifdef __linux__ fcloseall(); #endif options_destroy(&options); return EXIT_SUCCESS; } if (cret > 0) { options_destroy(&options); return EXIT_SUCCESS; } err_exit: options_destroy(&options); return EXIT_FAILURE; }
#define DBPATH1 "cursor-test-1.brt" CTEST(cursor, 1leaf) { int i; int ret; int R = 1000; char kbuf[KEY_SIZE]; char vbuf[VAL_SIZE]; struct options *opts; struct status *status; struct cache *cache; struct tree *tree; /* create */ opts = options_new(); status = status_new(); cache = dbcache_new(opts); tree = tree_new(DBPATH1, opts, status, cache, 1); /* insert to tree */ for (i = 0; i < R; i++) { memset(kbuf, 0 , KEY_SIZE); memset(vbuf, 0 , VAL_SIZE); snprintf(kbuf, KEY_SIZE, "key-%d", i); snprintf(vbuf, VAL_SIZE, "val-%d", i); struct msg k = {.data = kbuf, .size = KEY_SIZE}; struct msg v = {.data = vbuf, .size = VAL_SIZE}; tree_put(tree, &k, &v, MSG_PUT, DUMMY_TXID);
gint main_runShadow(gint argc, gchar* argv[]) { /* check the compiled GLib version */ if (!GLIB_CHECK_VERSION(2, 32, 0)) { g_printerr("** GLib version 2.32.0 or above is required but Shadow was compiled against version %u.%u.%u\n", (guint)GLIB_MAJOR_VERSION, (guint)GLIB_MINOR_VERSION, (guint)GLIB_MICRO_VERSION); return EXIT_FAILURE; } if(GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION == 40) { g_printerr("** You compiled against GLib version %u.%u.%u, which has bugs known to break Shadow. Please update to a newer version of GLib.\n", (guint)GLIB_MAJOR_VERSION, (guint)GLIB_MINOR_VERSION, (guint)GLIB_MICRO_VERSION); return EXIT_FAILURE; } /* check the that run-time GLib matches the compiled version */ const gchar* mismatch = glib_check_version(GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION); if(mismatch) { g_printerr("** The version of the run-time GLib library (%u.%u.%u) is not compatible with the version against which Shadow was compiled (%u.%u.%u). GLib message: '%s'\n", glib_major_version, glib_minor_version, glib_micro_version, (guint)GLIB_MAJOR_VERSION, (guint)GLIB_MINOR_VERSION, (guint)GLIB_MICRO_VERSION, mismatch); return EXIT_FAILURE; } /* parse the options from the command line */ gchar* cmds = g_strjoinv(" ", argv); gchar** cmdv = g_strsplit(cmds, " ", 0); g_free(cmds); Options* options = options_new(argc, cmdv); g_strfreev(cmdv); if(!options) { return EXIT_FAILURE; } /* if they just want the shadow version, print it and exit */ if(options_doRunPrintVersion(options)) { g_printerr("%s running GLib v%u.%u.%u and IGraph v%s\n%s\n", SHADOW_VERSION_STRING, (guint)GLIB_MAJOR_VERSION, (guint)GLIB_MINOR_VERSION, (guint)GLIB_MICRO_VERSION, #if defined(IGRAPH_VERSION) IGRAPH_VERSION, #else "(n/a)", #endif SHADOW_INFO_STRING); options_free(options); return EXIT_SUCCESS; } /* start up the logging subsystem to handle all future messages */ Logger* shadowLogger = logger_new(options_getLogLevel(options)); logger_setDefault(shadowLogger); /* disable buffering during startup so that we see every message immediately in the terminal */ logger_setEnableBuffering(shadowLogger, FALSE); gint returnCode = _main_helper(options); options_free(options); Logger* logger = logger_getDefault(); if(logger) { logger_setDefault(NULL); logger_unref(logger); } g_printerr("** Stopping Shadow, returning code %i (%s)\n", returnCode, (returnCode == 0) ? "success" : "error"); return returnCode; }
int main( int argc, char *argv[] ) { FILE *out_a, *out_b; char *h1, *h2, *s, *q; char *id = NULL; char *outname_a = NULL, *outname_b = NULL, *outdirname = NULL; int i; struct options *o = options_new( ); struct tokenset *t = tokenset_new( ); struct fqreader *fq = fqreader_new( NULL ); /* Get the command line options */ options_cmdline( o, argc, argv ); if ( _IS_NULL( o->outname ) || strlen( o->outname ) == 0 ) { o->outname = realloc( o->outname, 4 * sizeof ( char ) ); strcpy( o->outname, "FQU" ); } /* Create the output directory if necessary */ outdirname = realloc( outdirname, sizeof ( char ) * ( 2 + strlen( o->outname ) ) ); strcpy( outdirname, o->outname ); if ( strchr( outdirname, '/' ) ) { /* not the current directory */ dirname( outdirname ); mkdirp( outdirname, S_IRWXU | S_IRWXG | S_IRWXO ); } /* Read the id files */ for ( i = o->optind; i < argc; i++ ) { char *line; struct linereader *z = linereader_new( ); linereader_init( z, argv[i] ); if ( _IS_NULL( z ) ) { fprintf( stderr, "[ERROR] %s: Cannot open input file \"%s\"\n", _I_AM, argv[i] ); exit( 1 ); } while ( ( line = ( char * ) linereader_next( z ) ) ) { unsigned len; stru_trim( line ); if ( line[0] == '#' || stru_is_ws( line ) ) continue; len = strcspn( line, "\f\n\r\t\v " ); line[len] = '\0'; tokenset_add( t, line ); } linereader_free( z ); } outname_a = realloc( outname_a, sizeof ( char ) * ( 6 + strlen( o->outname ) ) ); strcpy( outname_a, o->outname ); strcat( outname_a, "_A.fq" ); out_a = fopen( outname_a, "wb" ); if ( _IS_NULL( out_a ) ) { fprintf( stderr, "Could not open first output file \"%s\"\n", outname_a ); exit( 1 ); } outname_b = realloc( outname_b, sizeof ( char ) * ( 6 + strlen( o->outname ) ) ); strcpy( outname_b, o->outname ); strcat( outname_b, "_B.fq" ); out_b = fopen( outname_b, "wb" ); if ( _IS_NULL( out_b ) ) { fprintf( stderr, "Could not open second output file \"%s\"\n", outname_b ); exit( 1 ); } while ( fqreader_next( fq, &h1, &h2, &s, &q ) ) { utils_extract_id( &id, h1 ); /* get the identifier from header 1 */ if ( tokenset_exists( t, id ) ) fprintf( out_a, "@%s\n%s\n+%s\n%s\n", h1, s, h2, q ); else fprintf( out_b, "@%s\n%s\n+%s\n%s\n", h1, s, h2, q ); } fclose( out_a ); fclose( out_b ); _FREE( id ); _FREE( outname_a ); _FREE( outname_b ); fqreader_free( fq ); options_free( o ); tokenset_free( t ); return 0; }
int main(int argc, char **argv) { int option_index = 0; if (argc < 2) { show_help(argv); return EXIT_FAILURE; } gru_status_t status = gru_status_new(); options_t *options = options_new(&status); if (!options) { fprintf(stderr, "Unable to create options object: %s", status.message); return EXIT_FAILURE; } set_options_object(options); gru_logger_set(gru_logger_default_printer); while (1) { static struct option long_options[] = {{"broker-url", required_argument, 0, 'b'}, {"duration", required_argument, 0, 'd'}, {"log-level", required_argument, 0, 'l'}, {"log-dir", required_argument, 0, 'L'}, {"parallel-count", required_argument, 0, 'p'}, {"message-size", required_argument, 0, 's'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; int c = getopt_long(argc, argv, "b:d:l:L:p:s:h", long_options, &option_index); if (c == -1) { break; } switch (c) { case 'b': if (!options_set_broker_uri(options, optarg, &status)) { fprintf(stderr, "%s\n", status.message); goto err_exit_0; } break; case 'd': if (!options_set_duration(options, optarg)) { fprintf(stderr, "Invalid duration: %s\n", optarg); goto err_exit_0; } break; case 'l': options_set_log_level(options, optarg); break; case 'p': options_set_parallel_count(options, optarg); break; case 's': options_set_message_size(options, optarg); break; case 'L': if (!options_set_logdir(options, optarg)) { fprintf(stderr, "Unable to allocate memory for setting the log directory\n"); goto err_exit_0; } break; case 'h': show_help(argv); options_destroy(&options); return EXIT_SUCCESS; default: printf("Invalid or missing option\n"); show_help(argv); options_destroy(&options); return EXIT_FAILURE; } } if (options_get_log_dir()) { remap_log_with_link(options_get_log_dir(), "mpt-receiver", 0, getpid(), stderr, &status); } else { if (options_get_parallel_count() > 1) { fprintf(stderr, "Multiple concurrent process require a log directory\n"); goto err_exit_0; } } logger_t logger = gru_logger_get(); vmsl_t vmsl = vmsl_init(); const gru_uri_t broker_uri = options_get_broker_uri(); if (!vmsl_assign_by_url(&broker_uri, &vmsl)) { goto err_exit_1; } logger(GRU_INFO, "Starting test"); if (receiver_start(&vmsl, options) == 0) { logger(GRU_INFO, "Test execution with process ID %d finished successfully\n", getpid()); options_destroy(&options); return EXIT_SUCCESS; } err_exit_1: logger(GRU_INFO, "Test execution with process ID %d finished with errors\n", getpid()); err_exit_0: options_destroy(&options); return EXIT_FAILURE; }