Skip to content

Commit ed6f928

Browse files
author
JojiiOfficial
committed
create namespaceNode
1 parent bc00a28 commit ed6f928

File tree

4 files changed

+77
-37
lines changed

4 files changed

+77
-37
lines changed

dmfs/dmfsRoot.go

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ import (
1010
"github.com/hanwen/go-fuse/v2/fs"
1111
)
1212

13-
type dmanagerRoot struct {
13+
type rootNode struct {
1414
fs.Inode
1515
}
1616

1717
// implement the interfaces
18-
var _ = (fs.NodeOnAdder)((*dmanagerRoot)(nil))
19-
var _ = (fs.NodeRenamer)((*dmanagerRoot)(nil))
20-
var _ = (fs.NodeRmdirer)((*dmanagerRoot)(nil))
18+
var _ = (fs.NodeOnAdder)((*rootNode)(nil))
19+
var _ = (fs.NodeRenamer)((*rootNode)(nil))
20+
var _ = (fs.NodeRmdirer)((*rootNode)(nil))
2121

2222
// OnAdd is called on mounting the file system. Use it to populate
2323
// the file system tree.
24-
func (root *dmanagerRoot) OnAdd(ctx context.Context) {
24+
func (root *rootNode) OnAdd(ctx context.Context) {
2525
root.debug("Init files")
2626

2727
err := data.loadUserAttributes()
@@ -39,40 +39,22 @@ func (root *dmanagerRoot) OnAdd(ctx context.Context) {
3939

4040
// Create namespace folder
4141
if nsp == nil {
42-
nsp = root.Inode.NewInode(ctx, &fs.Inode{}, fs.StableAttr{
42+
nsp = root.Inode.NewInode(ctx, &namespaceNode{
43+
namespace: nsName,
44+
groups: namespace.Groups,
45+
}, fs.StableAttr{
4346
Mode: syscall.S_IFDIR,
4447
})
45-
root.AddChild(nsName, nsp, true)
46-
}
47-
48-
// Use a no_group folder for files
49-
// not associated to a groud
50-
if len(namespace.Groups) == 0 {
51-
namespace.Groups = []string{"no_group"}
52-
}
5348

54-
// Add groups to namespace
55-
for _, group := range namespace.Groups {
56-
gp := nsp.GetChild(group)
57-
if gp == nil {
58-
gp = nsp.NewInode(ctx, &groupInode{
59-
group: group,
60-
namespace: namespace.Name,
61-
}, fs.StableAttr{
62-
Mode: syscall.S_IFDIR,
63-
})
64-
65-
nsp.AddChild(group, gp, true)
66-
}
49+
root.AddChild(nsName, nsp, true)
6750
}
6851
}
6952

7053
root.debug("Init files success")
71-
7254
}
7355

74-
// Unlink if virtual file was unlinked
75-
func (root *dmanagerRoot) Rmdir(ctx context.Context, name string) syscall.Errno {
56+
// Delete Namespace if virtual file was unlinked
57+
func (root *rootNode) Rmdir(ctx context.Context, name string) syscall.Errno {
7658
namespace := addNSName(name, data.libdm.Config)
7759

7860
// wait 2 seconds to ensure, user didn't cancel
@@ -91,12 +73,12 @@ func (root *dmanagerRoot) Rmdir(ctx context.Context, name string) syscall.Errno
9173
return 0
9274
}
9375

94-
// Rename if virtual file was renamed
95-
func (root *dmanagerRoot) Rename(ctx context.Context, name string, newParent fs.InodeEmbedder, newName string, flags uint32) syscall.Errno {
76+
// Rename namespace if virtual file was renamed
77+
func (root *rootNode) Rename(ctx context.Context, name string, newParent fs.InodeEmbedder, newName string, flags uint32) syscall.Errno {
9678
// Don't rename default ns
9779
if name == "default" {
9880
fmt.Println("Can't rename default namespace!")
99-
return syscall.EACCES
81+
return syscall.EPERM
10082
}
10183

10284
// Get real namespace names
@@ -115,7 +97,7 @@ func (root *dmanagerRoot) Rename(ctx context.Context, name string, newParent fs.
11597
return 0
11698
}
11799

118-
func (root *dmanagerRoot) debug(arg ...interface{}) {
100+
func (root *rootNode) debug(arg ...interface{}) {
119101
if data.mounter.Debug {
120102
fmt.Println(arg...)
121103
}

dmfs/fsGroup.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import (
1212
type groupInode struct {
1313
fs.Inode
1414

15-
namespace string
16-
group string
15+
namespace string
16+
group string
17+
isNoGroupPlaceholder bool
1718
}
1819

1920
var _ = (fs.NodeOnAdder)((*groupInode)(nil))
@@ -22,6 +23,7 @@ func (groupInode *groupInode) OnAdd(ctx context.Context) {
2223
groupAdd := []string{groupInode.group}
2324

2425
if len(groupAdd) == 1 && groupAdd[0] == "no_group" {
26+
groupInode.isNoGroupPlaceholder = true
2527
groupAdd = []string{}
2628
}
2729

dmfs/fsNamespace.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package dmfs
2+
3+
import (
4+
"context"
5+
"syscall"
6+
"time"
7+
8+
"github.com/hanwen/go-fuse/v2/fs"
9+
)
10+
11+
type namespaceNode struct {
12+
fs.Inode
13+
14+
namespace string
15+
groups []string
16+
}
17+
18+
var _ = (fs.NodeOnAdder)((*namespaceNode)(nil))
19+
var _ = (fs.NodeRmdirer)((*namespaceNode)(nil))
20+
21+
func (nsNode *namespaceNode) OnAdd(ctx context.Context) {
22+
// Use a no_group folder for files
23+
// not associated to a groud
24+
if len(nsNode.groups) == 0 {
25+
nsNode.groups = []string{"no_group"}
26+
}
27+
28+
// Add groups to namespace
29+
for _, group := range nsNode.groups {
30+
gp := nsNode.GetChild(group)
31+
if gp == nil {
32+
gp = nsNode.NewInode(ctx, &groupInode{
33+
group: group,
34+
namespace: nsNode.namespace,
35+
}, fs.StableAttr{
36+
Mode: syscall.S_IFDIR,
37+
})
38+
39+
nsNode.AddChild(group, gp, true)
40+
}
41+
}
42+
}
43+
44+
// Delete group if vfile was removed
45+
func (nsNode *namespaceNode) Rmdir(ctx context.Context, name string) syscall.Errno {
46+
// wait 2 seconds to ensure, user didn't cancel
47+
select {
48+
case <-ctx.Done():
49+
return syscall.ECANCELED
50+
case <-time.After(2 * time.Second):
51+
}
52+
53+
// TODO do delete request
54+
55+
return 0
56+
}

dmfs/mount.go

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

5656
// Create the fs
57-
root := &dmanagerRoot{}
57+
root := &rootNode{}
5858

5959
var err error
6060

0 commit comments

Comments
 (0)