Extract duplicate code in geoip and rephist.

Create new methods check_or_create_data_subdir() and
write_to_data_subdir() in config.c and use them throughout
rephist.c and geoip.c.
This should solve ticket #4282.
This commit is contained in:
Peter Retzlaff 2013-01-25 11:49:33 +01:00 committed by Nick Mathewson
parent b4b0063e48
commit 5b7eaa3765
4 changed files with 76 additions and 78 deletions

View File

@ -5961,6 +5961,43 @@ options_get_datadir_fname2_suffix(const or_options_t *options,
return fname;
}
/** Check wether the data directory has a private subdirectory
* <b>subdir</b>. If not, try to create it. Return 0 on success,
* -1 otherwise. */
int
check_or_create_data_subdir(const char *subdir)
{
char *statsdir = get_datadir_fname(subdir);
int return_val = 0;
if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
log_warn(LD_HIST, "Unable to create %s/ directory!", subdir);
return_val = -1;
}
tor_free(statsdir);
return return_val;
}
/** Create a file named <b>fname</b> with contents <b>str</b> in the
* subdirectory <b>subdir</b> of the data directory. <b>descr</b>
* should be a short description of the file's content and will be
* used for the warning message, if it's present and the write process
* fails. Return 0 on success, -1 otherwise.*/
int
write_to_data_subdir(const char* subdir, const char* fname,
const char* str, const char* descr)
{
char *filename = get_datadir_fname2(subdir, fname);
int return_val = 0;
if (write_str_to_file(filename, str, 0) < 0) {
log_warn(LD_HIST, "Unable to write %s to disk!", descr ? descr : fname);
return_val = -1;
}
tor_free(filename);
return return_val;
}
/** Given a file name check to see whether the file exists but has not been
* modified for a very long time. If so, remove it. */
void

View File

@ -59,6 +59,10 @@ char *options_get_datadir_fname2_suffix(const or_options_t *options,
#define get_datadir_fname_suffix(sub1, suffix) \
get_datadir_fname2_suffix((sub1), NULL, (suffix))
int check_or_create_data_subdir(const char *subdir);
int write_to_data_subdir(const char* subdir, const char* fname,
const char* str, const char* descr);
int get_num_cpus(const or_options_t *options);
const smartlist_t *get_configured_ports(void);

View File

@ -1132,7 +1132,7 @@ geoip_format_dirreq_stats(time_t now)
time_t
geoip_dirreq_stats_write(time_t now)
{
char *statsdir = NULL, *filename = NULL, *str = NULL;
char *str = NULL;
if (!start_of_dirreq_stats_interval)
return 0; /* Not initialized. */
@ -1146,21 +1146,13 @@ geoip_dirreq_stats_write(time_t now)
str = geoip_format_dirreq_stats(now);
/* Write dirreq-stats string to disk. */
statsdir = get_datadir_fname("stats");
if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
log_warn(LD_HIST, "Unable to create stats/ directory!");
goto done;
}
filename = get_datadir_fname2("stats", "dirreq-stats");
if (write_str_to_file(filename, str, 0) < 0)
log_warn(LD_HIST, "Unable to write dirreq statistics to disk!");
if (check_or_create_data_subdir("stats") < 0) {
write_to_data_subdir("stats", "dirreq-stats", str, "dirreq statistics");
/* Reset measurement interval start. */
geoip_reset_dirreq_stats(now);
}
done:
tor_free(statsdir);
tor_free(filename);
tor_free(str);
return start_of_dirreq_stats_interval + WRITE_STATS_INTERVAL;
}
@ -1297,7 +1289,7 @@ format_bridge_stats_controller(time_t now)
time_t
geoip_bridge_stats_write(time_t now)
{
char *filename = NULL, *val = NULL, *statsdir = NULL;
char *val = NULL;
/* Check if 24 hours have passed since starting measurements. */
if (now < start_of_bridge_stats_interval + WRITE_STATS_INTERVAL)
@ -1317,12 +1309,9 @@ geoip_bridge_stats_write(time_t now)
start_of_bridge_stats_interval = now;
/* Write it to disk. */
statsdir = get_datadir_fname("stats");
if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0)
goto done;
filename = get_datadir_fname2("stats", "bridge-stats");
write_str_to_file(filename, bridge_stats_extrainfo, 0);
if (!check_or_create_data_subdir("stats")) {
write_to_data_subdir("stats", "bridge-stats",
bridge_stats_extrainfo, "bridge statistics");
/* Tell the controller, "hey, there are clients!" */
{
@ -1331,10 +1320,9 @@ geoip_bridge_stats_write(time_t now)
control_event_clients_seen(controller_str);
tor_free(controller_str);
}
done:
tor_free(filename);
tor_free(statsdir);
}
done:
return start_of_bridge_stats_interval + WRITE_STATS_INTERVAL;
}
@ -1436,7 +1424,7 @@ geoip_format_entry_stats(time_t now)
time_t
geoip_entry_stats_write(time_t now)
{
char *statsdir = NULL, *filename = NULL, *str = NULL;
char *str = NULL;
if (!start_of_entry_stats_interval)
return 0; /* Not initialized. */
@ -1450,21 +1438,14 @@ geoip_entry_stats_write(time_t now)
str = geoip_format_entry_stats(now);
/* Write entry-stats string to disk. */
statsdir = get_datadir_fname("stats");
if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
log_warn(LD_HIST, "Unable to create stats/ directory!");
goto done;
}
filename = get_datadir_fname2("stats", "entry-stats");
if (write_str_to_file(filename, str, 0) < 0)
log_warn(LD_HIST, "Unable to write entry statistics to disk!");
if (!check_or_create_data_subdir("stats")) {
write_to_data_subdir("stats", "entry-stats", str, "entry statistics");
/* Reset measurement interval start. */
geoip_reset_entry_stats(now);
}
done:
tor_free(statsdir);
tor_free(filename);
tor_free(str);
return start_of_entry_stats_interval + WRITE_STATS_INTERVAL;
}

