-
Notifications
You must be signed in to change notification settings - Fork 194
Home
joe42 edited this page May 4, 2014
·
3 revisions
- Linux operations like writing a file with echo "content">/myfuse_mnt/file result in the call of multiple methods: http://www.slideshare.net/matteobertozzi/pythonfuse-pycon4
| CloudFusion: | A file system to access cloud storage |
|---|
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 stdef 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 objectdef 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 writtendef 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