Initial revision
[rrdtool.git] / src / rrd_resize.c
1 /*****************************************************************************
2  * RRDtool 1.0.33  Copyright Tobias Oetiker, 1997 - 2000
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("you must have at least one row in the RRA");
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     if ((rrdold.rra_def[target_rra].row_cnt+modify)<0) {
71         rrd_set_error("This RRA is not that big");
72         rrd_free(&rrdold);
73         fclose(infile);
74         return(-1);
75     }
76
77     rrdnew.stat_head = rrdold.stat_head;
78     rrdnew.ds_def    = rrdold.ds_def;
79     rrdnew.rra_def   = rrdold.rra_def;
80     rrdnew.live_head = rrdold.live_head;
81     rrdnew.pdp_prep  = rrdold.pdp_prep;
82     rrdnew.cdp_prep  = rrdold.cdp_prep;
83     rrdnew.rra_ptr   = rrdold.rra_ptr;
84
85     if ((outfile=fopen(outfilename,"wb"))==NULL) {
86         rrd_set_error("Can't create '%s'",outfilename);
87         return(-1);
88     }
89     if (LockRRD(outfile) != 0) {
90         rrd_set_error("could not lock new RRD");
91         rrd_free(&rrdold);
92         fclose(infile);
93         fclose(outfile);
94         return(-1);
95     }
96     fwrite(rrdnew.stat_head, sizeof(stat_head_t),1,outfile);
97     fwrite(rrdnew.ds_def,sizeof(ds_def_t),rrdnew.stat_head->ds_cnt,outfile);
98     fwrite(rrdnew.rra_def,sizeof(rra_def_t),rrdnew.stat_head->rra_cnt,outfile);
99     fwrite(rrdnew.live_head,sizeof(live_head_t),1,outfile);
100     fwrite(rrdnew.pdp_prep,sizeof(pdp_prep_t),rrdnew.stat_head->ds_cnt,outfile);
101     fwrite(rrdnew.cdp_prep,sizeof(cdp_prep_t),rrdnew.stat_head->ds_cnt*rrdnew.stat_head->rra_cnt,outfile);
102     fwrite(rrdnew.rra_ptr,sizeof(rra_ptr_t),rrdnew.stat_head->rra_cnt,outfile);
103
104     /* Move the CDPs from the old to the new database.
105     ** This can be made (much) faster but isn't worth the efford. Clarity
106     ** is much more important.
107     */
108
109     /* Move data in unmodified RRAs
110     */
111     l=0;
112     for (rra=0;rra<target_rra;rra++) {
113         l+=rrdnew.stat_head->ds_cnt * rrdnew.rra_def[rra].row_cnt;
114     }
115     while (l>0) {
116         fread(&buffer,sizeof(rrd_value_t),1,infile);
117         fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
118         l--;
119     }
120     /* Move data in this RRA, either removing or adding some rows
121     */
122     if (modify>0) {
123         /* Adding extra rows; insert unknown values just after the
124         ** current row number.
125         */
126         l = rrdnew.stat_head->ds_cnt * (rrdnew.rra_ptr[target_rra].cur_row+1);
127         while (l>0) {
128             fread(&buffer,sizeof(rrd_value_t),1,infile);
129             fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
130             l--;
131         }
132         buffer=DNAN;
133         l=rrdnew.stat_head->ds_cnt * modify;
134         while (l>0) {
135             fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
136             l--;
137         }
138     } else {
139         /* Removing rows. Normally this would be just after the cursor
140         ** however this may also mean that we wrap to the beginning of
141         ** the array.
142         */
143         signed long int remove_end=0;
144
145         remove_end=(rrdnew.rra_ptr[target_rra].cur_row-modify)%rrdnew.rra_def[target_rra].row_cnt;
146         if (remove_end <= rrdnew.rra_ptr[target_rra].cur_row) {
147             while (remove_end >= 0) {
148                 fseek(infile,sizeof(rrd_value_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
149                 rrdnew.rra_ptr[target_rra].cur_row--;
150                 rrdnew.rra_def[target_rra].row_cnt--;
151                 remove_end--;
152                 modify++;
153             }
154             remove_end=rrdnew.rra_def[target_rra].row_cnt-1;
155         }
156         for (l=0;l<=rrdnew.rra_ptr[target_rra].cur_row;l++) {
157             unsigned int tmp;
158             for (tmp=0;tmp<rrdnew.stat_head->ds_cnt;tmp++) {
159                 fread(&buffer,sizeof(rrd_value_t),1,infile);
160                 fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
161             }
162         }
163         while (modify<0) {
164             fseek(infile,sizeof(rrd_value_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
165             rrdnew.rra_def[target_rra].row_cnt--;
166             modify++;
167         }
168     }
169     /* Move the rest of the CDPs
170     */
171     while (!(feof(infile))) {
172         fread(&buffer,sizeof(rrd_value_t),1,infile);
173         fwrite(&buffer,sizeof(rrd_value_t),1,outfile);
174     }
175     rrdnew.rra_def[target_rra].row_cnt += modify;
176     fseek(outfile,sizeof(stat_head_t)+sizeof(ds_def_t)*rrdnew.stat_head->ds_cnt,SEEK_SET);
177     fwrite(rrdnew.rra_def,sizeof(rra_def_t),rrdnew.stat_head->rra_cnt, outfile);
178     fseek(outfile,sizeof(live_head_t),SEEK_CUR);
179     fseek(outfile,sizeof(pdp_prep_t)*rrdnew.stat_head->ds_cnt,SEEK_CUR);
180     fseek(outfile,sizeof(cdp_prep_t)*rrdnew.stat_head->ds_cnt*rrdnew.stat_head->rra_cnt,SEEK_CUR);
181     fwrite(rrdnew.rra_ptr,sizeof(rra_ptr_t),rrdnew.stat_head->rra_cnt, outfile);
182     
183     fclose(outfile);
184     rrd_free(&rrdold);
185     fclose(infile);
186     return(0);
187 }