# # To start with: # python localfs.py mnt # # Stop with Ctrl-C or with # lufsumount mnt # import lufs, os, thread from lufs import FileSystem class Filesystem(FileSystem): openfiles = {} oflock = thread.allocate_lock() def readdir(self, dir): flist = os.listdir(dir) if not dir.endswith('/'): dir += '/' #return [(fn, tuple(os.lstat(dir+fn))) for fn in flist] return flist def stat(self, filename): return os.lstat(filename) def mkdir(self, dirname, mode): os.mkdir(dirname, mode) def rmdir(self, dirname): os.rmdir(dirname) def unlink(self, filename): os.unlink(filename) def rename(self, fromname, toname): os.rename(fromname, toname) def create(self, filename, mode): fd = os.open(filename, os.O_CREAT|os.O_WRONLY|os.O_TRUNC, mode) self.oflock.acquire() try: fd1 = self.openfiles.pop(filename, None) self.openfiles[filename] = fd finally: self.oflock.release() if fd1 is not None: os.close(fd1) def open(self, filename, flags): fd = os.open(filename, flags) self.oflock.acquire() try: fd1 = self.openfiles.pop(filename, None) self.openfiles[filename] = fd finally: self.oflock.release() if fd1 is not None: os.close(fd1) #O_ACCMODE 0003 #O_RDONLY 00 #O_WRONLY 01 #O_RDWR 02 #O_CREAT 0100 #O_EXCL 0200 #O_NOCTTY 0400 #O_TRUNC 01000 #O_APPEND 02000 #O_NONBLOCK 04000 #O_NDELAY O_NONBLOCK #O_SYNC 010000 #FASYNC 020000 #O_DIRECT 040000 #O_LARGEFILE 0100000 #O_DIRECTORY 0200000 #O_NOFOLLOW 0400000 def release(self, filename): self.oflock.acquire() try: fd = self.openfiles.pop(filename, None) finally: self.oflock.release() if fd is not None: os.close(fd) def read(self, filename, offset, count): self.oflock.acquire() try: fd = self.openfiles[filename] os.lseek(fd, offset, 0) return os.read(fd, count) finally: self.oflock.release() def write(self, filename, offset, buf): self.oflock.acquire() try: fd = self.openfiles[filename] os.lseek(fd, offset, 0) return os.write(fd, buf) finally: self.oflock.release() def readlink(self, link): return os.readlink(link) def link(self, target, link): os.link(target, link) def symlink(self, target, link): os.symlink(target, link) def setattr(self, filename, attribs): st = os.lstat(filename) mode, nlink, uid, gid, size, atime, mtime, ctime = attribs if st.st_size != size is not None: fd = os.open(filename, os.O_WRONLY) os.ftruncate(fd, size) os.close(fd) st = os.lstat(filename) if st.st_mode != mode is not None: os.chmod(filename, mode) if (st.st_uid != uid or st.st_gid != gid) and uid is not None: os.lchown(filename, uid, gid) if (st.st_mtime != mtime or st.st_atime != atime) and mtime is not None: os.utime(filename, (atime, mtime)) #... # ____________________________________________________________ if __name__ == '__main__': import sys lufs.mount(Filesystem, sys.argv[1])