From: robk-tahoe <robk-tahoe@allmydata.com>
Date: Wed, 24 Sep 2008 18:28:54 +0000 (-0700)
Subject: fuse/impl_{a,b}: improve node-url handling
X-Git-Url: https://git.rkrishnan.org/components/architecture.txt?a=commitdiff_plain;h=4f04bf99a5f2915ec2e8214b915b98ce86a4fd00;p=tahoe-lafs%2Ftahoe-lafs.git

fuse/impl_{a,b}: improve node-url handling

specifically change the expectation of the code to be such that the node-url
(self.url) always includes the trailing slash to be a correctly formed url

moreover read the node-url from the 'node.url' file found in the node 'basedir'
and only if that doesn't exist, then fall back to reading the 'webport' file
from therein and assuming localhost.  This then supports the general tahoe
pattern that tools needing only a webapi server can be pointed at a directory
containing the node.url file, which can optionally point to another server,
rather than requiring a complete node dir and locally running node instance.
---

diff --git a/contrib/fuse/impl_a/tahoe_fuse.py b/contrib/fuse/impl_a/tahoe_fuse.py
index 71bcb169..c9553d3c 100644
--- a/contrib/fuse/impl_a/tahoe_fuse.py
+++ b/contrib/fuse/impl_a/tahoe_fuse.py
@@ -162,15 +162,19 @@ class TahoeFS (fuse.Fuse):
         self._init_rootdir()
 
     def _init_url(self):
-        f = open(os.path.join(self.confdir, 'webport'), 'r')
-        contents = f.read()
-        f.close()
-
-        fields = contents.split(':')
-        proto, port = fields[:2]
-        assert proto == 'tcp'
-        port = int(port)
-        self.url = 'http://localhost:%d' % (port,)
+        if os.path.exists(os.path.join(self.confdir, 'node.url')):
+            self.url = file(os.path.join(self.confdir, 'node.url'), 'rb').read().strip()
+            if not self.url.endswith('/'):
+                self.url += '/'
+        else:
+            f = open(os.path.join(self.confdir, 'webport'), 'r')
+            contents = f.read()
+            f.close()
+            fields = contents.split(':')
+            proto, port = fields[:2]
+            assert proto == 'tcp'
+            port = int(port)
+            self.url = 'http://localhost:%d' % (port,)
 
     def _init_rootdir(self):
         # For now we just use the same default as the CLI:
@@ -334,9 +338,11 @@ class TahoeNode (object):
             return TahoeFile(baseurl, uri)
         
     def __init__(self, baseurl, uri):
+        if not baseurl.endswith('/'):
+            baseurl += '/'
         self.burl = baseurl
         self.uri = uri
-        self.fullurl = '%s/uri/%s' % (self.burl, self.uri)
+        self.fullurl = '%suri/%s' % (self.burl, self.uri)
         self.inode = TahoeNode.NextInode
         TahoeNode.NextInode += 1
 
diff --git a/contrib/fuse/impl_b/pyfuse/tahoe.py b/contrib/fuse/impl_b/pyfuse/tahoe.py
index 835876fe..189298e4 100644
--- a/contrib/fuse/impl_b/pyfuse/tahoe.py
+++ b/contrib/fuse/impl_b/pyfuse/tahoe.py
@@ -30,15 +30,19 @@ class TahoeConnection:
         self._init_url()
 
     def _init_url(self):
-        f = open(os.path.join(self.confdir, 'webport'), 'r')
-        contents = f.read()
-        f.close()
-
-        fields = contents.split(':')
-        proto, port = fields[:2]
-        assert proto == 'tcp'
-        port = int(port)
-        self.url = 'http://localhost:%d' % (port,)
+        if os.path.exists(os.path.join(self.confdir, 'node.url')):
+            self.url = file(os.path.join(self.confdir, 'node.url'), 'rb').read().strip()
+            if not self.url.endswith('/'):
+                self.url += '/'
+        else:
+            f = open(os.path.join(self.confdir, 'webport'), 'r')
+            contents = f.read()
+            f.close()
+            fields = contents.split(':')
+            proto, port = fields[:2]
+            assert proto == 'tcp'
+            port = int(port)
+            self.url = 'http://localhost:%d/' % (port,)
 
     def get_root(self):
         # For now we just use the same default as the CLI:
@@ -61,7 +65,7 @@ class TahoeNode:
         return simplejson.loads(json)
 
     def _open(self, postfix=''):
-        url = '%s/uri/%s%s' % (self.conn.url, self.uri, postfix)
+        url = '%suri/%s%s' % (self.conn.url, self.uri, postfix)
         log('*** Fetching: %r', url)
         return urllib.urlopen(url)