Skip to content

Commit edf422b

Browse files
author
JojiiOfficial
committed
create groups on mkdir
other small improvements
1 parent 83f522e commit edf422b

File tree

7 files changed

+61
-10
lines changed

7 files changed

+61
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
main
1717
.vscode
1818
testMount
19+
data

dmfs/data.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (data *dataStruct) loadUserAttributes() error {
3939
if time.Now().Unix()-5 > data.lastUserAttrLoad {
4040
fmt.Println("reload")
4141
var err error
42-
data.userAttributes, err = data.libdm.GetUserAttributeData()
42+
data.userAttributes, err = data.libdm.GetUserAttributeData(1)
4343
if err != nil {
4444
printResponseError(err, "loading user attributes")
4545
return err

dmfs/fsNamespace.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,25 @@ var (
3232
// Create a new ns node from nsInfo
3333
func newNamespaceNode(nsInfo libdm.Namespaceinfo) *namespaceNode {
3434
return &namespaceNode{
35-
nsInfo: nsInfo,
35+
nsInfo: libdm.Namespaceinfo{
36+
Name: nsInfo.Name,
37+
Groups: formatGroups(nsInfo.Groups),
38+
},
3639
}
3740
}
3841

42+
func formatGroups(groups []string) []string {
43+
if groups == nil || len(groups) == 0 {
44+
return []string{NoGroupFolder}
45+
}
46+
47+
return append(groups, NoGroupFolder)
48+
}
49+
50+
func (nsNode *namespaceNode) updateGroups(groups []string) {
51+
nsNode.nsInfo.Groups = formatGroups(groups)
52+
}
53+
3954
// On Namespace dir accessed
4055
func (nsNode *namespaceNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
4156
// Reload groups if namespace was accessed
@@ -94,6 +109,7 @@ func (nsNode *namespaceNode) Lookup(ctx context.Context, name string, out *fuse.
94109
}
95110

96111
// Delete group if vfile was removed
112+
// Note: deleting groups doesn't mean the files are deleted!
97113
func (nsNode *namespaceNode) Rmdir(ctx context.Context, name string) syscall.Errno {
98114
if name == NoGroupFolder {
99115
return 0
@@ -106,6 +122,9 @@ func (nsNode *namespaceNode) Rmdir(ctx context.Context, name string) syscall.Err
106122
return syscall.ENOENT
107123
}
108124

125+
// Remove group from list
126+
nsNode.nsInfo.Groups = removeFromStringSlice(nsNode.nsInfo.Groups, name)
127+
109128
return 0
110129
}
111130

@@ -128,14 +147,20 @@ func (nsNode *namespaceNode) Rename(ctx context.Context, name string, newParent
128147

129148
// On group created
130149
func (nsNode *namespaceNode) Mkdir(ctx context.Context, name string, mode uint32, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
150+
// Create group
151+
_, err := data.libdm.CreateAttribute(libdm.GroupAttribute, nsNode.nsInfo.Name, name)
152+
if err != nil {
153+
printResponseError(err, "creating group")
154+
return nil, syscall.EIO
155+
}
156+
157+
nsNode.nsInfo.Groups = append(nsNode.nsInfo.Groups, name)
131158
node := nsNode.NewInode(ctx, &groupInode{
132159
group: name,
133160
namespace: nsNode.nsInfo.Name,
134161
}, fs.StableAttr{
135162
Mode: syscall.S_IFDIR,
136163
})
137164

138-
// TODO implement create group
139-
140165
return node, 0
141166
}

dmfs/fsRoot.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (root *rootNode) load(nsCB func(name string)) error {
105105
root.nsNodes[nsName] = newNamespaceNode(namespace)
106106
} else {
107107
// Update groups if exists
108-
v.nsInfo.Groups = namespace.Groups
108+
v.updateGroups(namespace.Groups)
109109
}
110110

111111
if nsCB != nil {
@@ -140,21 +140,27 @@ func (root *rootNode) Lookup(ctx context.Context, name string, out *fuse.EntryOu
140140
// Delete Namespace if virtual file was unlinked
141141
func (root *rootNode) Rmdir(ctx context.Context, name string) syscall.Errno {
142142
// Throw error if not exists
143-
if _, exists := root.nsNodes[data.trimmedNS(name)]; !exists {
143+
nsNode, exists := root.nsNodes[data.trimmedNS(name)]
144+
if !exists {
144145
return syscall.ENOENT
145146
}
146147

147-
namespace := data.fullNS(name)
148+
// skip 2s delay if no groups are available
149+
skipWait := make(chan bool, 1)
150+
if len(nsNode.nsInfo.Groups) == 0 {
151+
skipWait <- true
152+
}
148153

149154
// wait 2 seconds to ensure, user didn't cancel
150155
select {
151156
case <-ctx.Done():
152157
return syscall.ECANCELED
153158
case <-time.After(2 * time.Second):
159+
case <-skipWait:
154160
}
155161

156162
defer func() {
157-
delete(root.nsNodes, data.trimmedNS(namespace))
163+
delete(root.nsNodes, data.trimmedNS(name))
158164
child := root.GetChild(name)
159165
if child != nil {
160166
child.RmAllChildren()
@@ -164,7 +170,7 @@ func (root *rootNode) Rmdir(ctx context.Context, name string) syscall.Errno {
164170
}()
165171

166172
// Do delete request
167-
if _, err := data.libdm.DeleteNamespace(namespace); err != nil {
173+
if _, err := data.libdm.DeleteNamespace(data.fullNS(name)); err != nil {
168174
printResponseError(err, "rm namespace dir")
169175
return syscall.EFAULT
170176
}
@@ -196,6 +202,7 @@ func (root *rootNode) Rename(ctx context.Context, name string, newParent fs.Inod
196202
return 0
197203
}
198204

205+
// create namespace if ns folder was created
199206
func (root *rootNode) Mkdir(ctx context.Context, name string, mode uint32, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
200207
// Check if created namespace already exists
201208
_, h := root.nsNodes[name]

dmfs/utils.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,20 @@ func printError(message interface{}, err string) {
4343
func getError(message interface{}, err string) string {
4444
return fmt.Sprintf("Error %s: %s\n", message, err)
4545
}
46+
47+
func removeFromStringSlice(s []string, sub string) []string {
48+
i := -1
49+
for j := range s {
50+
if s[j] == sub {
51+
i = j
52+
break
53+
}
54+
}
55+
56+
if i == -1 {
57+
return s
58+
}
59+
60+
s[len(s)-1], s[i] = s[i], s[len(s)-1]
61+
return s[:len(s)-1]
62+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/DataManager-Go/DMM---DataManagerMount
33
go 1.14
44

55
require (
6-
github.com/DataManager-Go/libdatamanager v1.2.3
6+
github.com/DataManager-Go/libdatamanager v1.2.4
77
github.com/DataManager-Go/libdatamanager/config v0.0.0-20200418200810-31f4c1161174
88
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
99
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h
7373
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
7474
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
7575
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
76+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
7677
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7778
golang.org/x/sys v0.0.0-20200406155108-e3b113bbe6a4 h1:c1Sgqkh8v6ZxafNGG64r8C8UisIW2TKMJN8P86tKjr0=
7879
golang.org/x/sys v0.0.0-20200406155108-e3b113bbe6a4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

0 commit comments

Comments
 (0)