Skip to content

Commit

Permalink
Added reading meta info from device files
Browse files Browse the repository at this point in the history
  • Loading branch information
linux4life798 committed Oct 6, 2018
1 parent 53b6e99 commit afb3909
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
62 changes: 56 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ func (me *OpenChirpFS) GetAttr(name string, context *fuse.Context) (*fuse.Attr,
return &fuse.Attr{
Mode: fuse.S_IFDIR | 0755,
}, fuse.OK
} else if _, ok := l.devices[last]; ok {
} else if dev, ok := l.devices[last]; ok {
if dev.data == nil {
if err := dev.updateData(me.client); err != nil {
log.Printf("Failed to fetch device data: %v", err)
return nil, fuse.EIO
}
}
return &fuse.Attr{
Mode: fuse.S_IFREG | 0644, Size: uint64(len(name)),
Mode: fuse.S_IFREG | 0644, Size: uint64(len(dev.data)),
}, fuse.OK
}

Expand Down Expand Up @@ -135,13 +141,57 @@ func (me *OpenChirpFS) OpenDir(name string, context *fuse.Context) (c []fuse.Dir
}

func (me *OpenChirpFS) Open(name string, flags uint32, context *fuse.Context) (file nodefs.File, code fuse.Status) {
// if name != "file.txt" {
// return nil, fuse.ENOENT
// }
if flags&fuse.O_ANYWRITE != 0 {
return nil, fuse.EPERM
}
return nodefs.NewDataFile([]byte("Hello user")), fuse.OK

parts := strings.Split(name, string(os.PathSeparator))

var l = &me.root
l.mutex.Lock()
for _, p := range parts[:len(parts)-1] {
if p == "" {
continue
}
if !l.updatedChildren {
if err := l.updateChildren(me.client); err != nil {
l.mutex.Unlock()
log.Printf("Failed to update children: %v", err)
return nil, fuse.EIO
}
}

lNew, ok := l.children[p]
if !ok {
l.mutex.Unlock()
log.Printf("Failed to find path part: %s", p)
return nil, fuse.ENOENT
}

lNew.mutex.Lock()
l.mutex.Unlock()
l = lNew
}
defer l.mutex.Unlock()

if err := l.ensureChildrenAndDevices(me.client); err != nil {
log.Printf("Failed to ensure children and devices: %v", err)
return nil, fuse.EIO
}

last := parts[len(parts)-1]
if dev, ok := l.devices[last]; ok {
// fetch last value
if dev.data == nil {
if err := dev.updateData(me.client); err != nil {
log.Printf("Failed to fetch device data: %v", err)
return nil, fuse.EIO
}
}
return nodefs.NewDataFile(dev.data), fuse.OK
}

return nil, fuse.ENOENT
}

func main() {
Expand Down
18 changes: 17 additions & 1 deletion octree.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"log"
"strings"
"sync"

"github.com/openchirp/framework"
Expand All @@ -11,6 +12,21 @@ import (

type device struct {
rest.NodeDescriptor
data []byte
}

func (d *device) updateData(c *framework.UserClient) error {
node, err := c.FetchDeviceInfo(d.ID)
if err != nil {
return err
}
var out strings.Builder
out.WriteString(fmt.Sprintf("Name: %s\n", node.Name))
out.WriteString(fmt.Sprintf("ID: %s\n", node.ID))
out.WriteString(fmt.Sprintf("Owner: %s (%s)\n", node.Owner.Name, node.Owner.Email))
d.data = []byte(out.String())

return nil
}

type location struct {
Expand Down Expand Up @@ -61,7 +77,7 @@ func (l *location) updateDevices(c *framework.UserClient) error {
}
l.devices = make(map[string]*device, len(devices))
for _, d := range devices {
dev := &device{d}
dev := &device{NodeDescriptor: d}

name := dev.Name
var generation = 2
Expand Down

0 comments on commit afb3909

Please sign in to comment.