View File

@ -2313,7 +2313,7 @@ rep_hist_format_exit_stats(time_t now)
time_t
rep_hist_exit_stats_write(time_t now)
{
char *statsdir = NULL, *filename = NULL, *str = NULL;
char *str = NULL;
if (!start_of_exit_stats_interval)
return 0; /* Not initialized. */
@ -2329,19 +2329,12 @@ rep_hist_exit_stats_write(time_t now)
rep_hist_reset_exit_stats(now);
/* Try to write to disk. */
statsdir = get_datadir_fname("stats");
if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
log_warn(LD_HIST, "Unable to create stats/ directory!");
goto done;
if (!check_or_create_data_subdir("stats")) {
write_to_data_subdir("stats", "exit-stats", str, "exit port statistics");
}
filename = get_datadir_fname2("stats", "exit-stats");
if (write_str_to_file(filename, str, 0) < 0)
log_warn(LD_HIST, "Unable to write exit port statistics to disk!");
done:
tor_free(str);
tor_free(statsdir);
tor_free(filename);
return start_of_exit_stats_interval + WRITE_STATS_INTERVAL;
}
@ -2598,7 +2591,7 @@ time_t
rep_hist_buffer_stats_write(time_t now)
{
circuit_t *circ;
char *statsdir = NULL, *filename = NULL, *str = NULL;
char *str = NULL;
if (!start_of_buffer_stats_interval)
return 0; /* Not initialized. */
@ -2617,19 +2610,12 @@ rep_hist_buffer_stats_write(time_t now)
rep_hist_reset_buffer_stats(now);
/* Try to write to disk. */
statsdir = get_datadir_fname("stats");
if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
log_warn(LD_HIST, "Unable to create stats/ directory!");
goto done;
if (!check_or_create_data_subdir("stats")) {
write_to_data_subdir("stats", "buffer-stats", str, "buffer statistics");
}
filename = get_datadir_fname2("stats", "buffer-stats");
if (write_str_to_file(filename, str, 0) < 0)
log_warn(LD_HIST, "Unable to write buffer stats to disk!");
done:
tor_free(str);
tor_free(filename);
tor_free(statsdir);
return start_of_buffer_stats_interval + WRITE_STATS_INTERVAL;
}
@ -2741,7 +2727,7 @@ rep_hist_format_desc_stats(time_t now)
time_t
rep_hist_desc_stats_write(time_t now)
{
char *statsdir = NULL, *filename = NULL, *str = NULL;
char *filename = NULL, *str = NULL;
if (!start_of_served_descs_stats_interval)
return 0; /* We're not collecting stats. */
@ -2751,9 +2737,7 @@ rep_hist_desc_stats_write(time_t now)
str = rep_hist_format_desc_stats(now);
tor_assert(str != NULL);
statsdir = get_datadir_fname("stats");
if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
log_warn(LD_HIST, "Unable to create stats/ directory!");
if (check_or_create_data_subdir("stats") < 0) {
goto done;
}
filename = get_datadir_fname2("stats", "served-desc-stats");
@ -2763,7 +2747,6 @@ rep_hist_desc_stats_write(time_t now)
rep_hist_reset_desc_stats(now);
done:
tor_free(statsdir);
tor_free(filename);
tor_free(str);
return start_of_served_descs_stats_interval + WRITE_STATS_INTERVAL;
@ -2981,7 +2964,7 @@ rep_hist_format_conn_stats(time_t now)
time_t
rep_hist_conn_stats_write(time_t now)
{
char *statsdir = NULL, *filename = NULL, *str = NULL;
char *str = NULL;
if (!start_of_conn_stats_interval)
return 0; /* Not initialized. */
@ -2995,19 +2978,12 @@ rep_hist_conn_stats_write(time_t now)
rep_hist_reset_conn_stats(now);
/* Try to write to disk. */
statsdir = get_datadir_fname("stats");
if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
log_warn(LD_HIST, "Unable to create stats/ directory!");
goto done;
if (!check_or_create_data_subdir("stats")) {
write_to_data_subdir("stats", "conn-stats", str, "connection statistics");
}
filename = get_datadir_fname2("stats", "conn-stats");
if (write_str_to_file(filename, str, 0) < 0)
log_warn(LD_HIST, "Unable to write conn stats to disk!");
done:
tor_free(str);
tor_free(filename);
tor_free(statsdir);
return start_of_conn_stats_interval + WRITE_STATS_INTERVAL;
}