int main(int argc, char* argv[])
{
    int f;
    struct bson_info* bson;
    int ret;

    fprintf_blue(stdout, "BSON Printer -- By: Wolfgang Richter "
                         "<*****@*****.**>\n");

    if (argc < 2)
    {
        fprintf_light_red(stderr, "Usage: %s <BSON file>\n", argv[0]);
        return EXIT_FAILURE;
    }

    fprintf_cyan(stdout, "Analyzing BSON File: %s\n", argv[1]);

    f = open(argv[1], O_RDONLY);

    if (f < 0)
    {
        fprintf_light_red(stderr, "Error opening BSON file.\n");
        return EXIT_FAILURE;
    }

    bson = bson_init();

    while ((ret = bson_readf(bson, f)) == 1)
        bson_print(stdout, bson);
    
    bson_cleanup(bson);
    check_syscall(close(f));

    return EXIT_SUCCESS;
}
Beispiel #2
0
void redis_disconnect_callback(const redisAsyncContext* c, int status)
{
    struct nbd_handle* handle;

    if (c->data)
    {
       handle = (struct nbd_handle*) c->data;
    }
    else
    {
        fprintf_light_red(stderr, "FATAL: Handle not passed to disconnect "
                                  "callback.\n");
        assert(c->data != NULL);
        return;
    }

    if (status != REDIS_OK)
    {
        if (c->err == REDIS_ERR_EOF) /* probably standard timeout, reconnect */
        {
            fprintf_red(stderr, "Redis server disconnected us.\n");
            if ((handle->redis_c = redisAsyncConnect(handle->redis_server,
                 handle->redis_port)) != NULL)
            {
                fprintf_blue(stderr, "New Redis context, attaching to "
                                    "libevent.\n");
                handle->redis_c->data = c->data;
                redisLibeventAttach(handle->redis_c, handle->eb);
                fprintf_blue(stderr, "Setting disconnect callback.\n");
                if (redisAsyncSetDisconnectCallback(handle->redis_c,
                    &redis_disconnect_callback) != REDIS_ERR)
                {
                    assert(redisAsyncCommand(handle->redis_c,
                           &redis_async_callback, NULL, "select %d",
                           handle->redis_db) == REDIS_OK);
                    fprintf_light_blue(stderr, "Successfully reconnected to "
                                               "the Redis server.\n");
                }
                else
                {
                    fprintf_light_red(stderr, "Error setting disconnect "
                                              "callback handler for Redis.\n");
                }
            }
            else
            {
                fprintf_light_red(stderr, "Error trying to reconnect to "
                                          "Redis.\n");
            }
            return;
        }
        fprintf_light_red(stderr, "FATAL ERROR DISCONNECTION FROM REDIS\n");
        fprintf_light_blue(stderr, "Error: %s\n", c->errstr);
        assert(false);
    }
}
int main(int argc, char* argv[])
{
    struct nbd_handle* handle;
   
    if (argc < 9)
    {
        fprintf_light_red(stderr, USAGE, argv[0]);
        exit(EXIT_FAILURE);
    }

    fprintf_blue(stdout, "nbd-queuer-test program by: Wolfgang Richter "
                         "<*****@*****.**>\n");

    handle = nbd_init_redis(argv[1], argv[2], atoi(argv[3]), atoi(argv[4]),
                            atoll(argv[5]), argv[6], argv[7],
                            (strncmp(argv[8], "y", 1) == 0) ||
                            (strncmp(argv[8], "Y", 1) == 0));

    assert(handle != NULL);
    assert(handle->fd != 0);
    assert(strncmp(argv[1], handle->export_name, strlen(argv[1])) == 0);
    assert(handle->eb != NULL);
    assert(handle->conn != NULL);
    assert(handle->size >= 0);

    /* special case allow for example /dev/null to appear as a large file */
    if (handle->size == 0) handle->size = 1024*1024*1024*1024LL;
    
    nbd_run_loop(handle);
    nbd_shutdown(handle);

    fprintf_green(stdout, "-- Shutting down --\n");
    return EXIT_SUCCESS;
}
int read_loop(int fd, struct kv_store* handle, struct bitarray* bits)
{
    struct timeval start, end;
    uint8_t buf[QEMU_HEADER_SIZE];
    int64_t total = 0, read_ret = 0;
    struct qemu_bdrv_write write;
    size_t len = QEMU_HEADER_SIZE;
    uint64_t counter = 0;

    write.data = (uint8_t*) malloc(4096);

    if (write.data == NULL)
    {
        fprintf_light_red(stderr, "Failed initial alloc for write.data.\n");
        return EXIT_FAILURE;
    }

    while (1)
    {
        gettimeofday(&start, NULL);

        read_ret = read(fd, buf, QEMU_HEADER_SIZE);
        total = read_ret;

        while (read_ret > 0 && total < QEMU_HEADER_SIZE)
        {
            read_ret = read(fd, &buf[total], QEMU_HEADER_SIZE - total);
            total += read_ret;
        }

        /* check for EOF */
        if (read_ret == 0)
        {
            fprintf_light_red(stderr, "Total read: %"PRId64".\n", total);
            fprintf_light_red(stderr, "Reading from stream failed, assuming "
                                      "teardown.\n");
            return EXIT_SUCCESS;
        }

        if (read_ret < 0)
        {
            fprintf_light_red(stderr, "Unknown fatal error occurred, 0 bytes"
                                       "read from stream.\n");
            return EXIT_FAILURE;
        }

        qemu_parse_header(buf, &write);
        len = write.header.nb_sectors * SECTOR_SIZE;
        write.data = (uint8_t*) realloc(write.data, len);

        if (write.data == NULL)
        {
            fprintf_light_red(stderr, "realloc() failed, assuming OOM.\n");
            fprintf_light_red(stderr, "tried allocating: %d bytes\n",
                                      write.header.nb_sectors*SECTOR_SIZE);
            return EXIT_FAILURE;
        }

        read_ret  = read(fd, (uint8_t*) write.data,
                         write.header.nb_sectors*SECTOR_SIZE);
        total = read_ret;

        while (read_ret > 0 && total < write.header.nb_sectors*SECTOR_SIZE)
        {
            read_ret  = read(fd, (uint8_t*) &write.data[total],
                             write.header.nb_sectors*SECTOR_SIZE - total);
            total += read_ret;
        }

        if (read_ret <= 0)
        {
            fprintf_light_red(stderr, "Stream ended while reading sector "
                                      "data.\n");
            return EXIT_FAILURE;
        }

        /* the mountain of things i still regret ! */
        if (redis_async_write_enqueue(handle, bits, write.header.sector_num,
                                  write.data, len))
            fprintf_light_red(stderr, "\tqueue would block: dropping write\n");

        gettimeofday(&end, NULL);
        fprintf(stderr, "[%"PRIu64"]read_loop finished in %"PRIu64
                        " microseconds [%d bytes]\n", counter++,
                        diff_time(start, end),
                        write.header.nb_sectors*SECTOR_SIZE);
    }

    if (write.data)
        free(write.data);

    if (bits)
        bitarray_destroy(bits);

    return EXIT_SUCCESS;
}
/* main thread of execution */
int main(int argc, char* args[])
{
    int fd;
    char* index, *db, *stream;
    FILE* indexf;
    struct bitarray* bits;

    fprintf_blue(stdout, "gammaray Async Queuer -- "
                         "By: Wolfgang Richter "
                         "<*****@*****.**>\n");
    redis_print_version();

    if (argc < 4)
    {
        fprintf_light_red(stderr, "Usage: %s <index file> <stream file>"
                                  " <redis db num>\n", args[0]);
        return EXIT_FAILURE;
    }

    index = args[1];
    stream = args[2];
    db = args[3];

    /* ----------------- hiredis ----------------- */
    struct kv_store* handle = redis_init(db, true);
    if (handle == NULL)
    {
        fprintf_light_red(stderr, "Failed getting Redis context "
                                  "(connection failure?).\n");
        return EXIT_FAILURE;
    }

    fprintf_cyan(stdout, "Loading MD filter from: %s\n\n", index);
    indexf = fopen(index, "r");

    if (indexf == NULL)
    {
        fprintf_light_red(stderr, "Error opening index file to get MD "
                                  "filter.\n");
        return EXIT_FAILURE;
    }

    if (qemu_load_md_filter(indexf, &bits))
    {
        fprintf_light_red(stderr, "Error getting MD filter from BSON file.\n");
        bits = bitarray_init(5242880);
        bitarray_set_all(bits);
    }

    if (bits == NULL)
    {
        fprintf_light_red(stderr, "Bitarray is NULL!\n");
        return EXIT_FAILURE;
    }

    fclose(indexf);

    fprintf_cyan(stdout, "Attaching to stream: %s\n\n", stream);

    on_exit((void (*) (int, void *)) redis_shutdown, handle);

    if (strcmp(stream, "-") != 0)
    {
        fd = open(stream, O_RDONLY);
    }
    else
    {
        fd = STDIN_FILENO;
    }
    
    if (fd == -1)
    {
        fprintf_light_red(stderr, "Error opening stream file. "
                                  "Does it exist?\n");
        return EXIT_FAILURE;
    }

    read_loop(fd, handle, bits);
    close(fd);
    return EXIT_SUCCESS;
}