fix from alex
[rrdtool.git] / src / rrd_resize.c
index 52c208b..32a501a 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * RRDtool 1.2.23  Copyright by Tobi Oetiker, 1997-2007
+ * RRDtool 1.3.2  Copyright by Tobi Oetiker, 1997-2008
  *****************************************************************************
  * rrd_resize.c Alters size of an RRA
  *****************************************************************************
@@ -55,18 +55,21 @@ 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);
         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);
         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);
@@ -82,6 +85,31 @@ int rrd_resize(
             return (-1);
         }
 
+    rrd_init(&rrdnew);
+    /* These need to be initialised before calling rrd_open() with 
+       the RRD_CREATE flag */
+    if ((rrdnew.stat_head = 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);
+    }
+
+    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));
+    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,
@@ -89,7 +117,7 @@ int rrd_resize(
         rrd_free(&rrdnew);
         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(&rrdold);
         rrd_close(rrd_file);
@@ -97,16 +125,30 @@ int rrd_resize(
         return (-1);
     }
 /*XXX: do one write for those parts of header that are unchanged */
-    rrdnew.stat_head = rrdold.stat_head;
+    if ((rrdnew.rra_ptr = 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:
@@ -115,8 +157,10 @@ int rrd_resize(
     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;
     }
@@ -158,19 +202,24 @@ 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);
             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
@@ -203,7 +252,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++;
@@ -230,12 +280,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_free(&rrdnew);
-    rrd_close(rrd_out_file);
-
     return (0);
 }