Skip to content

Commit 43ca871

Browse files
author
JojiiOfficial
committed
improve ns/group loading
1 parent d0e957d commit 43ca871

File tree

6 files changed

+94
-27
lines changed

6 files changed

+94
-27
lines changed

dmfs/data.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/DataManager-Go/libdatamanager"
99
libdm "github.com/DataManager-Go/libdatamanager"
1010
dmConfig "github.com/DataManager-Go/libdatamanager/config"
11+
"github.com/hanwen/go-fuse/v2/fuse"
1112
)
1213

1314
var data dataStruct
@@ -47,3 +48,8 @@ func (data *dataStruct) loadUserAttributes() error {
4748

4849
return nil
4950
}
51+
52+
func (data *dataStruct) setUserAttr(inAttr *fuse.AttrOut) {
53+
inAttr.Gid = data.gid
54+
inAttr.Uid = data.uid
55+
}

dmfs/dmfsRoot.go

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package dmfs
33
import (
44
"context"
55
"fmt"
6-
"log"
6+
"sync"
77
"syscall"
88
"time"
99

1010
"github.com/hanwen/go-fuse/v2/fs"
1111
"github.com/hanwen/go-fuse/v2/fuse"
12+
"github.com/pkg/errors"
1213
)
1314

1415
/*
@@ -19,14 +20,25 @@ import (
1920
type rootNode struct {
2021
fs.Inode
2122

23+
loading bool
2224
nsNodes map[string]*namespaceNode
25+
26+
mx sync.Mutex
2327
}
2428

25-
// implement the interfaces
26-
var _ = (fs.NodeReaddirer)((*rootNode)(nil))
27-
var _ = (fs.NodeRenamer)((*rootNode)(nil))
28-
var _ = (fs.NodeRmdirer)((*rootNode)(nil))
29-
var _ = (fs.NodeLookuper)((*rootNode)(nil))
29+
// Implement required interfaces
30+
var (
31+
_ = (fs.NodeReaddirer)((*rootNode)(nil))
32+
_ = (fs.NodeRenamer)((*rootNode)(nil))
33+
_ = (fs.NodeRmdirer)((*rootNode)(nil))
34+
_ = (fs.NodeLookuper)((*rootNode)(nil))
35+
_ = (fs.NodeGetattrer)((*rootNode)(nil))
36+
)
37+
38+
var (
39+
// ErrAlreadyLoading error if a load process is already running
40+
ErrAlreadyLoading = errors.New("already loading")
41+
)
3042

3143
// Create new root Node
3244
func newRootNode() *rootNode {
@@ -37,19 +49,50 @@ func newRootNode() *rootNode {
3749

3850
// On dir access, load namespaces and groups
3951
func (root *rootNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
40-
fmt.Println("read root")
52+
r := make([]fuse.DirEntry, 0)
53+
54+
// Load namespaces and groups
55+
err := root.load(func(name string) {
56+
r = append(r, fuse.DirEntry{
57+
Name: name,
58+
Mode: syscall.S_IFDIR,
59+
})
60+
})
61+
62+
// If already loading, use current cache
63+
if err != nil && err != ErrAlreadyLoading {
64+
fmt.Println(err)
65+
return nil, syscall.EIO
66+
}
67+
68+
return fs.NewListDirStream(r), 0
69+
}
70+
71+
// Load groups and namespaces
72+
func (root *rootNode) load(nsCB func(name string)) error {
73+
if root.loading {
74+
return ErrAlreadyLoading
75+
}
76+
77+
root.mx.Lock()
78+
root.loading = true
79+
80+
defer func() {
81+
// Unlock and set loading to false
82+
// at the end
83+
root.loading = false
84+
root.mx.Unlock()
85+
}()
86+
4187
// Use dataStore to retrieve
4288
// groups and namespaces
4389
err := data.loadUserAttributes()
4490
if err != nil {
45-
log.Fatal(err)
46-
return nil, syscall.ENOENT
91+
return err
4792
}
4893

49-
r := make([]fuse.DirEntry, len(data.userAttributes.Namespace))
50-
5194
// Loop Namespaces and add childs in as folders
52-
for i, namespace := range data.userAttributes.Namespace {
95+
for _, namespace := range data.userAttributes.Namespace {
5396
nsName := removeNSName(namespace.Name)
5497

5598
// Find namespace node
@@ -62,13 +105,12 @@ func (root *rootNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno)
62105
v.nsInfo.Groups = namespace.Groups
63106
}
64107

65-
r[i] = fuse.DirEntry{
66-
Name: nsName,
67-
Mode: syscall.S_IFDIR,
108+
if nsCB != nil {
109+
nsCB(nsName)
68110
}
69111
}
70112

71-
return fs.NewListDirStream(r), 0
113+
return nil
72114
}
73115

74116
// Lookup -> something tries to lookup a file (namespace)
@@ -117,7 +159,7 @@ func (root *rootNode) Rename(ctx context.Context, name string, newParent fs.Inod
117159
// Don't rename default ns
118160
if name == "default" {
119161
fmt.Println("Can't rename default namespace!")
120-
return syscall.EPERM
162+
return syscall.EIO
121163
}
122164

123165
// Get real namespace names
@@ -129,13 +171,20 @@ func (root *rootNode) Rename(ctx context.Context, name string, newParent fs.Inod
129171
_, err := data.libdm.UpdateNamespace(oldNSName, newNSName)
130172
if err != nil {
131173
fmt.Println(err)
132-
return syscall.ENONET
174+
return syscall.EIO
133175
}
134176

135177
// Return success
136178
return 0
137179
}
138180

181+
// Set attributes for files
182+
func (root *rootNode) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
183+
// Set owner/group
184+
data.setUserAttr(out)
185+
return 0
186+
}
187+
139188
func (root *rootNode) debug(arg ...interface{}) {
140189
if data.mounter.Debug {
141190
fmt.Println(arg...)

dmfs/fsFile.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ func (fnode *fileInode) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.
2323
out.Ctime = uint64(fnode.file.CreationDate.Unix())
2424
out.Mtime = out.Ctime
2525
out.Mode = 0640
26-
out.Gid = data.gid
27-
out.Uid = data.uid
26+
27+
// Set owner/group
28+
data.setUserAttr(out)
2829

2930
return 0
3031
}

dmfs/fsNamespace.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ type namespaceNode struct {
2222
nsInfo libdm.Namespaceinfo
2323
}
2424

25-
// var _ = (fs.NodeOnAdder)((*namespaceNode)(nil))
26-
var _ = (fs.NodeRmdirer)((*namespaceNode)(nil))
27-
var _ = (fs.NodeMkdirer)((*namespaceNode)(nil))
28-
var _ = (fs.NodeRenamer)((*namespaceNode)(nil))
29-
var _ = (fs.NodeReaddirer)((*namespaceNode)(nil))
30-
var _ = (fs.NodeLookuper)((*namespaceNode)(nil))
25+
// Implement required interfaces
26+
var (
27+
_ = (fs.NodeRmdirer)((*namespaceNode)(nil))
28+
_ = (fs.NodeMkdirer)((*namespaceNode)(nil))
29+
_ = (fs.NodeRenamer)((*namespaceNode)(nil))
30+
_ = (fs.NodeReaddirer)((*namespaceNode)(nil))
31+
_ = (fs.NodeLookuper)((*namespaceNode)(nil))
32+
)
3133

3234
func newNamespaceNode(nsInfo libdm.Namespaceinfo) *namespaceNode {
3335
return &namespaceNode{
@@ -37,7 +39,12 @@ func newNamespaceNode(nsInfo libdm.Namespaceinfo) *namespaceNode {
3739

3840
// On Namespace dir accessed
3941
func (nsNode *namespaceNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
40-
// TODO reload groups in dmfsRoot on reading dir
42+
// Reload groups if namespace was accessed
43+
if _, parent := nsNode.Parent(); parent != nil {
44+
if dmfsroot, ok := (parent.Operations()).(*rootNode); ok {
45+
go dmfsroot.load(nil)
46+
}
47+
}
4148

4249
r := make([]fuse.DirEntry, len(nsNode.nsInfo.Groups))
4350

@@ -113,6 +120,7 @@ func (nsNode *namespaceNode) Rename(ctx context.Context, name string, newParent
113120
return 0
114121
}
115122

123+
// Rename group request
116124
_, err := data.libdm.UpdateAttribute(libdm.GroupAttribute, nsNode.nsInfo.Name, name, newName)
117125
if err != nil {
118126
return syscall.ENOENT

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ require (
88
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
99
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
1010
github.com/hanwen/go-fuse/v2 v2.0.3
11+
github.com/pkg/errors v0.9.1
1112
gopkg.in/alecthomas/kingpin.v2 v2.2.6
1213
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGe
5353
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
5454
github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw=
5555
github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
56+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
57+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
5658
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5759
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5860
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=

0 commit comments

Comments
 (0)