Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions drivers/strm/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,27 +175,25 @@ func deleteExtraFiles(driver *Strm, localPath string, objs []model.Obj) {
if driver.SaveLocalMode != SaveLocalSyncMode {
return
}
localFiles, err := getLocalFiles(localPath)
localFiles, localDirs, err := getLocalDirsAndFiles(localPath)
if err != nil {
log.Errorf("Failed to read local files from %s: %v", localPath, err)
return
}

objsSet := make(map[string]struct{})
objsBaseNameSet := make(map[string]struct{})
fileSet := make(map[string]struct{})
dirSet := make(map[string]struct{})
for _, obj := range objs {
objPath := stdpath.Join(localPath, obj.GetName())
if obj.IsDir() {
continue
dirSet[objPath] = struct{}{}
} else {
fileSet[objPath] = struct{}{}
}
objName := obj.GetName()
objsSet[stdpath.Join(localPath, objName)] = struct{}{}

objBaseName := strings.TrimSuffix(objName, utils.SourceExt(objName))
objsBaseNameSet[stdpath.Join(localPath, objBaseName[:len(objBaseName)-1])] = struct{}{}
}

for _, localFile := range localFiles {
if _, exists := objsSet[localFile]; !exists {
if _, exists := fileSet[localFile]; !exists {
err := os.Remove(localFile)
if err != nil {
log.Errorf("Failed to delete file: %s, error: %v\n", localFile, err)
Expand All @@ -204,20 +202,34 @@ func deleteExtraFiles(driver *Strm, localPath string, objs []model.Obj) {
}
}
}

for _, localDir := range localDirs {
if _, exists := dirSet[localDir]; !exists {
err := os.RemoveAll(localDir)
if err != nil {
log.Errorf("Failed to delete directory: %s, error: %v\n", localDir, err)
} else {
log.Infof("Deleted directory %s", localDir)
}
}
}
}

func getLocalFiles(localPath string) ([]string, error) {
var files []string
func getLocalDirsAndFiles(localPath string) ([]string, []string, error) {
var files, dirs []string
entries, err := os.ReadDir(localPath)
if err != nil {
return nil, err
return nil, nil, err
}
for _, entry := range entries {
if !entry.IsDir() {
files = append(files, stdpath.Join(localPath, entry.Name()))
fullPath := stdpath.Join(localPath, entry.Name())
if entry.IsDir() {
dirs = append(dirs, fullPath)
} else {
files = append(files, fullPath)
}
}
return files, nil
return files, dirs, nil
}

func init() {
Expand Down