File: fs_utils.go

package info (click to toggle)
dh-make-golang 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 344 kB
  • sloc: makefile: 12; sh: 9
file content (26 lines) | stat: -rw-r--r-- 595 bytes parent folder | download | duplicates (2)
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
package main

import (
	"io/fs"
	"os"
	"path/filepath"
)

// forceRemoveAll is a more robust alternative to [os.RemoveAll] that tries
// harder to remove all the files and directories.
func forceRemoveAll(path string) error {
	// first pass to make sure all the directories are writable
	err := filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
		if info.IsDir() {
			return os.Chmod(path, 0777)
		} else {
			// remove files by the way
			return os.Remove(path)
		}
	})
	if err != nil {
		return err
	}
	// remove the remaining directories
	return os.RemoveAll(path)
}