File: empty_dir_tracker.go

package info (click to toggle)
syncthing 1.19.2~ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 21,484 kB
  • sloc: javascript: 36,375; sh: 1,804; xml: 1,049; makefile: 67
file content (51 lines) | stat: -rw-r--r-- 1,137 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// 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 (
	"path/filepath"
	"sort"

	"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 {
	empty := []string{}
	for dir := range t {
		empty = append(empty, dir)
	}
	sort.Sort(sort.Reverse(sort.StringSlice(empty)))
	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 {
			l.Warnln("Versioner: can't remove directory", path, err)
		}
	}
}