|  | @@ -4,7 +4,6 @@ import (
 | 
	
		
			
				|  |  |  	"crypto/sha256"
 | 
	
		
			
				|  |  |  	"encoding/hex"
 | 
	
		
			
				|  |  |  	"encoding/json"
 | 
	
		
			
				|  |  | -	"flag"
 | 
	
		
			
				|  |  |  	"fmt"
 | 
	
		
			
				|  |  |  	"io"
 | 
	
		
			
				|  |  |  	"io/ioutil"
 | 
	
	
		
			
				|  | @@ -13,11 +12,16 @@ import (
 | 
	
		
			
				|  |  |  	"path/filepath"
 | 
	
		
			
				|  |  |  	"runtime"
 | 
	
		
			
				|  |  |  	"sync"
 | 
	
		
			
				|  |  | +	"time"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	flag "github.com/spf13/pflag"
 | 
	
		
			
				|  |  |  )
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  // Fdhashes struct for holding all informations about one folder.
 | 
	
		
			
				|  |  |  type Fdhashes struct {
 | 
	
		
			
				|  |  | +	Path   string
 | 
	
		
			
				|  |  |  	Hashes map[string]string
 | 
	
		
			
				|  |  | +	Times  map[string]time.Time
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  var hashes map[string]Fdhashes
 | 
	
	
		
			
				|  | @@ -29,7 +33,7 @@ var driveLetter string
 | 
	
		
			
				|  |  |  var rewrite bool
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  func init() {
 | 
	
		
			
				|  |  | -	flag.BoolVar(&rewrite, "rewrite", false, "rewrite all fhhashes files.")
 | 
	
		
			
				|  |  | +	flag.BoolVarP(&rewrite, "rewrite", "r", false, "rewrite all fhhashes files.")
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  func main() {
 | 
	
	
		
			
				|  | @@ -80,26 +84,34 @@ func outputHash(fileStr string) {
 | 
	
		
			
				|  |  |  	if fileName == ".fdhashes3" {
 | 
	
		
			
				|  |  |  		return
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  | +	// checking if hash is present
 | 
	
		
			
				|  |  |  	mu.Lock()
 | 
	
		
			
				|  |  |  	hashFile, ok := hashes[dir]
 | 
	
		
			
				|  |  |  	if !ok {
 | 
	
		
			
				|  |  |  		_, err := os.Stat(dir + ".fdhashes3")
 | 
	
		
			
				|  |  |  		if os.IsNotExist(err) {
 | 
	
		
			
				|  |  | -			hashFile = Fdhashes{Hashes: make(map[string]string)}
 | 
	
		
			
				|  |  | +			hashFile = Fdhashes{Path: dir, Hashes: make(map[string]string), Times: make(map[string]time.Time)}
 | 
	
		
			
				|  |  |  		} else {
 | 
	
		
			
				|  |  |  			hashFile = loadHashfile(dir + ".fdhashes3")
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  		hashes[dir] = hashFile
 | 
	
		
			
				|  |  | -		saveHashfile(hashFile, dir)
 | 
	
		
			
				|  |  | +		saveHashfile(hashFile)
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  	_, ok = hashFile.Hashes[fileName]
 | 
	
		
			
				|  |  | -	doHash = !ok
 | 
	
		
			
				|  |  |  	mu.Unlock()
 | 
	
		
			
				|  |  | +	doHash = !ok
 | 
	
		
			
				|  |  | +	// checking if dattime is identically
 | 
	
		
			
				|  |  | +	file, _ := os.Stat(fileStr)
 | 
	
		
			
				|  |  | +	time := file.ModTime()
 | 
	
		
			
				|  |  | +	savedTime, ok := hashFile.Times[fileName]
 | 
	
		
			
				|  |  | +	if !time.Equal(savedTime) || !ok {
 | 
	
		
			
				|  |  | +		doHash = true
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  |  	if doHash {
 | 
	
		
			
				|  |  |  		hash := getSha256Hash(fileStr)
 | 
	
		
			
				|  |  |  		mu.Lock()
 | 
	
		
			
				|  |  |  		hashFile.Hashes[fileName] = hash
 | 
	
		
			
				|  |  | +		hashFile.Times[fileName] = time
 | 
	
		
			
				|  |  |  		hashes[dir] = hashFile
 | 
	
		
			
				|  |  |  		saveHashfile(hashFile)
 | 
	
		
			
				|  |  |  		mu.Unlock()
 | 
	
	
		
			
				|  | @@ -108,9 +120,11 @@ func outputHash(fileStr string) {
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  var count int
 | 
	
		
			
				|  |  | +var addWork int
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  func processFolder(folder string) {
 | 
	
		
			
				|  |  |  	count = 0
 | 
	
		
			
				|  |  | +	addWork = 0
 | 
	
		
			
				|  |  |  	err := filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
 | 
	
		
			
				|  |  |  		count++
 | 
	
		
			
				|  |  |  		if (count % 100) == 0 {
 | 
	
	
		
			
				|  | @@ -125,8 +139,14 @@ func processFolder(folder string) {
 | 
	
		
			
				|  |  |  				fmt.Println(path)
 | 
	
		
			
				|  |  |  			}
 | 
	
		
			
				|  |  |  			if !info.IsDir() {
 | 
	
		
			
				|  |  | +				addWork++
 | 
	
		
			
				|  |  |  				wg.Add(1)
 | 
	
		
			
				|  |  |  				go outputHash(path)
 | 
	
		
			
				|  |  | +				if addWork > 100 {
 | 
	
		
			
				|  |  | +					fmt.Println("x")
 | 
	
		
			
				|  |  | +					wg.Wait()
 | 
	
		
			
				|  |  | +					addWork = 0
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  |  			}
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  		return nil
 | 
	
	
		
			
				|  | @@ -136,28 +156,30 @@ func processFolder(folder string) {
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -func saveHashfile(hashFile Fdhashes, dir string) {
 | 
	
		
			
				|  |  | +func saveHashfile(hashFile Fdhashes) {
 | 
	
		
			
				|  |  |  	b, err := json.Marshal(hashFile)
 | 
	
		
			
				|  |  |  	if err != nil {
 | 
	
		
			
				|  |  |  		fmt.Println(err)
 | 
	
		
			
				|  |  |  		return
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  | -	err = ioutil.WriteFile(dir+".fdhashes3", b, 0644)
 | 
	
		
			
				|  |  | +	err = ioutil.WriteFile(hashFile.Path+".fdhashes3", b, 0644)
 | 
	
		
			
				|  |  |  	if err != nil {
 | 
	
		
			
				|  |  |  		panic(err)
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  func loadHashfile(fileStr string) Fdhashes {
 | 
	
		
			
				|  |  | -	file, err := ioutil.ReadFile(fileStr)
 | 
	
		
			
				|  |  | -	if err != nil {
 | 
	
		
			
				|  |  | -		panic(err)
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  |  	dir, _ := filepath.Split(fileStr)
 | 
	
		
			
				|  |  | -	data := Fdhashes{Path: dir, Hashes: make(map[string]string)}
 | 
	
		
			
				|  |  | -	err = json.Unmarshal([]byte(file), &data)
 | 
	
		
			
				|  |  | -	if err != nil {
 | 
	
		
			
				|  |  | -		log.Printf("can't read file %s", fileStr)
 | 
	
		
			
				|  |  | +	data := Fdhashes{Path: dir, Hashes: make(map[string]string), Times: make(map[string]time.Time)}
 | 
	
		
			
				|  |  | +	if !rewrite {
 | 
	
		
			
				|  |  | +		file, err := ioutil.ReadFile(fileStr)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			panic(err)
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		err = json.Unmarshal([]byte(file), &data)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			log.Printf("can't read file %s", fileStr)
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  	if data.Path == "" {
 | 
	
		
			
				|  |  |  		data.Path = dir
 |