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