Browse Source

adding new feature

Klaas, Wilfried 5 years ago
parent
commit
91b20b8e4b
3 changed files with 75 additions and 40 deletions
  1. 1 1
      .vscode/launch.json
  2. 72 37
      GoHash.go
  3. 2 2
      build.cmd

+ 1 - 1
.vscode/launch.json

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

+ 72 - 37
GoHash.go

@@ -30,21 +30,23 @@ type Fdhashes struct {
 
 var hashes map[string]Fdhashes
 
-var wg sync.WaitGroup
 var mu sync.RWMutex
 var driveLetter string
 
 var rewrite bool
-var compare bool
+var prune bool
+var cleanup bool
+var report string
 
 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.")
+	flag.StringVarP(&report, "equals", "e", "", "compare all file hashes and writing a equlatity report.")
+	flag.BoolVarP(&prune, "prune", "p", false, "checking all fhhashes files.")
+	flag.BoolVarP(&cleanup, "clean", "c", false, "cleanup files.")
 }
 
 func main() {
 	log.Println("starting GoHash")
-	runtime.GOMAXPROCS(2)
 	hashes = make(map[string]Fdhashes)
 	flag.Parse()
 	myFile := flag.Arg(0)
@@ -58,12 +60,10 @@ func main() {
 		if runtime.GOOS == "windows" {
 			driveLetter = filepath.VolumeName(myFile) + "/"
 		}
-		if compare {
+		if report != "" {
 			compareFolder(myFile)
 		} else {
 			processFolder(myFile)
-			fmt.Println("waiting")
-			wg.Wait()
 			saveAllHashFiles()
 		}
 	} else {
@@ -93,7 +93,6 @@ var lock2 = sync.RWMutex{}
 func outputHash(fileStr string) {
 	var hashFile Fdhashes
 	doHash := true
-	defer wg.Done()
 	dir, fileName := filepath.Split(fileStr)
 	if fileName == ".fdhashes3" {
 		return
@@ -136,7 +135,7 @@ func outputHash(fileStr string) {
 		lock2.Lock()
 		hashFile.Times[fileName] = time
 		lock2.Unlock()
-		saveHashfile(&hashFile)
+		dirtyHashfile(&hashFile)
 		hashes[dir] = hashFile
 		mu.Unlock()
 		log.Printf("file \"%s\" has hash \"%s\"\n", fileStr, hash)
@@ -145,8 +144,10 @@ func outputHash(fileStr string) {
 
 var count int
 var addWork int
+var startTime time.Time
 
 func processFolder(folder string) {
+	startTime = time.Now()
 	count = 0
 	addWork = 0
 	err := filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
@@ -161,14 +162,15 @@ func processFolder(folder string) {
 		if filename[0:1] != "." {
 			if info.IsDir() {
 				fmt.Println(path)
+				if prune {
+					pruneHash(path)
+				}
 			}
 			if !info.IsDir() {
 				addWork++
-				wg.Add(1)
-				go outputHash(path)
-				if addWork > 1000 {
-					fmt.Println("x")
-					wg.Wait()
+				outputHash(path)
+				if time.Since(startTime).Seconds() > 10.0 {
+					startTime = time.Now()
 					saveAllHashFiles()
 					addWork = 0
 				}
@@ -181,7 +183,32 @@ func processFolder(folder string) {
 	}
 }
 
-func saveHashfile(hashFile *Fdhashes) {
+func pruneHash(dir string) {
+	_, err := os.Stat(dir + "/.fdhashes3")
+	if !os.IsNotExist(err) {
+		hashFile := loadHashfile(dir + "/.fdhashes3")
+		for filename := range hashFile.Hashes {
+			_, err := os.Stat(dir + "/" + filename)
+			if os.IsNotExist(err) {
+				delete(hashFile.Hashes, filename)
+				delete(hashFile.Times, filename)
+				hashFile.Dirty = true
+			}
+		}
+
+		for filename := range hashFile.Times {
+			_, err := os.Stat(dir + "/" + filename)
+			if os.IsNotExist(err) {
+				delete(hashFile.Hashes, filename)
+				delete(hashFile.Times, filename)
+				hashFile.Dirty = true
+			}
+		}
+		saveHashfile(&hashFile)
+	}
+}
+
+func dirtyHashfile(hashFile *Fdhashes) {
 	hashFile.Dirty = true
 }
 
@@ -190,16 +217,7 @@ func saveAllHashFiles() {
 
 	for _, hashFile := range hashes {
 		if hashFile.Dirty {
-			hashFile.Dirty = false
-			b, err := json.Marshal(hashFile)
-			if err != nil {
-				fmt.Println(err)
-				return
-			}
-			err = ioutil.WriteFile(hashFile.Path+".fdhashes3", b, 0644)
-			if err != nil {
-				panic(err)
-			}
+			saveHashfile(&hashFile)
 			hashList = append(hashList, hashFile)
 		}
 	}
@@ -211,8 +229,24 @@ func saveAllHashFiles() {
 
 }
 
+func saveHashfile(hashFile *Fdhashes) {
+	if hashFile.Dirty {
+		hashFile.Dirty = false
+		b, err := json.Marshal(hashFile)
+		if err != nil {
+			fmt.Println(err)
+			return
+		}
+		err = ioutil.WriteFile(hashFile.Path+"/.fdhashes3", b, 0644)
+		if err != nil {
+			panic(err)
+		}
+	}
+}
+
 func loadHashfile(fileStr string) Fdhashes {
 	dir, _ := filepath.Split(fileStr)
+	dir = filepath.ToSlash(filepath.Clean(dir))
 	data := Fdhashes{Path: dir, Hashes: make(map[string]string), Times: make(map[string]time.Time), Dirty: false}
 	if !rewrite {
 		file, err := ioutil.ReadFile(fileStr)
@@ -248,7 +282,7 @@ func compareFolder(folder string) {
 	}
 
 	size := len(index)
-	f, err := os.Create("report.txt")
+	f, err := os.Create(report)
 	check(err)
 	w := bufio.NewWriter(f)
 	count := 0
@@ -316,18 +350,19 @@ 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")
+		if info != nil {
+			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
 				}
-				hashes[path] = hashFile
 			}
 		}
 		return nil

+ 2 - 2
build.cmd

@@ -1,3 +1,3 @@
-@echo off
 go build -ldflags="-s -w" 
-copy GoHash.exe F:\
+copy GoHash.exe F:\
+pause