File: remove.go

package info (click to toggle)
golang-github-hlandau-goutils 0.0~git20160722.0.0cdb66a-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 160 kB
  • sloc: makefile: 2
file content (28 lines) | stat: -rw-r--r-- 652 bytes parent folder | download | duplicates (4)
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
package os

import gos "os"
import "path/filepath"

// Delete a tree of empty directories. Returns non-nil error if
// an empty directory cannot be deleted. Does not return an error
// if there are non-empty directories. This function can be used
// to prune empty directories from a tree.
func RemoveEmpty(path string) error {
	var dirs []string

	return filepath.Walk(path, func(path string, info gos.FileInfo, err error) error {
		if info.Mode().IsDir() {
			dirs = append(dirs, path)
		}
		return nil
	})

	for i := len(dirs) - 1; i >= 0; i-- {
		err := gos.Remove(dirs[i])
		if err != nil && !IsNotEmpty(err) {
			return err
		}
	}

	return nil
}