From 9469379b8001c494c7d0e7e36669cb4456bd9e65 Mon Sep 17 00:00:00 2001 From: lordwelch Date: Sun, 24 Jun 2018 17:18:00 -0700 Subject: [PATCH] init --- cmd/main.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ san.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 cmd/main.go create mode 100644 san.go diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..f38913e --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,57 @@ +package main + +import ( + "fmt" + "io" + "os" + "path/filepath" + "strings" + + "timmy.narnian.us/git/timmy/sanitize" +) + +func main() { + var previousDir string = "./" + filepath.Walk("./", func(path string, info os.FileInfo, err error) error { + fmt.Printf("%s >>>>> %s\t%t\n", previousDir, path, strings.HasPrefix(path, previousDir) || previousDir == ".") + defer func() { previousDir = filepath.Dir(path) }() + if err != nil { + println(err) + } + + if info.IsDir() && !DirIsEmpty(path) { + return nil + } + + newpath := sanitize.SanitizeFilepath(filepath.Dir(path)) + filename := sanitize.SanitizeFilename(info.Name()) + if path != filepath.Join(newpath, filename) { + fmt.Println(path, "->", filepath.Join(newpath, filename)) + os.MkdirAll(newpath, os.ModePerm) + os.Rename(path, filepath.Join(newpath, info.Name())) + } + if !(strings.HasPrefix(path, previousDir) || previousDir == "./") { + rmpath := previousDir + for len(rmpath) > 1 { + fmt.Println("rmdir", rmpath) + fmt.Println(rmpath, os.Remove(rmpath)) + rmpath = filepath.Dir(rmpath) + } + } + + return nil + }) + fmt.Println(previousDir) +} + +// also returns false if os.Open fails +func DirIsEmpty(path string) bool { + file, err := os.Open(path) + if err != nil { + return false + } + if _, err = file.Readdirnames(1); err == io.EOF { + return true + } + return false +} diff --git a/san.go b/san.go new file mode 100644 index 0000000..508f24a --- /dev/null +++ b/san.go @@ -0,0 +1,49 @@ +package sanitize + +import ( + "path/filepath" + "strings" +) + +const BadFilenameChars = "\n\t\r=*?:[]\"<>|(){}&'!;$`\\" + +//assumes unicode +func SanitizeFilename(filename string) string { + filename = strings.Map(func(r rune) rune { + if r < 32 || (r > 126 && r < 160) || strings.ContainsRune(BadFilenameChars, r) { + return -1 + } + return r + }, filename) + + filename = strings.TrimSpace(filename) + filename = strings.TrimLeft(filename, " -~") + filename = strings.TrimSpace(filename) + filename = strings.Replace(filename, " ", "_", -1) + filename = strings.Replace(filename, "_-_", "", -1) + var ( + st strings.Builder + prev rune + ) + st.Grow(len(filename)) + for _, r := range filename { + switch r { + case '_': + if r != prev { + st.WriteRune(r) + } + default: + st.WriteRune(r) + } + prev = r + } + return st.String() +} + +func SanitizeFilepath(path string) string { + var newpath string + for _, filename := range strings.Split(filepath.Clean(path), string(filepath.Separator)) { + newpath = filepath.Join(newpath, SanitizeFilename(filename)) + } + return newpath +}