X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=blobdiff_plain;f=src%2Frrd_resize.c;h=81410251d0071583db8a5f24cd12c24fa68a42de;hp=57adbf02ffeb5676915feac648b1aee8fd8182bf;hb=454dc9735d59ddbbfd1a658541683017e71c294a;hpb=0a5d814368107f20baa26ddba418ceab9d47f901 diff --git a/src/rrd_resize.c b/src/rrd_resize.c index 57adbf0..8141025 100644 --- a/src/rrd_resize.c +++ b/src/rrd_resize.c @@ -1,11 +1,13 @@ /***************************************************************************** - * RRDtool 1.3.2 Copyright by Tobi Oetiker, 1997-2008 + * 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( @@ -55,6 +57,7 @@ int rrd_resize( modify = -modify; + rrd_init(&rrdold); rrd_file = rrd_open(infilename, &rrdold, RRD_READWRITE | RRD_COPY); if (rrd_file == NULL) { rrd_free(&rrdold); @@ -83,41 +86,50 @@ int rrd_resize( rrd_close(rrd_file); return (-1); } - /* the size of the new file */ - /* yes we are abusing the float cookie for this, aargh */ - if ((rrdnew.stat_head = calloc(1, sizeof(stat_head_t))) == NULL) { + + 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); } - rrdnew.stat_head->float_cookie = rrd_file->file_len + - (rrdold.stat_head->ds_cnt * sizeof(rrd_value_t) * modify); + 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); - return (-1); - } - if (rrd_lock(rrd_out_file) != 0) { - rrd_set_error("could not lock new RRD"); rrd_free(&rrdold); rrd_close(rrd_file); - rrd_close(rrd_out_file); return (-1); } -/*XXX: do one write for those parts of header that are unchanged */ - if ((rrdnew.stat_head = malloc(sizeof(stat_head_t))) == NULL) { - rrd_set_error("allocating stat_head for new RRD"); + if (rrd_lock(rrd_out_file) != 0) { + rrd_set_error("could not lock new RRD"); rrd_free(&rrdnew); rrd_free(&rrdold); rrd_close(rrd_file); rrd_close(rrd_out_file); return (-1); } - - if ((rrdnew.rra_ptr = malloc(sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt)) == NULL) { +/*XXX: do one write for those parts of header that are unchanged */ + 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); @@ -126,18 +138,11 @@ int rrd_resize( return (-1); } - if ((rrdnew.rra_def = 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); - rrd_close(rrd_out_file); - return (-1); - } - - memcpy(rrdnew.stat_head,rrdold.stat_head,sizeof(stat_head_t)); + /* 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; - memcpy(rrdnew.rra_def,rrdold.rra_def,sizeof(rra_def_t) * rrdold.stat_head->rra_cnt); rrdnew.live_head = rrdold.live_head; rrdnew.pdp_prep = rrdold.pdp_prep; rrdnew.cdp_prep = rrdold.cdp_prep; @@ -151,11 +156,17 @@ int rrd_resize( case 3: break; case 1: - rrdold.stat_head->version[3] = '3'; + rrdnew.stat_head->version[3] = '3'; break; 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); rrd_close(rrd_file); @@ -208,17 +219,12 @@ int rrd_resize( rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1); l--; } -#ifndef HAVE_MMAP buffer = DNAN; l = rrdnew.stat_head->ds_cnt * modify; while (l > 0) { rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1); l--; } -#else - /* for the mmap case, we did already fill the whole new file with DNAN - * before we copied the old values, so nothing to do here. */ -#endif } else { /* Removing rows. Normally this would be just after the cursor ** however this may also mean that we wrap to the beginning of @@ -282,6 +288,12 @@ int rrd_resize( rrd_close(rrd_file); rrd_close(rrd_out_file); rrd_free(&rrdold); + + rrdnew.ds_def = NULL; + rrdnew.live_head = NULL; + rrdnew.pdp_prep = NULL; + rrdnew.cdp_prep = NULL; + rrd_free(&rrdnew); return (0); }