diff --git a/src/rrd_fetch.c b/src/rrd_fetch.c
index 87c3cd6789b66b6f10b87e645a7bebfb9910627e..917e982703cf0966bfd860d1771fba66b01ae8a9 100644 (file)
--- a/src/rrd_fetch.c
+++ b/src/rrd_fetch.c
/*****************************************************************************
- * RRDtool 1.2.23 Copyright by Tobi Oetiker, 1997-2007
+ * RRDtool 1.3rc4 Copyright by Tobi Oetiker, 1997-2008
*****************************************************************************
* rrd_fetch.c read date from an rrd to use for further processing
*****************************************************************************
char ***ds_namv, /* names of data sources */
rrd_value_t **data)
{ /* two dimensional array containing the data */
-
-
long step_tmp = 1;
time_t start_tmp = 0, end_tmp = 0;
const char *cf;
struct rrd_time_value start_tv, end_tv;
char *parsetime_error = NULL;
+ struct option long_options[] = {
+ {"resolution", required_argument, 0, 'r'},
+ {"start", required_argument, 0, 's'},
+ {"end", required_argument, 0, 'e'},
+ {0, 0, 0, 0}
+ };
optind = 0;
opterr = 0; /* initialize getopt */
parsetime("now", &end_tv);
while (1) {
- static struct option long_options[] = {
- {"resolution", required_argument, 0, 'r'},
- {"start", required_argument, 0, 's'},
- {"end", required_argument, 0, 'e'},
- {0, 0, 0, 0}
- };
int option_index = 0;
int opt;
cf = argv[optind + 1];
if (rrd_fetch_r(argv[optind], cf, start, end, step, ds_cnt, ds_namv, data)
- == -1)
+ != 0)
return (-1);
return (0);
}
rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
if (rrd_file == NULL)
- return (-1);
+ goto err_free;
/* when was the really last update of this file ? */
if (((*ds_namv) =
(char **) malloc(rrd.stat_head->ds_cnt * sizeof(char *))) == NULL) {
rrd_set_error("malloc fetch ds_namv array");
- rrd_free(&rrd);
- close(rrd_file->fd);
- return (-1);
+ goto err_close;
}
for (i = 0; (unsigned long) i < rrd.stat_head->ds_cnt; i++) {
if ((((*ds_namv)[i]) = malloc(sizeof(char) * DS_NAM_SIZE)) == NULL) {
rrd_set_error("malloc fetch ds_namv entry");
- rrd_free(&rrd);
- free(*ds_namv);
- close(rrd_file->fd);
- return (-1);
+ goto err_free_ds_namv;
}
strncpy((*ds_namv)[i], rrd.ds_def[i].ds_nam, DS_NAM_SIZE - 1);
(*ds_namv)[i][DS_NAM_SIZE - 1] = '\0';
tmp_step_diff = labs(*step - (rrd.stat_head->pdp_step
* rrd.rra_def[i].pdp_cnt));
/* best full match */
- if (cal_end >= *end && cal_start <= *start) {
+ if (cal_start <= *start) {
if (first_full || (tmp_step_diff < best_full_step_diff)) {
first_full = 0;
best_full_step_diff = tmp_step_diff;
best_full_rra = i;
#ifdef DEBUG
fprintf(stderr, "best full match so far\n");
-#endif
} else {
-#ifdef DEBUG
fprintf(stderr, "full match, not best\n");
#endif
}
tmp_match = full_match;
if (cal_start > *start)
tmp_match -= (cal_start - *start);
- if (cal_end < *end)
- tmp_match -= (*end - cal_end);
if (first_part ||
(best_match < tmp_match) ||
(best_match == tmp_match &&
else {
rrd_set_error
("the RRD does not contain an RRA matching the chosen CF");
- rrd_free(&rrd);
- close(rrd_file->fd);
- return (-1);
+ goto err_free_all_ds_namv;
}
/* set the wish parameters to their real values */
*ds_cnt = rrd.stat_head->ds_cnt;
if (((*data) = malloc(*ds_cnt * rows * sizeof(rrd_value_t))) == NULL) {
rrd_set_error("malloc fetch data area");
- for (i = 0; (unsigned long) i < *ds_cnt; i++)
- free((*ds_namv)[i]);
- free(*ds_namv);
- rrd_free(&rrd);
- close(rrd_file->fd);
- return (-1);
+ goto err_free_all_ds_namv;
}
data_ptr = (*data);
* sizeof(rrd_value_t))),
SEEK_SET) != 0) {
rrd_set_error("seek error in RRA");
- for (i = 0; (unsigned) i < *ds_cnt; i++)
- free((*ds_namv)[i]);
- free(*ds_namv);
- rrd_free(&rrd);
- free(*data);
- *data = NULL;
- close(rrd_file->fd);
- return (-1);
-
+ goto err_free_data;
}
#ifdef DEBUG
fprintf(stderr, "First Seek: rra_base %lu rra_pointer %lu\n",
* sizeof(rrd_value_t)),
SEEK_SET) != 0) {
rrd_set_error("wrap seek in RRA did fail");
- for (ii = 0; (unsigned) ii < *ds_cnt; ii++)
- free((*ds_namv)[ii]);
- free(*ds_namv);
- rrd_free(&rrd);
- free(*data);
- *data = NULL;
- close(rrd_file->fd);
- return (-1);
+ goto err_free_data;
}
#ifdef DEBUG
fprintf(stderr, "wrap seek ...\n");
if (rrd_read(rrd_file, data_ptr, sizeof(rrd_value_t) * (*ds_cnt))
!= (ssize_t) (sizeof(rrd_value_t) * (*ds_cnt))) {
rrd_set_error("fetching cdp from rra");
- for (ii = 0; (unsigned) ii < *ds_cnt; ii++)
- free((*ds_namv)[ii]);
- free(*ds_namv);
- rrd_free(&rrd);
- free(*data);
- *data = NULL;
- close(rrd_file->fd);
- return (-1);
- }
-#ifdef HAVE_POSIX_FADVISE
- /* don't pollute the buffer cache with data read from the file. We do this while reading to
- keep damage minimal */
- if (0 !=
- posix_fadvise(rrd_file->fd, rrd_file->header_len, 0,
- POSIX_FADV_DONTNEED)) {
- rrd_set_error("setting POSIX_FADV_DONTNEED on '%s': %s",
- filename, rrd_strerror(errno));
- close(rrd_file->fd);
- return (-1);
+ goto err_free_data;
}
-#endif
-
#ifdef DEBUG
fprintf(stderr, "post fetch %li -- ", i);
for (ii = 0; ii < *ds_cnt; ii++)
#endif
}
- rrd_free(&rrd);
-#ifdef HAVE_POSIX_FADVISE
- /* and just to be sure we drop everything except the header at the end */
- if (0 !=
- posix_fadvise(rrd_file->fd, rrd_file->header_len, 0,
- POSIX_FADV_DONTNEED)) {
- rrd_set_error("setting POSIX_FADV_DONTNEED on '%s': %s", filename,
- rrd_strerror(errno));
- close(rrd_file->fd);
- return (-1);
- }
-#endif
- close(rrd_file->fd);
+
+ rrd_close(rrd_file);
return (0);
+ err_free_data:
+ free(*data);
+ *data = NULL;
+ err_free_all_ds_namv:
+ for (i = 0; (unsigned long) i < rrd.stat_head->ds_cnt; ++i)
+ free((*ds_namv)[i]);
+ err_free_ds_namv:
+ free(*ds_namv);
+ err_close:
+ rrd_close(rrd_file);
+ err_free:
+ rrd_free(&rrd);
+ return (-1);
}