Code

* rrd_resize fixed so that it does NOT alter the original file (mmap side effect)
authoroetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa>
Sat, 4 Oct 2008 13:34:54 +0000 (13:34 +0000)
committeroetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa>
Sat, 4 Oct 2008 13:34:54 +0000 (13:34 +0000)
* rrd_resize can can deal with version 4 files, so let it

git-svn-id: svn://svn.oetiker.ch/rrdtool/trunk@1571 a5681a0c-68f1-0310-ab6d-d61299d08faa

program/src/rrd_resize.c
tutorial/lisa2008/rrd-by-example/ex/hw-demo.pl

index ac853184fa91460b5a58becb7a01eb054d26a904..57adbf02ffeb5676915feac648b1aee8fd8182bf 100644 (file)
@@ -60,6 +60,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 +68,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);
@@ -106,16 +108,46 @@ 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.stat_head = 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 = 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 = 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;
+    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;
-    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:
@@ -124,8 +156,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;
     }
@@ -245,11 +279,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);
 }
index baff57f3e4140b6fa345c99c01698bf973e431e8..2633f394621a6068e8d17b0effd8e17819293166 100755 (executable)
@@ -27,13 +27,10 @@ sub create {
           'DS:out:GAUGE:'.($p{step}*1.5).':U:U',
           'RRA:AVERAGE:0.5:1:'.$p{rows},
           'RRA:HWPREDICT:'.$p{rows}.':'.$p{alpha}.':'.$p{beta}.':'.$p{period};            
-    # it seems that resize is a bit broken ... will have to look into this
     system $R,'resize','hw-demo.rrd',5,'GROW',$p{rows};
-    system $R,'dump','hw-demo.rrd','x';
-    system $R,'restore','x','x.rrd';
-    rename 'x.rrd','hw-demo.rrd';
-    unlink 'x';
+    rename 'resize.rrd','hw-demo.rrd';
     system $R,'update','hw-demo.rrd',@updates;
+exit 1;
     return ($start,$end);
 }