package main import ( "log" "os" "context" "syscall" "github.com/hanwen/go-fuse/v2/fs" "github.com/hanwen/go-fuse/v2/fuse" "strconv" "strings" ) func (i *Device) HeartRateBytes() ([]byte, error) { v, err := i.HeartRate() return []byte(strconv.Itoa(int(v)) + "\n"), err } func (i *Device) BatteryLevelBytes() ([]byte, error) { v, err := i.BatteryLevel() return []byte(strconv.Itoa(int(v)) + "\n"), err } func (i *Device) StepCountBytes() ([]byte, error) { v, err := i.StepCount() return []byte(strconv.Itoa(int(v)) + "\n"), err } func (i *Device) MotionBytes() ([]byte, error) { v, err := i.Motion() return []byte(strconv.Itoa(int(v.X)) + " " + strconv.Itoa(int(v.Y)) + " " + strconv.Itoa(int(v.Z)) + "\n"), err } func (i *Device) AddressBytes() ([]byte, error) { v := i.Address() return []byte(v + "\n"), nil } func (i *Device) VersionBytes() ([]byte, error) { v, err := i.Version() return []byte(v + "\n"), err } type ITProperty struct { name string Ino uint64 f func() ([]byte, error) } type ITNode struct { fs.Inode kind int Ino uint64 lst []DirEntry self DirEntry path string } var properties = []ITProperty { ITProperty{"heartrate", 2, nil}, ITProperty{"battery", 3, nil}, ITProperty{"motion", 4, nil}, ITProperty{"stepcount", 5, nil}, ITProperty{"version", 6, nil}, ITProperty{"address", 7, nil}, } var myfs *FS = nil; var inodemap map[string]uint64 = nil; var _ = (fs.NodeReaddirer)((*ITNode)(nil)) // Readdir is part of the NodeReaddirer interface func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) { switch n.kind { case 0: // root folder r := make([]fuse.DirEntry, 2) r[0] = fuse.DirEntry{ Name: "device", Ino: 0, Mode: fuse.S_IFDIR, } r[1] = fuse.DirEntry{ Name: "fs", Ino: 1, Mode: fuse.S_IFDIR, } return fs.NewListDirStream(r), 0 case 1: // device folder r := make([]fuse.DirEntry, 6) for ind, value := range properties { r[ind] = fuse.DirEntry{ Name: value.name, Ino: value.Ino, Mode: fuse.S_IFREG, } } return fs.NewListDirStream(r), 0 case 2: // on device files, _ := myfs.ReadDir(n.path) r := make([]fuse.DirEntry, len(files)) n.lst = files for ind, file := range files { name := file.path[strings.LastIndex(file.path, "/")+1:] ino := inodemap[file.path] if ino == 0 { ino = uint64(len(inodemap)) + 1 inodemap[file.path] = ino } if file.flags & 0b1 == 1 { r[ind] = fuse.DirEntry{ Name: name, Mode: fuse.S_IFDIR, Ino : ino + 10, } } else { r[ind] = fuse.DirEntry{ Name: name, Mode: fuse.S_IFREG, Ino : ino + 10, } } } return fs.NewListDirStream(r), 0 } r := make([]fuse.DirEntry, 0) return fs.NewListDirStream(r), 0 } var _ = (fs.NodeLookuper)((*ITNode)(nil)) func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) { switch n.kind { case 0: // root folder if name == "device" { stable := fs.StableAttr{ Mode: fuse.S_IFDIR, Ino: uint64(0), } operations := &ITNode{kind: 1, Ino: 0} child := n.NewInode(ctx, operations, stable) return child, 0 } else if name == "fs" { stable := fs.StableAttr{ Mode: fuse.S_IFDIR, Ino: uint64(1), } operations := &ITNode{kind: 2, Ino: 1, path : ""} child := n.NewInode(ctx, operations, stable) return child, 0 } case 1: // device folder for _, value := range properties { if value.name == name { stable := fs.StableAttr{ Mode: fuse.S_IFREG, Ino: uint64(value.Ino), } operations := &ITNode{kind: 3, Ino: value.Ino} child := n.NewInode(ctx, operations, stable) return child, 0 } } return nil, syscall.ENOENT case 2: // FS object for _, file := range n.lst { if file.path != n.path + "/" + name { continue; } log.Println("matched", name) if file.flags & 0b1 == 1 { stable := fs.StableAttr{ Mode: fuse.S_IFDIR, Ino: inodemap[file.path], } operations := &ITNode{kind: 2, path: file.path} child := n.NewInode(ctx, operations, stable) return child, 0 } else { stable := fs.StableAttr{ Mode: fuse.S_IFREG, Ino: inodemap[file.path], } operations := &ITNode{ kind: 2, path: file.path, self: file, } child := n.NewInode(ctx, operations, stable) return child, 0 } break; } } return nil, syscall.ENOENT } type bytesFileReadHandle struct { content []byte } var _ = (fs.FileReader)((*bytesFileReadHandle)(nil)) func (fh *bytesFileReadHandle) Read(ctx context.Context, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) { end := off + int64(len(dest)) if end > int64(len(fh.content)) { end = int64(len(fh.content)) } return fuse.ReadResultData(fh.content[off:end]), 0 } type bytesFileWriteHandle struct { content []byte path string } var _ = (fs.FileWriter)((*bytesFileWriteHandle)(nil)) func (fh *bytesFileWriteHandle) Write(ctx context.Context, data []byte, off int64) (written uint32, errno syscall.Errno) { if off != int64(len(fh.content)) { } fh.content = append(fh.content[:], data[:]...) return uint32(len(data)), 0 } var _ = (fs.FileFlusher)((*bytesFileWriteHandle)(nil)) func (fh *bytesFileWriteHandle) Flush(ctx context.Context) (errno syscall.Errno) { fp, err := myfs.Create(fh.path, uint32(len(fh.content))) if err != nil { return syscall.EROFS } nread, err := fp.Write(fh.content) if err != nil || nread != len(fh.content) { return syscall.EROFS } return 0 } var _ = (fs.NodeGetattrer)((*ITNode)(nil)) func (bn *ITNode) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno { out.Ino = bn.Ino out.Mtime = bn.self.modtime out.Ctime = bn.self.modtime out.Atime = bn.self.modtime out.Size = uint64(bn.self.size) return 0 } var _ = (fs.NodeSetattrer)((*ITNode)(nil)) func (bn *ITNode) Setattr(ctx context.Context, fh fs.FileHandle, in *fuse.SetAttrIn, out *fuse.AttrOut) syscall.Errno { out.Size = 0; out.Mtime = 0; return 0 } var _ = (fs.NodeOpener)((*ITNode)(nil)) func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) { switch f.kind { case 2: // FS file if openFlags&syscall.O_RDWR != 0 { return nil, 0, syscall.EROFS } if openFlags & syscall.O_WRONLY != 0 { fh = &bytesFileWriteHandle{ path : f.path, content : make([]byte, 0), } return fh, fuse.FOPEN_DIRECT_IO, 0 } else { fp, err := myfs.Open(f.path) if err != nil { return nil, 0, syscall.EROFS } buf := make([]byte, f.self.size); nread, err := fp.Read(buf) if err != nil || nread != int(f.self.size) { return nil, 0, syscall.EROFS } fh = &bytesFileReadHandle{ content: buf, } return fh, fuse.FOPEN_DIRECT_IO, 0 } case 3: // Device file // disallow writes if openFlags&(syscall.O_RDWR|syscall.O_WRONLY) != 0 { return nil, 0, syscall.EROFS } for _, value := range properties { if value.Ino == f.Ino { ans, err := value.f() if err != nil { return nil, 0, syscall.EROFS } fh = &bytesFileReadHandle{ content: ans, } return fh, fuse.FOPEN_DIRECT_IO, 0 } } } return nil, 0, syscall.EROFS } var _ = (fs.NodeCreater)((*ITNode)(nil)) func (f *ITNode) Create(ctx context.Context, name string, flags uint32, mode uint32, out *fuse.EntryOut) (node *fs.Inode, fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) { if f.kind != 2 { return nil, nil, 0, syscall.EROFS } path := f.path + "/" + name ino := uint64(len(inodemap)) + 11 inodemap[path] = ino stable := fs.StableAttr{ Mode: fuse.S_IFREG, Ino: ino, } operations := &ITNode{ kind: 2, Ino: ino, path : path, } node = f.NewInode(ctx, operations, stable) fh = &bytesFileWriteHandle{ path : path, content : make([]byte, 0), } errno = 0 return node, fh, fuseFlags, 0 } var _ = (fs.NodeMkdirer)((*ITNode)(nil)) func (f *ITNode) Mkdir(ctx context.Context, name string, mode uint32, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) { if f.kind != 2 { return nil, syscall.EROFS } path := f.path + "/" + name err := myfs.Mkdir(path) if err != nil { return nil, syscall.EROFS } ino := uint64(len(inodemap)) + 11 inodemap[path] = ino stable := fs.StableAttr{ Mode: fuse.S_IFDIR, Ino: ino, } operations := &ITNode{ kind: 2, Ino: ino, path : path, } node := f.NewInode(ctx, operations, stable) return node, 0 } var _ = (fs.NodeRenamer)((*ITNode)(nil)) func (f *ITNode) Rename(ctx context.Context, name string, newParent fs.InodeEmbedder, newName string, flags uint32) syscall.Errno { p1 := f.path + "/" + name p2 := newParent.EmbeddedInode().Path(nil)[2:] + "/" + newName err := myfs.Rename(p1, p2) if err != nil { return syscall.EROFS } ino := inodemap[p1] delete(inodemap, p1) inodemap[p2] = ino return 0 } func main() { // This is where we'll mount the FS mntDir := "/tmp/x" os.Mkdir(mntDir, 0755) root := &ITNode{kind: 0} server, err := fs.Mount(mntDir, root, &fs.Options{ MountOptions: fuse.MountOptions{ // Set to true to see how the file system works. Debug: false, }, }) if err != nil { log.Panic(err) } dev := Device{}; properties[0].f = dev.HeartRateBytes; properties[1].f = dev.BatteryLevelBytes; properties[2].f = dev.MotionBytes; properties[3].f = dev.StepCountBytes; properties[4].f = dev.VersionBytes; properties[5].f = dev.AddressBytes; log.Printf("Mounted on %s", mntDir) log.Printf("Unmount by calling 'fusermount -u %s'", mntDir) myfs = &FS{}; inodemap = make(map[string]uint64) // Wait until unmount before exiting server.Wait() }