diff danboorufs.py @ 4:c40f0eed70cd draft

Add a symlink mode, for performances reason.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 22 Aug 2012 09:02:54 +0200
parents 880904f1071f
children a422e75bf464
line wrap: on
line diff
--- a/danboorufs.py
+++ b/danboorufs.py
@@ -41,7 +41,7 @@ class Danbooru(LoggingMixIn, Operations)
     Represent a list of images as a filesystem tree, with nice tag filtering.
     '''
 
-    def __init__(self, tagfiles, root):
+    def __init__(self, tagfiles, root, use_symlinks):
         '''
         Takes a list of files containing the tags. They have to be named as the
         image, with ".tags" at the end.
@@ -73,6 +73,7 @@ class Danbooru(LoggingMixIn, Operations)
         print('[%d] Index done.' % (time() - start))
 
         self.root = root
+        self.use_symlinks = use_symlinks
         self.rwlock = Lock()
 
     def _split_path(self, path):
@@ -103,10 +104,18 @@ class Danbooru(LoggingMixIn, Operations)
         _, filename = self._split_path(path)
         path = filename if filename else self.root
         stat = os.lstat(path)
-        return dict((key, getattr(stat, key)) for key in ('st_atime',
+        stat = dict((key, getattr(stat, key)) for key in ('st_atime',
                         'st_ctime', 'st_gid', 'st_mode', 'st_mtime',
                         'st_nlink', 'st_size', 'st_uid'))
 
+        if self.use_symlinks:
+            # Those modes are respectively for a symlink and a directory.
+            stat['st_mode'] = 0o120700 if filename else 0o40700
+            if filename:
+                stat['st_size'] = len(filename)
+
+        return stat
+
     getxattr = None
     listxattr = None
 
@@ -157,7 +166,9 @@ class Danbooru(LoggingMixIn, Operations)
         self.cache[key] = list(taglist) + list(files)
         return ['.', '..'] + self.cache[key]
 
-    readlink = os.readlink
+    def readlink(self, path):
+        _, filename = self._split_path(path)
+        return filename
 
     def release(self, path, file_handle):
         return os.close(file_handle)
@@ -173,12 +184,24 @@ class Danbooru(LoggingMixIn, Operations)
     utimens = os.utime
 
 
+def main(args):
+    mountpoint = args.pop()
+
+    if args[1] == '-s' or args[1] == '--use-symlinks':
+        use_symlinks = True
+        filelist = args[2:]
+    else:
+        use_symlinks = False
+        filelist = args[1:]
+
+    FUSE(Danbooru(filelist, os.path.dirname(mountpoint), use_symlinks),
+         mountpoint, foreground=True)
+
+
 if __name__ == '__main__':
     if len(argv) < 3:
-        print('usage: %s <tag file> [<tag file>...] <mountpoint>' % argv[0])
+        print('USAGE: %s' % argv[0], '[-s|--use-symlinks]',
+              '<tag file> [<tag file>...]', '<mountpoint>')
         exit(1)
 
-    mountpoint = argv.pop()
-
-    fuse = FUSE(Danbooru(argv[1:], os.path.dirname(mountpoint)), mountpoint,
-                foreground=True)
+    main(argv)