Initial commit

master
Ringo Wantanabe 6 years ago
commit d8032e8f32
No known key found for this signature in database
GPG Key ID: C1C1CD34CF2907B2

@ -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

@ -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] <file ...>\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;
}
Loading…
Cancel
Save