Code

do not close the filehandle to the resize file of there was a problem getting it...
[rrdtool-all.git] / program / src / rrd_resize.c
1 /*****************************************************************************
2  * RRDtool 1.3.9  Copyright by Tobi Oetiker, 1997-2009
3  *****************************************************************************
4  * rrd_resize.c Alters size of an RRA
5  *****************************************************************************
6  * Initial version by Alex van den Bogaerdt
7  *****************************************************************************/
9 #include "rrd_tool.h"
11 #ifdef WIN32
12 #include <stdlib.h>
13 #endif
15 int rrd_resize(
16     int argc,
17     char **argv)
18 {
19     char     *infilename, outfilename[11] = "resize.rrd";
20     rrd_t     rrdold, rrdnew;
21     rrd_value_t buffer;
22     int       version;
23     unsigned long l, rra;
24     long      modify;
25     unsigned long target_rra;
26     int       grow = 0, shrink = 0;
27     char     *endptr;
28     rrd_file_t *rrd_file, *rrd_out_file;
30     infilename = argv[1];
31     if (!strcmp(infilename, "resize.rrd")) {
32         rrd_set_error("resize.rrd is a reserved name");
33         return (-1);
34     }
35     if (argc != 5) {
36         rrd_set_error("wrong number of parameters");
37         return (-1);
38     }
40     target_rra = strtol(argv[2], &endptr, 0);
42     if (!strcmp(argv[3], "GROW"))
43         grow = 1;
44     else if (!strcmp(argv[3], "SHRINK"))
45         shrink = 1;
46     else {
47         rrd_set_error("I can only GROW or SHRINK");
48         return (-1);
49     }
51     modify = strtol(argv[4], &endptr, 0);
53     if ((modify < 1)) {
54         rrd_set_error("Please grow or shrink with at least 1 row");
55         return (-1);
56     }
58     if (shrink)
59         modify = -modify;
62     rrd_file = rrd_open(infilename, &rrdold, RRD_READWRITE | RRD_COPY);
63     if (rrd_file == NULL) {
64         rrd_free(&rrdold);
65         return (-1);
66     }
68     if (rrd_lock(rrd_file) != 0) {
69         rrd_set_error("could not lock original RRD");
70         rrd_free(&rrdold);
71         rrd_close(rrd_file);
72         return (-1);
73     }
76     if (target_rra >= rrdold.stat_head->rra_cnt) {
77         rrd_set_error("no such RRA in this RRD");
78         rrd_free(&rrdold);
79         rrd_close(rrd_file);
80         return (-1);
81     }
83     if (modify < 0)
84         if ((long) rrdold.rra_def[target_rra].row_cnt <= -modify) {
85             rrd_set_error("This RRA is not that big");
86             rrd_free(&rrdold);
87             rrd_close(rrd_file);
88             return (-1);
89         }
90     /* the size of the new file */
91     /* yes we are abusing the float cookie for this, aargh */
92     if ((rrdnew.stat_head = (stat_head_t*)calloc(1, sizeof(stat_head_t))) == NULL) {
93         rrd_set_error("allocating stat_head for new RRD");
94         rrd_free(&rrdold);
95         rrd_close(rrd_file);
96         return (-1);
97     }
98     rrdnew.stat_head->float_cookie = rrd_file->file_len +
99         (rrdold.stat_head->ds_cnt * sizeof(rrd_value_t) * modify);
100     rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_READWRITE | RRD_CREAT);
101     if (rrd_out_file == NULL) {
102         rrd_set_error("Can't create '%s': %s", outfilename,
103                       rrd_strerror(errno));
104         rrd_free(&rrdnew);
105         rrd_free(&rrdold);
106         rrd_close(rrd_file);
107         return (-1);
108     }
109     if (rrd_lock(rrd_out_file) != 0) {
110         rrd_set_error("could not lock new RRD");
111         rrd_free(&rrdnew);
112         rrd_free(&rrdold);
113         rrd_close(rrd_file);
114         rrd_close(rrd_out_file);
115         return (-1);
116     }
117 /*XXX: do one write for those parts of header that are unchanged */
118     if ((rrdnew.stat_head = (stat_head_t*)malloc(sizeof(stat_head_t))) == NULL) {
119         rrd_set_error("allocating stat_head for new RRD");
120         rrd_free(&rrdnew);
121         rrd_free(&rrdold);
122         rrd_close(rrd_file);
123         rrd_close(rrd_out_file);
124         return (-1);
125     }
127     if ((rrdnew.rra_ptr = (rra_ptr_t*)malloc(sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt)) == NULL) {
128         rrd_set_error("allocating rra_ptr for new RRD");
129         rrd_free(&rrdnew);
130         rrd_free(&rrdold);
131         rrd_close(rrd_file);
132         rrd_close(rrd_out_file);
133         return (-1);
134     }
136     if ((rrdnew.rra_def = (rra_def_t*)malloc(sizeof(rra_def_t) * rrdold.stat_head->rra_cnt)) == NULL) {
137         rrd_set_error("allocating rra_def for new RRD");
138         rrd_free(&rrdnew);
139         rrd_free(&rrdold);
140         rrd_close(rrd_file);
141         rrd_close(rrd_out_file);
142         return (-1);
143     }
144      
145     memcpy(rrdnew.stat_head,rrdold.stat_head,sizeof(stat_head_t));
146     rrdnew.ds_def = rrdold.ds_def;
148     /* we are going to free rrdold later on, so make sure its not still pointing to the new stuff */
149     rrdold.live_head = NULL;
150     memcpy(rrdnew.rra_def,rrdold.rra_def,sizeof(rra_def_t) * rrdold.stat_head->rra_cnt);      
151     rrdnew.live_head = rrdold.live_head;
152     rrdold.live_head = NULL;
153     rrdnew.pdp_prep = rrdold.pdp_prep;
154     rrdold.pdp_prep = NULL;
155     rrdnew.cdp_prep = rrdold.cdp_prep;
156     rrdold.cdp_prep = NULL;
157     memcpy(rrdnew.rra_ptr,rrdold.rra_ptr,sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt);
159     version = atoi(rrdold.stat_head->version);
160     switch (version) {
161     case 4:
162         break;        
163     case 3:
164         break;
165     case 1:
166         rrdnew.stat_head->version[3] = '3';
167         break;
168     default:
169         rrd_set_error("Do not know how to handle RRD version %s",
170                       rrdold.stat_head->version);
171         rrd_free(&rrdnew);
172         rrd_free(&rrdold);
173         rrd_close(rrd_file);
174         rrd_close(rrd_out_file);
175         return (-1);
176         break;
177     }
179 /* XXX: Error checking? */
180     rrd_write(rrd_out_file, rrdnew.stat_head, sizeof(stat_head_t) * 1);
181     rrd_write(rrd_out_file, rrdnew.ds_def,
182               sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt);
183     rrd_write(rrd_out_file, rrdnew.rra_def,
184               sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
185     rrd_write(rrd_out_file, rrdnew.live_head, sizeof(live_head_t) * 1);
186     rrd_write(rrd_out_file, rrdnew.pdp_prep,
187               sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt);
188     rrd_write(rrd_out_file, rrdnew.cdp_prep,
189               sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
190               rrdnew.stat_head->rra_cnt);
191     rrd_write(rrd_out_file, rrdnew.rra_ptr,
192               sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
194     /* Move the CDPs from the old to the new database.
195      ** This can be made (much) faster but isn't worth the effort. Clarity
196      ** is much more important.
197      */
199     /* Move data in unmodified RRAs
200      */
201     l = 0;
202     for (rra = 0; rra < target_rra; rra++) {
203         l += rrdnew.stat_head->ds_cnt * rrdnew.rra_def[rra].row_cnt;
204     }
205     while (l > 0) {
206         rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
207         rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
208         l--;
209     }
210     /* Move data in this RRA, either removing or adding some rows
211      */
212     if (modify > 0) {
213         /* Adding extra rows; insert unknown values just after the
214          ** current row number.
215          */
216         l = rrdnew.stat_head->ds_cnt *
217             (rrdnew.rra_ptr[target_rra].cur_row + 1);
218         while (l > 0) {
219             rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
220             rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
221             l--;
222         }
223         buffer = DNAN;
224         l = rrdnew.stat_head->ds_cnt * modify;
225         while (l > 0) {
226             rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
227             l--;
228         }
229     } else {
230         /* Removing rows. Normally this would be just after the cursor
231          ** however this may also mean that we wrap to the beginning of
232          ** the array.
233          */
234         signed long int remove_end = 0;
236         remove_end =
237             (rrdnew.rra_ptr[target_rra].cur_row -
238              modify) % rrdnew.rra_def[target_rra].row_cnt;
239         if (remove_end <=
240             (signed long int) rrdnew.rra_ptr[target_rra].cur_row) {
241             while (remove_end >= 0) {
242                 rrd_seek(rrd_file,
243                          sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
244                          SEEK_CUR);
245                 rrdnew.rra_ptr[target_rra].cur_row--;
246                 rrdnew.rra_def[target_rra].row_cnt--;
247                 remove_end--;
248                 modify++;
249             }
250             remove_end = rrdnew.rra_def[target_rra].row_cnt - 1;
251         }
252         for (l = 0; l <= rrdnew.rra_ptr[target_rra].cur_row; l++) {
253             unsigned int tmp;
255             for (tmp = 0; tmp < rrdnew.stat_head->ds_cnt; tmp++) {
256                 rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1);
257                 rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
258             }
259         }
260         while (modify < 0) {
261             rrd_seek(rrd_file,
262                      sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt,
263                      SEEK_CUR);
264             rrdnew.rra_def[target_rra].row_cnt--;
265             modify++;
266         }
267     }
268     /* Move the rest of the CDPs
269      */
270     while (1) {
271         if (rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1) <= 0)
272             break;
273         rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1);
274     }
275     rrdnew.rra_def[target_rra].row_cnt += modify;
276     rrd_seek(rrd_out_file,
277              sizeof(stat_head_t) +
278              sizeof(ds_def_t) * rrdnew.stat_head->ds_cnt, SEEK_SET);
279     rrd_write(rrd_out_file, rrdnew.rra_def,
280               sizeof(rra_def_t) * rrdnew.stat_head->rra_cnt);
281     rrd_seek(rrd_out_file, sizeof(live_head_t), SEEK_CUR);
282     rrd_seek(rrd_out_file, sizeof(pdp_prep_t) * rrdnew.stat_head->ds_cnt,
283              SEEK_CUR);
284     rrd_seek(rrd_out_file,
285              sizeof(cdp_prep_t) * rrdnew.stat_head->ds_cnt *
286              rrdnew.stat_head->rra_cnt, SEEK_CUR);
287     rrd_write(rrd_out_file, rrdnew.rra_ptr,
288               sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
289     rrd_close(rrd_file);    
290     rrd_close(rrd_out_file);    
291     rrd_free(&rrdold);
292     rrd_free(&rrdnew);
293     return (0);