aboutsummaryrefslogtreecommitdiff
path: root/refstore/files/transaction_cleanup_parents.go
blob: 1e62e6377d3cc82d6fb6ff6b62fe52d7638e805b (about) (plain) (blame)
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
package files

import (
	"errors"
	"os"
	"path"
)

func (tx *Transaction) tryRemoveEmptyParents(name string) {
	loc := tx.store.loosePath(name)
	tx.tryRemoveEmptyParentPaths(loc.root, loc.path)
}

func (tx *Transaction) tryRemoveEmptyParentPaths(kind rootKind, name string) {
	root := tx.store.rootFor(kind)
	dir := path.Dir(name)

	for dir != "." && dir != "/" {
		err := root.Remove(dir)
		if err != nil {
			if errors.Is(err, os.ErrNotExist) {
				return
			}

			var pathErr *os.PathError
			if errors.As(err, &pathErr) {
				return
			}

			return
		}

		dir = path.Dir(dir)
	}
}