X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=blobdiff_plain;f=src%2Frrd_resize.c;h=d429f1740403ac78872014a0220cff1657c287b3;hp=1778f42448d1a527446170126bf882f8e4bef126;hb=586cc1f6f770892aa24f08e38a6c14c2c47ee560;hpb=2a6a270edfda89b04722b42b57992907f871c671 diff --git a/src/rrd_resize.c b/src/rrd_resize.c index 1778f42..d429f17 100644 --- a/src/rrd_resize.c +++ b/src/rrd_resize.c @@ -1,11 +1,13 @@ /***************************************************************************** - * RRDtool 1.2.23 Copyright by Tobi Oetiker, 1997-2007 + * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010 ***************************************************************************** * rrd_resize.c Alters size of an RRA ***************************************************************************** * Initial version by Alex van den Bogaerdt *****************************************************************************/ +#include + #include "rrd_tool.h" int rrd_resize( @@ -19,7 +21,7 @@ int rrd_resize( unsigned long l, rra; long modify; unsigned long target_rra; - int grow = 0, shrink = 0; + int shrink = 0; char *endptr; rrd_file_t *rrd_file, *rrd_out_file; @@ -36,7 +38,7 @@ int rrd_resize( target_rra = strtol(argv[2], &endptr, 0); if (!strcmp(argv[3], "GROW")) - grow = 1; + shrink = 0; else if (!strcmp(argv[3], "SHRINK")) shrink = 1; else { @@ -55,22 +57,25 @@ int rrd_resize( modify = -modify; - rrd_file = rrd_open(infilename, &rrdold, RRD_READWRITE); + rrd_init(&rrdold); + rrd_file = rrd_open(infilename, &rrdold, RRD_READWRITE | RRD_COPY); if (rrd_file == NULL) { - rrd_set_error("could not open RRD"); + rrd_free(&rrdold); return (-1); } - if (LockRRD(rrd_file->fd) != 0) { + + if (rrd_lock(rrd_file) != 0) { rrd_set_error("could not lock original RRD"); rrd_free(&rrdold); - close(rrd_file->fd); + rrd_close(rrd_file); return (-1); } + if (target_rra >= rrdold.stat_head->rra_cnt) { rrd_set_error("no such RRA in this RRD"); rrd_free(&rrdold); - close(rrd_file->fd); + rrd_close(rrd_file); return (-1); } @@ -78,49 +83,98 @@ int rrd_resize( if ((long) rrdold.rra_def[target_rra].row_cnt <= -modify) { rrd_set_error("This RRA is not that big"); rrd_free(&rrdold); - close(rrd_file->fd); + rrd_close(rrd_file); return (-1); } - rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_CREAT); + rrd_init(&rrdnew); + /* These need to be initialised before calling rrd_open() with + the RRD_CREATE flag */ + + if ((rrdnew.stat_head = (stat_head_t*)calloc(1, sizeof(stat_head_t))) == NULL) { + rrd_set_error("allocating stat_head for new RRD"); + rrd_free(&rrdold); + rrd_close(rrd_file); + return (-1); + } + memcpy(rrdnew.stat_head,rrdold.stat_head,sizeof(stat_head_t)); + + if ((rrdnew.rra_def = (rra_def_t *)malloc(sizeof(rra_def_t) * rrdold.stat_head->rra_cnt)) == NULL) { + rrd_set_error("allocating rra_def for new RRD"); + rrd_free(&rrdnew); + rrd_free(&rrdold); + rrd_close(rrd_file); + return (-1); + } + memcpy(rrdnew.rra_def,rrdold.rra_def,sizeof(rra_def_t) * rrdold.stat_head->rra_cnt); + + /* Set this so that the file will be created with the correct size */ + rrdnew.rra_def[target_rra].row_cnt += modify; + + rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_READWRITE | RRD_CREAT); if (rrd_out_file == NULL) { rrd_set_error("Can't create '%s': %s", outfilename, rrd_strerror(errno)); + rrd_free(&rrdnew); + rrd_free(&rrdold); + rrd_close(rrd_file); return (-1); } - if (LockRRD(rrd_out_file->fd) != 0) { + if (rrd_lock(rrd_out_file) != 0) { rrd_set_error("could not lock new RRD"); + rrd_free(&rrdnew); rrd_free(&rrdold); - close(rrd_file->fd); - close(rrd_out_file->fd); + rrd_close(rrd_file); + rrd_close(rrd_out_file); return (-1); } /*XXX: do one write for those parts of header that are unchanged */ - rrdnew.stat_head = rrdold.stat_head; + if ((rrdnew.rra_ptr = (rra_ptr_t *)malloc(sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt)) == NULL) { + rrd_set_error("allocating rra_ptr for new RRD"); + rrd_free(&rrdnew); + rrd_free(&rrdold); + rrd_close(rrd_file); + rrd_close(rrd_out_file); + return (-1); + } + + /* Put this back the way it was so that the rest of the algorithm + below remains unchanged, it will be corrected later */ + rrdnew.rra_def[target_rra].row_cnt -= modify; + rrdnew.ds_def = rrdold.ds_def; - rrdnew.rra_def = rrdold.rra_def; rrdnew.live_head = rrdold.live_head; rrdnew.pdp_prep = rrdold.pdp_prep; rrdnew.cdp_prep = rrdold.cdp_prep; - rrdnew.rra_ptr = rrdold.rra_ptr; + memcpy(rrdnew.rra_ptr,rrdold.rra_ptr,sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt); + version = atoi(rrdold.stat_head->version); switch (version) { + case 4: + break; case 3: break; case 1: - rrdold.stat_head->version[3] = '3'; + rrdnew.stat_head->version[3] = '3'; break; - default:{ + default: rrd_set_error("Do not know how to handle RRD version %s", rrdold.stat_head->version); + + rrdnew.ds_def = NULL; + rrdnew.live_head = NULL; + rrdnew.pdp_prep = NULL; + rrdnew.cdp_prep = NULL; + + rrd_free(&rrdnew); rrd_free(&rrdold); - close(rrd_file->fd); + rrd_close(rrd_file); + rrd_close(rrd_out_file); return (-1); - } + break; } - /* XXX: Error checking? */ rrd_write(rrd_out_file, rrdnew.stat_head, sizeof(stat_head_t) * 1); rrd_write(rrd_out_file, rrdnew.ds_def, @@ -158,8 +212,8 @@ int rrd_resize( /* Adding extra rows; insert unknown values just after the ** current row number. */ - l = rrdnew.stat_head->ds_cnt * (rrdnew.rra_ptr[target_rra].cur_row + - 1); + l = rrdnew.stat_head->ds_cnt * + (rrdnew.rra_ptr[target_rra].cur_row + 1); while (l > 0) { rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1); rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1); @@ -203,7 +257,8 @@ int rrd_resize( } } while (modify < 0) { - rrd_seek(rrd_file, sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt, + rrd_seek(rrd_file, + sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt, SEEK_CUR); rrdnew.rra_def[target_rra].row_cnt--; modify++; @@ -212,9 +267,14 @@ int rrd_resize( /* Move the rest of the CDPs */ while (1) { - if (rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1) <= 0) + ssize_t b_read; + if ((b_read=rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1)) <= 0) break; - rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1); + if(rrd_out_file->pos+b_read > rrd_out_file->file_len) { + fprintf(stderr,"WARNING: ignoring last %zu bytes\nWARNING: if you see this message multiple times for a single file you're in trouble\n", b_read); + continue; + } + rrd_write(rrd_out_file, &buffer, b_read); } rrdnew.rra_def[target_rra].row_cnt += modify; rrd_seek(rrd_out_file, @@ -230,9 +290,15 @@ int rrd_resize( rrdnew.stat_head->rra_cnt, SEEK_CUR); rrd_write(rrd_out_file, rrdnew.rra_ptr, sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt); - - close(rrd_out_file->fd); + rrd_close(rrd_file); + rrd_close(rrd_out_file); rrd_free(&rrdold); - close(rrd_file->fd); + + rrdnew.ds_def = NULL; + rrdnew.live_head = NULL; + rrdnew.pdp_prep = NULL; + rrdnew.cdp_prep = NULL; + + rrd_free(&rrdnew); return (0); }