[tor-commits] [metrics-db/master] Sanity-check relay descriptors before writing them to disk.

karsten at torproject.org karsten at torproject.org
Wed May 9 10:17:06 UTC 2012


commit 1ea308da77f7b3f382be28bbce4989f176c3cf0a
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Wed May 9 12:12:47 2012 +0200

    Sanity-check relay descriptors before writing them to disk.
    
    In #5805 we found that we have quite a few files that are either empty or
    truncated.  Turns out that the current metrics-db code doesn't allow
    writing empty files, but it does allow writing truncated files.  We now
    parse all descriptors with metrics-lib and only store valid descriptors to
    disk.  Fixes part of #5813.
---
 build.xml                                      |    1 +
 src/org/torproject/ernie/db/ArchiveWriter.java |   15 +++++++++++++++
 2 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/build.xml b/build.xml
index b0ce368..f7cadef 100644
--- a/build.xml
+++ b/build.xml
@@ -10,6 +10,7 @@
     <pathelement location="lib/commons-compress-1.0.jar"/>
     <pathelement location="lib/postgresql-8.4-701.jdbc4.jar"/>
     <pathelement location="lib/junit-4.8.2.jar"/>
+    <pathelement location="lib/descriptor.jar"/>
   </path>
   <target name="init">
     <mkdir dir="${classes}"/>
diff --git a/src/org/torproject/ernie/db/ArchiveWriter.java b/src/org/torproject/ernie/db/ArchiveWriter.java
index e30cd1b..9d9e4de 100644
--- a/src/org/torproject/ernie/db/ArchiveWriter.java
+++ b/src/org/torproject/ernie/db/ArchiveWriter.java
@@ -8,10 +8,14 @@ import java.util.*;
 import java.util.logging.*;
 import org.apache.commons.codec.binary.*;
 
+import org.torproject.descriptor.DescriptorParser;
+import org.torproject.descriptor.DescriptorSourceFactory;
+import org.torproject.descriptor.impl.DescriptorParseException;
 
 public class ArchiveWriter {
   private Logger logger;
   private File outputDirectory;
+  private DescriptorParser descriptorParser;
   private int storedConsensuses = 0, storedVotes = 0, storedCerts = 0,
       storedServerDescriptors = 0, storedExtraInfoDescriptors = 0;
 
@@ -23,6 +27,8 @@ public class ArchiveWriter {
 
     this.logger = Logger.getLogger(ArchiveWriter.class.getName());
     this.outputDirectory = outputDirectory;
+    this.descriptorParser =
+        DescriptorSourceFactory.createDescriptorParser();
   }
 
   private boolean store(byte[] data, String filename) {
@@ -30,6 +36,12 @@ public class ArchiveWriter {
       File file = new File(filename);
       if (!file.exists()) {
         this.logger.finer("Storing " + filename);
+        if (this.descriptorParser.parseDescriptors(data, filename).size()
+            != 1) {
+          this.logger.info("Relay descriptor file " + filename
+              + " doesn't contain exactly one descriptor.  Not storing.");
+          return false;
+        }
         file.getParentFile().mkdirs();
         BufferedOutputStream bos = new BufferedOutputStream(
             new FileOutputStream(file));
@@ -37,6 +49,9 @@ public class ArchiveWriter {
         bos.close();
         return true;
       }
+    } catch (DescriptorParseException e) {
+      this.logger.log(Level.WARNING, "Could not parse relay descriptor "
+          + filename + " before storing it to disk.  Skipping.", e);
     } catch (IOException e) {
       this.logger.log(Level.WARNING, "Could not store relay descriptor "
           + filename, e);



More information about the tor-commits mailing list