|  | @@ -1,6 +1,7 @@
 | 
	
		
			
				|  |  |  package main
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import (
 | 
	
		
			
				|  |  | +	"bufio"
 | 
	
		
			
				|  |  |  	"crypto/sha256"
 | 
	
		
			
				|  |  |  	"encoding/hex"
 | 
	
		
			
				|  |  |  	"encoding/json"
 | 
	
	
		
			
				|  | @@ -32,9 +33,11 @@ var mu sync.RWMutex
 | 
	
		
			
				|  |  |  var driveLetter string
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  var rewrite bool
 | 
	
		
			
				|  |  | +var compare bool
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  func init() {
 | 
	
		
			
				|  |  |  	flag.BoolVarP(&rewrite, "rewrite", "r", false, "rewrite all fhhashes files.")
 | 
	
		
			
				|  |  | +	flag.BoolVarP(&compare, "compare", "c", false, "compare all file hashes and writing a compartion report.")
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  func main() {
 | 
	
	
		
			
				|  | @@ -53,14 +56,18 @@ func main() {
 | 
	
		
			
				|  |  |  		if runtime.GOOS == "windows" {
 | 
	
		
			
				|  |  |  			driveLetter = filepath.VolumeName(myFile) + "/"
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  | -		processFolder(myFile)
 | 
	
		
			
				|  |  | +		if compare {
 | 
	
		
			
				|  |  | +			compareFolder(myFile)
 | 
	
		
			
				|  |  | +		} else {
 | 
	
		
			
				|  |  | +			processFolder(myFile)
 | 
	
		
			
				|  |  | +			fmt.Println("waiting")
 | 
	
		
			
				|  |  | +			wg.Wait()
 | 
	
		
			
				|  |  | +			saveAllHashFiles()
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  |  	} else {
 | 
	
		
			
				|  |  |  		log.Printf("file %s has hash %s\n", myFile, getSha256Hash(myFile))
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	fmt.Println("waiting")
 | 
	
		
			
				|  |  | -	wg.Wait()
 | 
	
		
			
				|  |  | -	saveAllHashFiles()
 | 
	
		
			
				|  |  |  	log.Println("done")
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -219,3 +226,71 @@ func loadHashfile(fileStr string) Fdhashes {
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  	return data
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +func compareFolder(folder string) {
 | 
	
		
			
				|  |  | +	loadAllHashFiles(folder)
 | 
	
		
			
				|  |  | +	size := len(hashes)
 | 
	
		
			
				|  |  | +	f, err := os.Create("report.txt")
 | 
	
		
			
				|  |  | +	check(err)
 | 
	
		
			
				|  |  | +	w := bufio.NewWriter(f)
 | 
	
		
			
				|  |  | +	count := 0
 | 
	
		
			
				|  |  | +	for _, hashFile := range hashes {
 | 
	
		
			
				|  |  | +		count++
 | 
	
		
			
				|  |  | +		fmt.Printf("%d (%d) checking: %s\n", count, size, hashFile.Path)
 | 
	
		
			
				|  |  | +		//		fmt.Printf("checking: %s\n", hashFile.Path)
 | 
	
		
			
				|  |  | +		for filename, hash := range hashFile.Hashes {
 | 
	
		
			
				|  |  | +			if value, found := search(hash, filename, hashFile.Path); found {
 | 
	
		
			
				|  |  | +				w.WriteString("found identically hash\n")
 | 
	
		
			
				|  |  | +				w.WriteString(fmt.Sprintf("  src:  %s/%s\n", hashFile.Path, filename))
 | 
	
		
			
				|  |  | +				w.WriteString(fmt.Sprintf("  dest: %s\n", value))
 | 
	
		
			
				|  |  | +				w.Flush()
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +func search(srcHash string, exFilename string, exFilepath string) (value string, found bool) {
 | 
	
		
			
				|  |  | +	for _, hashFile := range hashes {
 | 
	
		
			
				|  |  | +		for filename, hash := range hashFile.Hashes {
 | 
	
		
			
				|  |  | +			if (filename != exFilename) && (hashFile.Path != exFilepath) {
 | 
	
		
			
				|  |  | +				if hash == srcHash {
 | 
	
		
			
				|  |  | +					value += fmt.Sprintf("%s/%s;", hashFile.Path, filename)
 | 
	
		
			
				|  |  | +					found = true
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +func loadAllHashFiles(folder string) {
 | 
	
		
			
				|  |  | +	count = 0
 | 
	
		
			
				|  |  | +	addWork = 0
 | 
	
		
			
				|  |  | +	err := filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
 | 
	
		
			
				|  |  | +		//filename := info.Name()
 | 
	
		
			
				|  |  | +		if info.IsDir() {
 | 
	
		
			
				|  |  | +			fmt.Print(".")
 | 
	
		
			
				|  |  | +			hashFile, ok := hashes[path]
 | 
	
		
			
				|  |  | +			if !ok {
 | 
	
		
			
				|  |  | +				_, err := os.Stat(path + "/.fdhashes3")
 | 
	
		
			
				|  |  | +				if os.IsNotExist(err) {
 | 
	
		
			
				|  |  | +					hashFile = Fdhashes{Path: path, Hashes: make(map[string]string), Times: make(map[string]time.Time), Dirty: true}
 | 
	
		
			
				|  |  | +				} else {
 | 
	
		
			
				|  |  | +					hashFile = loadHashfile(path + "/.fdhashes3")
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +				hashes[path] = hashFile
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		return nil
 | 
	
		
			
				|  |  | +	})
 | 
	
		
			
				|  |  | +	if err != nil {
 | 
	
		
			
				|  |  | +		panic(err)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	fmt.Printf("\nfound %d hash files.\n", len(hashes))
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +func check(e error) {
 | 
	
		
			
				|  |  | +	if e != nil {
 | 
	
		
			
				|  |  | +		panic(e)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +}
 |