使用go-fuse开发一个fuse 文件系统


go-fuse 是fuse 的包装,我们可以用来开发fuse 文件系统,以下是一个简单的学习

项目准备

  • go mod
go mod init demoapp
  • 添加依赖
go get github.com/hanwen/go-fuse/v2
  • 简单代码
main.go
package main
import (
    "context"
    "flag"
    "log"
    "syscall"
    "github.com/hanwen/go-fuse/v2/fs"
    "github.com/hanwen/go-fuse/v2/fuse"
)
type HelloRoot struct {
    fs.Inode
}
func (r *HelloRoot) OnAdd(ctx context.Context) {
    ch := r.NewPersistentInode(
        ctx, &fs.MemRegularFile{
            Data: []byte("file.txt"),
            Attr: fuse.Attr{
                Mode: 0644,
            },
        }, fs.StableAttr{Ino: 2})
    r.AddChild("file.txt", ch, false)
}
func (r *HelloRoot) Getattr(ctx context.Context, fh fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
    out.Mode = 0755
    return 0
}
var _ = (fs.NodeGetattrer)((*HelloRoot)(nil))
var _ = (fs.NodeOnAdder)((*HelloRoot)(nil))
func main() {
    debug := flag.Bool("debug", false, "print debug data")
    flag.Parse()
    if len(flag.Args()) < 1 {
        log.Fatal("Usage:\n hello MOUNTPOINT")
    }
    opts := &fs.Options{}
    opts.Debug = *debug
    server, err := fs.Mount(flag.Arg(0), &HelloRoot{}, opts)
    if err != nil {
        log.Fatalf("Mount fail: %v\n", err)
    }
    server.Wait()
}

构建&&运行

  • 构建
go build
  • 运行
mkdir /demo
./demoapp /demo
  • 效果

 

 

参考资料

https://github.com/hanwen/go-fuse


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM