2014-09-12 20:04:20 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2014-09-12 20:10:10 +00:00
|
|
|
|
2016-07-18 15:23:33 +00:00
|
|
|
"github.com/vbatts/dedupe-linker/base"
|
|
|
|
"github.com/vbatts/dedupe-linker/cryptomap"
|
|
|
|
"github.com/vbatts/dedupe-linker/file"
|
2014-09-12 20:04:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-09-12 18:55:10 +00:00
|
|
|
varBaseDir = filepath.Join(os.Getenv("HOME"), ".local/dedupe-linker/var")
|
|
|
|
|
|
|
|
flVarBase = flag.String("b", varBaseDir, "base directory where files are duplicated")
|
2014-09-12 20:04:20 +00:00
|
|
|
flCipher = flag.String("c", "sha1", "block cipher to use (sha1, or sha256)")
|
|
|
|
flWorkers = flag.Int("w", 2, "workers to do summing")
|
2014-10-14 20:54:28 +00:00
|
|
|
flNoop = flag.Bool("noop", false, "don't do any moving or linking")
|
2016-09-08 18:13:53 +00:00
|
|
|
flDebug = flag.Bool("debug", false, "enable debug output")
|
2014-09-12 20:04:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// give ourselves a little wiggle room
|
|
|
|
if runtime.NumCPU() > 1 && len(os.Getenv("GOMAXPROCS")) == 0 {
|
|
|
|
runtime.GOMAXPROCS(2)
|
|
|
|
}
|
2016-09-12 18:55:10 +00:00
|
|
|
|
|
|
|
if os.Getenv("VARBASEDIR") != "" {
|
|
|
|
varBaseDir = filepath.Clean(os.Getenv("VARBASEDIR"))
|
|
|
|
}
|
2014-09-12 20:04:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2014-09-16 21:12:52 +00:00
|
|
|
|
2016-09-08 18:13:53 +00:00
|
|
|
if *flDebug {
|
|
|
|
os.Setenv("DEBUG", "1")
|
|
|
|
}
|
|
|
|
|
2016-09-12 18:55:10 +00:00
|
|
|
if os.Getenv("DEBUG") != "" {
|
|
|
|
fmt.Fprintf(os.Stderr, "VARBASEDIR=%q\n", *flVarBase)
|
|
|
|
}
|
|
|
|
|
2014-09-16 21:12:52 +00:00
|
|
|
// TODO the *flCipher has not been checked yet, and would cause the directory to get created
|
|
|
|
ourbase, err := base.NewBase(*flVarBase, *flCipher)
|
|
|
|
if err != nil {
|
2014-09-12 20:04:20 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2014-09-16 21:12:52 +00:00
|
|
|
hash = cryptomap.DetermineHash(*flCipher)
|
2014-09-12 20:10:10 +00:00
|
|
|
//infos = []*file.FileHashInfo{}
|
|
|
|
//results := make(chan file.FileHashInfo, 2)
|
2014-09-12 20:04:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for _, arg := range flag.Args() {
|
2014-10-15 03:28:37 +00:00
|
|
|
if !*flNoop {
|
|
|
|
if m, err := file.SameDevPaths(*flVarBase, arg); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
} else if !m {
|
|
|
|
log.Printf("SKIPPING: %q is not on the same device as %q", arg, *flVarBase)
|
|
|
|
continue
|
|
|
|
}
|
2014-09-12 20:04:20 +00:00
|
|
|
}
|
|
|
|
done := make(chan struct{})
|
2014-09-12 20:10:10 +00:00
|
|
|
infos := file.HashFileGetter(arg, hash, *flWorkers, done)
|
2014-09-12 20:04:20 +00:00
|
|
|
for fi := range infos {
|
|
|
|
if fi.Err != nil {
|
|
|
|
log.Println(fi.Err)
|
2014-10-14 21:07:13 +00:00
|
|
|
//done <- struct{}{}
|
2014-09-12 20:04:20 +00:00
|
|
|
}
|
2014-10-14 20:54:28 +00:00
|
|
|
if *flNoop {
|
|
|
|
fmt.Printf("%s [%d] %s\n", fi.Hash, fi.Size, fi.Path)
|
2014-09-12 20:10:10 +00:00
|
|
|
} else {
|
2016-09-08 18:13:53 +00:00
|
|
|
if os.Getenv("DEBUG") != "" {
|
|
|
|
fmt.Printf("%q: %q\n", fi.Path, ourbase.HasBlob(fi.Hash))
|
|
|
|
}
|
2014-10-14 20:54:28 +00:00
|
|
|
if ourbase.HasBlob(fi.Hash) && !ourbase.SameFile(fi.Hash, fi.Path) {
|
|
|
|
if err := ourbase.LinkTo(fi.Path, fi.Hash); err != nil {
|
|
|
|
log.Println("ERROR-1", err)
|
|
|
|
}
|
2016-09-08 18:13:53 +00:00
|
|
|
} else if !ourbase.HasBlob(fi.Hash) {
|
2014-10-14 20:54:28 +00:00
|
|
|
if err := ourbase.LinkFrom(fi.Path, fi.Hash); err != nil {
|
|
|
|
log.Println("ERROR-2", err)
|
|
|
|
}
|
|
|
|
}
|
2014-09-12 20:10:10 +00:00
|
|
|
}
|
2014-09-12 20:04:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//if len(infos) > 0 {
|
|
|
|
//fmt.Println("collected", len(infos), "sums")
|
|
|
|
//}
|
|
|
|
}
|