dedupe-linker/main.go

71 lines
1.5 KiB
Go
Raw Normal View History

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
"./base"
2014-09-16 21:12:52 +00:00
"./cryptomap"
2014-09-12 20:10:10 +00:00
"./file"
2014-09-12 20:04:20 +00:00
)
var (
flVarBase = flag.String("b", filepath.Join(os.Getenv("HOME"), "var"), "base directory where files are duplicated")
flCipher = flag.String("c", "sha1", "block cipher to use (sha1, or sha256)")
flWorkers = flag.Int("w", 2, "workers to do summing")
)
func init() {
// give ourselves a little wiggle room
if runtime.NumCPU() > 1 && len(os.Getenv("GOMAXPROCS")) == 0 {
runtime.GOMAXPROCS(2)
}
}
func main() {
flag.Parse()
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-09-12 20:10:10 +00:00
if m, err := file.SameDevPaths(*flVarBase, arg); err != nil {
2014-09-12 20:04:20 +00:00
log.Fatal(err)
} else if !m {
log.Printf("SKIPPING: %q is not on the same device as %q", arg, *flVarBase)
continue
}
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)
done <- struct{}{}
}
2014-09-12 20:10:10 +00:00
fmt.Printf("%s %s\n", fi.Hash, fi.Path)
2014-09-16 21:12:52 +00:00
if ourbase.HasBlob(fi.Hash) {
2014-09-12 20:10:10 +00:00
// TODO check if they have the same Inode
// if not, then clobber
} else {
// TODO hard link to blobs
}
2014-09-12 20:04:20 +00:00
}
}
//if len(infos) > 0 {
//fmt.Println("collected", len(infos), "sums")
//}
}