~ubuntu-branches/ubuntu/maverick/rrdtool/maverick

« back to all changes in this revision

Viewing changes to src/rrd_resize.c

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2010-07-22 08:07:01 UTC
  • mfrom: (1.2.8 upstream) (3.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100722080701-k46mgdfz6euxwqsm
Tags: 1.4.3-1ubuntu1
* Merge from debian unstable, Remaining changes:
  - debian/control: Don't build against ruby1.9 as we don't want
    it in main.
* require libdbi >= 0.8.3 to prevent aborts when using dbi datasources

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*****************************************************************************
2
 
 * RRDtool 1.3.8  Copyright by Tobi Oetiker, 1997-2009
 
2
 * RRDtool 1.4.3  Copyright by Tobi Oetiker, 1997-2010
3
3
 *****************************************************************************
4
4
 * rrd_resize.c Alters size of an RRA
5
5
 *****************************************************************************
6
6
 * Initial version by Alex van den Bogaerdt
7
7
 *****************************************************************************/
8
8
 
 
9
#include <stdlib.h>
 
10
 
9
11
#include "rrd_tool.h"
10
12
 
11
 
#ifdef WIN32
12
 
#include <stdlib.h>
13
 
#endif
14
 
 
15
13
int rrd_resize(
16
14
    int argc,
17
15
    char **argv)
59
57
        modify = -modify;
60
58
 
61
59
 
 
60
    rrd_init(&rrdold);
62
61
    rrd_file = rrd_open(infilename, &rrdold, RRD_READWRITE | RRD_COPY);
63
62
    if (rrd_file == NULL) {
64
63
        rrd_free(&rrdold);
87
86
            rrd_close(rrd_file);
88
87
            return (-1);
89
88
        }
90
 
    /* the size of the new file */
91
 
    /* yes we are abusing the float cookie for this, aargh */
 
89
 
 
90
    rrd_init(&rrdnew);
 
91
    /* These need to be initialised before calling rrd_open() with 
 
92
       the RRD_CREATE flag */
 
93
 
92
94
    if ((rrdnew.stat_head = (stat_head_t*)calloc(1, sizeof(stat_head_t))) == NULL) {
93
95
        rrd_set_error("allocating stat_head for new RRD");
94
96
        rrd_free(&rrdold);
95
97
        rrd_close(rrd_file);
96
98
        return (-1);
97
99
    }
98
 
    rrdnew.stat_head->float_cookie = rrd_file->file_len +
99
 
        (rrdold.stat_head->ds_cnt * sizeof(rrd_value_t) * modify);
 
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
 
100
114
    rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_READWRITE | RRD_CREAT);
