瀏覽代碼

adding locks

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

+ 11 - 0
GoHash.go

@@ -76,6 +76,9 @@ func getSha256Hash(fileStr string) string {
 	return hex.EncodeToString(h.Sum(nil))
 }
 
+var lock1 = sync.RWMutex{}
+var lock2 = sync.RWMutex{}
+
 func outputHash(fileStr string) {
 	var hashFile Fdhashes
 	doHash := true
@@ -97,21 +100,29 @@ func outputHash(fileStr string) {
 		hashes[dir] = hashFile
 		saveHashfile(hashFile)
 	}
+	lock1.RLock()
 	_, ok = hashFile.Hashes[fileName]
+	lock1.RUnlock()
 	mu.Unlock()
 	doHash = !ok
 	// checking if dattime is identically
 	file, _ := os.Stat(fileStr)
 	time := file.ModTime()
+	lock2.RLock()
 	savedTime, ok := hashFile.Times[fileName]
+	lock2.RUnlock()
 	if !time.Equal(savedTime) || !ok {
 		doHash = true
 	}
 	if doHash {
 		hash := getSha256Hash(fileStr)
 		mu.Lock()
+		lock1.Lock()
 		hashFile.Hashes[fileName] = hash
+		lock1.Unlock()
+		lock2.Lock()
 		hashFile.Times[fileName] = time
+		lock2.Unlock()
 		hashes[dir] = hashFile
 		saveHashfile(hashFile)
 		mu.Unlock()