fix single key files and cache vsys call for efficiency
authorBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Wed, 23 Jun 2010 20:52:14 +0000 (22:52 +0200)
committerBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Wed, 23 Jun 2010 20:52:14 +0000 (22:52 +0200)
omf_keys/key_fs.py

index e3deadc..6993435 100644 (file)
@@ -28,9 +28,14 @@ from fuse import Fuse
 fuse.fuse_python_api = (0, 2)
 
 keys_dir = "/keys"
-
+all_ssh_keys = None
+all_ssh_keys_dict = None
 
 def read_keys():
+    global all_ssh_keys
+    if all_ssh_keys:
+        return all_ssh_keys
+        
     fin = os.open("/vsys/publickeys.in", os.O_NONBLOCK | os.O_WRONLY)
     fout = os.open("/vsys/publickeys.out", os.O_NONBLOCK | os.O_RDONLY)
     
@@ -43,22 +48,46 @@ def read_keys():
     fcntl.fcntl(fout, fcntl.F_SETFL, out_flags & ~os.O_NONBLOCK)
     
     f = os.fdopen(fout, "r")
-    return f.read()
+    all_ssh_keys = f.read()
+    return all_ssh_keys
+
 
 def all_keys():
-    keys = read_keys()
+    global all_ssh_keys_dict
+    if all_ssh_keys_dict:
+        return all_ssh_keys_dict
 
+    keys = read_keys()
     files = {}
     num = 0
     for line in keys.split('\n'):
         line = line.strip()
+        if not line: continue
+
+        filename = ""
+        in_key = False
+        in_name = False
         fields = line.split()
-        try:
-            name = fields[2]
-            files[name] = line
-        except:
+        for f in fields:
+            f = f.strip()
+            if f.startswith("ssh-"):
+                in_key = True
+                continue
+            elif in_key:
+                in_name = True
+                in_key = False
+                continue
+            elif in_name:
+                if filename:
+                    filename = "%s_%s" % (filename, f)
+                else:
+                    filename = f
+
+        if not filename:
             num += 1
             files["unnamed_key%d" % num] = line
+        else:
+            files[filename] = "%s\n" % line
 
     return files
 
@@ -125,7 +154,7 @@ class KeyFS(Fuse):
         if offset < slen:
             if offset + size > slen:
                 size = slen - offset
-            buf = keys[offset:offset+size]
+            buf = content[offset:offset+size]
         else:
             buf = ''
         return buf