Skip to content

Commit d0e957d

Browse files
author
JojiiOfficial
committed
add reload groups
add reload namespaces
1 parent 1f9673a commit d0e957d

File tree

4 files changed

+97
-45
lines changed

4 files changed

+97
-45
lines changed

dmfs/data.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dmfs
22

33
import (
4+
"fmt"
45
"os"
56
"time"
67

@@ -33,7 +34,8 @@ func (data *dataStruct) loadUserAttributes() error {
3334
// TODO make cachhe time configureable or even create
3435
// a genuius way to calculate a reasonable cachetime
3536

36-
if time.Now().Unix()-10 > data.lastUserAttrLoad {
37+
if time.Now().Unix()-5 > data.lastUserAttrLoad {
38+
fmt.Println("reload")
3739
var err error
3840
data.userAttributes, err = data.libdm.GetUserAttributeData()
3941
if err != nil {

dmfs/dmfsRoot.go

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@ import (
77
"syscall"
88
"time"
99

10-
"github.com/DataManager-Go/libdatamanager"
1110
"github.com/hanwen/go-fuse/v2/fs"
1211
"github.com/hanwen/go-fuse/v2/fuse"
1312
)
1413

14+
/*
15+
* the root fs is supposed to load and interact
16+
* with the namespaces and to show them as directories
17+
*/
18+
1519
type rootNode struct {
1620
fs.Inode
1721

18-
nsMap map[string][]string
22+
nsNodes map[string]*namespaceNode
1923
}
2024

2125
// implement the interfaces
@@ -24,44 +28,53 @@ var _ = (fs.NodeRenamer)((*rootNode)(nil))
2428
var _ = (fs.NodeRmdirer)((*rootNode)(nil))
2529
var _ = (fs.NodeLookuper)((*rootNode)(nil))
2630

27-
// OnAdd is called on mounting the file system. Use it to populate
28-
// the file system tree.
29-
func (root *rootNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
30-
r := make([]fuse.DirEntry, 0)
31+
// Create new root Node
32+
func newRootNode() *rootNode {
33+
rn := &rootNode{}
34+
rn.nsNodes = make(map[string]*namespaceNode)
35+
return rn
36+
}
3137

38+
// On dir access, load namespaces and groups
39+
func (root *rootNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
40+
fmt.Println("read root")
41+
// Use dataStore to retrieve
42+
// groups and namespaces
3243
err := data.loadUserAttributes()
3344
if err != nil {
3445
log.Fatal(err)
35-
return nil, syscall.EPERM
46+
return nil, syscall.ENOENT
3647
}
3748

38-
if root.nsMap == nil {
39-
root.nsMap = make(map[string][]string, 0)
40-
}
49+
r := make([]fuse.DirEntry, len(data.userAttributes.Namespace))
4150

4251
// Loop Namespaces and add childs in as folders
43-
for _, namespace := range data.userAttributes.Namespace {
52+
for i, namespace := range data.userAttributes.Namespace {
4453
nsName := removeNSName(namespace.Name)
4554

46-
_, has := root.nsMap[namespace.Name]
55+
// Find namespace node
56+
v, has := root.nsNodes[nsName]
4757
if !has {
48-
root.nsMap[namespace.Name] = namespace.Groups
58+
// Create new if not exists
59+
root.nsNodes[nsName] = newNamespaceNode(namespace)
60+
} else {
61+
// Update groups if exists
62+
v.nsInfo.Groups = namespace.Groups
4963
}
5064

51-
r = append(r, fuse.DirEntry{
65+
r[i] = fuse.DirEntry{
5266
Name: nsName,
5367
Mode: syscall.S_IFDIR,
54-
})
68+
}
5569
}
5670

5771
return fs.NewListDirStream(r), 0
5872
}
5973

74+
// Lookup -> something tries to lookup a file (namespace)
6075
func (root *rootNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
61-
fullNS := addNSName(name, data.mounter.Libdm.Config)
62-
6376
// Get cached namespaceInfo from map
64-
val, has := root.nsMap[fullNS]
77+
val, has := root.nsNodes[name]
6578
if !has {
6679
return nil, syscall.ENOENT
6780
}
@@ -71,12 +84,7 @@ func (root *rootNode) Lookup(ctx context.Context, name string, out *fuse.EntryOu
7184

7285
// Create new child if not found
7386
if child == nil {
74-
child = root.NewInode(ctx, &namespaceNode{
75-
nsInfo: &libdatamanager.Namespaceinfo{
76-
Name: fullNS,
77-
Groups: val,
78-
},
79-
}, fs.StableAttr{
87+
child = root.NewInode(ctx, val, fs.StableAttr{
8088
Mode: syscall.S_IFDIR,
8189
})
8290
}

dmfs/fsNamespace.go

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,80 @@ import (
1111
"github.com/hanwen/go-fuse/v2/fuse"
1212
)
1313

14+
/*
15+
* filesystem namespace is supposed to load and interact
16+
* with the groups and to show them as directories
17+
*/
18+
1419
type namespaceNode struct {
1520
fs.Inode
1621

17-
nsInfo *libdm.Namespaceinfo
22+
nsInfo libdm.Namespaceinfo
1823
}
1924

20-
var _ = (fs.NodeOnAdder)((*namespaceNode)(nil))
25+
// var _ = (fs.NodeOnAdder)((*namespaceNode)(nil))
2126
var _ = (fs.NodeRmdirer)((*namespaceNode)(nil))
2227
var _ = (fs.NodeMkdirer)((*namespaceNode)(nil))
2328
var _ = (fs.NodeRenamer)((*namespaceNode)(nil))
29+
var _ = (fs.NodeReaddirer)((*namespaceNode)(nil))
30+
var _ = (fs.NodeLookuper)((*namespaceNode)(nil))
2431

25-
func (nsNode *namespaceNode) OnAdd(ctx context.Context) {
26-
// Use a no_group folder for files
27-
// not associated to a groud
28-
if len(nsNode.nsInfo.Groups) == 0 {
29-
nsNode.nsInfo.Groups = []string{"no_group"}
32+
func newNamespaceNode(nsInfo libdm.Namespaceinfo) *namespaceNode {
33+
return &namespaceNode{
34+
nsInfo: nsInfo,
3035
}
36+
}
37+
38+
// On Namespace dir accessed
39+
func (nsNode *namespaceNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
40+
// TODO reload groups in dmfsRoot on reading dir
41+
42+
r := make([]fuse.DirEntry, len(nsNode.nsInfo.Groups))
43+
44+
// Load groups
45+
for i := range nsNode.nsInfo.Groups {
46+
r[i] = fuse.DirEntry{
47+
Mode: syscall.S_IFDIR,
48+
Name: nsNode.nsInfo.Groups[i],
49+
}
50+
}
51+
52+
return fs.NewListDirStream(r), 0
53+
}
3154

32-
// Add groups to namespace
33-
for _, group := range nsNode.nsInfo.Groups {
34-
gp := nsNode.GetChild(group)
35-
if gp == nil {
36-
gp = nsNode.NewInode(ctx, &groupInode{
37-
group: group,
38-
namespace: nsNode.nsInfo.Name,
39-
}, fs.StableAttr{
40-
Mode: syscall.S_IFDIR,
41-
})
42-
43-
nsNode.AddChild(group, gp, true)
55+
// On group looked up
56+
func (nsNode *namespaceNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
57+
found := false
58+
for gi := range nsNode.nsInfo.Groups {
59+
if nsNode.nsInfo.Groups[gi] == name {
60+
found = true
61+
break
4462
}
4563
}
64+
65+
// Return entry not-found-error
66+
// if entry wasn't found
67+
if !found {
68+
return nil, syscall.ENOENT
69+
}
70+
71+
// Reuse group child
72+
child := nsNode.GetChild(name)
73+
74+
// create child if not found
75+
if child == nil {
76+
v := &groupInode{
77+
group: name,
78+
isNoGroupPlaceholder: name == "no_group",
79+
namespace: nsNode.nsInfo.Name,
80+
}
81+
82+
child = nsNode.NewInode(ctx, v, fs.StableAttr{
83+
Mode: syscall.S_IFDIR,
84+
})
85+
}
86+
87+
return child, 0
4688
}
4789

4890
// Delete group if vfile was removed

dmfs/mount.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (mounter *Mounter) Mount() {
5757
initData()
5858

5959
// Create the fs
60-
root := &rootNode{}
60+
root := newRootNode()
6161

6262
var err error
6363

0 commit comments

Comments
 (0)