使用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