Ticket #4282: ticket4282.patch

File ticket4282.patch, 10.9 KB (added by piet, 7 years ago)
  • src/or/config.c

    From d2998313e40dac3dd8aace1897f403dbf914de60 Mon Sep 17 00:00:00 2001
    From: Peter Retzlaff <pe.retzlaff@gmail.com>
    Date: Fri, 25 Jan 2013 11:49:33 +0100
    Subject: [PATCH] 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.
    ---
     src/or/config.c  |   35 ++++++++++++++++++++++++++++
     src/or/config.h  |    4 ++++
     src/or/geoip.c   |   67 +++++++++++++++++++-----------------------------------
     src/or/rephist.c |   48 ++++++++++----------------------------
     4 files changed, 75 insertions(+), 79 deletions(-)
    
    diff --git a/src/or/config.c b/src/or/config.c
    index f61d50d..05036b0 100644
    a b options_get_datadir_fname2_suffix(const or_options_t *options, 
    57245724  return fname;
    57255725}
    57265726
     5727/** Check wether the data directory has a private subdirectory
     5728 * <b>subdir</b>. If not, try to create it. Return 0 on success,
     5729 * -1 otherwise. */
     5730int
     5731check_or_create_data_subdir(const char *subdir) {
     5732  char *statsdir = get_datadir_fname(subdir);
     5733  int return_val = 0;
     5734
     5735  if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
     5736    log_warn(LD_HIST, "Unable to create %s/ directory!", subdir);
     5737        return_val = -1;
     5738  }
     5739  tor_free(statsdir);
     5740  return return_val;
     5741}
     5742
     5743/** Create a file named <b>fname</b> with contents <b>str</b> in the
     5744 * subdirectory <b>subdir</b> of the data directory. <b>descr</b>
     5745 * should be a short description of the file's content and will be
     5746 * used for the warning message, if it's present and the write process
     5747 * fails. Return 0 on success, -1 otherwise.*/
     5748int
     5749write_to_data_subdir(const char* subdir, const char* fname,
     5750                     const char* str, const char* descr) {
     5751  char *filename = get_datadir_fname2(subdir, fname);
     5752  int return_val = 0;
     5753
     5754  if (write_str_to_file(filename, str, 0) < 0) {
     5755    log_warn(LD_HIST, "Unable to write %s to disk!", descr ? descr : fname);
     5756    return_val = -1;
     5757  }
     5758  tor_free(filename);
     5759  return return_val;
     5760}
     5761
    57275762/** Given a file name check to see whether the file exists but has not been
    57285763 * modified for a very long time.  If so, remove it. */
    57295764void
  • src/or/config.h

    diff --git a/src/or/config.h b/src/or/config.h
    index 8e34655..d0ffda7 100644
    a b char *options_get_datadir_fname2_suffix(const or_options_t *options, 
    5757#define get_datadir_fname_suffix(sub1, suffix) \
    5858  get_datadir_fname2_suffix((sub1), NULL, (suffix))
    5959
     60int check_or_create_data_subdir(const char *subdir);
     61int write_to_data_subdir(const char* subdir, const char* fname,
     62                                 const char* str, const char* descr);
     63
    6064int get_num_cpus(const or_options_t *options);
    6165
    6266const smartlist_t *get_configured_ports(void);
  • src/or/geoip.c

    diff --git a/src/or/geoip.c b/src/or/geoip.c
    index 9ba1e31..b692b3a 100644
    a b geoip_format_dirreq_stats(time_t now) 
    11321132time_t
    11331133geoip_dirreq_stats_write(time_t now)
    11341134{
    1135   char *statsdir = NULL, *filename = NULL, *str = NULL;
     1135  char *str = NULL;
    11361136
    11371137  if (!start_of_dirreq_stats_interval)
    11381138    return 0; /* Not initialized. */
    geoip_dirreq_stats_write(time_t now) 
    11461146  str = geoip_format_dirreq_stats(now);
    11471147
    11481148  /* Write dirreq-stats string to disk. */
    1149   statsdir = get_datadir_fname("stats");
    1150   if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
    1151     log_warn(LD_HIST, "Unable to create stats/ directory!");
    1152     goto done;
     1149  if (check_or_create_data_subdir("stats") < 0) {
     1150        write_to_data_subdir("stats", "dirreq-stats", str, "dirreq statistics");
     1151        /* Reset measurement interval start. */
     1152        geoip_reset_dirreq_stats(now);
    11531153  }
    1154   filename = get_datadir_fname2("stats", "dirreq-stats");
    1155   if (write_str_to_file(filename, str, 0) < 0)
    1156     log_warn(LD_HIST, "Unable to write dirreq statistics to disk!");
    1157 
    1158   /* Reset measurement interval start. */
    1159   geoip_reset_dirreq_stats(now);
    11601154
    11611155 done:
    1162   tor_free(statsdir);
    1163   tor_free(filename);
    11641156  tor_free(str);
    11651157  return start_of_dirreq_stats_interval + WRITE_STATS_INTERVAL;
    11661158}
    format_bridge_stats_controller(time_t now) 
    12971289time_t
    12981290geoip_bridge_stats_write(time_t now)
    12991291{
    1300   char *filename = NULL, *val = NULL, *statsdir = NULL;
     1292  char *val = NULL;
    13011293
    13021294  /* Check if 24 hours have passed since starting measurements. */
    13031295  if (now < start_of_bridge_stats_interval + WRITE_STATS_INTERVAL)
    geoip_bridge_stats_write(time_t now) 
    13171309  start_of_bridge_stats_interval = now;
    13181310
    13191311  /* Write it to disk. */
    1320   statsdir = get_datadir_fname("stats");
    1321   if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0)
    1322     goto done;
    1323   filename = get_datadir_fname2("stats", "bridge-stats");
    1324 
    1325   write_str_to_file(filename, bridge_stats_extrainfo, 0);
    1326 
    1327   /* Tell the controller, "hey, there are clients!" */
    1328   {
    1329     char *controller_str = format_bridge_stats_controller(now);
    1330     if (controller_str)
    1331       control_event_clients_seen(controller_str);
    1332     tor_free(controller_str);
     1312  if (!check_or_create_data_subdir("stats")) {
     1313        write_to_data_subdir("stats", "bridge-stats",
     1314                        bridge_stats_extrainfo, "bridge statistics");
     1315
     1316        /* Tell the controller, "hey, there are clients!" */
     1317        {
     1318          char *controller_str = format_bridge_stats_controller(now);
     1319          if (controller_str)
     1320            control_event_clients_seen(controller_str);
     1321          tor_free(controller_str);
     1322        }
    13331323  }
    1334  done:
    1335   tor_free(filename);
    1336   tor_free(statsdir);
    13371324
     1325 done:
    13381326  return start_of_bridge_stats_interval + WRITE_STATS_INTERVAL;
    13391327}
    13401328
    geoip_format_entry_stats(time_t now) 
    14331421time_t
    14341422geoip_entry_stats_write(time_t now)
    14351423{
    1436   char *statsdir = NULL, *filename = NULL, *str = NULL;
     1424  char *str = NULL;
    14371425
    14381426  if (!start_of_entry_stats_interval)
    14391427    return 0; /* Not initialized. */
    geoip_entry_stats_write(time_t now) 
    14471435  str = geoip_format_entry_stats(now);
    14481436
    14491437  /* Write entry-stats string to disk. */
    1450   statsdir = get_datadir_fname("stats");
    1451   if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
    1452     log_warn(LD_HIST, "Unable to create stats/ directory!");
    1453     goto done;
    1454   }
    1455   filename = get_datadir_fname2("stats", "entry-stats");
    1456   if (write_str_to_file(filename, str, 0) < 0)
    1457     log_warn(LD_HIST, "Unable to write entry statistics to disk!");
     1438  if (!check_or_create_data_subdir("stats")) {
     1439          write_to_data_subdir("stats", "entry-stats", str, "entry statistics");
    14581440
    1459   /* Reset measurement interval start. */
    1460   geoip_reset_entry_stats(now);
     1441          /* Reset measurement interval start. */
     1442          geoip_reset_entry_stats(now);
     1443  }
    14611444
    14621445 done:
    1463   tor_free(statsdir);
    1464   tor_free(filename);
    14651446  tor_free(str);
    14661447  return start_of_entry_stats_interval + WRITE_STATS_INTERVAL;
    14671448}
  • src/or/rephist.c

    diff --git a/src/or/rephist.c b/src/or/rephist.c
    index 74d459f..d5981d0 100644
    a b rep_hist_format_exit_stats(time_t now) 
    22972297time_t
    22982298rep_hist_exit_stats_write(time_t now)
    22992299{
    2300   char *statsdir = NULL, *filename = NULL, *str = NULL;
     2300  char *str = NULL;
    23012301
    23022302  if (!start_of_exit_stats_interval)
    23032303    return 0; /* Not initialized. */
    rep_hist_exit_stats_write(time_t now) 
    23132313  rep_hist_reset_exit_stats(now);
    23142314
    23152315  /* Try to write to disk. */
    2316   statsdir = get_datadir_fname("stats");
    2317   if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
    2318     log_warn(LD_HIST, "Unable to create stats/ directory!");
    2319     goto done;
     2316  if (!check_or_create_data_subdir("stats")) {
     2317    write_to_data_subdir("stats", "exit-stats", str, "exit port statistics");
    23202318  }
    2321   filename = get_datadir_fname2("stats", "exit-stats");
    2322   if (write_str_to_file(filename, str, 0) < 0)
    2323     log_warn(LD_HIST, "Unable to write exit port statistics to disk!");
    23242319
    23252320 done:
    23262321  tor_free(str);
    2327   tor_free(statsdir);
    2328   tor_free(filename);
    23292322  return start_of_exit_stats_interval + WRITE_STATS_INTERVAL;
    23302323}
    23312324
    time_t 
    25822575rep_hist_buffer_stats_write(time_t now)
    25832576{
    25842577  circuit_t *circ;
    2585   char *statsdir = NULL, *filename = NULL, *str = NULL;
     2578  char *str = NULL;
    25862579
    25872580  if (!start_of_buffer_stats_interval)
    25882581    return 0; /* Not initialized. */
    rep_hist_buffer_stats_write(time_t now) 
    26012594  rep_hist_reset_buffer_stats(now);
    26022595
    26032596  /* Try to write to disk. */
    2604   statsdir = get_datadir_fname("stats");
    2605   if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
    2606     log_warn(LD_HIST, "Unable to create stats/ directory!");
    2607     goto done;
     2597  if (!check_or_create_data_subdir("stats")) {
     2598    write_to_data_subdir("stats", "buffer-stats", str, "buffer statistics");
    26082599  }
    2609   filename = get_datadir_fname2("stats", "buffer-stats");
    2610   if (write_str_to_file(filename, str, 0) < 0)
    2611     log_warn(LD_HIST, "Unable to write buffer stats to disk!");
    26122600
    26132601 done:
    26142602  tor_free(str);
    2615   tor_free(filename);
    2616   tor_free(statsdir);
    26172603  return start_of_buffer_stats_interval + WRITE_STATS_INTERVAL;
    26182604}
    26192605
    rep_hist_format_desc_stats(time_t now) 
    27252711time_t
    27262712rep_hist_desc_stats_write(time_t now)
    27272713{
    2728   char *statsdir = NULL, *filename = NULL, *str = NULL;
     2714  char *filename = NULL, *str = NULL;
    27292715
    27302716  if (!start_of_served_descs_stats_interval)
    27312717    return 0; /* We're not collecting stats. */
    rep_hist_desc_stats_write(time_t now) 
    27352721  str = rep_hist_format_desc_stats(now);
    27362722  tor_assert(str != NULL);
    27372723
    2738   statsdir = get_datadir_fname("stats");
    2739   if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
    2740     log_warn(LD_HIST, "Unable to create stats/ directory!");
    2741       goto done;
     2724  if (check_or_create_data_subdir("stats") < 0) {
     2725    goto done;
    27422726  }
    27432727  filename = get_datadir_fname2("stats", "served-desc-stats");
    27442728  if (append_bytes_to_file(filename, str, strlen(str), 0) < 0)
    rep_hist_desc_stats_write(time_t now) 
    27472731  rep_hist_reset_desc_stats(now);
    27482732
    27492733 done:
    2750   tor_free(statsdir);
    27512734  tor_free(filename);
    27522735  tor_free(str);
    27532736  return start_of_served_descs_stats_interval + WRITE_STATS_INTERVAL;
    rep_hist_format_conn_stats(time_t now) 
    29652948time_t
    29662949rep_hist_conn_stats_write(time_t now)
    29672950{
    2968   char *statsdir = NULL, *filename = NULL, *str = NULL;
     2951  char *str = NULL;
    29692952
    29702953  if (!start_of_conn_stats_interval)
    29712954    return 0; /* Not initialized. */
    rep_hist_conn_stats_write(time_t now) 
    29792962  rep_hist_reset_conn_stats(now);
    29802963
    29812964  /* Try to write to disk. */
    2982   statsdir = get_datadir_fname("stats");
    2983   if (check_private_dir(statsdir, CPD_CREATE, get_options()->User) < 0) {
    2984     log_warn(LD_HIST, "Unable to create stats/ directory!");
    2985     goto done;
     2965  if (!check_or_create_data_subdir("stats")) {
     2966    write_to_data_subdir("stats", "conn-stats", str, "connection statistics");
    29862967  }
    2987   filename = get_datadir_fname2("stats", "conn-stats");
    2988   if (write_str_to_file(filename, str, 0) < 0)
    2989     log_warn(LD_HIST, "Unable to write conn stats to disk!");
    29902968
    29912969 done:
    29922970  tor_free(str);
    2993   tor_free(filename);
    2994   tor_free(statsdir);
    29952971  return start_of_conn_stats_interval + WRITE_STATS_INTERVAL;
    29962972}
    29972973