-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
223 lines (192 loc) · 5.15 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// This program uses FUSE to virtualize OpenChirp as a filesystem.
// Craig Hesling 2018
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"strings"
"github.com/openchirp/framework"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/hanwen/go-fuse/fuse/pathfs"
)
type OpenChirpFS struct {
pathfs.FileSystem
client *framework.UserClient
root location
}
func (me *OpenChirpFS) GetAttr(name string, context *fuse.Context) (*fuse.Attr, fuse.Status) {
fmt.Println("GetAttr(", name, ")")
// If root
if name == "" {
return &fuse.Attr{
Mode: fuse.S_IFDIR | 0755,
}, 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 _, ok := l.children[last]; ok {
return &fuse.Attr{
Mode: fuse.S_IFDIR | 0755,
}, fuse.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(dev.data)),
}, fuse.OK
}
return nil, fuse.ENOENT
}
// location/location/device/info
// location/device/info
// location/device/
func (me *OpenChirpFS) OpenDir(name string, context *fuse.Context) (c []fuse.DirEntry, code fuse.Status) {
fmt.Println("name =", name)
var l = &me.root
l.mutex.Lock()
for _, p := range strings.Split(name, string(os.PathSeparator)) {
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
}
c = make([]fuse.DirEntry, len(l.children)+len(l.devices))
var index int
for name := range l.children {
c[index] = fuse.DirEntry{Name: name, Mode: fuse.S_IFDIR}
index++
}
for name := range l.devices {
c[index] = fuse.DirEntry{Name: name, Mode: fuse.S_IFREG}
index++
}
return c, fuse.OK
}
func (me *OpenChirpFS) Open(name string, flags uint32, context *fuse.Context) (file nodefs.File, code fuse.Status) {
if flags&fuse.O_ANYWRITE != 0 {
return nil, fuse.EPERM
}
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() {
mnt := flag.String("mount", "/mnt", "The directory that you wish to bind to OCFS")
userid := flag.String("userid", "", "The OpenChirp userid")
usertoken := flag.String("usertoken", "", "The OpenChirp user token")
flag.Parse()
client, err := framework.StartUserClient("https://api.openchirp.io", "tls://mqtt.openchirp.io:8883", *userid, *usertoken)
if err != nil {
flag.Usage()
log.Fatal("Failed connect to OC ", err)
}
ocfs := &OpenChirpFS{FileSystem: pathfs.NewDefaultFileSystem(), client: client}
if err := ocfs.root.pull(client); err != nil {
log.Fatalf("Fetching root item failed: %v\n", err)
}
nfs := pathfs.NewPathNodeFs(ocfs, nil)
server, _, err := nodefs.MountRoot(*mnt, nfs.Root(), nil)
if err != nil {
log.Fatalf("Mount fail: %v\n", err)
}
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
go server.Serve()
<-signals
fmt.Println("unmount status =", server.Unmount())
// fmt.Println("unmount root", nfs.Unmount(flag.Arg(0)))
}