Thanks to visit codestin.com
Credit goes to github.com

Skip to content
joe42 edited this page May 4, 2014 · 3 revisions

Developer Resources

Implementations

CloudFusion: A file system to access cloud storage

Methods

The methods of the Operation class with return value examples.

from errno import *
import stat
def getattr(self, path, fh=None):
    '''This method is called before all operations at least once, to check if the file object at *path* exists at all'''
    if does_not_exist(path):      # pseudo code to check if file object at path exists
        raise FuseOSError(ENOENT) # Throw a FuseOSError with any errno code (ENOENT means file object does not exist)
    now = time.time()
    st = {}
    # mode decides access permissions and if file object is a directory (stat.S_IFDIR), file (stat.S_IFREG) or a special file
    st['st_mode']   = 0777 | stat.S_IFREG
    st['st_ino']    = 0
    st['st_dev']    = 0
    st['st_nlink']  = 1
    st['st_uid']    = os.getuid() #file object's user id
    st['st_gid']    = os.getgid() #file object's group id
    st['st_size']   = 100    #size in bytes
    st['st_atime']  = now  #last access time in seconds
    st['st_mtime']  = now  #last modified time in seconds
    st['st_ctime']  = now
    #st_blocks is the amount of blocks of the file object, and depends on the block size of the file system (here: 512 Bytes)
    st['st_blocks'] = (int) ((st['st_size'] + 511) / 512)
    return st
def open(self, path, flags):
    '''This method is called before the first call to the read or write methods,
       to open a file. May open a file at path, i.e. setting flags for permission or mode.
       The return value can be a file object, which will be passed as the fh parameter
       to subsequent calls to the methods read or write.
       This method does not need to be implemented; you can also open the file in the
       read/write methods.'''
return 0 # or return a file object
def read(self, path, size, offset, fh):
    '''This method is called when you read a file. I.e. by calling cat /my_filesystem/my_file.
       It may be called multiple times, to read data sequentially or at random.
       But first the method getattr will be called once and then the method open.
       Returns data as a string.
       Should return *size* bytes of data associated with the file *path* at position *offset*.'''
return "Hello World" # note that getattr needs to set st_size to 11, so that cat reads 11 bytes
# or better yet use string slice:
# return "Hello World!"[ offset : offset+size ]
def write(self, path, buf, offset, fh):
    '''This method is called when you write to a file.
       I.e. by calling echo "hello world" > /my_filesystem/my_file
       It may be called multiple times, to write data sequentially or at random.
       But first the method getattr will be called once and then the method open.
       Returns data as a string.
       Should write the strin in buf to the file *path* at position *offset*.'''
return len(buf) # number of bytes written
def readdir(self, path, fh):
    '''This method is called when you list a directory.
       I.e. by calling ls /my_filesystem/
       The method getattr is called first, to see if path exists and is a directory.
       Returns a list of strings with the file or directory names in the directory *path*.'''
file_objects = [".", "..", "my_file"]
return file_objects
Clone this wiki locally