From: Karsten Loesing Date: Wed, 19 Aug 2009 14:30:00 +0000 (+0200) Subject: Include only the last 24 hours of stats in extra-info documents. X-Git-Tag: tor-0.2.2.1-alpha~9^2~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a5508583eef66a023d09f3715549f756ea08c268;p=thirdparty%2Ftor.git Include only the last 24 hours of stats in extra-info documents. --- diff --git a/src/or/router.c b/src/or/router.c index 3249f49ce0..f33add8269 100644 --- a/src/or/router.c +++ b/src/or/router.c @@ -1826,18 +1826,38 @@ router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router, return (int)written+1; } -/** Load the contents of filename and write its contents to - * **contents. Return 1 for success, 0 if the file does not exit, - * or -1 for failure. */ +/** Load the contents of filename, find the first line starting + * with end_line that has a timestamp after after, and write + * the file contents starting with that line to **contents. Return + * 1 for success, 0 if the file does not exist or does not contain a line + * matching these criteria, or -1 for failure. */ static int -load_stats_file(const char *filename, char **contents) +load_stats_file(const char *filename, const char *end_line, time_t after, + char **out) { int r = -1; char *fname = get_datadir_fname(filename); + char *contents, *start, timestr[ISO_TIME_LEN+1]; + time_t written; switch (file_status(fname)) { case FN_FILE: - if ((*contents = read_file_to_str(fname, 0, NULL))) + if ((contents = read_file_to_str(fname, 0, NULL))) { + do { + if ((start = strstr(contents, end_line))) + goto err; + if (strlen(start) < strlen(end_line) + sizeof(timestr)) + goto err; + if (strlcpy(timestr, start + strlen(end_line), sizeof(timestr))) + goto err; + if (parse_iso_time(timestr, &written) < 0) + goto err; + } while (written < after); + *out = tor_malloc(strlen(start)); + strlcpy(*out, start, strlen(start)); r = 1; + } + err: + tor_free(contents); break; case FN_NOENT: r = 0; @@ -1880,9 +1900,11 @@ extrainfo_dump_to_string(char *s, size_t maxlen, extrainfo_t *extrainfo, if (options->ExtraInfoStatistics && write_stats_to_extrainfo) { char *contents = NULL; + time_t since = time(NULL) - (24*60*60); log_info(LD_GENERAL, "Adding stats to extra-info descriptor."); if (options->DirReqStatistics && - load_stats_file("dirreq-stats", &contents) > 0) { + load_stats_file("dirreq-stats", "dirreq-stats-end", since, + &contents) > 0) { int pos = strlen(s); if (strlcpy(s + pos, contents, maxlen - strlen(s)) != strlen(contents)) { @@ -1893,7 +1915,8 @@ extrainfo_dump_to_string(char *s, size_t maxlen, extrainfo_t *extrainfo, tor_free(contents); } if (options->EntryStatistics && - load_stats_file("entry-stats", &contents) > 0) { + load_stats_file("entry-stats", "entry-stats-end", since, + &contents) > 0) { int pos = strlen(s); if (strlcpy(s + pos, contents, maxlen - strlen(s)) != strlen(contents)) { @@ -1904,7 +1927,8 @@ extrainfo_dump_to_string(char *s, size_t maxlen, extrainfo_t *extrainfo, tor_free(contents); } if (options->CellStatistics && - load_stats_file("buffer-stats", &contents) > 0) { + load_stats_file("buffer-stats", "cell-stats-end", since, + &contents) > 0) { int pos = strlen(s); if (strlcpy(s + pos, contents, maxlen - strlen(s)) != strlen(contents)) { @@ -1915,7 +1939,8 @@ extrainfo_dump_to_string(char *s, size_t maxlen, extrainfo_t *extrainfo, tor_free(contents); } if (options->ExitPortStatistics && - load_stats_file("exit-stats", &contents) > 0) { + load_stats_file("exit-stats", "exit-stats-end", since, + &contents) > 0) { int pos = strlen(s); if (strlcpy(s + pos, contents, maxlen - strlen(s)) != strlen(contents)) {