prepare for the release of rrdtool-1.2.11
[rrdtool.git] / src / rrd_resize.c
1 /*****************************************************************************
2  * RRDtool 1.2.11  Copyright by Tobi Oetiker, 1997-2005
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
12 rrd_resize(int argc, char **argv)
13 {
14     char             *infilename,outfilename[11]="resize.rrd";
15     FILE             *infile,*outfile;
16     rrd_t             rrdold,rrdnew;
17     rrd_value_t       buffer;
18     unsigned long     l,rra;
19     long              modify;
20     unsigned long     target_rra;
21     int               grow=0,shrink=0;
22     char             *endptr;
23
24     infilename=argv[1];
25     if (!strcmp(infilename,"resize.rrd")) {
26         rrd_set_error("resize.rrd is a reserved name");
27         return(-1);
28     }
29     if (argc!=5) {
30         rrd_set_error("wrong number of parameters");
31         return(-1);
32     }
33
34     target_rra=strtol(argv[2],&endptr,0);
35
36     if (!strcmp(argv[3],"GROW")) grow=1;
37     else if (!strcmp(argv[3],"SHRINK")) shrink=1;
38     else {
39         rrd_set_error("I can only GROW or SHRINK");
40         return(-1);
41     }
42
43     modify=strtol(argv[4],&endptr,0);
44
45     if ((modify<1)) {
46         rrd_set_error("Please grow or shrink with at least 1 row");
47         return(-1);
48     }
49
50     if (shrink) modify = -modify;
51
52
53     if (rrd_open(infilename, &infile, &rrdold, RRD_READWRITE)==-1) {
54         rrd_set_error("could not open RRD");
55         return(-1);
56     }
57     if (LockRRD(infile) != 0) {
58         rrd_set_error("could not lock original RRD");
59         rrd_free(&rrdold);
60         fclose(infile);
61         return(-1);
62     }
63
64     if (target_rra >= rrdold.stat_head->rra_cnt) {
65         rrd_set_error("no such RRA in this RRD");
66         rrd_free(&rrdold);
67         fclose(infile);
68         return(-1);
69     }
70
71     if (modify < 0)
72         if ((long)rrdold.rra_def[target_rra].row_cnt <= -modify) {
73             rrd_set_error("This RRA is not that big");
74             rrd_free(&rrdold);
75             fclose(infile);
76             return(-1);
77         }
78
79     rrdnew.stat_head = rrdold.stat_head;
80     rrdnew.ds_def    = rrdold.ds_def;
81     rrdnew.rra_def   = rrdold.rra_def;
82     rrdnew.live_head = rrdold.live_head;
83     rrdnew.pdp_prep  = rrdold.pdp_prep;
84     rrdnew.cdp_prep  = rrdold.cdp_prep;
85     rrdnew.rra_ptr   = rrdold.rra_ptr;
86
87     if ((outfile=fopen(outfilename,"wb"))==NULL) {
88         rrd_set_error("Can't create '%s'",outfilename);
89         return(-1);
90     }
91     if (LockRRD(outfile) != 0) {
92         rrd_set_error("could not lock new RRD");
93         rrd_free(&rrdold);
94         fclose(infile);
95         fclose(outfile);
96         return(-1);
97     }
98     fwrite(rrdnew.stat_head, sizeof(stat_head_t),1,outfile);
99     fwrite(rrdnew.ds_def,sizeof(ds_def_t),rrdnew.stat_head->ds_cnt,outfile);
100     fwrite(rrdnew.rra_def,sizeof(rra_def_t),rrdnew.stat_head->rra_cnt,outfile);
101     fwrite(rrdnew.live_head,sizeof(live_head_t),1,outfile);
102     fwrite(rrdnew.pdp_prep,sizeof(pdp_prep_t),rrdnew.stat_head->ds_cnt,outfile);
103     fwrite(rrdnew.cdp_prep,sizeof(cdp_prep_t),rrdnew.stat_head->ds_cnt*rrdnew.stat_head->rra_cnt,outfile);
104     fwrite(rrdnew.rra_ptr,sizeof(rra_ptr_t),rrdnew.stat_head->rra_cnt,outfile);
105
106     /* Move the CDPs from the old to the new database.
107     ** This can be made (much) faster but isn't worth the effort. Clarity
108     ** is much more important.
109     */
110
111     /* Move data in unmodified RRAs
112     */
113     l=0;
114     for (rra=0;rra<target_rra;rra++) {
115         l+=rrdnew.stat_head->ds_cnt * rrdnew.rra_def[rra].row_cnt;
116     }
117     while (l>0) {
118         fread(&buffer,sizeof(rrd_value_t),1,infile);
119         fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
120         l--;
121     }
122     /* Move data in this RRA, either removing or adding some rows
123     */
124     if (modify>0) {
125         /* Adding extra rows; insert unknown values just after the
126         ** current row number.
127         */
128         l = rrdnew.stat_head->ds_cnt * (rrdnew.rra_ptr[target_rra].cur_row+1);
129         while (l>0) {
130             fread(&buffer,sizeof(rrd_value_t),1,infile);
131             fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
132             l--;
133         }
134         buffer=DNAN;
135         l=rrdnew.stat_head->ds_cnt * modify;
136         while (l>0) {
137             fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
138             l--;
139         }
140     } else {
141         /* Removing rows. Normally this would be just after the cursor
142         ** however this may also mean that we wrap to the beginning of
143         ** the array.
144         */
145         signed long int remove_end=0;
146
147         remove_end=(rrdnew.rra_ptr[target_rra].cur_row-modify)%rrdnew.rra_def[target_rra].row_cnt;
148         if (remove_end <= (signed long int)rrdnew.rra_ptr[target_rra].cur_row) {
149             while (remove_end >= 0) {
150                 fseek(infile,sizeof(rrd_value_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
151                 rrdnew.rra_ptr[target_rra].cur_row--;
152                 rrdnew.rra_def[target_rra].row_cnt--;
153                 remove_end--;
154                 modify++;
155             }
156             remove_end=rrdnew.rra_def[target_rra].row_cnt-1;
157         }
158         for (l=0;l<=rrdnew.rra_ptr[target_rra].cur_row;l++) {
159             unsigned int tmp;
160             for (tmp=0;tmp<rrdnew.stat_head->ds_cnt;tmp++) {
161                 fread(&buffer,sizeof(rrd_value_t),1,infile);
162                 fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
163             }
164         }
165         while (modify<0) {
166             fseek(infile,sizeof(rrd_value_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
167             rrdnew.rra_def[target_rra].row_cnt--;
168             modify++;
169         }
170     }
171     /* Move the rest of the CDPs
172     */
173     while (1) {
174         fread(&buffer,sizeof(rrd_value_t),1,infile);
175         if (feof(infile))
176             break;
177         fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
178     }
179     rrdnew.rra_def[target_rra].row_cnt += modify;
180     fseek(outfile,sizeof(stat_head_t)+sizeof(ds_def_t)*rrdnew.stat_head->ds_cnt,SEEK_SET);
181     fwrite(rrdnew.rra_def,sizeof(rra_def_t),rrdnew.stat_head->rra_cnt, outfile);
182     fseek(outfile,sizeof(live_head_t),SEEK_CUR);
183     fseek(outfile,sizeof(pdp_prep_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
184     fseek(outfile,sizeof(cdp_prep_t)*rrdnew.stat_head->ds_cnt*rrdnew.stat_head->rra_cnt,SEEK_CUR);
185     fwrite(rrdnew.rra_ptr,sizeof(rra_ptr_t),rrdnew.stat_head->rra_cnt, outfile);
186     
187     fclose(outfile);
188     rrd_free(&rrdold);
189     fclose(infile);
190     return(0);
191 }