Code

Imported upstream version 1.4.8
[pkg-rrdtool.git] / src / rrd_resize.c
index f53d3c58eebff00006e1a344374dc039f1f961ee..3fdc1b9abf4971eda8feaa0ce010df93776edd00 100644 (file)
@@ -1,16 +1,14 @@
 /*****************************************************************************
- * RRDtool 1.3.7  Copyright by Tobi Oetiker, 1997-2009
+ * RRDtool 1.4.8  Copyright by Tobi Oetiker, 1997-2013
  *****************************************************************************
  * rrd_resize.c Alters size of an RRA
  *****************************************************************************
  * Initial version by Alex van den Bogaerdt
  *****************************************************************************/
 
-#include "rrd_tool.h"
-
-#ifdef WIN32
 #include <stdlib.h>
-#endif
+
+#include "rrd_tool.h"
 
 int rrd_resize(
     int argc,
@@ -23,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;
 
@@ -40,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 {
@@ -59,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);
@@ -87,16 +86,31 @@ 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 */
+
+    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,
@@ -104,7 +118,6 @@ int rrd_resize(
         rrd_free(&rrdnew);
         rrd_free(&rrdold);
         rrd_close(rrd_file);
-        rrd_close(rrd_out_file);
         return (-1);
     }
     if (rrd_lock(rrd_out_file) != 0) {
@@ -116,16 +129,7 @@ int rrd_resize(
         return (-1);
     }
 /*XXX: do one write for those parts of header that are unchanged */
-    if ((rrdnew.stat_head = (stat_head_t*)malloc(sizeof(stat_head_t))) == NULL) {
-        rrd_set_error("allocating stat_head for new RRD");
-        rrd_free(&rrdnew);
-        rrd_free(&rrdold);
-        rrd_close(rrd_file);
-        rrd_close(rrd_out_file);
-        return (-1);
-    }
-
-    if ((rrdnew.rra_ptr = (rra_ptr_t*)malloc(sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt)) == NULL) {
+    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);
@@ -134,71 +138,16 @@ int rrd_resize(
         return (-1);
     }
 
-    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);
-        rrd_close(rrd_out_file);
-        return (-1);
-    }
-     
-#ifndef WIN32
-    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;
     memcpy(rrdnew.rra_ptr,rrdold.rra_ptr,sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt);
-#else // WIN32
-       /*
-        * For windows Other fields also should be allocated. Crashes otherwise
-        */
 
-    if ((rrdnew.ds_def = (ds_def_t*)malloc(sizeof(ds_def_t) * rrdold.stat_head->ds_cnt)) == NULL) {
-        rrd_set_error("allocating ds_def for new RRD");
-        rrd_free(&rrdnew);
-        rrd_free(&rrdold);
-        rrd_close(rrd_file);
-        rrd_close(rrd_out_file);
-        return (-1);
-    }
-
-    if ((rrdnew.live_head = (live_head_t*)malloc(sizeof(live_head_t))) == NULL) {
-        rrd_set_error("allocating live_head for new RRD");
-        rrd_free(&rrdnew);
-        rrd_free(&rrdold);
-        rrd_close(rrd_file);
-        rrd_close(rrd_out_file);
-        return (-1);
-    }
-
-    if ((rrdnew.pdp_prep = (pdp_prep_t*)malloc(sizeof(pdp_prep_t))) == NULL) {
-        rrd_set_error("allocating pdp_prep for new RRD");
-        rrd_free(&rrdnew);
-        rrd_free(&rrdold);
-        rrd_close(rrd_file);
-        rrd_close(rrd_out_file);
-        return (-1);
-    }
-
-    if ((rrdnew.cdp_prep = (cdp_prep_t*)malloc(sizeof(cdp_prep_t))) == NULL) {
-        rrd_set_error("allocating cdp_prep 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));
-    memcpy(rrdnew.ds_def,rrdold.ds_def,sizeof(ds_def_t) * rrdold.stat_head->ds_cnt);
-    memcpy(rrdnew.rra_def,rrdold.rra_def,sizeof(rra_def_t) * rrdold.stat_head->rra_cnt);    
-    memcpy(rrdnew.live_head,rrdold.live_head,sizeof(live_head_t));
-    memcpy(rrdnew.pdp_prep,rrdold.pdp_prep,sizeof(pdp_prep_t));
-    memcpy(rrdnew.cdp_prep,rrdold.cdp_prep,sizeof(cdp_prep_t));
-    memcpy(rrdnew.rra_ptr,rrdold.rra_ptr,sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt);
-#endif // WIN32
 
     version = atoi(rrdold.stat_head->version);
     switch (version) {
@@ -212,6 +161,12 @@ int rrd_resize(
     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);
@@ -312,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,
@@ -333,6 +293,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);
 }