fix for debian bug 359071 ... reporting the long cdef in the error message was too...
[rrdtool.git] / src / rrd_restore.c
1 /*****************************************************************************
2  * RRDtool 1.2.13  Copyright by Tobi Oetiker, 1997-2006
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","%1000s",buffer);
195                  parseCDEF_DS(buffer,rrd,rrd -> stat_head -> ds_cnt - 1);
196                  if (rrd_test_error()) return -1;
197           }
198
199       read_tag(&ptr2,"last_ds","%30s",rrd->pdp_prep[rrd->stat_head->ds_cnt-1].last_ds);
200       read_tag(&ptr2,"value","%lf",&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1].scratch[PDP_val].u_val));
201       read_tag(&ptr2,"unknown_sec","%lu",&(rrd->pdp_prep[rrd->stat_head->ds_cnt-1].scratch[PDP_unkn_sec_cnt].u_cnt));      
202       eat_tag(&ptr2,"/ds");
203       ptr=ptr2;
204   }
205   
206   ptr2 = ptr;
207   while (eat_tag(&ptr2,"rra") == 1){
208       rrd->stat_head->rra_cnt++;
209
210       /* allocate and reset rra definition areas */
211       if((rrd->rra_def = rrd_realloc(rrd->rra_def,rrd->stat_head->rra_cnt*sizeof(rra_def_t)))==NULL){
212           rrd_set_error("allocating rra_def"); return -1; }      
213       memset(&(rrd->rra_def[rrd->stat_head->rra_cnt-1]), 0, sizeof(rra_def_t));
214
215       /* allocate and reset consolidation point areas */
216       if((rrd->cdp_prep = rrd_realloc(rrd->cdp_prep,
217                                   rrd->stat_head->rra_cnt
218                                   *rrd->stat_head->ds_cnt*sizeof(cdp_prep_t)))==NULL){
219           rrd_set_error("allocating cdp_prep"); return -1; }
220
221       memset(&(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rrd->stat_head->rra_cnt-1)]), 
222              0, rrd->stat_head->ds_cnt*sizeof(cdp_prep_t));
223
224       
225       read_tag(&ptr2,"cf",CF_NAM_FMT,rrd->rra_def[rrd->stat_head->rra_cnt-1].cf_nam);
226       /* test for valid type */
227       if( (int)cf_conv(rrd->rra_def[rrd->stat_head->rra_cnt-1].cf_nam) == -1) return -1;
228
229       read_tag(&ptr2,"pdp_per_row","%lu",&(rrd->rra_def[rrd->stat_head->rra_cnt-1].pdp_cnt));
230       /* support to read RRA parameters */
231       rra_index = rrd->stat_head->rra_cnt - 1;
232       if ( input_version < 2 ){
233          read_tag(&ptr2, "xff","%lf",
234             &(rrd->rra_def[rra_index].par[RRA_cdp_xff_val].u_val));
235       } else {
236         eat_tag(&ptr2, "params");
237         skip(&ptr2);
238         /* backwards compatibility w/ old patch */
239       if (strncmp(ptr2, "<value>",7) == 0) {
240           parse_patch1028_RRA_params(&ptr2,rrd,rra_index); 
241       } else {
242       switch(cf_conv(rrd -> rra_def[rra_index].cf_nam)) {
243       case CF_HWPREDICT:
244          read_tag(&ptr2, "hw_alpha", "%lf", 
245             &(rrd->rra_def[rra_index].par[RRA_hw_alpha].u_val));
246          read_tag(&ptr2, "hw_beta", "%lf", 
247             &(rrd->rra_def[rra_index].par[RRA_hw_beta].u_val));
248          read_tag(&ptr2, "dependent_rra_idx", "%lu", 
249             &(rrd->rra_def[rra_index].par[RRA_dependent_rra_idx].u_cnt));
250          break;
251       case CF_SEASONAL:
252       case CF_DEVSEASONAL:
253          read_tag(&ptr2, "seasonal_gamma", "%lf", 
254             &(rrd->rra_def[rra_index].par[RRA_seasonal_gamma].u_val));
255          read_tag(&ptr2, "seasonal_smooth_idx", "%lu", 
256             &(rrd->rra_def[rra_index].par[RRA_seasonal_smooth_idx].u_cnt));
257          read_tag(&ptr2, "dependent_rra_idx", "%lu", 
258             &(rrd->rra_def[rra_index].par[RRA_dependent_rra_idx].u_cnt));
259          break;
260       case CF_FAILURES:
261          read_tag(&ptr2, "delta_pos", "%lf", 
262             &(rrd->rra_def[rra_index].par[RRA_delta_pos].u_val));
263          read_tag(&ptr2, "delta_neg", "%lf", 
264             &(rrd->rra_def[rra_index].par[RRA_delta_neg].u_val));
265          read_tag(&ptr2, "window_len", "%lu", 
266             &(rrd->rra_def[rra_index].par[RRA_window_len].u_cnt));
267          read_tag(&ptr2, "failure_threshold", "%lu", 
268             &(rrd->rra_def[rra_index].par[RRA_failure_threshold].u_cnt));
269          /* fall thru */
270       case CF_DEVPREDICT:
271          read_tag(&ptr2, "dependent_rra_idx", "%lu", 
272             &(rrd->rra_def[rra_index].par[RRA_dependent_rra_idx].u_cnt));
273          break;
274       case CF_AVERAGE:
275       case CF_MAXIMUM:
276       case CF_MINIMUM:
277       case CF_LAST:
278       default:
279          read_tag(&ptr2, "xff","%lf",
280             &(rrd->rra_def[rra_index].par[RRA_cdp_xff_val].u_val));
281       }
282       }
283       eat_tag(&ptr2, "/params");
284    }
285
286
287       eat_tag(&ptr2,"cdp_prep");
288       for(i=0;i< (int)rrd->stat_head->ds_cnt;i++)
289       {
290       eat_tag(&ptr2,"ds");
291       /* support to read CDP parameters */
292       rra_index = rrd->stat_head->rra_cnt-1; 
293       skip(&ptr2);
294       if ( input_version < 2 ){
295           rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)+i].scratch[CDP_primary_val].u_val = 0.0;
296           rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)+i].scratch[CDP_secondary_val].u_val = 0.0;
297           read_tag(&ptr2,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
298                *(rra_index) +i].scratch[CDP_val].u_val));
299           read_tag(&ptr2,"unknown_datapoints","%lu",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
300               *(rra_index) +i].scratch[CDP_unkn_pdp_cnt].u_cnt));
301       } else {
302
303       if (strncmp(ptr2, "<value>",7) == 0) {
304          parse_patch1028_CDP_params(&ptr2,rrd,rra_index,i);
305       } else {
306          read_tag(&ptr2, "primary_value","%lf",
307                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
308                +i].scratch[CDP_primary_val].u_val));
309          read_tag(&ptr2, "secondary_value","%lf",
310                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
311                +i].scratch[CDP_secondary_val].u_val));
312          switch(cf_conv(rrd->rra_def[rra_index].cf_nam)) {
313          case CF_HWPREDICT:
314             read_tag(&ptr2,"intercept","%lf", 
315                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
316                +i].scratch[CDP_hw_intercept].u_val));
317             read_tag(&ptr2,"last_intercept","%lf", 
318                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
319                +i].scratch[CDP_hw_last_intercept].u_val));
320             read_tag(&ptr2,"slope","%lf", 
321                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
322                +i].scratch[CDP_hw_slope].u_val));
323             read_tag(&ptr2,"last_slope","%lf", 
324                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
325                +i].scratch[CDP_hw_last_slope].u_val));
326             read_tag(&ptr2,"nan_count","%lu", 
327                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
328                +i].scratch[CDP_null_count].u_cnt));
329             read_tag(&ptr2,"last_nan_count","%lu", 
330                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
331                +i].scratch[CDP_last_null_count].u_cnt));
332             break;
333          case CF_SEASONAL:
334          case CF_DEVSEASONAL:
335             read_tag(&ptr2,"seasonal","%lf", 
336                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
337                +i].scratch[CDP_hw_seasonal].u_val));
338             read_tag(&ptr2,"last_seasonal","%lf", 
339                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
340                +i].scratch[CDP_hw_last_seasonal].u_val));
341             read_tag(&ptr2,"init_flag","%lu", 
342                &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
343                +i].scratch[CDP_init_seasonal].u_cnt));
344             break;
345          case CF_DEVPREDICT:
346             break;
347          case CF_FAILURES:
348             parse_FAILURES_history(&ptr2,rrd,rra_index,i); 
349             break;
350          case CF_AVERAGE:
351          case CF_MAXIMUM:
352          case CF_MINIMUM:
353          case CF_LAST:
354          default:
355             read_tag(&ptr2,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
356                *(rra_index) +i].scratch[CDP_val].u_val));
357             read_tag(&ptr2,"unknown_datapoints","%lu",&(rrd->cdp_prep[rrd->stat_head->ds_cnt
358                *(rra_index) +i].scratch[CDP_unkn_pdp_cnt].u_cnt));
359             break;
360          }
361       }
362       }
363       eat_tag(&ptr2,"/ds");
364       }
365       eat_tag(&ptr2,"/cdp_prep");
366       rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt=0;
367       eat_tag(&ptr2,"database");
368       ptr3 = ptr2;      
369       while (eat_tag(&ptr3,"row") == 1){
370         
371           if(mempool==0){
372             mempool = 1000;
373             if((rrd->rrd_value = rrd_realloc(rrd->rrd_value,
374                                          (rows+mempool)*(rrd->stat_head->ds_cnt)
375                                          *sizeof(rrd_value_t)))==NULL) {
376               rrd_set_error("allocating rrd_values"); return -1; }
377           }
378           rows++;
379           mempool--;
380           rrd->rra_def[rrd->stat_head->rra_cnt-1].row_cnt++;
381           for(i=0;i< (int)rrd->stat_head->ds_cnt;i++){
382
383                   rrd_value_t  * value = &(rrd->rrd_value[(rows-1)*rrd->stat_head->ds_cnt+i]);
384
385                   read_tag(&ptr3,"v","%lf", value);
386                   
387                   if (
388                           (rc == 1)                     /* do we have to check for the ranges */
389                           &&
390                       (!isnan(*value))  /* not a NAN value */
391                       &&
392                           (dst_conv(rrd->ds_def[i].dst) != DST_CDEF)
393                           &&
394                       (                                 /* min defined and in the range ? */
395                           (!isnan(rrd->ds_def[i].par[DS_min_val].u_val) 
396                                 && (*value < rrd->ds_def[i].par[DS_min_val].u_val)) 
397                           ||                            /* max defined and in the range ? */
398                           (!isnan(rrd->ds_def[i].par[DS_max_val].u_val) 
399                                 && (*value > rrd->ds_def[i].par[DS_max_val].u_val))
400                       )
401                   ) {
402                       fprintf (stderr, "out of range found [ds: %lu], [value : %0.10e]\n", i, *value);
403                       *value = DNAN;
404                   }
405           }
406           eat_tag(&ptr3,"/row");                  
407           ptr2=ptr3;
408       }
409       eat_tag(&ptr2,"/database");
410       eat_tag(&ptr2,"/rra");                  
411       ptr=ptr2;
412   }  
413   eat_tag(&ptr,"/rrd");
414
415   if((rrd->rra_ptr = calloc(1,sizeof(rra_ptr_t)*rrd->stat_head->rra_cnt)) == NULL) {
416       rrd_set_error("allocating rra_ptr");
417       return(-1);
418   }
419
420   for(i=0; i < (int)rrd->stat_head->rra_cnt; i++) {
421           /* last row in the xml file is the most recent; as
422            * rrd_update increments the current row pointer, set cur_row
423            * here to the last row. */
424       rrd->rra_ptr[i].cur_row = rrd->rra_def[i].row_cnt-1;
425   }
426   if (ptr==NULL)
427       return -1;
428   return 1;
429 }
430   
431     
432
433
434
435 /* create and empty rrd file according to the specs given */
436
437 int
438 rrd_write(char *file_name, rrd_t *rrd, char force_overwrite)
439 {
440     unsigned long    i,ii,val_cnt;
441     FILE             *rrd_file=NULL;
442     int                 fdflags;
443     int                 fd;
444
445     if (strcmp("-",file_name)==0){
446       rrd_file= stdout;
447     } else {
448 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
449       fdflags = O_RDWR|O_BINARY|O_CREAT;
450 #else
451       fdflags = O_WRONLY|O_CREAT;
452 #endif            
453       if (force_overwrite == 0) {
454         fdflags |= O_EXCL;
455       }
456       fd = open(file_name,fdflags,0666);
457       if (fd == -1 || (rrd_file = fdopen(fd,"wb")) == NULL) {
458         rrd_set_error("creating '%s': %s",file_name,rrd_strerror(errno));
459         if (fd != -1)
460           close(fd);
461         return(-1);
462       }
463     }
464     fwrite(rrd->stat_head,
465            sizeof(stat_head_t), 1, rrd_file);
466
467     fwrite(rrd->ds_def,
468            sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file);
469
470     fwrite(rrd->rra_def,
471            sizeof(rra_def_t), rrd->stat_head->rra_cnt, rrd_file);
472
473     fwrite(rrd->live_head, sizeof(live_head_t),1, rrd_file);
474
475     fwrite( rrd->pdp_prep, sizeof(pdp_prep_t),rrd->stat_head->ds_cnt,rrd_file);
476     
477     fwrite( rrd->cdp_prep, sizeof(cdp_prep_t),rrd->stat_head->rra_cnt*
478             rrd->stat_head->ds_cnt,rrd_file);
479     fwrite( rrd->rra_ptr, sizeof(rra_ptr_t), rrd->stat_head->rra_cnt,rrd_file);
480
481
482
483     /* calculate the number of rrd_values to dump */
484     val_cnt=0;
485     for(i=0; i <  rrd->stat_head->rra_cnt; i++)
486         for(ii=0; ii <  rrd->rra_def[i].row_cnt * rrd->stat_head->ds_cnt;ii++)
487             val_cnt++;
488     fwrite( rrd->rrd_value, sizeof(rrd_value_t),val_cnt,rrd_file);
489
490     /* lets see if we had an error */
491     if(ferror(rrd_file)){
492         rrd_set_error("a file error occurred while creating '%s'",file_name);
493         fclose(rrd_file);       
494         return(-1);
495     }
496     
497     fclose(rrd_file);    
498     return 0;
499 }
500
501
502 int
503 rrd_restore(int argc, char **argv) 
504 {
505     rrd_t          rrd;
506     char          *buf;
507         char                    rc = 0;
508         char                    force_overwrite = 0;    
509
510     /* init rrd clean */
511     optind = 0; opterr = 0;  /* initialize getopt */
512         while (1) {
513                 static struct option long_options[] =
514                 {
515                         {"range-check",      no_argument, 0,  'r'},
516                         {"force-overwrite",  no_argument, 0,  'f'},
517                         {0,0,0,0}
518                 };
519                 int option_index = 0;
520                 int opt;
521                 
522                 
523                 opt = getopt_long(argc, argv, "rf", long_options, &option_index);
524                 
525                 if (opt == EOF)
526                         break;
527                 
528                 switch(opt) {
529                 case 'r':
530                         rc=1;
531                         break;
532                 case 'f':
533                         force_overwrite=1;
534                         break;
535                 default:
536                         rrd_set_error("usage rrdtool %s [--range-check|-r] [--force-overwrite/-f]  file.xml file.rrd",argv[0]);
537                         return -1;
538                         break;
539                 }
540     }
541
542     if (argc-optind != 2) {
543                 rrd_set_error("usage rrdtool %s [--range-check/-r] [--force-overwrite/-f] file.xml file.rrd",argv[0]);
544                 return -1;
545     }
546         
547     if (readfile(argv[optind],&buf,0)==-1){
548       return -1;
549     }
550
551     rrd_init(&rrd);
552
553     if (xml2rrd(buf,&rrd,rc)==-1) {
554         rrd_free(&rrd);
555         free(buf);
556         return -1;
557     }
558
559     free(buf);
560
561     if(rrd_write(argv[optind+1],&rrd,force_overwrite)==-1){
562         rrd_free(&rrd); 
563         return -1;      
564     };
565     rrd_free(&rrd);    
566     return 0;
567 }
568
569 /* a backwards compatibility routine that will parse the RRA params section
570  * generated by the aberrant patch to 1.0.28. */
571
572 void
573 parse_patch1028_RRA_params(char **buf, rrd_t *rrd, int rra_index)
574 {
575    int i;
576    for (i = 0; i < MAX_RRA_PAR_EN; i++)
577    {
578    if (i == RRA_dependent_rra_idx ||
579        i == RRA_seasonal_smooth_idx ||
580        i == RRA_failure_threshold)
581       read_tag(buf, "value","%lu",
582          &(rrd->rra_def[rra_index].par[i].u_cnt));
583    else
584       read_tag(buf, "value","%lf",
585          &(rrd->rra_def[rra_index].par[i].u_val));
586    }
587 }
588
589 /* a backwards compatibility routine that will parse the CDP params section
590  * generated by the aberrant patch to 1.0.28. */
591 void
592 parse_patch1028_CDP_params(char **buf, rrd_t *rrd, int rra_index, int ds_index)
593 {
594    int ii;
595    for (ii = 0; ii < MAX_CDP_PAR_EN; ii++)
596    {
597    if (cf_conv(rrd->rra_def[rra_index].cf_nam) == CF_FAILURES ||
598        ii == CDP_unkn_pdp_cnt ||
599        ii == CDP_null_count ||
600        ii == CDP_last_null_count)
601    {
602       read_tag(buf,"value","%lu",
603        &(rrd->cdp_prep[rrd->stat_head->ds_cnt*(rra_index) + ds_index].scratch[ii].u_cnt));
604    } else {
605       read_tag(buf,"value","%lf",&(rrd->cdp_prep[rrd->stat_head->ds_cnt*
606        (rra_index) + ds_index].scratch[ii].u_val));
607    }
608    }
609 }
610
611 void
612 parse_FAILURES_history(char **buf, rrd_t *rrd, int rra_index, int ds_index)
613 {
614    char history[MAX_FAILURES_WINDOW_LEN + 1];
615    char *violations_array;
616    unsigned short i;
617
618    /* 28 = MAX_FAILURES_WINDOW_LEN */ 
619    read_tag(buf, "history", "%28[0-1]", history);
620    violations_array = (char*) rrd -> cdp_prep[rrd->stat_head->ds_cnt*(rra_index)
621       + ds_index].scratch;
622    
623    for (i = 0; i < rrd -> rra_def[rra_index].par[RRA_window_len].u_cnt; ++i)
624       violations_array[i] = (history[i] == '1') ? 1 : 0;
625
626 }