From 7d040f70ebd19e7f0c10b572bdd5657b23caf008 Mon Sep 17 00:00:00 2001 From: Avril Date: Mon, 27 Sep 2021 21:34:47 +0100 Subject: [PATCH] Default behavious changed: Filenames that look like hashes are skipped. Pass --always to override this. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for lazy-rebuild's current commit: Future small blessing − 末小吉 --- Makefile | 4 +++- go.mod | 3 +++ lazy-rebuild.go | 17 +++++++++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 go.mod diff --git a/Makefile b/Makefile index dc4a7e9..a6de1de 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ -all: clean lazy-rebuild test +.PHONY: all +all: | clean + $(MAKE) lazy-rebuild test clean: rm -rf test diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ca5081c --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module lazy-rebuild + +go 1.17 diff --git a/lazy-rebuild.go b/lazy-rebuild.go index 7e7a0bb..ebf4a0e 100644 --- a/lazy-rebuild.go +++ b/lazy-rebuild.go @@ -1,4 +1,3 @@ -//go:binary-only-package package main import ( @@ -12,6 +11,7 @@ import ( "os" "sync" "strconv" + "regexp" ) var keepLongExt bool = false @@ -42,6 +42,12 @@ func hash(file string) []byte { return h.Sum(nil) } +var isHashRE *regexp.Regexp = regexp.MustCompile("^[0-9a-f]{64}$") +func is_hash(name string) bool { + name = strings.Split(name, ".")[0] + return isHashRE.MatchString(name) +} + func extractExt(x string) string { if (keepLongExt) { return last(strings.SplitN(x, ".", 2)[1:]) @@ -50,8 +56,13 @@ func extractExt(x string) string { } } +var alwaysCheck bool = false func dofile(i int, y string) { loc, x := filepath.Split(y) + if !alwaysCheck && is_hash(x) { + fmt.Printf("E: %s already a hash, skipping.\n", x) + return + } sha := hash(y) str := fmt.Sprintf("%x", sha) newname := fmt.Sprintf("%s%s%s", loc, str, extractExt(x)) @@ -72,7 +83,7 @@ func main() { var wg sync.WaitGroup if (len(ar) < 1) { - fmt.Printf("Usage: %s [--long] [--fake] [--recurse] [-quiet] [--threads ] \n\t--long\tKeep long file extensions\n\t--fake\tDo not rename files.\n\t--recurse\tWalk path recursively.\n\t--quiet\tDo not show (some) errors.\n", os.Args[0]) + fmt.Printf("Usage: %s [--long] [--fake] [--recurse] [-quiet] [--threads ] [--always] \n\t--long\tKeep long file extensions\n\t--fake\tDo not rename files.\n\t--recurse\tWalk path recursively.\n\t--quiet\tDo not show (some) errors.\n\t--always\tDo not skip filenames which are already hashes.\n", os.Args[0]) return; } var ignore = false @@ -91,6 +102,8 @@ func main() { recurse = !recurse case "quiet": noErr = !noErr + case "always": + alwaysCheck = true case "threads": if i