Browse Source

first report version

Klaas, Wilfried 5 years ago
parent
commit
b6b46a723d
2 changed files with 80 additions and 5 deletions
  1. 1 1
      .vscode/launch.json
  2. 79 4
      GoHash.go

+ 1 - 1
.vscode/launch.json

@@ -11,7 +11,7 @@
             "mode": "auto",
             "mode": "auto",
             "program": "${fileDirname}",
             "program": "${fileDirname}",
             "env": {},
             "env": {},
-            "args": ["F:\\noShare\\pics\\tumblr\\data"]
+            "args": ["-c", "F:\\noShare\\pics\\tumblr\\data"]
         }
         }
     ]
     ]
 }
 }

+ 79 - 4
GoHash.go

@@ -1,6 +1,7 @@
 package main
 package main
 
 
 import (
 import (
+	"bufio"
 	"crypto/sha256"
 	"crypto/sha256"
 	"encoding/hex"
 	"encoding/hex"
 	"encoding/json"
 	"encoding/json"
@@ -32,9 +33,11 @@ var mu sync.RWMutex
 var driveLetter string
 var driveLetter string
 
 
 var rewrite bool
 var rewrite bool
+var compare bool
 
 
 func init() {
 func init() {
 	flag.BoolVarP(&rewrite, "rewrite", "r", false, "rewrite all fhhashes files.")
 	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() {
 func main() {
@@ -53,14 +56,18 @@ func main() {
 		if runtime.GOOS == "windows" {
 		if runtime.GOOS == "windows" {
 			driveLetter = filepath.VolumeName(myFile) + "/"
 			driveLetter = filepath.VolumeName(myFile) + "/"
 		}
 		}
-		processFolder(myFile)
+		if compare {
+			compareFolder(myFile)
+		} else {
+			processFolder(myFile)
+			fmt.Println("waiting")
+			wg.Wait()
+			saveAllHashFiles()
+		}
 	} else {
 	} else {
 		log.Printf("file %s has hash %s\n", myFile, getSha256Hash(myFile))
 		log.Printf("file %s has hash %s\n", myFile, getSha256Hash(myFile))
 	}
 	}
 
 
-	fmt.Println("waiting")
-	wg.Wait()
-	saveAllHashFiles()
 	log.Println("done")
 	log.Println("done")
 }
 }
 
 
@@ -219,3 +226,71 @@ func loadHashfile(fileStr string) Fdhashes {
 	}
 	}
 	return data
 	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)
+	}
+}