mirror of
https://github.com/JaCoB1123/dupe-finder.git
synced 2025-05-18 22:21:55 +02:00
Build dictionary and output as json
This commit is contained in:
parent
fdb70b74a6
commit
14b7835f72
61
main.go
61
main.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@ -19,35 +20,71 @@ func main() {
|
|||||||
fmt.Println(path)
|
fmt.Println(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
json, _ := json.MarshalIndent(filesMap.FilesBySize, "", " ")
|
||||||
|
fmt.Printf("\n\n\n%s\n\n\n", json)
|
||||||
}
|
}
|
||||||
|
|
||||||
type filesMap struct {
|
type filesMap struct {
|
||||||
Files map[int64]map[string]*fileEntry
|
FilesBySize map[int64]map[string][]*fileEntry
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fm *filesMap) Add(path string, info os.FileInfo) {
|
func (fm *filesMap) Add(path string, info os.FileInfo) error {
|
||||||
|
if info.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
fileInfo := &fileEntry{
|
fileInfo := &fileEntry{
|
||||||
Path: path,
|
Path: path,
|
||||||
Size: info.Size(),
|
Size: info.Size(),
|
||||||
}
|
}
|
||||||
|
|
||||||
existing := fm.Files[fileInfo.Size]
|
filesByHash := fm.FilesBySize[fileInfo.Size]
|
||||||
if existing == nil {
|
|
||||||
existing = map[string]*fileEntry{}
|
// first file with same size
|
||||||
fm.Files[fileInfo.Size] = existing
|
// => create new map for size
|
||||||
existing[""] = fileInfo
|
if filesByHash == nil {
|
||||||
return
|
filesByHash = map[string][]*fileEntry{}
|
||||||
|
fm.FilesBySize[fileInfo.Size] = filesByHash
|
||||||
|
filesByHash[""] = []*fileEntry{fileInfo}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Dupes: " + path)
|
// second file with same size
|
||||||
fmt.Println(existing[""])
|
// => calculate hashes for all entries
|
||||||
fm.Files[fileInfo.Size] = nil
|
if _, hasEmptyHash := filesByHash[""]; hasEmptyHash {
|
||||||
|
err := appendByFileHash(filesByHash, fileInfo)
|
||||||
|
err2 := appendByFileHash(filesByHash, filesByHash[""][0])
|
||||||
|
|
||||||
|
delete(filesByHash, "")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return err2
|
||||||
|
}
|
||||||
|
|
||||||
|
return appendByFileHash(filesByHash, fileInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
func appendByFileHash(filesByHash map[string][]*fileEntry, fileInfo *fileEntry) error {
|
||||||
|
hash, err := calculateHash(fileInfo.Path)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := filesByHash[hash]; ok {
|
||||||
|
filesByHash[hash] = append(filesByHash[hash], fileInfo)
|
||||||
|
} else {
|
||||||
|
filesByHash[hash] = []*fileEntry{fileInfo}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newFilesMap() filesMap {
|
func newFilesMap() filesMap {
|
||||||
return filesMap{
|
return filesMap{
|
||||||
Files: map[int64]map[string]*fileEntry{},
|
FilesBySize: map[int64]map[string][]*fileEntry{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user