fuse的API修改

fuse的API修改

这里的fuse API基于python进行修改fuse的安装请参照fuse的安装博客

 

在你挂载的目录下你进行的操作才会调用到的FUSE的文件系统,例如,你将/usr 挂载到了/opt/fuse下面,当你进入/opt/fuse下以后你使用的指令才是你写的文件系统的指令

这些指令不光是由一个fuse的函数执行而是由多个函数共同实现的

如:cd 操作调用到的函数由

_full_path

getattr

access

_full_path函数返回当前文件的原本路径

getattr函数事项文件属性的获取

access函数实现当前径的转换

所以必须修改这两个函数才能实现cd操作

修改API大多调用python中的os,os.path库函数,大家可以试着去看一看这些库函数

如_full_path函数

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)

以下提供以下指令调用函数的顺序

---挂载

_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])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Linux中的FUSE(Filesystem in Userspace)是一个允许用户自定义文件系统的接口,它将文件系统的实现放在了用户空间中。FUSE的核心代码包括fuse_kern_chan_send、fuse_kern_chan_receive和fuse_ll_process等函数。fuse_kern_chan_send函数用于向fuse_chan发送数据,而fuse_kern_chan_receive函数用于从fuse_chan接收数据。fuse_ll_process函数则是处理从文件系统收到的请求。 通过使用FUSE接口,用户可以在Linux系统中创建自定义的文件系统,从而实现对特定需求的定制化。用户可以使用FUSE提供的API来实现文件系统的各种操作,包括文件读写、目录遍历、权限控制等等。FUSE将用户空间中的操作映射到内核空间中的文件系统,使得用户可以通过常规的系统调用来访问和操作这个文件系统。 总结起来,Linux中的FUSE是一个允许用户自定义文件系统的接口,通过fuse_kern_chan_send、fuse_kern_chan_receive和fuse_ll_process等函数,用户可以在用户空间中实现自己的文件系统,并通过常规的系统调用进行访问和操作。这为用户带来了更大的灵活性和可定制性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [linux fuse 阻塞,FUSE原理总结](https://blog.csdn.net/weixin_30356433/article/details/116964336)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懒人烂命

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值