Wilfried Klaas преди 5 години
родител
ревизия
87897fc29c
променени са 6 файла, в които са добавени 181 реда и са изтрити 0 реда
  1. 4 0
      .gitignore
  2. 19 0
      .vscode/launch.json
  3. 4 0
      .vscode/settings.json
  4. 16 0
      .vscode/tasks.json
  5. 135 0
      GoHash.go
  6. 3 0
      build.cmd

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+
+*.exe
+pkg
+src/github.com

+ 19 - 0
.vscode/launch.json

@@ -0,0 +1,19 @@
+{
+    // Use IntelliSense to learn about possible attributes.
+    // Hover to view descriptions of existing attributes.
+    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
+    "version": "0.2.0",
+    "configurations": [
+        {
+            "name": "Launch",
+            "type": "go",
+            "request": "launch",
+            "mode": "auto",
+            "program": "${fileDirname}",
+            "env": {},
+            "args": [
+                "e:\\temp"
+            ]
+        }
+    ]
+}

+ 4 - 0
.vscode/settings.json

@@ -0,0 +1,4 @@
+{
+    "go.inferGopath": true,
+    "go.gopath": "E:\\daten\\git-sourcen\\GoHash"
+}

+ 16 - 0
.vscode/tasks.json

@@ -0,0 +1,16 @@
+{
+    // See https://go.microsoft.com/fwlink/?LinkId=733558
+    // for the documentation about the tasks.json format
+    "version": "2.0.0",
+    "tasks": [
+        {
+            "label": "Build and run",
+            "type": "shell",
+            "command": "go build",
+            "group": {
+                "kind": "build",
+                "isDefault": true
+            }
+        }
+    ]
+}

+ 135 - 0
GoHash.go

@@ -0,0 +1,135 @@
+package main
+
+import (
+	"crypto/sha256"
+	"encoding/hex"
+	"encoding/json"
+	"flag"
+	"fmt"
+	"io"
+	"io/ioutil"
+	"log"
+	"os"
+	"path/filepath"
+	"runtime"
+	"sync"
+)
+
+type Fdhashes struct {
+	Path   string
+	Hashes map[string]string
+}
+
+var hashes map[string]Fdhashes
+
+var wg sync.WaitGroup
+var mu sync.RWMutex
+
+func main() {
+	runtime.GOMAXPROCS(5)
+	hashes = make(map[string]Fdhashes)
+	flag.Parse()
+	myFile := flag.Arg(0)
+	file, err := os.Stat(myFile)
+	if os.IsNotExist(err) {
+		log.Fatalln("File does not exists:", myFile)
+	}
+	if file.IsDir() {
+		log.Println("start with folder:", myFile)
+		processFolder(myFile)
+	} else {
+		log.Printf("file %s has hash %s\n", myFile, getHash(myFile))
+	}
+
+	fmt.Println("waiting")
+	wg.Wait()
+	log.Println("done")
+}
+
+func getHash(fileStr string) string {
+	f, err := os.Open(fileStr)
+	if err != nil {
+		log.Fatal(err)
+	}
+	defer f.Close()
+
+	h := sha256.New()
+	if _, err := io.Copy(h, f); err != nil {
+		log.Fatal(err)
+	}
+	return hex.EncodeToString(h.Sum(nil))
+}
+
+func outputHash(fileStr string) {
+	var hashFile Fdhashes
+	doHash := true
+	defer wg.Done()
+	fmt.Print(".")
+	dir, fileName := filepath.Split(fileStr)
+	if fileName == ".fdhashes3" {
+		return
+	}
+	mu.Lock()
+	hashFile, ok := hashes[dir]
+	if !ok {
+		_, err := os.Stat(dir + ".fdhashes3")
+		if os.IsNotExist(err) {
+			hashFile = Fdhashes{Path: dir, Hashes: make(map[string]string)}
+		} else {
+			hashFile = loadHashfile(dir + ".fdhashes3")
+		}
+		hashes[dir] = hashFile
+		saveHashfile(hashFile)
+	}
+
+	_, ok = hashFile.Hashes[fileName]
+	doHash = !ok
+	hashFile = hashes[dir]
+	mu.Unlock()
+	if doHash {
+		hash := getHash(fileStr)
+		mu.Lock()
+		hashFile.Hashes[fileName] = hash
+		saveHashfile(hashFile)
+		mu.Unlock()
+		log.Printf("file %s has hash %s\n", fileStr, hash)
+	}
+}
+
+func processFolder(folder string) {
+	err := filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
+		if !info.IsDir() {
+			wg.Add(1)
+			go outputHash(path)
+		}
+		return nil
+	})
+	if err != nil {
+		panic(err)
+	}
+}
+
+func saveHashfile(hashFile Fdhashes) {
+	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(filename string) Fdhashes {
+	file, err := ioutil.ReadFile(filename)
+	if err != nil {
+		panic(err)
+	}
+	data := Fdhashes{}
+	err = json.Unmarshal([]byte(file), &data)
+	if err != nil {
+		panic(err)
+	}
+	return data
+}

+ 3 - 0
build.cmd

@@ -0,0 +1,3 @@
+@echo off
+set GOPATH=%cd%
+go build