From 1b0a6fc41a7d5c4d61bd8e9e6fb3226519625b99 Mon Sep 17 00:00:00 2001 From: oetiker Date: Mon, 2 Apr 2007 06:21:19 +0000 Subject: [PATCH] new function rrd_fetch_r() (and make the strings const char* instead of char*). The only difference between rrd_fetch_r() and rrd_fetch_fn() is that rrd_fetch_r() receives the consolidation function as a string (instead of an enum cf_en) and is thread-safe -- Sam Umbach git-svn-id: svn://svn.oetiker.ch/rrdtool/branches/1.2@1028 a5681a0c-68f1-0310-ab6d-d61299d08faa --- program/doc/rrdthreads.pod | 2 +- program/src/fnv.h | 6 +++--- program/src/hash_32.c | 12 ++++++------ program/src/rrd.h | 14 ++++++++++---- program/src/rrd_create.c | 12 ++++++------ program/src/rrd_fetch.c | 36 +++++++++++++++++++++++++++++------- program/src/rrd_format.c | 2 +- program/src/rrd_rpncalc.c | 2 +- program/src/rrd_rpncalc.h | 2 +- program/src/rrd_tool.h | 6 +++--- program/src/rrd_update.c | 28 ++++++++++++++-------------- 11 files changed, 75 insertions(+), 47 deletions(-) diff --git a/program/doc/rrdthreads.pod b/program/doc/rrdthreads.pod index f05d6ea2..ace7ee8c 100644 --- a/program/doc/rrdthreads.pod +++ b/program/doc/rrdthreads.pod @@ -137,7 +137,7 @@ operation (e.g., C, but not in C) =head2 CURRENTLY IMPLEMENTED THREAD SAFE FUNCTIONS Currently there exist thread-safe variants of C, -C, C, C and C. +C, C, C, C, and C. =head1 AUTHOR diff --git a/program/src/fnv.h b/program/src/fnv.h index 22f6ff69..e69f9cee 100644 --- a/program/src/fnv.h +++ b/program/src/fnv.h @@ -99,10 +99,10 @@ typedef unsigned long Fnv32_t; */ #define FNV1_32_INIT ((Fnv32_t)0x811c9dc5) -Fnv32_t fnv_32_buf(void *, size_t, Fnv32_t); +Fnv32_t fnv_32_buf(const void *, size_t, Fnv32_t); -Fnv32_t fnv_32_str(char *, Fnv32_t ); +Fnv32_t fnv_32_str(const char *, Fnv32_t ); -unsigned long FnvHash(char *); +unsigned long FnvHash(const char *); #endif /* __FNV_H__ */ diff --git a/program/src/hash_32.c b/program/src/hash_32.c index 3fda164c..d7edbee7 100644 --- a/program/src/hash_32.c +++ b/program/src/hash_32.c @@ -86,10 +86,10 @@ * argument on the first call to either fnv_32_buf() or fnv_32_str(). */ Fnv32_t -fnv_32_buf(void *buf, size_t len, Fnv32_t hval) +fnv_32_buf(const void *buf, size_t len, Fnv32_t hval) { - unsigned char *bp = (unsigned char *)buf; /* start of buffer */ - unsigned char *be = bp + len; /* beyond end of buffer */ + const unsigned char *bp = (const unsigned char *)buf; /* start of buffer */ + const unsigned char *be = bp + len; /* beyond end of buffer */ /* * FNV-1 hash each octet in the buffer @@ -125,9 +125,9 @@ fnv_32_buf(void *buf, size_t len, Fnv32_t hval) * argument on the first call to either fnv_32_buf() or fnv_32_str(). */ Fnv32_t -fnv_32_str(char *str, Fnv32_t hval) +fnv_32_str(const char *str, Fnv32_t hval) { - unsigned char *s = (unsigned char *)str; /* unsigned string */ + const unsigned char *s = (const unsigned char *)str; /* unsigned string */ /* * FNV-1 hash each octet in the buffer @@ -146,7 +146,7 @@ fnv_32_str(char *str, Fnv32_t hval) } /* a wrapper function for fnv_32_str */ -unsigned long FnvHash(char *str) +unsigned long FnvHash(const char *str) { return fnv_32_str(str,FNV1_32_INIT); } diff --git a/program/src/rrd.h b/program/src/rrd.h index e83afcd1..abc183ec 100644 --- a/program/src/rrd.h +++ b/program/src/rrd.h @@ -79,14 +79,20 @@ int rrd_xport(int, char **, int *, time_t *, time_t *, char ***, rrd_value_t **); /* thread-safe (hopefully) */ -int rrd_create_r(char *filename, +int rrd_create_r(const char *filename, unsigned long pdp_step, time_t last_up, - int argc, char **argv); + int argc, const char **argv); /* NOTE: rrd_update_r are only thread-safe if no at-style time specifications get used!!! */ -int rrd_update_r(char *filename, char *_template, - int argc, char **argv); +int rrd_update_r(const char *filename, const char *_template, + int argc, const char **argv); +int rrd_fetch_r(const char *filename, const char* cf, + time_t *start, time_t *end, + unsigned long *step, + unsigned long *ds_cnt, + char ***ds_namv, + rrd_value_t **data); int rrd_dump_r(const char *filename, char *outname); time_t rrd_last_r(const char *filename); time_t rrd_first_r(const char *filename, int rraindex); diff --git a/program/src/rrd_create.c b/program/src/rrd_create.c index 8af9e9ab..1c3edc18 100644 --- a/program/src/rrd_create.c +++ b/program/src/rrd_create.c @@ -10,9 +10,9 @@ #include "rrd_is_thread_safe.h" -unsigned long FnvHash(char *str); +unsigned long FnvHash(const char *str); int create_hw_contingent_rras(rrd_t *rrd, unsigned short period, unsigned long hashed_name); -void parseGENERIC_DS(char *def,rrd_t *rrd, int ds_idx); +void parseGENERIC_DS(const char *def,rrd_t *rrd, int ds_idx); int rrd_create(int argc, char **argv) @@ -91,9 +91,9 @@ rrd_create(int argc, char **argv) /* #define DEBUG */ int -rrd_create_r(char *filename, +rrd_create_r(const char *filename, unsigned long pdp_step, time_t last_up, - int argc, char **argv) + int argc, const char **argv) { rrd_t rrd; long i; @@ -439,7 +439,7 @@ rrd_create_r(char *filename, return rrd_create_fn(filename, &rrd); } -void parseGENERIC_DS(char *def,rrd_t *rrd, int ds_idx) +void parseGENERIC_DS(const char *def,rrd_t *rrd, int ds_idx) { char minstr[DS_NAM_SIZE], maxstr[DS_NAM_SIZE]; /* @@ -547,7 +547,7 @@ create_hw_contingent_rras(rrd_t *rrd, unsigned short period, unsigned long hashe /* create and empty rrd file according to the specs given */ int -rrd_create_fn(char *file_name, rrd_t *rrd) +rrd_create_fn(const char *file_name, rrd_t *rrd) { unsigned long i,ii; FILE *rrd_file; diff --git a/program/src/rrd_fetch.c b/program/src/rrd_fetch.c index 3a794e9c..927cc303 100644 --- a/program/src/rrd_fetch.c +++ b/program/src/rrd_fetch.c @@ -53,6 +53,8 @@ *****************************************************************************/ #include "rrd_tool.h" + +#include "rrd_is_thread_safe.h" /*#define DEBUG*/ int @@ -71,7 +73,7 @@ rrd_fetch(int argc, long step_tmp =1; time_t start_tmp=0, end_tmp=0; - enum cf_en cf_idx; + const char *cf; struct rrd_time_value start_tv, end_tv; char *parsetime_error = NULL; @@ -148,19 +150,39 @@ rrd_fetch(int argc, rrd_set_error("not enough arguments"); return -1; } - - if ((int)(cf_idx=cf_conv(argv[optind+1])) == -1 ){ - return -1; - } - if (rrd_fetch_fn(argv[optind],cf_idx,start,end,step,ds_cnt,ds_namv,data) == -1) + cf = argv[optind+1]; + + if (rrd_fetch_r(argv[optind],cf,start,end,step,ds_cnt,ds_namv,data) == -1) return(-1); return (0); } +int +rrd_fetch_r( + const char *filename, /* name of the rrd */ + const char *cf, /* which consolidation function ?*/ + time_t *start, + time_t *end, /* which time frame do you want ? + * will be changed to represent reality */ + unsigned long *step, /* which stepsize do you want? + * will be changed to represent reality */ + unsigned long *ds_cnt, /* number of data sources in file */ + char ***ds_namv, /* names of data_sources */ + rrd_value_t **data) /* two dimensional array containing the data */ +{ + enum cf_en cf_idx; + + if ((int)(cf_idx=cf_conv(cf)) == -1 ){ + return -1; + } + + return (rrd_fetch_fn(filename,cf_idx,start,end,step,ds_cnt,ds_namv,data)); +} + int rrd_fetch_fn( - char *filename, /* name of the rrd */ + const char *filename, /* name of the rrd */ enum cf_en cf_idx, /* which consolidation function ?*/ time_t *start, time_t *end, /* which time frame do you want ? diff --git a/program/src/rrd_format.c b/program/src/rrd_format.c index 0fd043a8..ecd0c60d 100644 --- a/program/src/rrd_format.c +++ b/program/src/rrd_format.c @@ -65,7 +65,7 @@ enum dst_en dst_conv(char *string) } -enum cf_en cf_conv(char *string) +enum cf_en cf_conv(const char *string) { converter(AVERAGE,CF_AVERAGE) diff --git a/program/src/rrd_rpncalc.c b/program/src/rrd_rpncalc.c index 972f486b..3892e384 100644 --- a/program/src/rrd_rpncalc.c +++ b/program/src/rrd_rpncalc.c @@ -188,7 +188,7 @@ short addop2str(enum op_en op, enum op_en op_type, char *op_str, return 0; } -void parseCDEF_DS(char *def,rrd_t *rrd, int ds_idx) +void parseCDEF_DS(const char *def,rrd_t *rrd, int ds_idx) { rpnp_t *rpnp = NULL; rpn_cdefds_t *rpnc = NULL; diff --git a/program/src/rrd_rpncalc.h b/program/src/rrd_rpncalc.h index 35758082..cbde13f5 100644 --- a/program/src/rrd_rpncalc.h +++ b/program/src/rrd_rpncalc.h @@ -47,7 +47,7 @@ typedef struct rpnstack_t { void rpnstack_init(rpnstack_t *rpnstack); void rpnstack_free(rpnstack_t *rpnstack); -void parseCDEF_DS(char *def, rrd_t *rrd, int ds_idx); +void parseCDEF_DS(const char *def, rrd_t *rrd, int ds_idx); long lookup_DS(void *rrd_vptr, char *ds_name); short rpn_compact(rpnp_t *rpnp,rpn_cdefds_t **rpnc,short *count); diff --git a/program/src/rrd_tool.h b/program/src/rrd_tool.h index 954dac89..3f6416ee 100644 --- a/program/src/rrd_tool.h +++ b/program/src/rrd_tool.h @@ -159,8 +159,8 @@ info_t *info_push(info_t *, char *, enum info_type, infoval); int PngSize(FILE *, long *, long *); -int rrd_create_fn(char *file_name, rrd_t *rrd); -int rrd_fetch_fn(char *filename, enum cf_en cf_idx, +int rrd_create_fn(const char *file_name, rrd_t *rrd); +int rrd_fetch_fn(const char *filename, enum cf_en cf_idx, time_t *start,time_t *end, unsigned long *step, unsigned long *ds_cnt, @@ -177,7 +177,7 @@ int readfile(const char *file, char **buffer, int skipfirst); #define RRD_READONLY 0 #define RRD_READWRITE 1 -enum cf_en cf_conv(char *string); +enum cf_en cf_conv(const char *string); enum dst_en dst_conv(char *string); long ds_match(rrd_t *rrd,char *ds_nam); double rrd_diff(char *a, char *b); diff --git a/program/src/rrd_update.c b/program/src/rrd_update.c index 0143e9e6..d853c5b1 100644 --- a/program/src/rrd_update.c +++ b/program/src/rrd_update.c @@ -87,8 +87,8 @@ info_t *write_RRA_row (rrd_t *rrd, unsigned long rra_idx, unsigned short CDP_scratch_idx, FILE *rrd_file, info_t *pcdp_summary, time_t *rra_time); #endif -int rrd_update_r(char *filename, char *tmplt, int argc, char **argv); -int _rrd_update(char *filename, char *tmplt, int argc, char **argv, +int rrd_update_r(const char *filename, const char *tmplt, int argc, const char **argv); +int _rrd_update(const char *filename, const char *tmplt, int argc, const char **argv, info_t*); #define IFDNAN(X,Y) (isnan(X) ? (Y) : (X)); @@ -186,13 +186,13 @@ rrd_update(int argc, char **argv) } int -rrd_update_r(char *filename, char *tmplt, int argc, char **argv) +rrd_update_r(const char *filename, const char *tmplt, int argc, const char **argv) { return _rrd_update(filename, tmplt, argc, argv, NULL); } int -_rrd_update(char *filename, char *tmplt, int argc, char **argv, +_rrd_update(const char *filename, const char *tmplt, int argc, const char **argv, info_t *pcdp_summary) { @@ -364,13 +364,13 @@ _rrd_update(char *filename, char *tmplt, int argc, char **argv, /* we should work on a writeable copy here */ char *dsname; unsigned int tmpl_len; - tmplt = strdup(tmplt); - dsname = tmplt; + char *tmplt_copy = strdup(tmplt); + dsname = tmplt_copy; tmpl_cnt = 1; /* the first entry is the time */ - tmpl_len = strlen(tmplt); + tmpl_len = strlen(tmplt_copy); for(i=0;i<=tmpl_len ;i++) { - if (tmplt[i] == ':' || tmplt[i] == '\0') { - tmplt[i] = '\0'; + if (tmplt_copy[i] == ':' || tmplt_copy[i] == '\0') { + tmplt_copy[i] = '\0'; if (tmpl_cnt>rrd.stat_head->ds_cnt){ rrd_set_error("tmplt contains more DS definitions than RRD"); free(updvals); free(pdp_temp); @@ -380,23 +380,23 @@ _rrd_update(char *filename, char *tmplt, int argc, char **argv, if ((tmpl_idx[tmpl_cnt++] = ds_match(&rrd,dsname)) == -1){ rrd_set_error("unknown DS name '%s'",dsname); free(updvals); free(pdp_temp); - free(tmplt); + free(tmplt_copy); free(tmpl_idx); rrd_free(&rrd); fclose(rrd_file); return(-1); } else { /* the first element is always the time */ tmpl_idx[tmpl_cnt-1]++; - /* go to the next entry on the tmplt */ - dsname = &tmplt[i+1]; + /* go to the next entry on the tmplt_copy */ + dsname = &tmplt_copy[i+1]; /* fix the damage we did before */ if (ids_cnt))==NULL){ -- 2.30.2