This changes the data type for the buffer of the read and write syscalls from string to bytes. On Python 2, this has no effect. On Python 3, it is a breaking change, but fixes a serious usibility bug that limits file data to valid UTF-8 data. With these changes, files can contain arbitrary data.
91 lines
2.2 KiB
Python
91 lines
2.2 KiB
Python
#!/usr/bin/env python
|
|
|
|
# Copyright (C) 2006 Andrew Straw <strawman@astraw.com>
|
|
#
|
|
# This program can be distributed under the terms of the GNU LGPL.
|
|
# See the file COPYING.
|
|
#
|
|
|
|
import os, stat, errno
|
|
# pull in some spaghetti to make this stuff work without fuse-py being installed
|
|
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)
|
|
|
|
hello_path = '/hello'
|
|
hello_str = b'Hello World!\n'
|
|
|
|
class MyStat(fuse.Stat):
|
|
def __init__(self):
|
|
self.st_mode = 0
|
|
self.st_ino = 0
|
|
self.st_dev = 0
|
|
self.st_nlink = 0
|
|
self.st_uid = 0
|
|
self.st_gid = 0
|
|
self.st_size = 0
|
|
self.st_atime = 0
|
|
self.st_mtime = 0
|
|
self.st_ctime = 0
|
|
|
|
class HelloFS(Fuse):
|
|
|
|
def getattr(self, path):
|
|
st = MyStat()
|
|
if path == '/':
|
|
st.st_mode = stat.S_IFDIR | 0o755
|
|
st.st_nlink = 2
|
|
elif path == hello_path:
|
|
st.st_mode = stat.S_IFREG | 0o444
|
|
st.st_nlink = 1
|
|
st.st_size = len(hello_str)
|
|
else:
|
|
return -errno.ENOENT
|
|
return st
|
|
|
|
def readdir(self, path, offset):
|
|
for r in '.', '..', hello_path[1:]:
|
|
yield fuse.Direntry(r)
|
|
|
|
def open(self, path, flags):
|
|
if path != hello_path:
|
|
return -errno.ENOENT
|
|
accmode = os.O_RDONLY | os.O_WRONLY | os.O_RDWR
|
|
if (flags & accmode) != os.O_RDONLY:
|
|
return -errno.EACCES
|
|
|
|
def read(self, path, size, offset):
|
|
if path != hello_path:
|
|
return -errno.ENOENT
|
|
slen = len(hello_str)
|
|
if offset < slen:
|
|
if offset + size > slen:
|
|
size = slen - offset
|
|
buf = hello_str[offset:offset+size]
|
|
else:
|
|
buf = b''
|
|
return buf
|
|
|
|
def main():
|
|
usage="""
|
|
Userspace hello example
|
|
|
|
""" + Fuse.fusage
|
|
server = HelloFS(version="%prog " + fuse.__version__,
|
|
usage=usage,
|
|
dash_s_do='setsingle')
|
|
|
|
server.parse(errex=1)
|
|
server.main()
|
|
|
|
if __name__ == '__main__':
|
|
main()
|