101
115
    if (rrd_out_file == NULL) {
102
116
        rrd_set_error("Can't create '%s': %s", outfilename,
116
130
        return (-1);
117
131
    }
118
132
/*XXX: do one write for those parts of header that are unchanged */
119
 
    if ((rrdnew.stat_head = (stat_head_t*)malloc(sizeof(stat_head_t))) == NULL) {
120
 
        rrd_set_error("allocating stat_head for new RRD");
121
 
        rrd_free(&rrdnew);
122
 
        rrd_free(&rrdold);
123
 
        rrd_close(rrd_file);
124
 
        rrd_close(rrd_out_file);
125
 
        return (-1);
126
 
    }
127
 
 
128
 
    if ((rrdnew.rra_ptr = (rra_ptr_t*)malloc(sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt)) == NULL) {
 
133
    if ((rrdnew.rra_ptr = (rra_ptr_t *)malloc(sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt)) == NULL) {
129
134
        rrd_set_error("allocating rra_ptr for new RRD");
130
135
        rrd_free(&rrdnew);
131
136
        rrd_free(&rrdold);
134
139
        return (-1);
135
140
    }
136
141
 
137
 
    if ((rrdnew.rra_def = (rra_def_t*)malloc(sizeof(rra_def_t) * rrdold.stat_head->rra_cnt)) == NULL) {
138
 
        rrd_set_error("allocating rra_def for new RRD");
139
 
        rrd_free(&rrdnew);
140
 
        rrd_free(&rrdold);
141
 
        rrd_close(rrd_file);
142
 
        rrd_close(rrd_out_file);
143
 
        return (-1);
144
 
    }
145
 
     
146
 
#ifndef WIN32
147
 
    memcpy(rrdnew.stat_head,rrdold.stat_head,sizeof(stat_head_t));
 
142
    /* Put this back the way it was so that the rest of the algorithm
 
143
       below remains unchanged, it will be corrected later */
 
144
    rrdnew.rra_def[target_rra].row_cnt -= modify;
 
145
 
148
146
    rrdnew.ds_def = rrdold.ds_def;
149
 
    memcpy(rrdnew.rra_def,rrdold.rra_def,sizeof(rra_def_t) * rrdold.stat_head->rra_cnt);    
150
147
    rrdnew.live_head = rrdold.live_head;
151
148
    rrdnew.pdp_prep = rrdold.pdp_prep;
152
149
    rrdnew.cdp_prep = rrdold.cdp_prep;
153
150
    memcpy(rrdnew.rra_ptr,rrdold.rra_ptr,sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt);
154
 
#else // WIN32
155
 
        /*
156
 
         * For windows Other fields also should be allocated. Crashes otherwise
157
 
         */
158
 
 
159
 
    if ((rrdnew.ds_def = (ds_def_t*)malloc(sizeof(ds_def_t) * rrdold.stat_head->ds_cnt)) == NULL) {
160
 
        rrd_set_error("allocating ds_def for new RRD");
161
 
        rrd_free(&rrdnew);
162
 
        rrd_free(&rrdold);
163
 
        rrd_close(rrd_file);
164
 
        rrd_close(rrd_out_file);
165
 
        return (-1);
166
 
    }
167
 
 
168
 
    if ((rrdnew.live_head = (live_head_t*)malloc(sizeof(live_head_t))) == NULL) {
169
 
        rrd_set_error("allocating live_head for new RRD");
170
 
        rrd_free(&rrdnew);
171
 
        rrd_free(&rrdold);
172
 
        rrd_close(rrd_file);
173
 
        rrd_close(rrd_out_file);
174
 
        return (-1);
175
 
    }
176
 
 
177
 
    if ((rrdnew.pdp_prep = (pdp_prep_t*)malloc(sizeof(pdp_prep_t))) == NULL) {
178
 
        rrd_set_error("allocating pdp_prep for new RRD");
179
 
        rrd_free(&rrdnew);
180
 
        rrd_free(&rrdold);
181
 
        rrd_close(rrd_file);
182
 
        rrd_close(rrd_out_file);
183
 
        return (-1);
184
 
    }
185
 
 
186
 
    if ((rrdnew.cdp_prep = (cdp_prep_t*)malloc(sizeof(cdp_prep_t))) == NULL) {
187
 
        rrd_set_error("allocating cdp_prep for new RRD");
188
 
        rrd_free(&rrdnew);
189
 
        rrd_free(&rrdold);
190
 
        rrd_close(rrd_file);
191
 
        rrd_close(rrd_out_file);
192
 
        return (-1);
193
 
    }
194
 
    memcpy(rrdnew.stat_head,rrdold.stat_head,sizeof(stat_head_t));
195
 
    memcpy(rrdnew.ds_def,rrdold.ds_def,sizeof(ds_def_t) * rrdold.stat_head->ds_cnt);
196
 
    memcpy(rrdnew.rra_def,rrdold.rra_def,sizeof(rra_def_t) * rrdold.stat_head->rra_cnt);    
197
 
    memcpy(rrdnew.live_head,rrdold.live_head,sizeof(live_head_t));
198
 
    memcpy(rrdnew.pdp_prep,rrdold.pdp_prep,sizeof(pdp_prep_t));
199
 
    memcpy(rrdnew.cdp_prep,rrdold.cdp_prep,sizeof(cdp_prep_t));
200
 
    memcpy(rrdnew.rra_ptr,rrdold.rra_ptr,sizeof(rra_ptr_t) * rrdold.stat_head->rra_cnt);
201
 
#endif // WIN32
 
151
 
202
152
 
203
153
    version = atoi(rrdold.stat_head->version);
204
154
    switch (version) {