Code

prepare for the release of rrdtool-1.3.8
[rrdtool-all.git] / program / src / rrd_resize.c
index 803c961bb77726d03898c78569b6452b162d3f37..771209f438f37d679b65a97e3e7db234bcb31a63 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * RRDtool 1.3.4  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)
@@ -85,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);
@@ -98,17 +102,21 @@ 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 */
-    if ((rrdnew.stat_head = malloc(sizeof(stat_head_t))) == NULL) {
+    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);
@@ -117,7 +125,7 @@ int rrd_resize(
         return (-1);
     }
 
-    if ((rrdnew.rra_ptr = 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);
@@ -126,7 +134,7 @@ int rrd_resize(
         return (-1);
     }
 
-    if ((rrdnew.rra_def = malloc(sizeof(rra_def_t) * rrdold.stat_head->rra_cnt)) == NULL) {
+    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);
@@ -135,6 +143,7 @@ int rrd_resize(
         return (-1);
     }
      
+#ifndef WIN32
     memcpy(rrdnew.stat_head,rrdold.stat_head,sizeof(stat_head_t));
     rrdnew.ds_def = rrdold.ds_def;
     memcpy(rrdnew.rra_def,rrdold.rra_def,sizeof(rra_def_t) * rrdold.stat_head->rra_cnt);    
@@ -142,7 +151,54 @@ int rrd_resize(
     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) {
@@ -151,7 +207,7 @@ 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",
@@ -208,17 +264,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