dedupe-linker/main.go

70 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"
"./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-12 20:10:10 +00:00
if err := base.InitVarBase(*flVarBase); err != nil {
2014-09-12 20:04:20 +00:00
log.Fatal(err)
}
var (
2014-09-12 20:10:10 +00:00
hash = DetermineHash(*flCipher)
ourbase = base.Base{Path: *flVarBase}
//infos = []*file.FileHashInfo{}
2014-09-12 20:04:20 +00:00
//mu = sync.Mutex{}
2014-09-12 20:10:10 +00:00
//results := make(chan file.FileHashInfo, 2)
2014-09-12 20:04:20 +00:00
//wg := sync.WaitGroup{}
)
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)
if ourbase.HasBlob(fi.HashType, fi.Hash) {
// 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")
//}
}