35 lines
632 B
Go
35 lines
632 B
Go
package auxtool
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// Clean only overbase elements of dir path if possible
|
|
func CleanDirs(basedir, datadir string) {
|
|
separator := string(os.PathSeparator)
|
|
|
|
basedir = filepath.Clean(separator + basedir)
|
|
datadir = filepath.Clean(separator + datadir)
|
|
|
|
items := strings.Split(datadir, separator)
|
|
for i := len(items); i > 0; i-- {
|
|
p := filepath.Join(items[0:i]...)
|
|
p = filepath.Clean(separator + p)
|
|
if p == basedir {
|
|
break
|
|
}
|
|
fileInfo, err := os.Stat(p)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if fileInfo.IsDir() {
|
|
err = os.Remove(p)
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|