Skip to content

Instantly share code, notes, and snippets.

@bpowers
Created August 20, 2014 13:01
Show Gist options
  • Save bpowers/d90bbb48845fc3e825f1 to your computer and use it in GitHub Desktop.
Save bpowers/d90bbb48845fc3e825f1 to your computer and use it in GitHub Desktop.
package main
import (
"bazil.org/fuse"
"bazil.org/fuse/fs"
"flag"
"log"
"os"
)
type FS struct {
inodeNum chan uint64
root Dir
}
func NewFS() (*FS, error) {
fs := &FS{inodeNum: make(chan uint64)}
fs.root.inode = 1
fs.root.file = File{2, "file.txt"}
return fs, nil
}
func (fs *FS) Root() (fs.Node, fuse.Error) {
return &fs.root, nil
}
type Dir struct {
inode uint64
file File
}
func (d *Dir) Attr() fuse.Attr {
return fuse.Attr{Inode: d.inode, Mode: os.ModeDir | 0555}
}
func (d *Dir) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) {
if name == d.file.name {
return &d.file, nil
}
return nil, fuse.ENOENT
}
func (d *Dir) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
return []fuse.Dirent{
{
Inode: d.file.inode,
Type: fuse.DT_File,
Name: d.file.name,
},
}, nil
}
type File struct {
inode uint64
name string
}
func (f *File) Attr() fuse.Attr {
return fuse.Attr{Inode: f.inode, Mode: 0444}
}
func (f *File) ReadAll(intr fs.Intr) ([]byte, fuse.Error) {
log.Printf("readall: %s", f.name)
return []byte(f.name), nil
}
func main() {
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
os.Exit(1)
}
hellofs, err := NewFS()
if err != nil {
log.Fatalf("FS(): %s", err)
}
c, err := fuse.Mount(flag.Arg(0))
if err != nil {
log.Fatalf("Mount(%s): %s", flag.Arg(0), err)
}
defer c.Close()
if err = fs.Serve(c, hellofs); err != nil {
log.Fatalf("Serve: %s", err)
}
<-c.Ready
if err := c.MountError; err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment