- if ((long)rrdold.rra_def[target_rra].row_cnt <= -modify) {
- rrd_set_error("This RRA is not that big");
- rrd_free(&rrdold);
- fclose(infile);
- return(-1);
- }
-
- rrdnew.stat_head = rrdold.stat_head;
- rrdnew.ds_def = rrdold.ds_def;
- rrdnew.rra_def = rrdold.rra_def;
+ if ((long) rrdold.rra_def[target_rra].row_cnt <= -modify) {
+ rrd_set_error("This RRA is not that big");
+ rrd_free(&rrdold);
+ rrd_close(rrd_file);
+ return (-1);
+ }
+ /* the size of the new file */
+ /* yes we are abusing the float cookie for this, aargh */
+ if ((rrdnew.stat_head = calloc(1, sizeof(stat_head_t))) == NULL) {
+ rrd_set_error("allocating stat_head for new RRD");
+ rrd_free(&rrdold);
+ rrd_close(rrd_file);
+ return (-1);
+ }
+ rrdnew.stat_head->float_cookie = rrd_file->file_len +
+ (rrdold.stat_head->ds_cnt * sizeof(rrd_value_t) * modify);
+ rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_READWRITE | RRD_CREAT);
+ if (rrd_out_file == NULL) {
+ rrd_set_error("Can't create '%s': %s", outfilename,
+ rrd_strerror(errno));
+ rrd_free(&rrdnew);
+ return (-1);
+ }
+ if (rrd_lock(rrd_out_file) != 0) {
+ rrd_set_error("could not lock new RRD");
+ rrd_free(&rrdold);
+ rrd_close(rrd_file);
+ rrd_close(rrd_out_file);
+ return (-1);
+ }
+/*XXX: do one write for those parts of header that are unchanged */
+ if ((rrdnew.stat_head = malloc(sizeof(stat_head_t))) == NULL) {
+ rrd_set_error("allocating stat_head for new RRD");
+ rrd_free(&rrdnew);
+ rrd_free(&rrdold);
+ rrd_close(rrd_file);
+ rrd_close(rrd_out_file);
+ return (-1);
+ }
+
+ if ((rrdnew.rra_ptr = malloc(sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt)) == NULL) {
+ rrd_set_error("allocating rra_ptr for new RRD");
+ rrd_free(&rrdnew);
+ rrd_free(&rrdold);
+ rrd_close(rrd_file);
+ rrd_close(rrd_out_file);
+ return (-1);
+ }
+
+ if ((rrdnew.rra_def = malloc(sizeof(rra_def_t) * rrdold.stat_head->rra_cnt)) == NULL) {
+ rrd_set_error("allocating rra_def for new RRD");
+ rrd_free(&rrdnew);
+ rrd_free(&rrdold);
+ rrd_close(rrd_file);
+ rrd_close(rrd_out_file);
+ return (-1);
+ }
+
+ memcpy(rrdnew.stat_head,rrdold.stat_head,sizeof(stat_head_t));
+ rrdnew.ds_def = rrdold.ds_def;
+ memcpy(rrdnew.rra_def,rrdold.rra_def,sizeof(rra_def_t) * rrdold.stat_head->rra_cnt);