Skip to content

Instantly share code, notes, and snippets.

@Zirak
Created September 15, 2015 11:06
Show Gist options
  • Save Zirak/63ea1d1ec580923470c7 to your computer and use it in GitHub Desktop.
Save Zirak/63ea1d1ec580923470c7 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"math/rand"
"os"
"strconv"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"golang.org/x/net/context"
)
func main() {
client, err := fuse.Mount(
"mnt",
fuse.FSName("stupid-rand-example"),
fuse.Subtype("stupid-rand-example"),
)
if err != nil {
log.Fatal(err)
}
defer client.Close()
fs.Serve(client, FS{})
<-client.Ready
if err := client.MountError; err != nil {
log.Fatal(err)
}
log.Println("Bye bye!")
}
type FS struct{}
func (FS) Root() (fs.Node, error) {
return RandDir{}, nil
}
type RandDir struct{}
func (RandDir) Attr(ctx context.Context, attr *fuse.Attr) error {
log.Println("RandDir Attr")
attr.Mode = os.ModeDir | 0555
return nil
}
func (RandDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
log.Println("RandDir ReadDirAll")
ret := []fuse.Dirent{}
for i := 0; i < 10; i += 1 {
ret = append(ret, fuse.Dirent{Name: strconv.Itoa(i), Type: fuse.DT_Dir})
}
return ret, nil
}
func (RandDir) Lookup(ctx context.Context, name string) (fs.Node, error) {
log.Println("RandDir lookup")
return RandNumberDir{rand.Int()}, nil
}
type RandNumberDir struct {
number int
}
func (RandNumberDir) Attr(ctx context.Context, attr *fuse.Attr) error {
log.Println("RandNumberDir Attr")
attr.Mode = os.ModeDir | 0555
return nil
}
func (dir RandNumberDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
return []fuse.Dirent{
{Name: "rand", Type: fuse.DT_File},
}, nil
}
func (dir RandNumberDir) Lookup(ctx context.Context, name string) (fs.Node, error) {
log.Println("RandNumberDir Lookup")
if name == "rand" {
return StupidFile{strconv.Itoa(dir.number)}, nil
}
return nil, fuse.ENOENT
}
type StupidFile struct {
content string
}
func (file StupidFile) Attr(ctx context.Context, attr *fuse.Attr) error {
log.Printf("StupidFile (%s) Attr\n", file.content)
attr.Mode = 0444
attr.Size = uint64(len(file.content))
return nil
}
func (file StupidFile) ReadAll(ctx context.Context) ([]byte, error) {
log.Printf("StupidFile (%s) ReadAll\n", file.content)
return []byte(file.content), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment