Code

make sure rrdold members are not pointing anywhere after asigning them to rrdnew...
[rrdtool-all.git] / program / src / rrd_resize.c
index 2d07c6549faa115da45a115518fcabd65f550ce7..2049982aa46bd1b0d1fb2d4aa3db3b46bd59c55e 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * RRDtool 1.3.3  Copyright by Tobi Oetiker, 1997-2008
+ * RRDtool 1.3.8  Copyright by Tobi Oetiker, 1997-2009
  *****************************************************************************
  * rrd_resize.c Alters size of an RRA
  *****************************************************************************
@@ -8,6 +8,10 @@
 
 #include "rrd_tool.h"
 
+#ifdef WIN32
+#include <stdlib.h>
+#endif
+
 int rrd_resize(
     int argc,
     char **argv)
@@ -60,6 +64,7 @@ int rrd_resize(
         rrd_free(&rrdold);
         return (-1);
     }
+
     if (rrd_lock(rrd_file) != 0) {
         rrd_set_error("could not lock original RRD");
         rrd_free(&rrdold);
@@ -67,6 +72,7 @@ int rrd_resize(
         return (-1);
     }
 
+
     if (target_rra >= rrdold.stat_head->rra_cnt) {
         rrd_set_error("no such RRA in this RRD");
         rrd_free(&rrdold);
@@ -83,7 +89,7 @@ int rrd_resize(
         }
     /* 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) {
+    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);
@@ -96,36 +102,77 @@ int rrd_resize(
         rrd_set_error("Can't create '%s': %s", outfilename,
                       rrd_strerror(errno));
         rrd_free(&rrdnew);
+        rrd_free(&rrdold);
+        rrd_close(rrd_file);
+        rrd_close(rrd_out_file);
         return (-1);
     }
     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);
     }
 /*XXX: do one write for those parts of header that are unchanged */
-    rrdnew.stat_head = rrdold.stat_head;
+    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) {
+        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);
+    }
+
+    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);
+    }
+     
+    memcpy(rrdnew.stat_head,rrdold.stat_head,sizeof(stat_head_t));
     rrdnew.ds_def = rrdold.ds_def;
-    rrdnew.rra_def = rrdold.rra_def;
+
+    /* we are going to free rrdold later on, so make sure its not still pointing to the new stuff */
+    rrdold.live_head = NULL;
+    memcpy(rrdnew.rra_def,rrdold.rra_def,sizeof(rra_def_t) * rrdold.stat_head->rra_cnt);      
     rrdnew.live_head = rrdold.live_head;
+    rrdold.live_head = NULL;
     rrdnew.pdp_prep = rrdold.pdp_prep;
+    rrdold.pdp_prep = NULL;
     rrdnew.cdp_prep = rrdold.cdp_prep;
-    rrdnew.rra_ptr = rrdold.rra_ptr;
+    rrdold.cdp_prep = NULL;
+    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:
         rrd_set_error("Do not know how to handle RRD version %s",
                       rrdold.stat_head->version);
-        rrd_close(rrd_file);
+        rrd_free(&rrdnew);
         rrd_free(&rrdold);
+        rrd_close(rrd_file);
+        rrd_close(rrd_out_file);
         return (-1);
         break;
     }
@@ -174,17 +221,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
@@ -245,11 +287,9 @@ 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);
-
+    rrd_close(rrd_file);    
+    rrd_close(rrd_out_file);    
     rrd_free(&rrdold);
-    rrd_close(rrd_file);
-
-    rrd_close(rrd_out_file);
-
+    rrd_free(&rrdnew);
     return (0);
 }