summaryrefslogtreecommitdiff
path: root/src/or/rephist.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2007-08-29 19:02:43 +0000
committerNick Mathewson <nickm@torproject.org>2007-08-29 19:02:43 +0000
commitf189ecbf202b8cb96ff3ecea775c259083a3b8e3 (patch)
tree5ef5ad3894ab9d5eabc685c38dab5e075427073e /src/or/rephist.c
parent8408122222bda77347e46dedafc12abcd2b45e1e (diff)
downloadtor-f189ecbf202b8cb96ff3ecea775c259083a3b8e3.tar.gz
tor-f189ecbf202b8cb96ff3ecea775c259083a3b8e3.zip
r14832@catbus: nickm | 2007-08-29 15:00:27 -0400
Use (and debug) new file-writing functions in order to simplify code that formerly built big strings in RAM. svn:r11301
Diffstat (limited to 'src/or/rephist.c')
-rw-r--r--src/or/rephist.c58
1 files changed, 28 insertions, 30 deletions
diff --git a/src/or/rephist.c b/src/or/rephist.c
index d21f81723c..72a7af5834 100644
--- a/src/or/rephist.c
+++ b/src/or/rephist.c
@@ -563,14 +563,23 @@ get_mtbf_filename(void)
int
rep_hist_record_mtbf_data(void)
{
- char buf[128];
char time_buf[ISO_TIME_LEN+1];
- smartlist_t *lines;
digestmap_iter_t *orhist_it;
const char *digest;
void *or_history_p;
or_history_t *hist;
+ open_file_t *open_file = NULL;
+ FILE *f;
+
+ {
+ char *filename = get_mtbf_filename();
+ f = start_writing_to_stdio_file(filename, OPEN_FLAGS_REPLACE|O_TEXT, 0600,
+ &open_file);
+ tor_free(filename);
+ if (!f)
+ return -1;
+ }
/* File format is:
* FormatLine *KeywordLine Data
@@ -581,27 +590,24 @@ rep_hist_record_mtbf_data(void)
* RouterMTBFLine = Fingerprint SP WeightedRunLen SP
* TotalRunWeights [SP S=StartRunTime] NL
*/
+#define PUT(s) STMT_BEGIN if (fputs((s),f)<0) goto err; STMT_END
+#define PRINTF(args) STMT_BEGIN if (fprintf args <0) goto err; STMT_END
- lines = smartlist_create();
-
- smartlist_add(lines, tor_strdup("format 1\n"));
+ PUT("format 1\n");
format_iso_time(time_buf, time(NULL));
- tor_snprintf(buf, sizeof(buf), "stored-at %s\n", time_buf);
- smartlist_add(lines, tor_strdup(buf));
+ PRINTF((f, "stored-at %s\n", time_buf));
if (started_tracking_stability) {
format_iso_time(time_buf, started_tracking_stability);
- tor_snprintf(buf, sizeof(buf), "tracked-since %s\n", time_buf);
- smartlist_add(lines, tor_strdup(buf));
+ PRINTF((f, "tracked-since %s\n", time_buf));
}
if (stability_last_downrated) {
format_iso_time(time_buf, stability_last_downrated);
- tor_snprintf(buf, sizeof(buf), "last-downrated %s\n", time_buf);
- smartlist_add(lines, tor_strdup(buf));
+ PRINTF((f, "last-downrated %s\n", time_buf));
}
- smartlist_add(lines, tor_strdup("data\n"));
+ PUT("data\n");
for (orhist_it = digestmap_iter_init(history_map);
!digestmap_iter_done(orhist_it);
@@ -616,28 +622,20 @@ rep_hist_record_mtbf_data(void)
format_iso_time(time_buf, hist->start_of_run);
t = time_buf;
}
- tor_snprintf(buf, sizeof(buf), "%s %lu %.5lf%s%s\n",
- dbuf, hist->weighted_run_length, hist->total_run_weights,
- t ? " S=" : "", t ? t : "");
- smartlist_add(lines, tor_strdup(buf));
+ PRINTF((f, "%s %lu %.5lf%s%s\n",
+ dbuf, hist->weighted_run_length, hist->total_run_weights,
+ t ? " S=" : "", t ? t : ""));
}
- smartlist_add(lines, tor_strdup(".\n"));
+ PUT(".\n");
- {
- size_t sz;
- /* XXXX This isn't terribly efficient; line-at-a-time would be
- * way faster. */
- char *filename = get_mtbf_filename();
- char *data = smartlist_join_strings(lines, "", 0, &sz);
- int r = write_bytes_to_file(filename, data, sz, 0);
+#undef PUT
+#undef PRINTF
- tor_free(data);
- tor_free(filename);
- SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
- smartlist_free(lines);
- return r;
- }
+ return finish_writing_to_file(open_file);
+ err:
+ abort_writing_to_file(open_file);
+ return -1;
}
/** Load MTBF data from disk. Returns 0 on success or recoverable error, -1