Skip to content

Commit c329a9b

Browse files
author
JojiiOfficial
committed
fix remove local file after delete
1 parent 86cb541 commit c329a9b

File tree

4 files changed

+54
-48
lines changed

4 files changed

+54
-48
lines changed

dmfs/data.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ func (data *dataStruct) loadFiles(attributes libdatamanager.FileAttributes) ([]l
8080
return data.filesCache[attributes.Namespace], nil
8181
}
8282

83+
func (data *dataStruct) removeCachedFile(fileName, namespaceName string) {
84+
for i := range data.filesCache[namespaceName] {
85+
if data.filesCache[namespaceName][i].Name == fileName {
86+
data.filesCache[namespaceName] = removeFileByIndex(data.filesCache[namespaceName], i)
87+
return
88+
}
89+
}
90+
91+
// invalide caches
92+
data.lastFileload[namespaceName] = 0
93+
}
94+
8395
func (data *dataStruct) getLastFileLoad(namespace string) int64 {
8496
v, h := data.lastFileload[namespace]
8597

dmfs/fsGroup.go

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package dmfs
22

33
import (
44
"context"
5-
"fmt"
65
"sync"
76
"syscall"
7+
"time"
88

99
"github.com/DataManager-Go/libdatamanager"
1010
libdm "github.com/DataManager-Go/libdatamanager"
@@ -15,7 +15,7 @@ import (
1515
const (
1616
// NoGroupFolder foldername for
1717
// files without groups
18-
NoGroupFolder = "no_group"
18+
NoGroupFolder = "all_files"
1919
)
2020

2121
var (
@@ -31,18 +31,21 @@ type groupNode struct {
3131
group string
3232
isNoGroupPlaceholder bool
3333

34-
files []libdm.FileResponseItem
34+
fileMap map[string]*libdm.FileResponseItem
3535

3636
mx sync.Mutex
3737
}
3838

3939
// Create a new group node
4040
func newGroupNode(namespace, group string) *groupNode {
41-
return &groupNode{
41+
groupNode := &groupNode{
4242
namespace: namespace,
4343
group: group,
4444
isNoGroupPlaceholder: group == NoGroupFolder,
4545
}
46+
groupNode.fileMap = make(map[string]*libdm.FileResponseItem)
47+
48+
return groupNode
4649
}
4750
func (groupNode *groupNode) getRequestAttributes() libdatamanager.FileAttributes {
4851
// Don't send any group to get
@@ -60,43 +63,41 @@ func (groupNode *groupNode) getRequestAttributes() libdatamanager.FileAttributes
6063

6164
// List files in group
6265
func (groupNode *groupNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
63-
fmt.Println("readdir node")
6466
r := make([]fuse.DirEntry, 0)
6567

66-
files, err := data.loadFiles(groupNode.getRequestAttributes())
67-
if err != nil {
68-
return nil, syscall.EIO
69-
}
70-
71-
for i := range files {
68+
err := groupNode.loadfiles(func(name string) {
7269
r = append(r, fuse.DirEntry{
7370
Mode: syscall.S_IFREG,
74-
Name: files[i].Name,
71+
Name: name,
7572
})
73+
})
74+
75+
if err != nil {
76+
return nil, syscall.EIO
7677
}
7778

78-
groupNode.files = files
7979
return fs.NewListDirStream(r), 0
8080
}
8181

82-
func (groupNode *groupNode) loadfiles() error {
83-
fmt.Println("readdir node")
84-
82+
func (groupNode *groupNode) loadfiles(nsCB func(name string)) error {
8583
files, err := data.loadFiles(groupNode.getRequestAttributes())
8684
if err != nil {
8785
return err
8886
}
8987

90-
groupNode.files = files
88+
for i := range files {
89+
groupNode.fileMap[files[i].Name] = &files[i]
90+
if nsCB != nil {
91+
nsCB(files[i].Name)
92+
}
93+
}
94+
9195
return nil
9296
}
9397

9498
func (groupNode *groupNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
95-
groupNode.mx.Lock()
96-
defer groupNode.mx.Unlock()
97-
98-
file := groupNode.findFile(name)
99-
if file == nil {
99+
file, has := groupNode.fileMap[name]
100+
if !has || file == nil {
100101
return nil, syscall.ENOENT
101102
}
102103

@@ -131,18 +132,16 @@ func (groupNode *groupNode) setFileAttrs(file *libdatamanager.FileResponseItem,
131132
Gid: data.gid,
132133
Uid: data.uid,
133134
}
135+
136+
out.SetEntryTimeout(1 * time.Millisecond)
134137
}
135138

136139
// Delete file
137140
func (groupNode *groupNode) Unlink(ctx context.Context, name string) syscall.Errno {
138-
f := groupNode.findFile(name)
139-
if f == nil {
141+
file := groupNode.findFile(name)
142+
if file == nil {
140143
return syscall.ENOENT
141144
}
142-
file := *f
143-
144-
// Remove local
145-
groupNode.removeFile(file)
146145

147146
// Make delete http request
148147
_, err := data.libdm.DeleteFile("", file.ID, false, groupNode.getRequestAttributes())
@@ -151,33 +150,23 @@ func (groupNode *groupNode) Unlink(ctx context.Context, name string) syscall.Err
151150
return syscall.EIO
152151
}
153152

154-
// FIXME delete correctly from slice
153+
groupNode.removeFile(name)
154+
groupNode.GetChild(name).ForgetPersistent()
155155

156156
return 0
157157
}
158158

159159
// Find file by name
160160
func (groupNode *groupNode) findFile(name string) *libdatamanager.FileResponseItem {
161-
for i := range groupNode.files {
162-
if groupNode.files[i].Name == name {
163-
return &groupNode.files[i]
164-
}
161+
file, has := groupNode.fileMap[name]
162+
if !has {
163+
return nil
165164
}
166165

167-
return nil
166+
return file
168167
}
169168

170-
// Remove file from list
171-
func (groupNode *groupNode) removeFile(file libdatamanager.FileResponseItem) bool {
172-
groupNode.mx.Lock()
173-
defer groupNode.mx.Unlock()
174-
175-
for i := range groupNode.files {
176-
if groupNode.files[i].ID == file.ID {
177-
groupNode.files = removeFileByIndex(groupNode.files, i)
178-
return true
179-
}
180-
}
181-
182-
return false
169+
func (groupNode *groupNode) removeFile(name string) {
170+
delete(groupNode.fileMap, name)
171+
data.removeCachedFile(name, groupNode.namespace)
183172
}

dmfs/fsNamespace.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func (nsNode *namespaceNode) Rmdir(ctx context.Context, name string) syscall.Err
122122

123123
// Remove group from list
124124
nsNode.nsInfo.Groups = removeFromStringSlice(nsNode.nsInfo.Groups, name)
125+
data.lastUserAttrLoad = 0
125126

126127
return 0
127128
}
@@ -148,7 +149,7 @@ func (nsNode *namespaceNode) Mkdir(ctx context.Context, name string, mode uint32
148149
// Create group
149150
_, err := data.libdm.CreateAttribute(libdm.GroupAttribute, nsNode.nsInfo.Name, name)
150151
if err != nil {
151-
printResponseError(err, "creating group")
152+
printResponseError(err, "creating group "+name)
152153
return nil, syscall.EIO
153154
}
154155

dmfs/fsRoot.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ func (root *rootNode) Rmdir(ctx context.Context, name string) syscall.Errno {
177177
return syscall.EFAULT
178178
}
179179

180+
data.lastUserAttrLoad = 0
181+
180182
return 0
181183
}
182184

@@ -200,6 +202,8 @@ func (root *rootNode) Rename(ctx context.Context, name string, newParent fs.Inod
200202
return syscall.EIO
201203
}
202204

205+
data.lastUserAttrLoad = 0
206+
203207
// Return success
204208
return 0
205209
}

0 commit comments

Comments
 (0)