[tor-commits] [metrics-web/master] Use a temp file for writing raw bridge usage data.

karsten at torproject.org karsten at torproject.org
Wed Jun 6 08:03:19 UTC 2012


commit 7ce5984810a05bdd59790b8010da0d1f47e8910a
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Wed Jun 6 10:00:49 2012 +0200

    Use a temp file for writing raw bridge usage data.
    
    Fixes more of #6064.
---
 .../ernie/cron/BridgeStatsFileHandler.java         |   24 ++++++++++++++++---
 1 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/org/torproject/ernie/cron/BridgeStatsFileHandler.java b/src/org/torproject/ernie/cron/BridgeStatsFileHandler.java
index aba7804..70a6a6f 100644
--- a/src/org/torproject/ernie/cron/BridgeStatsFileHandler.java
+++ b/src/org/torproject/ernie/cron/BridgeStatsFileHandler.java
@@ -50,6 +50,11 @@ public class BridgeStatsFileHandler {
   private File bridgeStatsRawFile;
 
   /**
+   * Temp file for writing intermediate results.
+   */
+  private File bridgeStatsRawTempFile;
+
+  /**
    * Bridge user numbers by country as seen by single bridges on a given
    * day. Map keys are bridge and date written as "bridge,date", map
    * values are lines as read from <code>stats/bridge-stats-raw</code>.
@@ -118,6 +123,7 @@ public class BridgeStatsFileHandler {
 
     /* Initialize file names for intermediate and final results. */
     this.bridgeStatsRawFile = new File("stats/bridge-stats-raw");
+    this.bridgeStatsRawTempFile = new File("stats/bridge-stats-raw.tmp");
     this.bridgeStatsFile = new File("stats/bridge-stats");
     this.hashedRelayIdentitiesFile = new File(
         "stats/hashed-relay-identities");
@@ -368,10 +374,12 @@ public class BridgeStatsFileHandler {
     /* Write observations made by single bridges to disk. */
     try {
       this.logger.fine("Writing file "
-          + this.bridgeStatsRawFile.getAbsolutePath() + "...");
-      this.bridgeStatsRawFile.getParentFile().mkdirs();
+          + this.bridgeStatsRawFile.getAbsolutePath() + " (using "
+          + this.bridgeStatsRawTempFile.getAbsolutePath() + " as temp "
+          + "file)...");
+      this.bridgeStatsRawTempFile.getParentFile().mkdirs();
       BufferedWriter bw = new BufferedWriter(new FileWriter(
-          this.bridgeStatsRawFile));
+          this.bridgeStatsRawTempFile));
       bw.append("bridge,date,time");
       for (String c : this.countries) {
         if (c.equals("zy")) {
@@ -399,11 +407,19 @@ public class BridgeStatsFileHandler {
         }
       }
       bw.close();
+      if (!this.bridgeStatsRawTempFile.renameTo(
+          this.bridgeStatsRawFile)) {
+        this.logger.fine("Failed to rename "
+            + this.bridgeStatsRawTempFile.getAbsolutePath() + " to "
+            + this.bridgeStatsRawFile.getAbsolutePath() + ".");
+      }
       this.logger.fine("Finished writing file "
           + this.bridgeStatsRawFile.getAbsolutePath() + ".");
     } catch (IOException e) {
       this.logger.log(Level.WARNING, "Failed to write "
-          + this.bridgeStatsRawFile.getAbsolutePath() + "!", e);
+          + this.bridgeStatsRawFile.getAbsolutePath() + " (using "
+          + this.bridgeStatsRawTempFile.getAbsolutePath() + " as temp "
+          + "file)!", e);
     }
 
     /* Aggregate per-day statistics. */



More information about the tor-commits mailing list