Skip to content

Commit ea4b5e3

Browse files
authored
fix(drivers/strm): delete extra local directories in sync mode (OpenListTeam#1980)
1 parent 378e37b commit ea4b5e3

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

drivers/strm/hook.go

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -175,27 +175,25 @@ func deleteExtraFiles(driver *Strm, localPath string, objs []model.Obj) {
175175
if driver.SaveLocalMode != SaveLocalSyncMode {
176176
return
177177
}
178-
localFiles, err := getLocalFiles(localPath)
178+
localFiles, localDirs, err := getLocalDirsAndFiles(localPath)
179179
if err != nil {
180180
log.Errorf("Failed to read local files from %s: %v", localPath, err)
181181
return
182182
}
183183

184-
objsSet := make(map[string]struct{})
185-
objsBaseNameSet := make(map[string]struct{})
184+
fileSet := make(map[string]struct{})
185+
dirSet := make(map[string]struct{})
186186
for _, obj := range objs {
187+
objPath := stdpath.Join(localPath, obj.GetName())
187188
if obj.IsDir() {
188-
continue
189+
dirSet[objPath] = struct{}{}
190+
} else {
191+
fileSet[objPath] = struct{}{}
189192
}
190-
objName := obj.GetName()
191-
objsSet[stdpath.Join(localPath, objName)] = struct{}{}
192-
193-
objBaseName := strings.TrimSuffix(objName, utils.SourceExt(objName))
194-
objsBaseNameSet[stdpath.Join(localPath, objBaseName[:len(objBaseName)-1])] = struct{}{}
195193
}
196194

197195
for _, localFile := range localFiles {
198-
if _, exists := objsSet[localFile]; !exists {
196+
if _, exists := fileSet[localFile]; !exists {
199197
err := os.Remove(localFile)
200198
if err != nil {
201199
log.Errorf("Failed to delete file: %s, error: %v\n", localFile, err)
@@ -204,20 +202,34 @@ func deleteExtraFiles(driver *Strm, localPath string, objs []model.Obj) {
204202
}
205203
}
206204
}
205+
206+
for _, localDir := range localDirs {
207+
if _, exists := dirSet[localDir]; !exists {
208+
err := os.RemoveAll(localDir)
209+
if err != nil {
210+
log.Errorf("Failed to delete directory: %s, error: %v\n", localDir, err)
211+
} else {
212+
log.Infof("Deleted directory %s", localDir)
213+
}
214+
}
215+
}
207216
}
208217

209-
func getLocalFiles(localPath string) ([]string, error) {
210-
var files []string
218+
func getLocalDirsAndFiles(localPath string) ([]string, []string, error) {
219+
var files, dirs []string
211220
entries, err := os.ReadDir(localPath)
212221
if err != nil {
213-
return nil, err
222+
return nil, nil, err
214223
}
215224
for _, entry := range entries {
216-
if !entry.IsDir() {
217-
files = append(files, stdpath.Join(localPath, entry.Name()))
225+
fullPath := stdpath.Join(localPath, entry.Name())
226+
if entry.IsDir() {
227+
dirs = append(dirs, fullPath)
228+
} else {
229+
files = append(files, fullPath)
218230
}
219231
}
220-
return files, nil
232+
return files, dirs, nil
221233
}
222234

223235
func init() {

0 commit comments

Comments
 (0)