|
@@ -13,6 +13,7 @@ import (
|
|
"path/filepath"
|
|
"path/filepath"
|
|
"runtime"
|
|
"runtime"
|
|
"sort"
|
|
"sort"
|
|
|
|
+ "strings"
|
|
"sync"
|
|
"sync"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
@@ -30,7 +31,7 @@ type Fdhashes struct {
|
|
}
|
|
}
|
|
|
|
|
|
var hashes map[string]Fdhashes
|
|
var hashes map[string]Fdhashes
|
|
-
|
|
|
|
|
|
+var ignoreLines []string
|
|
var mu sync.RWMutex
|
|
var mu sync.RWMutex
|
|
var driveLetter string
|
|
var driveLetter string
|
|
|
|
|
|
@@ -38,19 +39,37 @@ var rewrite bool
|
|
var prune bool
|
|
var prune bool
|
|
var outputJson bool
|
|
var outputJson bool
|
|
var report string
|
|
var report string
|
|
|
|
+var ignores string
|
|
|
|
|
|
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.StringVarP(&report, "equals", "e", "", "compare all file hashes and writing a equlatity report.")
|
|
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(&prune, "prune", "p", false, "checking all fdhashes files.")
|
|
flag.BoolVarP(&outputJson, "json", "j", false, "output as json.")
|
|
flag.BoolVarP(&outputJson, "json", "j", false, "output as json.")
|
|
|
|
+ flag.StringVarP(&ignores, "ignores", "i", "", "list of files to ignore in report.")
|
|
}
|
|
}
|
|
|
|
|
|
func main() {
|
|
func main() {
|
|
log.Println("starting GoHash")
|
|
log.Println("starting GoHash")
|
|
|
|
+ ignoreLines = make([]string, 0)
|
|
hashes = make(map[string]Fdhashes)
|
|
hashes = make(map[string]Fdhashes)
|
|
flag.Parse()
|
|
flag.Parse()
|
|
myFile := flag.Arg(0)
|
|
myFile := flag.Arg(0)
|
|
|
|
+ if rewrite {
|
|
|
|
+ log.Println("rewrite active")
|
|
|
|
+ }
|
|
|
|
+ if prune {
|
|
|
|
+ log.Println("prune active")
|
|
|
|
+ }
|
|
|
|
+ if outputJson {
|
|
|
|
+ log.Println("output json format active")
|
|
|
|
+ }
|
|
|
|
+ if report != "" {
|
|
|
|
+ log.Println("report active, file: ", report)
|
|
|
|
+ }
|
|
|
|
+ if ignores != "" {
|
|
|
|
+ log.Println("ignores file: ", ignores)
|
|
|
|
+ }
|
|
file, err := os.Stat(myFile)
|
|
file, err := os.Stat(myFile)
|
|
if os.IsNotExist(err) {
|
|
if os.IsNotExist(err) {
|
|
log.Fatalln("File does not exists:", myFile)
|
|
log.Fatalln("File does not exists:", myFile)
|
|
@@ -269,8 +288,8 @@ func loadHashfile(fileStr string) Fdhashes {
|
|
}
|
|
}
|
|
|
|
|
|
func compareFolder(folder string) {
|
|
func compareFolder(folder string) {
|
|
|
|
+ loadIgnoreFile(ignores)
|
|
loadAllHashFiles(folder)
|
|
loadAllHashFiles(folder)
|
|
-
|
|
|
|
// putting all hashes into one big map key = hash, value list of files with that hash
|
|
// putting all hashes into one big map key = hash, value list of files with that hash
|
|
size := len(hashes)
|
|
size := len(hashes)
|
|
index := make(map[string][]string)
|
|
index := make(map[string][]string)
|
|
@@ -286,10 +305,13 @@ func compareFolder(folder string) {
|
|
values = make([]string, 0)
|
|
values = make([]string, 0)
|
|
}
|
|
}
|
|
filepath := fmt.Sprintf("%s/%s", hashFile.Path, filename)
|
|
filepath := fmt.Sprintf("%s/%s", hashFile.Path, filename)
|
|
- _, err := os.Stat(filepath)
|
|
|
|
- if err == nil {
|
|
|
|
- values = append(values, filepath)
|
|
|
|
- index[hash] = values
|
|
|
|
|
|
+ pos := sort.SearchStrings(ignoreLines, filepath)
|
|
|
|
+ if pos == len(ignoreLines) {
|
|
|
|
+ _, err := os.Stat(filepath)
|
|
|
|
+ if err == nil {
|
|
|
|
+ values = append(values, filepath)
|
|
|
|
+ index[hash] = values
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -382,6 +404,14 @@ func compareFolder(folder string) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func loadIgnoreFile(filename string) {
|
|
|
|
+ content, err := ioutil.ReadFile(filename)
|
|
|
|
+ if err == nil {
|
|
|
|
+ ignoreLines = strings.Split(string(content), "\n")
|
|
|
|
+ sort.Strings(ignoreLines)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
func search(srcHash string, exFilename string, exFilepath string) (value string, found bool) {
|
|
func search(srcHash string, exFilename string, exFilepath string) (value string, found bool) {
|
|
for _, hashFile := range hashes {
|
|
for _, hashFile := range hashes {
|
|
for filename, hash := range hashFile.Hashes {
|
|
for filename, hash := range hashFile.Hashes {
|