Source

go-extra / fuse / example / hello.go

Full commit
package main

import (
	. "bitbucket.org/taruti/go-extra/fuse"
	"flag"
	"fmt"
)

func main() {
	flag.Parse()
	if flag.NArg() != 1 {
		panic("Should be called with the mountpoint")
	}

	fuseFd, err := MountFuse(flag.Arg(0), []string{})
	fmt.Printf("f = %v, err = %v\n", fuseFd, err)
	defer UnmountFuse(flag.Arg(0))
	err = FuseLoop(My{}, fuseFd)
	fmt.Printf("FuseLoop -> err = %v\n", err)
	fmt.Printf("done\n")
}


type My struct{}

func newDirAttr(ino uint64) *Attr {
	a := new(Attr)
	a.Ino = ino
	a.Mode = S_IFDIR
	return a
}

func (My) FuseInit(*Init) Error {
	return OK
}
func (My) FuseForget(*Forget) {}

func (My) FuseGetAttr(p *GetAttr) (*Attr, Error) {
	return newDirAttr(p.Nodeid), OK
}

func (My) FuseGetXAttrSize(h *Header, attr []byte) (int, Error) {
	return 0, ENOATTR
}
func (My) FuseGetXAttr(h *Header, attr []byte) ([]byte, Error) {
	return nil, ENOATTR
}
func (My) FuseListXAttr(h *Header, x *XAttrList) Error {
	return OK
}
func (My) FuseDelete(h *Header, name []byte) Error {
	return EPERM
}

var myDir = Dir{}

func (My) FuseOpen(o *Open) (Flags, FuseFile, Error) {
	return 0, &myDir, OK
}
func (My) FuseAccess(*Access) Error {
	return OK
}
func (My) FuseLookup(h *Header, n []byte) (*Attr, Error) {
	if string(n) == "hello" {
		return newDirAttr(h.Nodeid + 1), OK
	}
	if string(n) == "hallo" {
		return newDirAttr(h.Nodeid + 2), OK
	}
	return nil, ENOENT
}
func (My) FuseMkDir(*MkDir, []uint8) (*Attr, Error) { return nil, EPERM }
func (My) FuseCreate(c *Create, n []byte) (Flags, FuseFile, *Attr, Error) {
	fmt.Printf("Tried to create %s\n", n)
	return 0,nil,nil,EPERM
}

func (My) NilFile() FuseFile { return &myDir }

type Dir struct{}

func (*Dir) FuseFlush(*Flush) Error { return OK }
func (*Dir) FuseRead(r *Io) ([]uint8, Error) {
	offset := r.Offset
	fmt.Printf("read offset = %d new\n", offset)
	if offset > 0 {
		return nil, OK
	} // FIXME

	de := new(DEntryList)
	if r.Nodeid == 1 {
		de.AddString("hello", r.Nodeid+1, S_IFDIR)
		de.AddString("hallo", r.Nodeid+2, S_IFDIR)
	}
	return de.Bytes(), OK
}
func (*Dir) FuseRelease(*Release) Error             { return OK }
func (*Dir) FuseWrite(*Io, []uint8) (uint64, Error) { return 0, EPERM }