|
@@ -11,7 +11,6 @@ import (
|
|
"log"
|
|
"log"
|
|
"os"
|
|
"os"
|
|
"path/filepath"
|
|
"path/filepath"
|
|
- "runtime"
|
|
|
|
"sort"
|
|
"sort"
|
|
"strings"
|
|
"strings"
|
|
"sync"
|
|
"sync"
|
|
@@ -20,20 +19,12 @@ import (
|
|
"code.cloudfoundry.org/bytefmt"
|
|
"code.cloudfoundry.org/bytefmt"
|
|
|
|
|
|
flag "github.com/spf13/pflag"
|
|
flag "github.com/spf13/pflag"
|
|
|
|
+ "wkla.no-ip.biz/gogs/Willie/GoHash/pkg/hash"
|
|
)
|
|
)
|
|
|
|
|
|
-// Fdhashes struct for holding all informations about one folder.
|
|
|
|
-type Fdhashes struct {
|
|
|
|
- Path string
|
|
|
|
- Hashes map[string]string
|
|
|
|
- Times map[string]time.Time
|
|
|
|
- Dirty bool
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-var hashes map[string]Fdhashes
|
|
|
|
|
|
+var hashes map[string]hash.Fdhashes
|
|
var ignoreLines []string
|
|
var ignoreLines []string
|
|
var mu sync.RWMutex
|
|
var mu sync.RWMutex
|
|
-var driveLetter string
|
|
|
|
|
|
|
|
var rewrite bool
|
|
var rewrite bool
|
|
var prune bool
|
|
var prune bool
|
|
@@ -52,7 +43,7 @@ func init() {
|
|
func main() {
|
|
func main() {
|
|
log.Println("starting GoHash")
|
|
log.Println("starting GoHash")
|
|
ignoreLines = make([]string, 0)
|
|
ignoreLines = make([]string, 0)
|
|
- hashes = make(map[string]Fdhashes)
|
|
|
|
|
|
+ hashes = make(map[string]hash.Fdhashes)
|
|
flag.Parse()
|
|
flag.Parse()
|
|
myFile := flag.Arg(0)
|
|
myFile := flag.Arg(0)
|
|
if rewrite {
|
|
if rewrite {
|
|
@@ -76,10 +67,6 @@ func main() {
|
|
}
|
|
}
|
|
if file.IsDir() {
|
|
if file.IsDir() {
|
|
log.Println("start with folder:", myFile)
|
|
log.Println("start with folder:", myFile)
|
|
- driveLetter = ""
|
|
|
|
- if runtime.GOOS == "windows" {
|
|
|
|
- driveLetter = filepath.VolumeName(myFile) + "/"
|
|
|
|
- }
|
|
|
|
|
|
|
|
if report != "" {
|
|
if report != "" {
|
|
compareFolder(myFile)
|
|
compareFolder(myFile)
|
|
@@ -112,7 +99,7 @@ var lock1 = sync.RWMutex{}
|
|
var lock2 = sync.RWMutex{}
|
|
var lock2 = sync.RWMutex{}
|
|
|
|
|
|
func calculateHash(fileStr string) {
|
|
func calculateHash(fileStr string) {
|
|
- var hashFile Fdhashes
|
|
|
|
|
|
+ var hashFile hash.Fdhashes
|
|
doHash := true
|
|
doHash := true
|
|
dir, fileName := filepath.Split(fileStr)
|
|
dir, fileName := filepath.Split(fileStr)
|
|
if fileName == ".fdhashes3" {
|
|
if fileName == ".fdhashes3" {
|
|
@@ -123,10 +110,12 @@ func calculateHash(fileStr string) {
|
|
hashFile, ok := hashes[dir]
|
|
hashFile, ok := hashes[dir]
|
|
if !ok {
|
|
if !ok {
|
|
_, err := os.Stat(dir + ".fdhashes3")
|
|
_, err := os.Stat(dir + ".fdhashes3")
|
|
- if os.IsNotExist(err) {
|
|
|
|
- hashFile = Fdhashes{Path: dir, Hashes: make(map[string]string), Times: make(map[string]time.Time), Dirty: true}
|
|
|
|
|
|
+ if os.IsNotExist(err) || rewrite {
|
|
|
|
+ hashFile = hash.Fdhashes{Path: dir, Hashes: make(map[string]string), Times: make(map[string]time.Time), Dirty: true}
|
|
} else {
|
|
} else {
|
|
- hashFile = loadHashfile(dir + ".fdhashes3")
|
|
|
|
|
|
+ hf, err := hash.LoadHashfile(dir + ".fdhashes3")
|
|
|
|
+ check(err)
|
|
|
|
+ hashFile = *hf
|
|
}
|
|
}
|
|
hashes[dir] = hashFile
|
|
hashes[dir] = hashFile
|
|
}
|
|
}
|
|
@@ -208,7 +197,8 @@ func processFolder(folder string) {
|
|
func pruneHash(dir string) {
|
|
func pruneHash(dir string) {
|
|
_, err := os.Stat(dir + "/.fdhashes3")
|
|
_, err := os.Stat(dir + "/.fdhashes3")
|
|
if !os.IsNotExist(err) {
|
|
if !os.IsNotExist(err) {
|
|
- hashFile := loadHashfile(dir + "/.fdhashes3")
|
|
|
|
|
|
+ hashFile, err := hash.LoadHashfile(dir + "/.fdhashes3")
|
|
|
|
+ check(err)
|
|
for filename := range hashFile.Hashes {
|
|
for filename := range hashFile.Hashes {
|
|
_, err := os.Stat(dir + "/" + filename)
|
|
_, err := os.Stat(dir + "/" + filename)
|
|
if os.IsNotExist(err) {
|
|
if os.IsNotExist(err) {
|
|
@@ -226,16 +216,16 @@ func pruneHash(dir string) {
|
|
hashFile.Dirty = true
|
|
hashFile.Dirty = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- saveHashfile(&hashFile)
|
|
|
|
|
|
+ saveHashfile(hashFile)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func dirtyHashfile(hashFile *Fdhashes) {
|
|
|
|
|
|
+func dirtyHashfile(hashFile *hash.Fdhashes) {
|
|
hashFile.Dirty = true
|
|
hashFile.Dirty = true
|
|
}
|
|
}
|
|
|
|
|
|
func saveAllHashFiles() {
|
|
func saveAllHashFiles() {
|
|
- hashList := make([]Fdhashes, 0)
|
|
|
|
|
|
+ hashList := make([]hash.Fdhashes, 0)
|
|
|
|
|
|
for _, hashFile := range hashes {
|
|
for _, hashFile := range hashes {
|
|
if hashFile.Dirty {
|
|
if hashFile.Dirty {
|
|
@@ -244,14 +234,14 @@ func saveAllHashFiles() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- hashes = make(map[string]Fdhashes)
|
|
|
|
|
|
+ hashes = make(map[string]hash.Fdhashes)
|
|
for _, hashFile := range hashList {
|
|
for _, hashFile := range hashList {
|
|
hashes[hashFile.Path] = hashFile
|
|
hashes[hashFile.Path] = hashFile
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-func saveHashfile(hashFile *Fdhashes) {
|
|
|
|
|
|
+func saveHashfile(hashFile *hash.Fdhashes) {
|
|
if hashFile.Dirty {
|
|
if hashFile.Dirty {
|
|
hashFile.Dirty = false
|
|
hashFile.Dirty = false
|
|
b, err := json.Marshal(hashFile)
|
|
b, err := json.Marshal(hashFile)
|
|
@@ -266,27 +256,6 @@ func saveHashfile(hashFile *Fdhashes) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-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)
|
|
|
|
- if err != nil {
|
|
|
|
- panic(err)
|
|
|
|
- }
|
|
|
|
- err = json.Unmarshal([]byte(file), &data)
|
|
|
|
- if err != nil {
|
|
|
|
- log.Printf("can't read file %s", fileStr)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if data.Path != dir {
|
|
|
|
- data.Path = dir
|
|
|
|
- data.Dirty = true
|
|
|
|
- }
|
|
|
|
- return data
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func compareFolder(folder string) {
|
|
func compareFolder(folder string) {
|
|
loadIgnoreFile(ignores)
|
|
loadIgnoreFile(ignores)
|
|
loadAllHashFiles(folder)
|
|
loadAllHashFiles(folder)
|
|
@@ -441,9 +410,11 @@ func loadAllHashFiles(folder string) {
|
|
if !ok {
|
|
if !ok {
|
|
_, err := os.Stat(path + "/.fdhashes3")
|
|
_, err := os.Stat(path + "/.fdhashes3")
|
|
if os.IsNotExist(err) {
|
|
if os.IsNotExist(err) {
|
|
- hashFile = Fdhashes{Path: path, Hashes: make(map[string]string), Times: make(map[string]time.Time), Dirty: true}
|
|
|
|
|
|
+ hashFile = hash.Fdhashes{Path: path, Hashes: make(map[string]string), Times: make(map[string]time.Time), Dirty: true}
|
|
} else {
|
|
} else {
|
|
- hashFile = loadHashfile(path + "/.fdhashes3")
|
|
|
|
|
|
+ hf, err := hash.LoadHashfile(path + "/.fdhashes3")
|
|
|
|
+ check(err)
|
|
|
|
+ hashFile = *hf
|
|
}
|
|
}
|
|
hashes[path] = hashFile
|
|
hashes[path] = hashFile
|
|
}
|
|
}
|
|
@@ -451,9 +422,7 @@ func loadAllHashFiles(folder string) {
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
})
|
|
})
|
|
- if err != nil {
|
|
|
|
- panic(err)
|
|
|
|
- }
|
|
|
|
|
|
+ check(err)
|
|
fmt.Printf("\nfound %d hash files.\n", len(hashes))
|
|
fmt.Printf("\nfound %d hash files.\n", len(hashes))
|
|
}
|
|
}
|
|
|
|
|