fewer double frees ... -- slamb slamb.org
[rrdtool.git] / src / rrd_restore.c
1 /*****************************************************************************
2  * RRDtool 1.2.12  Copyright by Tobi Oetiker, 1997-2005
3  *****************************************************************************
4  * rrd_restore.c  creates new rrd from data dumped by rrd_dump.c
5  *****************************************************************************/
6
7 #include "rrd_tool.h"
8 #include "rrd_rpncalc.h"
9 #include <fcntl.h>
10
11 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
12 #include <io.h>
13 #define open _open
14 #define close _close
15 #endif
16
17 /* Prototypes */
18
19 void xml_lc(char*);
20 int skip(char **);
21 int eat_tag(char **, char *);
22 int read_tag(char **, char *, char *, void *);
23 int xml2rrd(char*, rrd_t*, char);
24 int rrd_write(char *, rrd_t *, char);
25 void parse_patch1028_RRA_params(char **buf, rrd_t *rrd, int rra_index);
26 void parse_patch1028_CDP_params(char **buf, rrd_t *rrd, int rra_index, int ds_index);
27 void parse_FAILURES_history(char **buf, rrd_t *rrd, int rra_index, int ds_index);
28
29 /* convert all occurrences of <BlaBlaBla> to <blablabla> */
30
31 void xml_lc(char* buf){
32   int intag=0;
33   while((*buf)){
34     if (intag ==0 && (*buf) == '<') {
35       intag = 1;
36     }
37     else if (intag ==1 && (*buf) == '>') {
38       intag = 0;
39       continue;
40     } else  if (intag ==1) {
41       *buf = tolower(*buf);
42     }
43     buf++;    
44   }
45 }
46
47 int skip(char **buf){
48   char *ptr;  
49   ptr=(*buf);
50   do {
51     (*buf)=ptr;
52     while((*(ptr+1)) && ((*ptr)==' ' ||  (*ptr)=='\r' || (*ptr)=='\n' || (*ptr)=='\t')) ptr++;
53     if (strncmp(ptr,"<!--",4) == 0) {
54       ptr= strstr(ptr,"-->");
55       if (ptr) ptr+=3; else {
56         rrd_set_error("Dangling Comment");
57         (*buf) = NULL;
58         return -1;
59       }
60     }
61   } while ((*buf)!=ptr);  
62   return 1;
63 }
64
65 int eat_tag(char **buf, char *tag){ 
66   if ((*buf)==NULL) return -1;   /* fall though clause */
67
68   rrd_clear_error();
69   skip(buf);
70   if ((**buf)=='<' 
71       && strncmp((*buf)+1,tag,strlen(tag)) == 0 
72       && *((*buf)+strlen(tag)+1)=='>') {
73     (*buf) += strlen(tag)+2;
74   }
75   else {
76     rrd_set_error("No <%s> tag found",tag);
77     (*buf) = NULL;
78     return -1;
79   }
80   skip(buf);
81   return 1;
82 }
83
84 int read_tag(char **buf, char *tag, char *format, void *value){
85     char *end_tag;
86     int matches;
87     if ((*buf)==NULL) return -1;   /* fall though clause */
88     rrd_clear_error();
89     if (eat_tag(buf,tag)==1){
90         char *temp;
91         temp = (*buf);
92         while(*((*buf)+1) && (*(*buf) != '<')) (*buf)++; /*find start of endtag*/
93         *(*buf) = '\0';
94         matches =sscanf(temp,format,value);
95         *(*buf) = '<';
96         end_tag = malloc((strlen(tag)+2)*sizeof(char));
97         sprintf(end_tag,"/%s",tag);
98         eat_tag(buf,end_tag);
99         free(end_tag);
100         if (matches == 0 && strcmp(format,"%lf") == 0)
101             (*((double* )(value))) = DNAN;
102         if (matches != 1)       return 0;       
103         return 1;
104     }
105     return -1;
106 }
107
108
109 /* parse the data stored in buf and return a filled rrd structure */
110 int xml2rrd(char* buf, rrd_t* rrd, char rc){
111   /* pass 1 identify number of RRAs  */
112   char *ptr,*ptr2,*ptr3; /* walks thought the buffer */
113   long rows=0,mempool=0,i=0;
114   int rra_index;
115   int input_version;
116   xml_lc(buf); /* lets lowercase all active parts of the xml */
117   ptr=buf;
118   ptr2=buf;
119   ptr3=buf;
120   /* start with an RRD tag */
121
122   eat_tag(&ptr,"rrd");
123   /* allocate static header */
124   if((rrd->stat_head = calloc(1,sizeof(stat_head_t)))==NULL){
125     rrd_set_error("allocating rrd.stat_head");
126     return -1;    
127   };
128
129   strcpy(rrd->stat_head->cookie,RRD_COOKIE);
130   read_tag(&ptr,"version","%4[0-9]",rrd->stat_head->version);
131   input_version = atoi(rrd->stat_head->version);
132   /* added primitive version checking */
133   if (input_version > atoi(RRD_VERSION) || input_version < 1)
134   {
135     rrd_set_error("Incompatible file version, detected version %s. This is not supported by the version %s restore tool.\n",
136                   rrd -> stat_head -> version, RRD_VERSION );
137     free(rrd -> stat_head); 
138     rrd->stat_head = NULL; 
139     return -1;
140   }
141   /* make sure we output the right version */
142   strcpy(rrd->stat_head->version,RRD_VERSION);
143
144   /*  if (atoi(rrd -> stat_head -> version) < 2) 
145   {
146     rrd_set_error("Can only restore version >= 2 (Not %s). Dump your old rrd using a current rrdtool dump.",  rrd -> stat_head -> version );
147     return -1;
148   } */
149
150   rrd->stat_head->float_cookie = FLOAT_COOKIE;
151   rrd->stat_head->ds_cnt = 0;
152   rrd->stat_head->rra_cnt = 0;
153   read_tag(&ptr,"step","%lu",&(rrd->stat_head->pdp_step));
154
155   /* allocate live head */
156   if((rrd->live_head = calloc(1,sizeof(live_head_t)))==NULL){
157     rrd_set_error("allocating rrd.live_head");
158     return -1;    
159   }
160   read_tag(&ptr,"lastupdate","%lu",&(rrd->live_head->last_up));
161
162   /* Data Source Definition Part */
163   ptr2 = ptr;
164   while (eat_tag(&ptr2,"ds") == 1){
165       rrd->stat_head->ds_cnt++;
166       if((rrd->ds_def = rrd_realloc(rrd->ds_def,rrd->stat_head->ds_cnt*sizeof(ds_def_t)))==NULL){
167           rrd_set_error("allocating rrd.ds_def");
168           return -1;
169       };
170       /* clean out memory to make sure no data gets stored from previous tasks */
171       memset(&(rrd->ds_def[rrd->stat_head->ds_cnt-1]), 0, sizeof(ds_def_t));
172       if((rrd->pdp_prep = rrd_realloc(rrd->pdp_prep,rrd->stat_head->ds_cnt
173                                   *sizeof(pdp_prep_t)))==NULL){
174         rrd_set_error("allocating pdp_prep");
175         return(-1);
176       }
177       /* clean out memory to make sure no data gets stored from previous tasks */
178       memset(&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1]), 0, sizeof(pdp_prep_t));
179
180       read_tag(&ptr2,"name",DS_NAM_FMT,rrd->ds_def[rrd->stat_head->ds_cnt-1].ds_nam);
181
182       read_tag(&ptr2,"type",DST_FMT,rrd->ds_def[rrd->stat_head->ds_cnt-1].dst);
183       /* test for valid type */
184       if( (int)dst_conv(rrd->ds_def[rrd->stat_head->ds_cnt-1].dst) == -1) return -1;      
185
186           if (dst_conv(rrd->ds_def[rrd->stat_head->ds_cnt-1].dst) != DST_CDEF)
187           {
188       read_tag(&ptr2,"minimal_heartbeat","%lu",
189                &(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_mrhb_cnt].u_cnt));
190       read_tag(&ptr2,"min","%lf",&(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_min_val].u_val));
191       read_tag(&ptr2,"max","%lf",&(rrd->ds_def[rrd->stat_head->ds_cnt-1].par[DS_max_val].u_val));
192           } else { /* DST_CDEF */
193                  char buffer[1024];
194              read_tag(&ptr2,"cdef","%s",buffer);
195                  parseCDEF_DS(buffer,rrd,rrd -> stat_head -> ds_cnt - 1);
196           }
197
198       read_tag(&ptr2,"last_ds","%30s",rrd->pdp_prep[rrd->stat_head->ds_cnt-1].last_ds);
199       read_tag(&ptr2,"value","%lf",&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1].scratch[PDP_val].u_val));
200       read_tag(&ptr2,"unknown_sec","%lu",&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1].scratch[PDP_unkn_sec_cnt].u_cnt));      
201       eat_tag(&ptr2,"/ds");
202       ptr=ptr2;
203   }
204   
205   ptr2 = ptr;
206   while (eat_tag(&ptr2,"rra") == 1){
207       rrd->stat_head->rra_cnt++;
208
209       /* allocate and reset rra definition areas */
210       if((rrd->rra_def = rrd_realloc(rrd->rra_def,rrd->stat_head->rra_cnt*sizeof(rra_def_t)))==NULL){
211           rrd_set_error("allocating rra_def"); return -1; }      
212       memset(&(rrd->rra_def[rrd->stat_head->rra_cnt-1]), 0, sizeof(rra_def_t));
213
214       /* allocate and reset consolidation point areas */
215       if((rrd->cdp_prep = rrd_realloc(rrd->cdp_prep,
216                                   rrd->stat_head->rra_cnt
217                                   *rrd->stat_head->ds_cnt*sizeof(cdp_prep_t)))==NULL){
218           rrd_set_error("allocating cdp_prep"); return -1; }
219
220       memset(&(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rrd->stat_head->rra_cnt-1)]), 
221              0, rrd->stat_head->ds_cnt*sizeof(cdp_prep_t));
222
223       
224       read_tag(&ptr2,"cf",CF_NAM_FMT,rrd->rra_def[rrd->stat_head->rra_cnt-1].cf_nam);
225       /* test for valid type */
226       if( (int)cf_conv(rrd->rra_def[rrd->stat_head->rra_cnt-1].cf_nam) == -1) return -1;
227
228       read_tag(&ptr2,"pdp_per_row","%lu",&(rrd->rra_def[rrd->stat_head->rra_cnt-1].pdp_cnt));
229       /* support to read RRA parameters */
230       rra_index = rrd->stat_head->rra_cnt - 1;
231       if ( input_version < 2 ){
232          read_tag(&ptr2, "xff","%lf",
233             &(rrd->rra_def[rra_index].par[RRA_cdp_xff_val].u_val));
234       } else {
235         eat_tag(&ptr2, "params");
236         skip(&ptr2);
237         /* backwards compatibility w/ old patch */
238       if (strncmp(ptr2, "<value>",7) == 0) {
239           parse_patch1028_RRA_params(&ptr2,rrd,rra_index); 
240       } else {
241       switch(cf_conv(rrd -> rra_def[rra_index].cf_nam)) {
242       case CF_HWPREDICT:
243          read_tag(&ptr2, "hw_alpha", "%lf", 
244             &(rrd->rra_def[rra_index].par[RRA_hw_alpha].u_val));
245          read_tag(&ptr2, "hw_beta", "%lf", 
246             &(rrd->rra_def[rra_index].par[RRA_hw_beta].u_val));
247          read_tag(&ptr2, "dependent_rra_idx", "%lu", 
248             &(rrd->rra_def[rra_index].par[RRA_dependent_rra_idx].u_cnt));
249          break;
250       case CF_SEASONAL:
251       case CF_DEVSEASONAL:
252          read_tag(&ptr2, "seasonal_gamma", "%lf", 
253             &(rrd->rra_def[rra_index].par[RRA_seasonal_gamma].u_val));
254          read_tag(&ptr2, "seasonal_smooth_idx", "%lu", 
255             &(rrd->rra_def[rra_index].par[RRA_seasonal_smooth_idx].u_cnt));
256          read_tag(&ptr2, "dependent_rra_idx", "%lu", 
257             &(rrd->rra_def[rra_index].par[RRA_dependent_rra_idx].u_cnt));
258          break;
259       case CF_FAILURES:
260          read_tag(&ptr2, "delta_pos", "%lf", 
261             &(rrd->rra_def[rra_index].par[RRA_delta_pos].u_val));
262          read_tag(&ptr2, "delta_neg", "%lf", 
263             &(rrd->rra_def[rra_index].par[RRA_delta_neg].u_val));
264          read_tag(&ptr2, "window_len", "%lu", 
265             &(rrd->rra_def[rra_index].par[RRA_window_len].u_cnt));
266          read_tag(&ptr2, "failure_threshold", "%lu", 
267             &(rrd->rra_def[rra_index].par[RRA_failure_threshold].u_cnt));
268          /* fall thru */
269       case CF_DEVPREDICT:
270          read_tag(&ptr2, "dependent_rra_idx", "%lu", 
271             &(rrd->rra_def[rra_index].par[RRA_dependent_rra_idx].u_cnt));
272          break;
273       case CF_AVERAGE:
274       case CF_MAXIMUM:
275       case CF_MINIMUM:
276       case CF_LAST:
277       default:
278          read_tag(&ptr2, "xff","%lf",
279             &(rrd->rra_def[rra_index].par[RRA_cdp_xff_val].u_val));
280       }
281       }
282       eat_tag(&ptr2, "/params");
283    }
284
285
286       eat_tag(&ptr2,"cdp_prep");
287       for(i=0;i< (int)rrd->stat_head->ds_cnt;i++)
288       {
289       eat_tag(&ptr2,"ds");
290       /* support to read CDP parameters */
291       rra_index = rrd->stat_head->rra_cnt-1; 
292       skip(&ptr2);
293       if ( input_version < 2 ){
294           rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)+i].scratch[CDP_primary_val].u_val = 0.0;
295           rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)+i].scratch[CDP_secondary_val].u_val = 0.0;
296           read_tag(&ptr2,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
297                *(rra_index) +i].scratch[CDP_val].u_val));
298           read_tag(&ptr2,"unknown_datapoints","%lu",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
299               *(rra_index) +i].scratch[CDP_unkn_pdp_cnt].u_cnt));
300       } else {
301
302       if (strncmp(ptr2, "<value>",7) == 0) {
303          parse_patch1028_CDP_params(&ptr2,rrd,rra_index,i);
304       } else {
305          read_tag(&ptr2, "primary_value","%lf",
306                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
307                +i].scratch[CDP_primary_val].u_val));
308          read_tag(&ptr2, "secondary_value","%lf",
309                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
310                +i].scratch[CDP_secondary_val].u_val));
311          switch(cf_conv(rrd->rra_def[rra_index].cf_nam)) {
312          case CF_HWPREDICT:
313             read_tag(&ptr2,"intercept","%lf", 
314                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
315                +i].scratch[CDP_hw_intercept].u_val));
316             read_tag(&ptr2,"last_intercept","%lf", 
317                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
318                +i].scratch[CDP_hw_last_intercept].u_val));
319             read_tag(&ptr2,"slope","%lf", 
320                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
321                +i].scratch[CDP_hw_slope].u_val));
322             read_tag(&ptr2,"last_slope","%lf", 
323                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
324                +i].scratch[CDP_hw_last_slope].u_val));
325             read_tag(&ptr2,"nan_count","%lu", 
326                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
327                +i].scratch[CDP_null_count].u_cnt));
328             read_tag(&ptr2,"last_nan_count","%lu", 
329                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
330                +i].scratch[CDP_last_null_count].u_cnt));
331             break;
332          case CF_SEASONAL:
333          case CF_DEVSEASONAL:
334             read_tag(&ptr2,"seasonal","%lf", 
335                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
336                +i].scratch[CDP_hw_seasonal].u_val));
337             read_tag(&ptr2,"last_seasonal","%lf", 
338                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
339                +i].scratch[CDP_hw_last_seasonal].u_val));
340             read_tag(&ptr2,"init_flag","%lu", 
341                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
342                +i].scratch[CDP_init_seasonal].u_cnt));
343             break;
344          case CF_DEVPREDICT:
345             break;
346          case CF_FAILURES:
347             parse_FAILURES_history(&ptr2,rrd,rra_index,i); 
348             break;
349          case CF_AVERAGE:
350          case CF_MAXIMUM:
351          case CF_MINIMUM:
352          case CF_LAST:
353          default:
354             read_tag(&ptr2,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
355                *(rra_index) +i].scratch[CDP_val].u_val));
356             read_tag(&ptr2,"unknown_datapoints","%lu",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
357                *(rra_index) +i].scratch[CDP_unkn_pdp_cnt].u_cnt));
358             break;
359          }
360       }
361       }
362       eat_tag(&ptr2,"/ds");
363       }
364       eat_tag(&ptr2,"/cdp_prep");
365       rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt=0;
366       eat_tag(&ptr2,"database");
367       ptr3 = ptr2;      
368       while (eat_tag(&ptr3,"row") == 1){
369         
370           if(mempool==0){
371             mempool = 1000;
372             if((rrd->rrd_value = rrd_realloc(rrd->rrd_value,
373                                          (rows+mempool)*(rrd->stat_head->ds_cnt)
374                                          *sizeof(rrd_value_t)))==NULL) {
375               rrd_set_error("allocating rrd_values"); return -1; }
376           }
377           rows++;
378           mempool--;
379           rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt++;
380           for(i=0;i< (int)rrd->stat_head->ds_cnt;i++){
381
382                   rrd_value_t  * value = &(rrd->rrd_value[(rows-1)*rrd->stat_head->ds_cnt+i]);
383
384                   read_tag(&ptr3,"v","%lf", value);
385                   
386                   if (
387                           (rc == 1)                     /* do we have to check for the ranges */
388                           &&
389                       (!isnan(*value))  /* not a NAN value */
390                       &&
391                           (dst_conv(rrd->ds_def[i].dst) != DST_CDEF)
392                           &&
393                       (                                 /* min defined and in the range ? */
394                           (!isnan(rrd->ds_def[i].par[DS_min_val].u_val) 
395                                 && (*value < rrd->ds_def[i].par[DS_min_val].u_val)) 
396                           ||                            /* max defined and in the range ? */
397                           (!isnan(rrd->ds_def[i].par[DS_max_val].u_val) 
398                                 && (*value > rrd->ds_def[i].par[DS_max_val].u_val))
399                       )
400                   ) {
401                       fprintf (stderr, "out of range found [ds: %lu], [value : %0.10e]\n", i, *value);
402                       *value = DNAN;
403                   }
404           }
405           eat_tag(&ptr3,"/row");                  
406           ptr2=ptr3;
407       }
408       eat_tag(&ptr2,"/database");
409       eat_tag(&ptr2,"/rra");                  
410       ptr=ptr2;
411   }  
412   eat_tag(&ptr,"/rrd");
413
414   if((rrd->rra_ptr = calloc(1,sizeof(rra_ptr_t)*rrd->stat_head->rra_cnt)) == NULL) {
415       rrd_set_error("allocating rra_ptr");
416       return(-1);
417   }
418
419   for(i=0; i < (int)rrd->stat_head->rra_cnt; i++) {
420           /* last row in the xml file is the most recent; as
421            * rrd_update increments the current row pointer, set cur_row
422            * here to the last row. */
423       rrd->rra_ptr[i].cur_row = rrd->rra_def[i].row_cnt-1;
424   }
425   if (ptr==NULL)
426       return -1;
427   return 1;
428 }
429   
430     
431
432
433
434 /* create and empty rrd file according to the specs given */
435
436 int
437 rrd_write(char *file_name, rrd_t *rrd, char force_overwrite)
438 {
439     unsigned long    i,ii,val_cnt;
440     FILE             *rrd_file=NULL;
441     int                 fdflags;
442     int                 fd;
443
444     if (strcmp("-",file_name)==0){
445       rrd_file= stdout;
446     } else {
447 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
448       fdflags = O_RDWR|O_BINARY|O_CREAT;
449 #else
450       fdflags = O_WRONLY|O_CREAT;
451 #endif            
452       if (force_overwrite == 0) {
453         fdflags |= O_EXCL;
454       }
455       fd = open(file_name,fdflags,0666);
456       if (fd == -1 || (rrd_file = fdopen(fd,"wb")) == NULL) {
457         rrd_set_error("creating '%s': %s",file_name,rrd_strerror(errno));
458         if (fd != -1)
459           close(fd);
460         return(-1);
461       }
462     }
463     fwrite(rrd->stat_head,
464            sizeof(stat_head_t), 1, rrd_file);
465
466     fwrite(rrd->ds_def,
467            sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
468
469     fwrite(rrd->rra_def,
470            sizeof(rra_def_t), rrd->stat_head->rra_cnt, rrd_file);
471
472     fwrite(rrd->live_head, sizeof(live_head_t),1, rrd_file);
473
474     fwrite( rrd->pdp_prep, sizeof(pdp_prep_t),rrd->stat_head->ds_cnt,rrd_file);
475     
476     fwrite( rrd->cdp_prep, sizeof(cdp_prep_t),rrd->stat_head->rra_cnt*
477             rrd->stat_head->ds_cnt,rrd_file);
478     fwrite( rrd->rra_ptr, sizeof(rra_ptr_t), rrd->stat_head->rra_cnt,rrd_file);
479
480
481
482     /* calculate the number of rrd_values to dump */
483     val_cnt=0;
484     for(i=0; i <  rrd->stat_head->rra_cnt; i++)
485         for(ii=0; ii <  rrd->rra_def[i].row_cnt * rrd->stat_head->ds_cnt;ii++)
486             val_cnt++;
487     fwrite( rrd->rrd_value, sizeof(rrd_value_t),val_cnt,rrd_file);
488
489     /* lets see if we had an error */
490     if(ferror(rrd_file)){
491         rrd_set_error("a file error occurred while creating '%s'",file_name);
492         fclose(rrd_file);       
493         return(-1);
494     }
495     
496     fclose(rrd_file);    
497     return 0;
498 }
499
500
501 int
502 rrd_restore(int argc, char **argv) 
503 {
504     rrd_t          rrd;
505     char          *buf;
506         char                    rc = 0;
507         char                    force_overwrite = 0;    
508
509     /* init rrd clean */
510     optind = 0; opterr = 0;  /* initialize getopt */
511         while (1) {
512                 static struct option long_options[] =
513                 {
514                         {"range-check",      no_argument, 0,  'r'},
515                         {"force-overwrite",  no_argument, 0,  'f'},
516                         {0,0,0,0}
517                 };
518                 int option_index = 0;
519                 int opt;
520                 
521                 
522                 opt = getopt_long(argc, argv, "rf", long_options, &option_index);
523                 
524                 if (opt == EOF)
525                         break;
526                 
527                 switch(opt) {
528                 case 'r':
529                         rc=1;
530                         break;
531                 case 'f':
532                         force_overwrite=1;
533                         break;
534                 default:
535                         rrd_set_error("usage rrdtool %s [--range-check|-r] [--force-overwrite/-f]  file.xml file.rrd",argv[0]);
536                         return -1;
537                         break;
538                 }
539     }
540
541     if (argc-optind != 2) {
542                 rrd_set_error("usage rrdtool %s [--range-check/-r] [--force-overwrite/-f] file.xml file.rrd",argv[0]);
543                 return -1;
544     }
545         
546     if (readfile(argv[optind],&buf,0)==-1){
547       return -1;
548     }
549
550     rrd_init(&rrd);
551
552     if (xml2rrd(buf,&rrd,rc)==-1) {
553         rrd_free(&rrd);
554         free(buf);
555         return -1;
556     }
557
558     free(buf);
559
560     if(rrd_write(argv[optind+1],&rrd,force_overwrite)==-1){
561         rrd_free(&rrd); 
562         return -1;      
563     };
564     rrd_free(&rrd);    
565     return 0;
566 }
567
568 /* a backwards compatibility routine that will parse the RRA params section
569  * generated by the aberrant patch to 1.0.28. */
570
571 void
572 parse_patch1028_RRA_params(char **buf, rrd_t *rrd, int rra_index)
573 {
574    int i;
575    for (i = 0; i < MAX_RRA_PAR_EN; i++)
576    {
577    if (i == RRA_dependent_rra_idx ||
578        i == RRA_seasonal_smooth_idx ||
579        i == RRA_failure_threshold)
580       read_tag(buf, "value","%lu",
581          &(rrd->rra_def[rra_index].par[i].u_cnt));
582    else
583       read_tag(buf, "value","%lf",
584          &(rrd->rra_def[rra_index].par[i].u_val));
585    }
586 }
587
588 /* a backwards compatibility routine that will parse the CDP params section
589  * generated by the aberrant patch to 1.0.28. */
590 void
591 parse_patch1028_CDP_params(char **buf, rrd_t *rrd, int rra_index, int ds_index)
592 {
593    int ii;
594    for (ii = 0; ii < MAX_CDP_PAR_EN; ii++)
595    {
596    if (cf_conv(rrd->rra_def[rra_index].cf_nam) == CF_FAILURES ||
597        ii == CDP_unkn_pdp_cnt ||
598        ii == CDP_null_count ||
599        ii == CDP_last_null_count)
600    {
601       read_tag(buf,"value","%lu",
602        &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index) + ds_index].scratch[ii].u_cnt));
603    } else {
604       read_tag(buf,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt*
605        (rra_index) + ds_index].scratch[ii].u_val));
606    }
607    }
608 }
609
610 void
611 parse_FAILURES_history(char **buf, rrd_t *rrd, int rra_index, int ds_index)
612 {
613    char history[MAX_FAILURES_WINDOW_LEN + 1];
614    char *violations_array;
615    unsigned short i;
616
617    /* 28 = MAX_FAILURES_WINDOW_LEN */ 
618    read_tag(buf, "history", "%28[0-1]", history);
619    violations_array = (char*) rrd -> cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
620       + ds_index].scratch;
621    
622    for (i = 0; i < rrd -> rra_def[rra_index].par[RRA_window_len].u_cnt; ++i)
623       violations_array[i] = (history[i] == '1') ? 1 : 0;
624
625 }