github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/lib/python/fusepy/memory3.py (about)

     1  #!/usr/bin/env python
     2  
     3  from fuse3 import FUSE, Operations, LoggingMixIn
     4  
     5  from collections import defaultdict
     6  from errno import ENOENT
     7  from stat import S_IFDIR, S_IFLNK, S_IFREG
     8  from sys import argv, exit
     9  from time import time
    10  
    11  import logging
    12  
    13  
    14  class Memory(LoggingMixIn, Operations):
    15      """Example memory filesystem. Supports only one level of files."""
    16      
    17      def __init__(self):
    18          self.files = {}
    19          self.data = defaultdict(bytearray)
    20          self.fd = 0
    21          now = time()
    22          self.files['/'] = dict(st_mode=(S_IFDIR | 0o755), st_ctime=now,
    23              st_mtime=now, st_atime=now, st_nlink=2)
    24          
    25      def chmod(self, path, mode):
    26          self.files[path]['st_mode'] &= 0o770000
    27          self.files[path]['st_mode'] |= mode
    28          return 0
    29  
    30      def chown(self, path, uid, gid):
    31          self.files[path]['st_uid'] = uid
    32          self.files[path]['st_gid'] = gid
    33      
    34      def create(self, path, mode):
    35          self.files[path] = dict(st_mode=(S_IFREG | mode), st_nlink=1,
    36              st_size=0, st_ctime=time(), st_mtime=time(), st_atime=time())
    37          self.fd += 1
    38          return self.fd
    39      
    40      def getattr(self, path, fh=None):
    41          if path not in self.files:
    42              raise OSError(ENOENT, '')
    43          st = self.files[path]
    44          return st
    45      
    46      def getxattr(self, path, name, position=0):
    47          attrs = self.files[path].get('attrs', {})
    48          try:
    49              return attrs[name]
    50          except KeyError:
    51              return ''       # Should return ENOATTR
    52      
    53      def listxattr(self, path):
    54          attrs = self.files[path].get('attrs', {})
    55          return attrs.keys()
    56      
    57      def mkdir(self, path, mode):
    58          self.files[path] = dict(st_mode=(S_IFDIR | mode), st_nlink=2,
    59                  st_size=0, st_ctime=time(), st_mtime=time(), st_atime=time())
    60          self.files['/']['st_nlink'] += 1
    61      
    62      def open(self, path, flags):
    63          self.fd += 1
    64          return self.fd
    65      
    66      def read(self, path, size, offset, fh):
    67          return bytes(self.data[path][offset:offset + size])
    68      
    69      def readdir(self, path, fh):
    70          return ['.', '..'] + [x[1:] for x in self.files if x != '/']
    71      
    72      def readlink(self, path):
    73          return self.data[path].decode('utf-8')
    74      
    75      def removexattr(self, path, name):
    76          attrs = self.files[path].get('attrs', {})
    77          try:
    78              del attrs[name]
    79          except KeyError:
    80              pass        # Should return ENOATTR
    81      
    82      def rename(self, old, new):
    83          self.files[new] = self.files.pop(old)
    84      
    85      def rmdir(self, path):
    86          self.files.pop(path)
    87          self.files['/']['st_nlink'] -= 1
    88      
    89      def setxattr(self, path, name, value, options, position=0):
    90          # Ignore options
    91          attrs = self.files[path].setdefault('attrs', {})
    92          attrs[name] = value
    93      
    94      def statfs(self, path):
    95          return dict(f_bsize=512, f_blocks=4096, f_bavail=2048)
    96      
    97      def symlink(self, target, source):
    98          source = source.encode('utf-8')
    99          self.files[target] = dict(st_mode=(S_IFLNK | 0o777), st_nlink=1,
   100              st_size=len(source))
   101          self.data[target] = bytearray(source)
   102      
   103      def truncate(self, path, length, fh=None):
   104          del self.data[path][length:]
   105          self.files[path]['st_size'] = length
   106      
   107      def unlink(self, path):
   108          self.files.pop(path)
   109      
   110      def utimens(self, path, times=None):
   111          now = time()
   112          atime, mtime = times if times else (now, now)
   113          self.files[path]['st_atime'] = atime
   114          self.files[path]['st_mtime'] = mtime
   115      
   116      def write(self, path, data, offset, fh):
   117          del self.data[path][offset:]
   118          self.data[path].extend(data)
   119          self.files[path]['st_size'] = len(self.data[path])
   120          return len(data)
   121  
   122  
   123  if __name__ == "__main__":
   124      if len(argv) != 2:
   125          print('usage: %s <mountpoint>' % argv[0])
   126          exit(1)
   127      logging.getLogger().setLevel(logging.DEBUG)
   128      fuse = FUSE(Memory(), argv[1], foreground=True)