bake-mkpool.c 2.44 KB
Newer Older
1 2 3 4 5 6
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

7 8
#include "bake-config.h"

9 10 11 12 13
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <libpmemobj.h>
14
#include <bake-server.h>
15 16 17 18 19 20 21 22 23 24

struct options
{
    char *pmem_pool;
    size_t pool_size;
    mode_t pool_mode;
};

void usage(int argc, char *argv[])
{
25
    fprintf(stderr, "Usage: bake-mkpool [OPTIONS] <pmem_pool>\n");
26
    fprintf(stderr, "       pmem_pool is the path to the pmemobj pool to create\n");
27
    fprintf(stderr, "       [-s size] is the desired size of the pool (K, M, G, etc. suffixes allowed) (%lu is default)\n", PMEMOBJ_MIN_POOL);
28
    fprintf(stderr, "Example: ./bake-mkpool -s 16M /dev/shm/foo.dat\n");
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    return;
}

int parse_size(char *str, size_t *size_out)
{
    const char *suffixes[] = { "B", "K", "M", "G", "T", "P" };
    size_t size_mults[] = { 1ULL, 1ULL << 10, 1ULL << 20, 1ULL << 30, 1ULL << 40, 1ULL << 50 };
    size_t size;
    char suff[2] = {0};
    int i;
    int ret;

    ret = sscanf(str, "%zu%2s", &size, suff);
    if(ret == 1)
    {
        *size_out = size;
        return(0);
    }
    else if(ret == 2)
    {
        for(i = 0; i < 6; i++)
        {
            if(strcmp(suffixes[i], suff) == 0)
            {
                *size_out = (size * size_mults[i]);
                return(0);
            }
        }
    }
    return(-1);
}

void parse_args(int argc, char *argv[], struct options *opts)
{
    int opt;
    int ret;

    /* set default options */
    memset(opts, 0, sizeof(*opts));
    opts->pool_size = PMEMOBJ_MIN_POOL;
    opts->pool_mode = 0664;

    /* get options */
    while((opt = getopt(argc, argv, "s:")) != -1)
    {
        switch(opt)
        {
            case 's':
                ret = parse_size(optarg, &opts->pool_size);
                if(ret != 0)
                {
                    usage(argc, argv);
                    exit(EXIT_FAILURE);
                }
                break;
            default:
                usage(argc, argv);
                exit(EXIT_FAILURE);
        }
    }

    /* get required arguments after options */
    if((argc - optind) != 1)
    {
        usage(argc, argv);
        exit(EXIT_FAILURE);
    }
    opts->pmem_pool = argv[optind++];

    return;
}

int main(int argc, char *argv[])
{
    struct options opts;
    int ret;

    parse_args(argc, argv, &opts);

    ret = bake_server_makepool(opts.pmem_pool, opts.pool_size, opts.pool_mode);

    return(0);
}