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