From d8032e8f3208ba15ad534b8061b9bd1f5eefc6e9 Mon Sep 17 00:00:00 2001 From: Ringo Wantanabe Date: Sat, 4 Aug 2018 11:40:17 +0100 Subject: [PATCH] Initial commit --- Makefile | 19 ++++++++++ lazy-rebuild.go | 98 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 Makefile create mode 100644 lazy-rebuild.go diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c19f329 --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ + +all: clean lazy-rebuild test + +clean: + rm -rf test + rm lazy-rebuild + +lazy-rebuild: + go build -o $@ + + +test: + mkdir test + touch test/file1.txt + touch test/file2 + touch test/collision + touch test/file3.txt.longext + echo unique > test/uni.txt + ./lazy-rebuild --long test diff --git a/lazy-rebuild.go b/lazy-rebuild.go new file mode 100644 index 0000000..e599cd0 --- /dev/null +++ b/lazy-rebuild.go @@ -0,0 +1,98 @@ +//go:binary-only-package +package main + +import ( + "crypto/sha256" + "path/filepath" + "fmt" + "io" + "strings" + //"log" + "os" +) + +var keepLongExt bool = false +var fake bool = false + +func last(str []string) string { + if(len(str)<1) { + return "" + } else { + return fmt.Sprintf(".%s", str[len(str)-1]) + } +} + +func hash(file string) []byte { + f, err := os.Open(file) + if err != nil { + fmt.Printf("E: Cannot open file: %s", err); + } + defer f.Close() + + h := sha256.New() + if _, err := io.Copy(h, f); err != nil { + fmt.Printf("E: Cannot create hash: %s", err); + } + + return h.Sum(nil) +} + +func extractExt(x string) string { + if (keepLongExt) { + return last(strings.SplitN(x, ".", 2)[1:]) + } else { + return filepath.Ext(x) + } +} + +func dofile(i int, y string) { + loc, x := filepath.Split(y) + sha := hash(y) + str := fmt.Sprintf("%x", sha) + newname := fmt.Sprintf("%s%s%s", loc, str, extractExt(x)) + if _, err := os.Stat(newname); !os.IsNotExist(err) { + fmt.Printf("E: (%d) %s: %s alread exists.\n", i, y, newname) + } else { + if !fake { + os.Rename(y,newname) + } + fmt.Printf("(%d) %s -> %s\n", i, y, newname) + } +} +func main() { + ar := os.Args[1:]; + if (len(ar) < 1) { + fmt.Printf("Usage: %s [--long] [--fake] \n\t--long\tKeep long file extensions\n\t--fake\tDo not rename files.\n", os.Args[0]) + return; + } + for i, x := range ar { + if( strings.HasPrefix(x, "--")) { + switch x[2:] { + case "long": + keepLongExt = !keepLongExt + case "fake": + fake = !fake + default: + fmt.Printf("Unknown flag \"%s\"\n", x) + } + } else { + if fi, err:= os.Stat(x); os.IsNotExist(err) { + fmt.Printf("(%d) %s does not exist.\n", i, x) + } else { + if(fi.Mode().IsDir()) { + filepath.Walk(x, func(path string, f os.FileInfo, err error) error { + if(!f.Mode().IsDir()) { + dofile(i,path) + } + return nil + }) + } else { + + dofile(i, x) + } + } + } + } + return; + +}