Beispiel #1
0
int rrd_restore(
    int argc,
    char **argv)
{
    rrd_t    *rrd;
    /* init rrd clean */
    optind = 0;
    opterr = 0;         /* initialize getopt */
    while (42) {
        int       opt;
        int       option_index = 0;
        static struct option long_options[] = {
            {"range-check", no_argument, 0, 'r'},
            {"force-overwrite", no_argument, 0, 'f'},
            {0, 0, 0, 0}
        };

        opt = getopt_long(argc, argv, "rf", long_options, &option_index);

        if (opt == EOF)
            break;

        switch (opt) {
        case 'r':
            opt_range_check = 1;
            break;

        case 'f':
            opt_force_overwrite = 1;
            break;

        default:
            rrd_set_error("usage rrdtool %s [--range-check|-r] "
                          "[--force-overwrite/-f]  file.xml file.rrd",
                          argv[0]);
            return (-1);
            break;
        }
    }                   /* while (42) */

    if ((argc - optind) != 2) {
        rrd_set_error("usage rrdtool %s [--range-check/-r] "
                      "[--force-overwrite/-f] file.xml file.rrd", argv[0]);
        return (-1);
    }

    rrd = parse_file(argv[optind]);

    if (rrd == NULL)
        return (-1);
    
    if (write_file(argv[optind + 1], rrd) != 0) {
        local_rrd_free(rrd);
        return (-1);
    }
    local_rrd_free(rrd);


    return (0);
}                       /* int rrd_restore */
int rrd_restore(
    int argc,
    char **argv)
{
    struct optparse_long longopts[] = {
        {"range-check", 'r', OPTPARSE_NONE},
        {"force-overwrite", 'f', OPTPARSE_NONE},
        {0},
    };
    struct    optparse options;
    int       opt;
    rrd_t    *rrd;

    optparse_init(&options, argc, argv);
    while ((opt = optparse_long(&options, longopts, NULL)) != -1) {
        switch (opt) {
        case 'r':
            opt_range_check = 1;
            break;

        case 'f':
            opt_force_overwrite = 1;
            break;

        case '?':
            rrd_set_error("%s", options.errmsg);
            return -1;
        }
    } /* while (opt != -1) */

    if (options.argc - options.optind != 2) {
        rrd_set_error("usage rrdtool %s [--range-check|-r] "
                      "[--force-overwrite|-f] file.xml file.rrd",
                      options.argv[0]);
        return -1;
    }

    rrd = parse_file(options.argv[options.optind]);

    if (rrd == NULL)
        return (-1);
    
    if (write_file(options.argv[options.optind + 1], rrd) != 0) {
        local_rrd_free(rrd);
        return (-1);
    }
    local_rrd_free(rrd);


    return (0);
}                       /* int rrd_restore */
static rrd_t *parse_file(
    const char *filename)
{
    xmlTextReaderPtr reader;
    int       status;

    rrd_t    *rrd;
    stdioXmlReaderContext *sctx = NULL;

    /* special handling for XML on stdin (like it is the case when using
       the pipe interface) */
    if (strcmp(filename, "-") == 0) {
		sctx = (stdioXmlReaderContext *) malloc(sizeof(*sctx));
	if (sctx == NULL) {
	    rrd_set_error("parse_file: malloc failed.");
	    return (NULL);
	}
	sctx->stream = stdin;
	sctx->freeOnClose = 1;
	sctx->closed = 0;
	sctx->eofchar = 0x1A; /* ctrl-Z */

	xmlSetGenericErrorFunc(NULL, ignoringErrorFunc);

        reader = xmlReaderForIO(stdioXmlInputReadCallback,
                                stdioXmlInputCloseCallback,
                                sctx, 
                                filename,
                                NULL,
                                0);
    } else {
        reader = xmlNewTextReaderFilename(filename);
    } 
    if (reader == NULL) {
	if (sctx != NULL) free(sctx);

        rrd_set_error("Could not create xml reader for: %s",filename);
        return (NULL);
    }

    /* NOTE: from now on, sctx will get freed implicitly through
     * xmlFreeTextReader and its call to
     * stdioXmlInputCloseCallback. */

    if (expect_element(reader,"rrd") != 0) {
        xmlFreeTextReader(reader);
        return (NULL);
    }

    rrd = (rrd_t *) malloc(sizeof(rrd_t));
    if (rrd == NULL) {
        rrd_set_error("parse_file: malloc failed.");
        xmlFreeTextReader(reader);
        return (NULL);
    }
    memset(rrd, '\0', sizeof(rrd_t));

    rrd->stat_head = (stat_head_t *) malloc(sizeof(stat_head_t));
    if (rrd->stat_head == NULL) {
        rrd_set_error("parse_tag_rrd: malloc failed.");
        xmlFreeTextReader(reader);
        free(rrd);
        return (NULL);
    }
    memset(rrd->stat_head, '\0', sizeof(stat_head_t));

    strncpy(rrd->stat_head->cookie, "RRD", sizeof(rrd->stat_head->cookie));
    rrd->stat_head->float_cookie = FLOAT_COOKIE;

    rrd->live_head = (live_head_t *) malloc(sizeof(live_head_t));
    if (rrd->live_head == NULL) {
        rrd_set_error("parse_tag_rrd: malloc failed.");
        xmlFreeTextReader(reader);
        free(rrd->stat_head);
        free(rrd);
        return (NULL);
    }
    memset(rrd->live_head, '\0', sizeof(live_head_t));

    status = parse_tag_rrd(reader, rrd);

    xmlFreeTextReader(reader);

    if (status != 0) {
        local_rrd_free(rrd);
        rrd = NULL;
    }

    return (rrd);
}                       /* rrd_t *parse_file */