[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Use newer cached microdescriptors if available



commit ddb1a360dac0a22c084e884f10c1bc7327cf7f7d
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Mon Nov 26 13:04:23 2018 -0800

    Use newer cached microdescriptors if available
    
    Our data directory has up to two microdescriptor files: cached-microdescs and
    cached-microdescs.new.
    
    If the former is unavailable but the later is present we should use it...
    
      https://trac.torproject.org/projects/tor/ticket/28508
    
    Maybe more important, when looking into this I realized that our attempt to get
    tor's data directory stacktraces if not explicitly present in the torrc...
    
      >>> list(controller.get_microdescriptors())
      Traceback (most recent call last):
        File "<console>", line 1, in <module>
        File "/home/atagar/Desktop/stem/stem/control.py", line 490, in wrapped
          for val in func(self, *args, **kwargs):
        File "/home/atagar/Desktop/stem/stem/control.py", line 1791, in get_microdescriptors
          if not os.path.exists(data_directory):
        File "/usr/lib/python2.7/genericpath.py", line 26, in exists
          os.stat(path)
      TypeError: coercing to Unicode: need string or buffer, NoneType found
    
    Changed it so we'll instead provide a generic exception saying we were unable
    to determine the data directory. Fortunatley this whole thing is a fallback, so
    eventually we'll be able to remove it.
---
 stem/control.py | 35 +++++++++++++++++++++--------------
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/stem/control.py b/stem/control.py
index 68873d76..6d926ff0 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -1783,27 +1783,34 @@ class Controller(BaseController):
     else:
       # TODO: remove when tor versions that require this are obsolete
 
-      try:
-        data_directory = self.get_conf('DataDirectory')
-      except stem.ControllerError as exc:
-        raise stem.OperationFailed(message = 'Unable to determine the data directory (%s)' % exc)
+      data_directory = self.get_conf('DataDirectory', None)
 
-      cached_descriptor_path = os.path.join(data_directory, 'cached-microdescs')
+      if data_directory is None:
+        raise stem.OperationFailed(message = "Unable to determine tor's data directory")
 
       if not os.path.exists(data_directory):
         raise stem.OperationFailed(message = "Data directory reported by tor doesn't exist (%s)" % data_directory)
-      elif not os.path.exists(cached_descriptor_path):
-        raise stem.OperationFailed(message = "Data directory doesn't contain cached microdescriptors (%s)" % cached_descriptor_path)
 
-      with stem.descriptor.reader.DescriptorReader([cached_descriptor_path]) as reader:
-        for desc in reader:
-          # It shouldn't be possible for these to be something other than
-          # microdescriptors but as the saying goes: trust but verify.
+      microdescriptor_file = None
+
+      for filename in ('cached-microdescs', 'cached-microdescs.new'):
+        cached_descriptors = os.path.join(data_directory, filename)
+
+        if os.path.exists(cached_descriptors):
+          microdescriptor_file = cached_descriptors
+          break
+
+      if microdescriptor_file is None:
+        raise stem.OperationFailed(message = "Data directory doesn't contain cached microdescriptors (%s)" % data_directory)
 
-          if not isinstance(desc, stem.descriptor.microdescriptor.Microdescriptor):
-            raise stem.OperationFailed(message = 'BUG: Descriptor reader provided non-microdescriptor content (%s)' % type(desc))
+      for desc in stem.descriptor.parse_file(microdescriptor_file):
+        # It shouldn't be possible for these to be something other than
+        # microdescriptors but as the saying goes: trust but verify.
 
-          yield desc
+        if not isinstance(desc, stem.descriptor.microdescriptor.Microdescriptor):
+          raise stem.OperationFailed(message = 'BUG: Descriptor reader provided non-microdescriptor content (%s)' % type(desc))
+
+        yield desc
 
   @with_default()
   def get_server_descriptor(self, relay = None, default = UNDEFINED):

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits