fuse的API修改
fuse的API修改
這里的fuse API基于python進行修改fuse的安裝請參照fuse的安裝博客
?
在你掛載的目錄下你進行的操作才會調(diào)用到的FUSE的文件系統(tǒng),例如,你將/usr 掛載到了/opt/fuse下面,當你進入/opt/fuse下以后你使用的指令才是你寫的文件系統(tǒng)的指令
這些指令不光是由一個fuse的函數(shù)執(zhí)行而是由多個函數(shù)共同實現(xiàn)的
如:cd 操作調(diào)用到的函數(shù)由
_full_path
getattr
access
_full_path函數(shù)返回當前文件的原本路徑
getattr函數(shù)事項文件屬性的獲取
access函數(shù)實現(xiàn)當前徑的轉(zhuǎn)換
所以必須修改這兩個函數(shù)才能實現(xiàn)cd操作
修改API大多調(diào)用python中的os,os.path庫函數(shù),大家可以試著去看一看這些庫函數(shù)
如_full_path函數(shù)
def _full_path(self, partial):
? ? ? ? if partial.startswith("/"):
? ? ? ? ? ? partial = partial[1:]
? ? ? ? path = os.path.join(self.root, partial)
?
? ? ? ? return path
如getattr修改代碼:
? ? def getattr(self, path, fh=None):
? ? ? ? full_path = self._full_path(path)
? ? ? ? st = os.lstat(full_path)
? ? ? ? return dict((key, getattr(st, key)) for key in ('st_atime', 'st_ctime',
?
? ? ? ? ? ? ? ? ? ? ?'st_gid', 'st_mode', 'st_mtime', 'st_nlink', 'st_size', 'st_uid'))
?
如access代碼:
? ? def access(self, path, mode):
? ? ? ? full_path = self._full_path(path)
? ? ? ? if not os.access(full_path, mode):
?
? ? ? ? ? ? raise FuseOSError(errno.EACCES)
以下提供以下指令調(diào)用函數(shù)的順序
---掛載
_init_
?
---cd
?
---getattr
_full_path
access
?
_full_path
?
---ls
readdir
_full_path
getattr
_full_path
readline
_full_path
?
getattr
?
---mkdir
?
getattr
_full_path
mkdir
_full_path
getattr
?
_full_path
?
---rm
?
getattr
_full_path
getattr
_full_path
readdir
_full_path
rmdir
?
_full_path
?
---tab(鍵)
readdir
_full_path
getattr
?
_full_path
?
---cat
?
getattr
_full_path
open
_full_path
read
getattr
_full_path
flush
release
?
?
附帶一個可運行的fuse
#!/usr/bin/env python
?
from __future__ import with_statement
?
import os
import sys
import errno
?
from fuse import FUSE, FuseOSError, Operations
?
class Passthrough(Operations):
?
def __init__(self, root):
? ? ? ? self.root = root
?
? ?def _full_path(self, partial):
? ? ? ? if partial.startswith("/"):
? ? ? ? ? ? partial = partial[1:]
? ? ? ? path = os.path.join(self.root, partial)
? ? ? ? return path
?
? ? def access(self, path, mode):
? ? ? ? full_path = self._full_path(path)
? ? ? ? if not os.access(full_path, mode):
? ? ? ? ? ? raise FuseOSError(errno.EACCES)
?
? ? def getattr(self, path, fh=None):
? ? ? ? full_path = self._full_path(path)
? ? ? ? st = os.lstat(full_path)
? ? ? ? return dict((key, getattr(st, key)) for key in ('st_atime', 'st_ctime',
? ? ? ? ? ? ? ? ? ? ?'st_gid', 'st_mode', 'st_mtime', 'st_nlink', 'st_size', 'st_uid'))
?
? ?def readdir(self, path, fh):
? ? ? ? full_path = self._full_path(path)
?
? ? ? ? dirents = ['.', '..']
? ? ? ? if os.path.isdir(full_path):
? ? ? ? ? ? dirents.extend(os.listdir(full_path))
? ? ? ? for r in dirents:
? ? ? ? ? ? yield r
?
def main(mountpoint, root):
? ? FUSE(Passthrough(root), mountpoint, foreground=True)
?
if __name__ == '__main__':
?
? ? main(sys.argv[2], sys.argv[1])
總結(jié)
以上是生活随笔為你收集整理的fuse的API修改的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nginx生成自定义证书
- 下一篇: rac下重做控制文件