-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmfs.go
313 lines (286 loc) · 8.3 KB
/
mmfs.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package main
import (
"context"
"fmt"
"os"
"syscall"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/mattermost/mattermost-server/v5/model"
)
// MMFS models a mattermost fuse file system
// |- root
// |- team1
// | |- channel1
// | | |- in
// | | |- unread
// | |
// | |- DM1
// | |- in
// | |- unread
// |
// |- team1
// |- channel1
// | |- in
// | |- unread
// |
// |- DM1
// |- in
// |- unread
type MMFS struct {
mmClient *MMClient
mmTeams map[string]*MMTeam
}
// NewMMFS returns a new MMFS object
func NewMMFS(server, username, password, caCertPath string) (*MMFS, error) {
c, err := NewMMClient(server, username, password, caCertPath)
if err != nil {
return nil, err
}
return &MMFS{
mmClient: c,
}, nil
}
// Root is called to obtain the Node for the file system root.
// satisfies the fs.Fuse interface
func (mmfs *MMFS) Root() (fs.Node, error) {
teams, err := mmfs.mmClient.GetTeams()
if err != nil {
return nil, err
}
mmfs.mmTeams = make(map[string]*MMTeam)
for _, team := range teams {
mmfs.mmTeams[team.Name], err = NewMMTeam(team, mmfs.mmClient)
if err != nil {
return nil, err
}
}
return mmfs, nil
}
// Attr fills attr with the standard metadata for the node.
// satisfies the fs.Node interface
func (mmfs *MMFS) Attr(_ context.Context, a *fuse.Attr) error {
a.Inode = 1
a.Mode = os.ModeDir | 0o555 // TODO fix permissions
return nil
}
// Lookup returns a fs.Node that corresponds to the given entry inside the
// root directory, which in this case are directories corresponding to
// mattermost teams
// Satisfies the fs.NodeStringLookuper interface
func (mmfs *MMFS) Lookup(_ context.Context, name string) (fs.Node, error) {
teamNode, ok := mmfs.mmTeams[name]
if !ok {
return nil, syscall.ENOENT
}
return teamNode, nil
}
// ReadDirAll returns all directory entries, i.e. mattermost teams, inside the root directory
// Satisfies the fs.HandleReadAller interface
func (mmfs *MMFS) ReadDirAll(_ context.Context) ([]fuse.Dirent, error) {
dirs := make([]fuse.Dirent, 0, len(mmfs.mmTeams))
for name := range mmfs.mmTeams {
dirs = append(dirs, fuse.Dirent{
Inode: mmfs.mmTeams[name].inode,
Name: name,
Type: fuse.DT_Dir,
})
}
return dirs, nil
}
// MMTeam models a mattermost team as a FUSE directory
type MMTeam struct {
mmClient *MMClient
id string
name string
inode uint64
mmChannels map[string]*MMChannel
}
// NewMMTeam returns a new MMTeam
func NewMMTeam(team *model.Team, client *MMClient) (*MMTeam, error) {
channels, err := client.GetNormalizedChannels(team.Id)
if err != nil {
return nil, err
}
mmTeam := &MMTeam{
mmClient: client,
id: team.Id,
name: team.Name,
inode: fs.GenerateDynamicInode(1, team.Name),
mmChannels: make(map[string]*MMChannel),
}
for _, channel := range channels {
mmChannel := &MMChannel{
id: channel.Id,
name: channel.Name,
inode: fs.GenerateDynamicInode(mmTeam.inode, channel.Name),
client: client,
}
mmChannel.unreadFile = &UnreadFile{
mmclient: client,
channelID: channel.Id,
inode: fs.GenerateDynamicInode(mmChannel.inode, "unread"),
}
mmChannel.inFile = &InFile{
mmclient: client,
channelID: channel.Id,
inode: fs.GenerateDynamicInode(mmChannel.inode, "in"),
}
mmTeam.mmChannels[channel.Name] = mmChannel
}
return mmTeam, nil
}
// Attr fills attr with the standard metadata for the MMTeam node.
// satisfies the fs.Node interface
func (mmt *MMTeam) Attr(_ context.Context, a *fuse.Attr) error {
a.Inode = mmt.inode
a.Mode = os.ModeDir | 0o555 // TODO restrict permissions
return nil
}
// Lookup returns a fs.Node that corresponds to the given entry inside this
// directory (team), which in this case are channels in a team
// Satisfies the fs.NodeStringLookuper interface
func (mmt *MMTeam) Lookup(_ context.Context, name string) (fs.Node, error) {
channelNode, ok := mmt.mmChannels[name]
if !ok {
return nil, syscall.ENOENT
}
return channelNode, nil
}
// ReadDirAll returns all directory entries, i.e. mattermost channels, inside the directory
// corresponding to this team
// Satisfies the fs.HandleReadAller interface
func (mmt *MMTeam) ReadDirAll(_ context.Context) ([]fuse.Dirent, error) {
dirs := make([]fuse.Dirent, 0, len(mmt.mmChannels))
for name := range mmt.mmChannels {
dirs = append(dirs, fuse.Dirent{
Inode: mmt.mmChannels[name].inode,
Name: name,
Type: fuse.DT_Dir,
})
}
return dirs, nil
}
// MMChannel models a mattermost channel as a FUSE directory
type MMChannel struct {
id string
name string
inode uint64
client *MMClient
unreadFile *UnreadFile
inFile *InFile
}
// Attr fills attr with the standard metadata for the node.
// satisfies the fs.Node interface
func (mmc *MMChannel) Attr(_ context.Context, a *fuse.Attr) error {
a.Inode = mmc.inode
a.Mode = os.ModeDir | 0o555 // TODO restrict permissions
return nil
}
// Lookup returns a fs.Node that corresponds to the given entry inside this
// directory (channel). Currently only an unread file is supported.
// Satisfies the fs.NodeStringLookuper interface
func (mmc *MMChannel) Lookup(_ context.Context, name string) (fs.Node, error) {
switch name {
case "unread":
return mmc.unreadFile, nil
case "in":
return mmc.inFile, nil
}
return mmc.unreadFile, nil
}
// ReadDirAll returns all directory entries inside the directory corresponding
// corresponding to this channel. Currently only an unread file is supported
// Satisfies the fs.HandleReadAller interface
func (mmc *MMChannel) ReadDirAll(_ context.Context) ([]fuse.Dirent, error) {
return []fuse.Dirent{
{
Inode: mmc.unreadFile.inode,
Name: "unread",
Type: fuse.DT_File,
},
{
Inode: mmc.inFile.inode,
Name: "in",
Type: fuse.DT_File,
},
}, nil
}
// UnreadFile models a file containing unread messages in a channel
type UnreadFile struct {
mmclient *MMClient
channelID string
inode uint64
}
// Attr fills attr with the standard metadata for the UnreadFile.
// satisfies the fs.Node interface
func (uf *UnreadFile) Attr(_ context.Context, a *fuse.Attr) error {
a.Inode = uf.inode
a.Mode = 0o777
return nil
}
// ReadAll returns the unread text of the corresponding channel
// satisfies the fs.HandleReadAller interface
func (uf *UnreadFile) ReadAll(_ context.Context) ([]byte, error) {
// TODO use ctx to timeout calls
postList, err := uf.mmclient.GetChannelUnread(uf.channelID)
if err != nil {
return nil, err
}
text, err := uf.mmclient.FormatPostsForDisplay(postList)
if err != nil {
return nil, err
}
err = uf.mmclient.MarkChannelAsRead(uf.channelID)
if err != nil {
return nil, err
}
return []byte(text), nil
}
// Open opens the UnreadFile
// Reads for a file are done upto the size reported by Attr. We can't know
// the unread file content (and therefore size) until we query the
// mattermost server in the ReadAll method. We set the file in DirectIO mode to
// get around this.
func (uf *UnreadFile) Open(_ context.Context, _ *fuse.OpenRequest, resp *fuse.OpenResponse) (
fs.Handle, error) {
resp.Flags = fuse.OpenDirectIO | fuse.OpenNonSeekable
return uf, nil
}
// InFile models a file used to write data to a channel
type InFile struct {
mmclient *MMClient
channelID string
inode uint64
}
// Attr fills attr with the standard metadata for the InFile.
// satisfies the fs.Node interface
func (ifl *InFile) Attr(_ context.Context, a *fuse.Attr) error {
a.Inode = ifl.inode
a.Mode = 0o333
return nil
}
// Write posts a message to the channel
// satisfies the fs.HandleWriter interface
func (ifl *InFile) Write(
_ context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse,
) error {
err := ifl.mmclient.CreatePost(ifl.channelID, req.Data)
if err != nil {
fmt.Println(err)
return err
}
resp.Size = len(req.Data)
return nil
}
// Open opens the InFile
// Reads for a file are done upto the size reported by Attr. We can't know
// the unread file content (and therefore size) until we query the
// mattermost server in the ReadAll method. We set the file in DirectIO mode to
// get around this.
func (ifl *InFile) Open(
_ context.Context, _ *fuse.OpenRequest, resp *fuse.OpenResponse,
) (fs.Handle, error) {
resp.Flags = fuse.OpenDirectIO | fuse.OpenNonSeekable
return ifl, nil
}