Files
mstore/pkg/auxtool/cleandir.go
T
2026-02-06 15:31:35 +02:00

44 lines
940 B
Go

/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*
* This work is published and licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* Distribution of this work is permitted, but commercial use and
* modifications are strictly prohibited.
*/
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
}
}
}
}