Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #476 from gnurag/master
[toyFS] Added a toy filesystem.
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# toyfs | ||
Toy filesystem based on FUSE | ||
|
||
## Installation | ||
yum install fuse fuse-python | ||
|
||
## Mounting toyFS | ||
python toyfs.py /path/to/mount/point | ||
|
||
## Unmounting | ||
fusermount -u /path/to/mount/point |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import fuse | ||
import stat | ||
import errno | ||
import time | ||
|
||
class ToyStat(fuse.Stat): | ||
def __init__(self): | ||
""" | ||
The following stat structure members are implemented. | ||
""" | ||
self.st_mode = 0 # (protection bits) | ||
self.st_ino = 0 # (inode number) | ||
self.st_dev = 0 # (device) | ||
self.st_nlink = 0 # (number of hard links) | ||
self.st_uid = 500 # (user ID of owner) | ||
self.st_gid = 500 # (group ID of owner) | ||
self.st_size = 0 # (size of file, in bytes) | ||
self.st_atime = 0 # (time of most recent access) | ||
self.st_mtime = 0 # (time of most recent content modification) | ||
self.st_ctime = 0 # (time of most recent metadata change) | ||
|
||
# Helper for Toy filesystem | ||
class Toy(): | ||
def __init__(self): | ||
self.data = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In augue sapien, congue fermentum tortor ut, consequat maximus posuere.\n' | ||
|
||
def getattr(self, path): | ||
st = ToyStat() | ||
if path.endswith('.txt'): | ||
st.st_mode = stat.S_IFREG | 0644 | ||
st.st_size = len(self.data) | ||
else: | ||
st.st_mode = stat.S_IFDIR | 0755 | ||
return st | ||
|
||
def readdir(self, path): | ||
contents = { | ||
'/': ['hello', 'world', 'docs'], | ||
'/hello': ['welt'], | ||
'/hello/welt': ['last_level'], | ||
'/docs': ['readme.txt'] | ||
} | ||
res = ['.','..'] + contents.get(path) | ||
return res | ||
|
||
def read(self, path, length, offset): | ||
return self.data | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env python | ||
|
||
import os | ||
import logging | ||
import fuse | ||
import time | ||
|
||
import toy | ||
|
||
fuse.fuse_python_api = (0, 2) | ||
|
||
# Logging | ||
logger = logging.getLogger("toyfs") | ||
handler = logging.FileHandler('toyfs.log') | ||
logger.addHandler(handler) | ||
logger.setLevel(logging.INFO) | ||
|
||
fs = toy.Toy() | ||
|
||
# toyFS | ||
class ToyFS(fuse.Fuse): | ||
def getattr(self, path): | ||
logger.info("[getattr] (path=%s)" % path) | ||
st = fs.getattr(path) | ||
return st | ||
|
||
def readdir(self, path, offset): | ||
logger.info("[readdir] (path=%s, offset=%s)" % (path, offset)) | ||
directories = fs.readdir(path) | ||
for directory in directories: | ||
yield fuse.Direntry(directory) | ||
|
||
def open(self, path, flags): | ||
logger.info("[open] (path=%s, flags=%s)" % (path, flags)) | ||
|
||
def read(self, path, length, offset): | ||
logger.info("[read] (path=%s, length=%s, offset=%s)" % (path, length, offset)) | ||
return fs.read(path, length, offset) | ||
|
||
if __name__ == '__main__': | ||
toyfs = ToyFS(version = '%prog ' + '0.0.1', | ||
usage = 'Toy filesystem ' + fuse.Fuse.fusage, | ||
dash_s_do = 'setsingle') | ||
toyfs.parse(errex = 1) | ||
toyfs.flags = 0 | ||
toyfs.multithreaded = 0 | ||
toyfs.main() |