Skip to content

Commit d55dcc9

Browse files
author
JojiiOfficial
committed
improve file loading
1 parent edf422b commit d55dcc9

File tree

5 files changed

+117
-26
lines changed

5 files changed

+117
-26
lines changed

dmfs/data.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,29 @@ var data dataStruct
1616

1717
// data provides the data for the fs
1818
type dataStruct struct {
19+
// Lib and config stuff
1920
mounter *Mounter
2021
config *dmConfig.Config
2122
libdm *libdatamanager.LibDM
2223

24+
// Request stuff + cache
25+
// -- User attributes
2326
userAttributes *libdm.UserAttributeDataResponse
2427
lastUserAttrLoad int64
28+
// -- Files
29+
filesCache map[string][]libdm.FileResponseItem
30+
lastFileload map[string]int64
2531

32+
// Global values
2633
gid, uid uint32
2734
}
2835

2936
func initData() {
3037
data.gid = uint32(os.Getegid())
3138
data.uid = uint32(os.Getuid())
39+
40+
data.filesCache = make(map[string][]libdm.FileResponseItem)
41+
data.lastFileload = make(map[string]int64)
3242
}
3343

3444
// load user attributes (namespaces, groups)
@@ -39,7 +49,7 @@ func (data *dataStruct) loadUserAttributes() error {
3949
if time.Now().Unix()-5 > data.lastUserAttrLoad {
4050
fmt.Println("reload")
4151
var err error
42-
data.userAttributes, err = data.libdm.GetUserAttributeData(1)
52+
data.userAttributes, err = data.libdm.GetUserAttributeData()
4353
if err != nil {
4454
printResponseError(err, "loading user attributes")
4555
return err
@@ -51,6 +61,37 @@ func (data *dataStruct) loadUserAttributes() error {
5161
return nil
5262
}
5363

64+
func (data *dataStruct) loadFiles(attributes libdatamanager.FileAttributes) ([]libdatamanager.FileResponseItem, error) {
65+
lastLoad := data.getLastFileLoad(attributes.Namespace)
66+
67+
if time.Now().Unix()-5 > lastLoad {
68+
fmt.Println("fresh file load")
69+
resp, err := data.libdm.ListFiles("", 0, false, attributes, 0)
70+
if err != nil {
71+
return nil, err
72+
}
73+
74+
// Set cache
75+
data.filesCache[attributes.Namespace] = resp.Files
76+
return resp.Files, nil
77+
}
78+
79+
return data.filesCache[attributes.Namespace], nil
80+
}
81+
82+
func (data *dataStruct) getLastFileLoad(namespace string) int64 {
83+
v, h := data.lastFileload[namespace]
84+
defer func() {
85+
data.lastFileload[namespace] = time.Now().Unix()
86+
}()
87+
88+
if !h {
89+
return 0
90+
}
91+
92+
return v
93+
}
94+
5495
func (data *dataStruct) setUserAttr(inAttr *fuse.AttrOut) {
5596
inAttr.Gid = data.gid
5697
inAttr.Uid = data.uid

dmfs/fsFile.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dmfs
22

33
import (
44
"context"
5+
"fmt"
56
"syscall"
67

78
libdm "github.com/DataManager-Go/libdatamanager"
@@ -19,6 +20,7 @@ var _ = (fs.NodeGetattrer)((*fileInode)(nil))
1920

2021
// Set file attributes
2122
func (fnode *fileInode) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
23+
fmt.Println("getatr")
2224
out.Size = uint64(fnode.file.Size)
2325
out.Ctime = uint64(fnode.file.CreationDate.Unix())
2426
out.Mtime = out.Ctime

dmfs/fsGroup.go

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package dmfs
22

33
import (
44
"context"
5+
"fmt"
56
"syscall"
67

78
"github.com/DataManager-Go/libdatamanager"
9+
libdm "github.com/DataManager-Go/libdatamanager"
810
"github.com/hanwen/go-fuse/v2/fs"
11+
"github.com/hanwen/go-fuse/v2/fuse"
912
)
1013

1114
const (
@@ -15,44 +18,91 @@ const (
1518
)
1619

1720
var (
18-
_ = (fs.NodeOnAdder)((*groupInode)(nil))
21+
_ = (fs.NodeReaddirer)((*groupNode)(nil))
22+
_ = (fs.NodeLookuper)((*groupNode)(nil))
1923
)
2024

21-
type groupInode struct {
25+
type groupNode struct {
2226
fs.Inode
2327

2428
namespace string
2529
group string
2630
isNoGroupPlaceholder bool
31+
32+
files []libdm.FileResponseItem
2733
}
2834

29-
// List files
30-
func (groupInode *groupInode) OnAdd(ctx context.Context) {
31-
groupAdd := []string{groupInode.group}
35+
// Create a new group node
36+
func newGroupNode(namespace, group string) *groupNode {
37+
return &groupNode{
38+
namespace: namespace,
39+
group: group,
40+
isNoGroupPlaceholder: group == NoGroupFolder,
41+
}
42+
}
3243

33-
// Check if group is no_group
34-
if len(groupAdd) == 1 && groupAdd[0] == NoGroupFolder {
35-
groupInode.isNoGroupPlaceholder = true
36-
groupAdd = []string{}
44+
func (groupNode *groupNode) getRequestAttributes() libdatamanager.FileAttributes {
45+
// Don't send any group to get
46+
// all files without groups
47+
reqGroup := []string{groupNode.group}
48+
if groupNode.isNoGroupPlaceholder {
49+
reqGroup = []string{}
3750
}
3851

39-
files, err := data.libdm.ListFiles("", 0, false, libdatamanager.FileAttributes{
40-
Groups: groupAdd,
41-
Namespace: groupInode.namespace,
42-
}, 0)
52+
return libdatamanager.FileAttributes{
53+
Namespace: groupNode.namespace,
54+
Groups: reqGroup,
55+
}
56+
}
4357

58+
// List files in group
59+
func (groupNode *groupNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
60+
r := make([]fuse.DirEntry, 0)
61+
files, err := data.loadFiles(groupNode.getRequestAttributes())
4462
if err != nil {
45-
printResponseError(err, "getting files for "+groupInode.group)
46-
return
63+
return nil, syscall.EIO
64+
}
65+
66+
for i := range files {
67+
r = append(r, fuse.DirEntry{
68+
Mode: syscall.S_IFREG,
69+
Name: files[i].Name,
70+
})
71+
72+
fmt.Println(files[i].Name)
4773
}
4874

49-
for _, file := range files.Files {
50-
fileNode := groupInode.NewInode(ctx, &fileInode{
51-
file: &file,
75+
groupNode.files = files
76+
77+
return fs.NewListDirStream(r), 0
78+
}
79+
80+
func (groupNode *groupNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
81+
file := groupNode.findFile(name)
82+
if file == nil {
83+
return nil, syscall.ENOENT
84+
}
85+
86+
file.Attributes.Namespace = groupNode.namespace
87+
88+
child := groupNode.GetChild(name)
89+
if child == nil {
90+
child = groupNode.NewInode(ctx, &fileInode{
91+
file: file,
5292
}, fs.StableAttr{
5393
Mode: syscall.S_IFREG,
5494
})
95+
}
5596

56-
groupInode.AddChild(file.Name, fileNode, true)
97+
return child, 0
98+
}
99+
100+
func (groupNode *groupNode) findFile(name string) *libdatamanager.FileResponseItem {
101+
for i := range groupNode.files {
102+
if groupNode.files[i].Name == name {
103+
return &groupNode.files[i]
104+
}
57105
}
106+
107+
return nil
58108
}

dmfs/fsNamespace.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,7 @@ func (nsNode *namespaceNode) Lookup(ctx context.Context, name string, out *fuse.
9494

9595
// create child if not found
9696
if child == nil {
97-
v := &groupInode{
98-
group: name,
99-
isNoGroupPlaceholder: name == NoGroupFolder,
100-
namespace: nsNode.nsInfo.Name,
101-
}
97+
v := newGroupNode(nsNode.nsInfo.Name, name)
10298

10399
child = nsNode.NewInode(ctx, v, fs.StableAttr{
104100
Mode: syscall.S_IFDIR,
@@ -155,7 +151,7 @@ func (nsNode *namespaceNode) Mkdir(ctx context.Context, name string, mode uint32
155151
}
156152

157153
nsNode.nsInfo.Groups = append(nsNode.nsInfo.Groups, name)
158-
node := nsNode.NewInode(ctx, &groupInode{
154+
node := nsNode.NewInode(ctx, &groupNode{
159155
group: name,
160156
namespace: nsNode.nsInfo.Name,
161157
}, fs.StableAttr{

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
33
github.com/DataManager-Go/libdatamanager v1.0.10/go.mod h1:iLQrv9tvUkcx9nA+x51eUR9juV0JgIOnFrhPISxWWUE=
44
github.com/DataManager-Go/libdatamanager v1.2.3 h1:a6qTHmrFG6tpBecMM4XxEtV5sqerjnRPS52NMPdHyKM=
55
github.com/DataManager-Go/libdatamanager v1.2.3/go.mod h1:s2ywdIZpWEfgxJ7iMtCOdLCNaHVnO8dr7e9cE2NzgL4=
6+
github.com/DataManager-Go/libdatamanager v1.2.4 h1:K5AHX32XPLO4RyU3WrE0tBSYVDAaWWrwuuKBMq5RMVs=
7+
github.com/DataManager-Go/libdatamanager v1.2.4/go.mod h1:s2ywdIZpWEfgxJ7iMtCOdLCNaHVnO8dr7e9cE2NzgL4=
68
github.com/DataManager-Go/libdatamanager/config v0.0.0-20200418200810-31f4c1161174 h1:exehOGqNqQ8/g5R4PPP/Qcok6kt4Q+os6b0+rEcsWlU=
79
github.com/DataManager-Go/libdatamanager/config v0.0.0-20200418200810-31f4c1161174/go.mod h1:RfoOPe8dQgubuBUmtqEYu1Ny0fT+htZH/ArrEl0dcHQ=
810
github.com/JojiiOfficial/configService v0.0.0-20200219132202-6e71512e2e28 h1:nYoIExG+Z/gSLS9Jbpu6lnrh+m6e9gTxQfGhanTsExE=

0 commit comments

Comments
 (0)