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