2017-11-18 15:56:53 +00:00
|
|
|
// Copyright (C) 2017 The Syncthing Authors.
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
|
|
package versioner
|
|
|
|
|
|
|
|
|
|
import (
|
2025-08-07 11:19:36 +02:00
|
|
|
"log/slog"
|
2017-11-18 15:56:53 +00:00
|
|
|
"path/filepath"
|
2025-05-26 20:37:49 +02:00
|
|
|
"slices"
|
|
|
|
|
"strings"
|
2017-11-18 15:56:53 +00:00
|
|
|
|
2025-08-07 11:19:36 +02:00
|
|
|
"github.com/syncthing/syncthing/internal/slogutil"
|
2017-11-18 15:56:53 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type emptyDirTracker map[string]struct{}
|
|
|
|
|
|
|
|
|
|
func (t emptyDirTracker) addDir(path string) {
|
|
|
|
|
if path == "." {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
t[path] = struct{}{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove all dirs from the path to the file
|
|
|
|
|
func (t emptyDirTracker) addFile(path string) {
|
|
|
|
|
dir := filepath.Dir(path)
|
|
|
|
|
for dir != "." {
|
|
|
|
|
delete(t, dir)
|
|
|
|
|
dir = filepath.Dir(dir)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t emptyDirTracker) emptyDirs() []string {
|
2023-07-18 14:44:37 +00:00
|
|
|
var empty []string
|
|
|
|
|
|
2017-11-18 15:56:53 +00:00
|
|
|
for dir := range t {
|
|
|
|
|
empty = append(empty, dir)
|
|
|
|
|
}
|
2025-05-26 20:37:49 +02:00
|
|
|
slices.SortFunc(empty, func(a, b string) int {
|
|
|
|
|
return strings.Compare(b, a)
|
|
|
|
|
})
|
2017-11-18 15:56:53 +00:00
|
|
|
return empty
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t emptyDirTracker) deleteEmptyDirs(fs fs.Filesystem) {
|
|
|
|
|
for _, path := range t.emptyDirs() {
|
|
|
|
|
l.Debugln("Cleaner: deleting empty directory", path)
|
|
|
|
|
err := fs.Remove(path)
|
|
|
|
|
if err != nil {
|
2025-08-07 11:19:36 +02:00
|
|
|
slog.Warn("Failed to remove versioned directory", slogutil.FilePath(path), slogutil.Error(err))
|
2017-11-18 15:56:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|