Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/the-maldridge/dumbsync

go 1.20

require github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
22 changes: 20 additions & 2 deletions pkg/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import (
"bytes"
"crypto/md5"
"errors"
"hash"
"io"
"io/fs"
"log"
"os"
"strings"
"sync"

"github.com/cespare/xxhash/v2"
)

// IndexPath walks the directory structure below basepath and
Expand All @@ -21,7 +25,15 @@ func (i *Indexer) IndexPath(basepath string) (*Index, error) {
i.basepath = basepath
i.idx = &Index{Files: make(map[string][]byte), Mutex: new(sync.Mutex)}
i.idx.fs = os.DirFS(i.basepath)
i.idx.HashType = MD5

switch strings.ToUpper(os.Getenv("DUMBSYNC_HASH")) {
case "MD5":
i.idx.HashType = MD5
case "XX":
i.idx.HashType = XXHash
default:
i.idx.HashType = MD5
}

if err := fs.WalkDir(i.idx.fs, ".", i.walkDir); err != nil {
return new(Index), err
Expand Down Expand Up @@ -55,7 +67,13 @@ func (i *Indexer) handleFile(path string, d fs.DirEntry) {
}
defer f.Close()

h := md5.New()
var h hash.Hash
switch i.idx.HashType {
case MD5:
h = md5.New()
case XXHash:
h = xxhash.New()
}
if _, err := io.Copy(h, f); err != nil {
return
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/index/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ const (
// MD5 is a basic hash type. Not great, not bad, just a basic
// hash type.
MD5 HashType = iota

// XXHash is a very fast non-crypto hash. It is great for
// detecting file differences but is not as resistant to
// tampering.
XXHash
)