瀏覽代碼

change cleanup to json

Klaas, Wilfried 5 年之前
父節點
當前提交
e89593de7a
共有 1 個文件被更改,包括 64 次插入24 次删除
  1. 64 24
      GoHash.go

+ 64 - 24
GoHash.go

@@ -35,14 +35,14 @@ var driveLetter string
 
 var rewrite bool
 var prune bool
-var cleanup bool
+var outputJson bool
 var report string
 
 func init() {
 	flag.BoolVarP(&rewrite, "rewrite", "r", false, "rewrite all fhhashes files.")
 	flag.StringVarP(&report, "equals", "e", "", "compare all file hashes and writing a equlatity report.")
 	flag.BoolVarP(&prune, "prune", "p", false, "checking all fdhashes files.")
-	flag.BoolVarP(&cleanup, "clean", "c", false, "cleanup files.")
+	flag.BoolVarP(&outputJson, "json", "j", false, "output as json.")
 }
 
 func main() {
@@ -184,6 +184,7 @@ func processFolder(folder string) {
 	}
 }
 
+/* delete unused hash values from the hash file */
 func pruneHash(dir string) {
 	_, err := os.Stat(dir + "/.fdhashes3")
 	if !os.IsNotExist(err) {
@@ -282,33 +283,68 @@ func compareFolder(folder string) {
 		}
 	}
 
-	size := len(index)
-	f, err := os.Create(report)
-	check(err)
-	w := bufio.NewWriter(f)
-	count := 0
-	var filesize int64
-	fileCount := 0
-	for _, values := range index {
-		count++
-		if count%100 == 0 {
-			fmt.Printf("%d (%d) checking\n", count, size)
+	if outputJson {
+		size := len(index)
+		var filesize int64
+		fileCount := 0
+		for hash, values := range index {
+			count++
+			if count%100 == 0 {
+				fmt.Printf("%d (%d) checking\n", count, size)
+			}
+			if len(values) > 1 {
+				info, err := os.Stat(values[0])
+				if err == nil {
+					fmt.Printf("found identically hash: %s size: %d\n", hash, info.Size())
+					filesize += int64(len(values)-1) * info.Size()
+				}
+				fileCount += len(values) - 1
+				for _, filename := range values {
+					fmt.Printf("  %s\n", filename)
+				}
+			} else {
+				delete(index, hash)
+			}
 		}
-		if len(values) > 1 {
-			info, err := os.Stat(values[0])
-			if err == nil {
-				w.WriteString(fmt.Sprintf("found identically hash: size: %d\n", info.Size()))
-				filesize += int64(len(values)-1) * info.Size()
+
+		b, err := json.Marshal(index)
+		if err != nil {
+			fmt.Println(err)
+			return
+		}
+		err = ioutil.WriteFile(report, b, 0644)
+		if err != nil {
+			panic(err)
+		}
+	} else {
+		size := len(index)
+		f, err := os.Create(report)
+		check(err)
+		w := bufio.NewWriter(f)
+		count := 0
+		var filesize int64
+		fileCount := 0
+		for _, values := range index {
+			count++
+			if count%100 == 0 {
+				fmt.Printf("%d (%d) checking\n", count, size)
 			}
-			fileCount += len(values) - 1
-			for _, filename := range values {
-				w.WriteString(fmt.Sprintf("  %s\n", filename))
+			if len(values) > 1 {
+				info, err := os.Stat(values[0])
+				if err == nil {
+					w.WriteString(fmt.Sprintf("found identically hash: size: %d\n", info.Size()))
+					filesize += int64(len(values)-1) * info.Size()
+				}
+				fileCount += len(values) - 1
+				for _, filename := range values {
+					w.WriteString(fmt.Sprintf("  %s\n", filename))
+				}
+				w.Flush()
 			}
-			w.Flush()
 		}
+		w.WriteString(fmt.Sprintf("can save up to %s on %d files\n", bytefmt.ByteSize(uint64(filesize)), fileCount))
+		w.Flush()
 	}
-	w.WriteString(fmt.Sprintf("can save up to %s on %d files\n", bytefmt.ByteSize(uint64(filesize)), fileCount))
-	w.Flush()
 }
 
 func search(srcHash string, exFilename string, exFilepath string) (value string, found bool) {
@@ -331,7 +367,11 @@ func loadAllHashFiles(folder string) {
 	err := filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
 		if info != nil {
 			if info.IsDir() {
+				count++
 				fmt.Print(".")
+				if (count % 100) == 0 {
+					fmt.Println()
+				}
 				hashFile, ok := hashes[path]
 				if !ok {
 					_, err := os.Stat(path + "/.fdhashes3")