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