Updated: Dec. 7, 2010, 2:37 p.m.

File systems in User-space with Python

Notes on legoing with FUSE and specifically with user space implementation in Python, this tutorial came in handy.

sudo apt-get install python-fuse

File System Skeleton

#!/usr/bin/python
import pprint
import errno
import time
import stat
import os

try:
    import _find_fuse_parts
except ImportError:
    pass
import fuse
from fuse import Fuse

if not hasattr(fuse, '__version__'):
    raise RuntimeError, \
        "your fuse-py doesn't know of fuse.__version__, probably it's too old."

fuse.fuse_python_api = (0, 2)

def log(fn):

  def wrapped(*args, **kwargs):
    fd = open("/tmp/skeletor.log", "a")
    fd.write("***"+fn.__name__+str(args)+str(kwargs)+"\n")
    fd.close()
    return fn(*args, **kwargs)

  wrapped.__doc__ = fn.__doc__
  return wrapped

class Skeletor(Fuse):
  """Skeletor File System."""

  @log
  def __init__(self, *args, **kw):    
    Fuse.__init__(self, *args, **kw)

  @log
  def getattr(self, path):
    return -errno.ENOSYS

  @log
  def readdir(self, path, offset):
    return -errno.ENOSYS

  @log
  def mythread ( self ):
    return -errno.ENOSYS

  @log
  def chmod ( self, path, mode ):
    return -errno.ENOSYS

  @log
  def chown ( self, path, uid, gid ):
    return -errno.ENOSYS

  @log
  def fsync ( self, path, isFsyncFile ):
    return -errno.ENOSYS

  @log
  def link ( self, targetPath, linkPath ):
    return -errno.ENOSYS

  @log
  def mkdir ( self, path, mode ):
    return -errno.ENOSYS

  @log
  def mknod ( self, path, mode, dev ):
    return -errno.ENOSYS

  @log
  def open ( self, path, flags ):
    return -errno.ENOSYS

  @log
  def read ( self, path, length, offset ):
    return -errno.ENOSYS

  @log
  def readlink ( self, path ):
    return -errno.ENOSYS

  @log
  def release ( self, path, flags ):
    return -errno.ENOSYS

  @log
  def rename ( self, oldPath, newPath ):
    return -errno.ENOSYS

  @log
  def rmdir ( self, path ):
    return -errno.ENOSYS

  @log
  def statfs ( self ):
    return -errno.ENOSYS

  @log
  def symlink ( self, targetPath, linkPath ):
    return -errno.ENOSYS

  @log
  def truncate ( self, path, size ):
    return -errno.ENOSYS

  @log
  def unlink ( self, path ):
    return -errno.ENOSYS

  @log
  def utime ( self, path, times ):
    return -errno.ENOSYS

  @log
  def write ( self, path, buf, offset ):
    return -errno.ENOSYS

def main():

  usage="""Skeletor - A Skeleton for PythonFuse File Systems.""" + Fuse.fusage
  server = Skeletor(
    version="%prog " + fuse.__version__,
    usage=usage,
    dash_s_do='setsingle'
  )

  server.parse(errex=1)
  server.main()

if __name__ == '__main__':
  main()