Пример #1
0
int main(int argc, char *argv[])
{
    fs_gnu_options(argc, argv, "<kbname>\n");

    if (argc != 2) {
        fprintf(stderr, "Usage: %s <kbname>\n", basename(argv[0]));

        return 1;
    }

    const char *kbname = argv[1];

    fs_backend *be = fs_backend_init(kbname, 0);
    if (!be) {
        return 1;
    }

    int segments[FS_MAX_SEGMENTS];
    char *tmp = g_strdup_printf("du -hs "FS_KB_DIR" | sed 's/.\\/var.*/ disk usage/; s/^/  /'", kbname);
    system(tmp);
    g_free(tmp);

    return 0;
}
Пример #2
0
int main(int argc, char *argv[])
{
    char *optstring = "nv";
    int help = 0;
    int c, opt_index = 0;
    kbconfig config = {
        .name = NULL,
        .password = NULL,
        .node = 0,
        .cluster = 1,
        .segments = 2,
        .mirror = 0,
        .model_files = 0,
    };

    static struct option long_options[] = {
        { "help", 0, 0, 'h' },
        { "version", 0, 0, 'V' },
        { "verbose", 0, 0, 'v' },
        { "mirror", 0, 0, 'm' },
        { "model-files", 0, 0, 'f' },
        { "print-only", 0, 0, 'n' },
        { "node", 1, 0, 'N' },
        { "cluster", 1, 0, 'C' },
        { "segments", 1, 0, 'S' },
        { "password", 1, 0, 'P' },
        { 0, 0, 0, 0 }
    };

    setlocale(LC_ALL, NULL);
    int help_return = 1;

    while ((c = getopt_long (argc, argv, optstring, long_options, &opt_index)) != -1) {
	if (c == 'v') {
	    verbosity++;
	} else if (c == 'm') {
	    config.mirror = 1;
	} else if (c == 'f') {
	    config.model_files = 1;
	} else if (c == 'n') {
	    dummy = 1;
	} else if (c == 'N') {
	    config.node = atoi(optarg);
	} else if (c == 'C') {
	    config.cluster = atoi(optarg);
	} else if (c == 'S') {
	    config.segments = atoi(optarg);
	} else if (c == 'P') {
	    config.password = optarg;
	} else if (c == 'h') {
	    help = 1;
	    help_return = 0;
	} else if (c == 'V') {
	    printf("%s, built for 4store %s\n", basename(argv[0]), GIT_REV);
	    exit(0);
	} else {
	    help++;
	}
    }

    if (optind == argc - 1) {
	config.name = argv[optind];
    }

    if (config.segments < 1 || config.node < 0 ||
        config.node >= config.cluster || config.name == NULL) {
        help = 1;
    }

    if (help) {
        fprintf(stdout, "%s, built for 4store %s\n", basename(argv[0]), GIT_REV);
        fprintf(stdout, "Usage: %s [-v] --node <id> --cluster <size>\n\t--segments <seg> [--password <pw>] <kbname>\n", basename(argv[0]));
        fprintf(stdout, "   --node <id>       node id 0 ... cluster-1\n");
        fprintf(stdout, "   --cluster <size>  number of nodes in cluster\n");
        fprintf(stdout, "   --segments <seg>  number of segments in cluster\n");
        fprintf(stdout, "   --password <pw>   password for authentication\n");
        fprintf(stdout, "   -m, --mirror      mirror segments\n");
        fprintf(stdout, "   --model-files     use a file per-model (for large models)\n");
        fprintf(stdout, "   -v, --verbose     increase verbosity\n");
        fprintf(stdout, "   -n, --print-only  dont execute commands, just show\n");
        fprintf(stdout, "This command creates KBs, if the KB already exists, its contents are lost.\n");
        return help_return;
    }

    /* check segments is a power of 2 */
    for (int s= config.segments; s > 0; s = s >> 1) {
      if ((s & 1) == 1 && s != 1) {
         fprintf(stderr, "Number of segments must be a power of 2\n");
         return 1;
      }
    }

    fsp_syslog_enable(); /* from now on errors are logged */

    for (int k= 0; k < strlen(config.name); ++k) {
      if (!index(FS_LEGAL_KB_CHARS, config.name[k])) {
        fs_error(LOG_ERR, "character '%c' not allowed in KB name %s", config.name[k], config.name);
        return 1;
      }
    }

    fs_backend *be = fs_backend_init(config.name, FS_BACKEND_NO_OPEN);
    create_dir(&config);

    erase_db(&config);
    fs_error(LOG_INFO, "erased files for KB %s", config.name);

    setup_metadata(&config);

    fs_backend_fini(be);

    return 0;
}

void create_dir(kbconfig *config)
{
    char *tmp;

    LOG(1, "Creating data directory");
    tmp = g_strdup_printf("%s/%s", FS_STORE_ROOT, config->name);
    if (mkdir(tmp, 0755)) {
	if (errno != EEXIST) {
	    fprintf(stderr, "Failed to create store directory '%s': %s\n", 
		tmp, strerror(errno));
	    exit(1);
	}
    }
    g_free(tmp);

    LOG(1, "Create segment directories");
    for (int i=0; i<config->segments; i++) {
	if (!uses_segment(config, i)) continue;
	tmp = g_strdup_printf("%s/%s/%04x", FS_STORE_ROOT, config->name, i);
	if (mkdir(tmp, 0755)) {
	    if (errno != EEXIST) {
		fprintf(stderr, "Failed to create store directory '%s': %s\n", 
		    tmp, strerror(errno));
		exit(1);
	    }
	}
	g_free(tmp);
	fs_backend *be = fs_backend_init(config->name, FS_BACKEND_NO_OPEN);
	if (!be) {
	    fprintf(stderr, "Failed to open backed\n");
	    exit(1);
	}
	fs_backend_open_files(be, i, O_CREAT | O_RDWR | O_TRUNC, FS_OPEN_ALL);
	fs_backend_cleanup_files(be);
	fs_backend_close_files(be, i);
	/* create directories for model indexes */
	tmp = g_strdup_printf("%s/%s/%04x/m", FS_STORE_ROOT,
			      config->name, i);
	mkdir(tmp, 0755);
	g_free(tmp);
	fs_backend_fini(be);
    }
}