Browse Source

new version with rewrite parameter and better performance and timestamp.

Klaas, Wilfried 5 years ago
parent
commit
92c3bf0748
3 changed files with 42 additions and 16 deletions
  1. 38 16
      GoHash.go
  2. 2 0
      go.mod
  3. 2 0
      go.sum

+ 38 - 16
GoHash.go

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

+ 2 - 0
go.mod

@@ -1,3 +1,5 @@
 module github.com/willie/gohash
 module github.com/willie/gohash
 
 
 go 1.12
 go 1.12
+
+require github.com/spf13/pflag v1.0.3

+ 2 - 0
go.sum

@@ -0,0 +1,2 @@
+github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
+github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=