-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch to using blake3 hashes and skipping empty blocks #29
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,18 @@ import ( | |
"time" | ||
|
||
"github.com/go-logr/logr" | ||
"golang.org/x/crypto/blake2b" | ||
"github.com/zeebo/blake3" | ||
) | ||
|
||
const ( | ||
DefaultBlockSize = int64(64 * 1024) | ||
defaultConcurrency = 25 | ||
blake3HashLength = 32 | ||
) | ||
|
||
var ( | ||
emptyBlock []byte | ||
zeroHash []byte | ||
) | ||
|
||
type Hasher interface { | ||
|
@@ -30,6 +36,7 @@ type Hasher interface { | |
SerializeHashes(io.Writer) error | ||
DeserializeHashes(io.Reader) (int64, map[int64][]byte, error) | ||
BlockSize() int64 | ||
CompareHashOfHash(io.ReadWriter) (bool, error) | ||
} | ||
|
||
type OffsetHash struct { | ||
|
@@ -44,9 +51,13 @@ type FileHasher struct { | |
blockSize int64 | ||
fileSize int64 | ||
log logr.Logger | ||
// Hash of hashes | ||
hashHash []byte | ||
} | ||
|
||
func NewFileHasher(blockSize int64, log logr.Logger) Hasher { | ||
emptyBlock = make([]byte, blockSize) | ||
zeroHash = computeZeroHash() | ||
return &FileHasher{ | ||
blockSize: blockSize, | ||
queue: make(chan int64, defaultConcurrency), | ||
|
@@ -56,10 +67,17 @@ func NewFileHasher(blockSize int64, log logr.Logger) Hasher { | |
} | ||
} | ||
|
||
func computeZeroHash() []byte { | ||
h := blake3.New() | ||
h.Write(emptyBlock) | ||
return h.Sum(nil) | ||
} | ||
|
||
func (f *FileHasher) HashFile(fileName string) (int64, error) { | ||
f.log.V(3).Info("Hashing file", "file", fileName) | ||
t := time.Now() | ||
defer func() { | ||
f.hashHash = f.calculateHashHash() | ||
f.log.V(3).Info("Hashing took", "milliseconds", time.Since(t).Milliseconds()) | ||
}() | ||
done := make(chan struct{}) | ||
|
@@ -79,10 +97,7 @@ func (f *FileHasher) HashFile(fileName string) (int64, error) { | |
|
||
for i := 0; i < count; i++ { | ||
wg.Add(1) | ||
h, err := blake2b.New512(nil) | ||
if err != nil { | ||
return 0, err | ||
} | ||
h := blake3.New() | ||
go func(h hash.Hash) { | ||
defer wg.Done() | ||
osFile, err := os.Open(fileName) | ||
|
@@ -110,6 +125,14 @@ func (f *FileHasher) HashFile(fileName string) (int64, error) { | |
} | ||
} | ||
|
||
func (f *FileHasher) calculateHashHash() []byte { | ||
h := blake3.New() | ||
for _, v := range f.hashes { | ||
h.Write(v) | ||
} | ||
return h.Sum(nil) | ||
} | ||
|
||
func (f *FileHasher) getFileSize(fileName string) (int64, error) { | ||
file, err := os.Open(fileName) | ||
if err != nil { | ||
|
@@ -153,17 +176,23 @@ func (f *FileHasher) calculateHash(offset int64, rs io.ReadSeeker, h hash.Hash) | |
f.log.V(5).Info("Failed to read") | ||
return err | ||
} | ||
n, err = h.Write(buf[:n]) | ||
if err != nil { | ||
f.log.V(5).Info("Failed to write to hash") | ||
return err | ||
} | ||
if n != len(buf) { | ||
f.log.V(5).Info("Finished reading file") | ||
var hash []byte | ||
if bytes.Equal(buf, emptyBlock) { | ||
hash = zeroHash | ||
} else { | ||
n, err = h.Write(buf[:n]) | ||
if err != nil { | ||
f.log.V(5).Info("Failed to write to hash") | ||
return err | ||
} | ||
if n != len(buf) { | ||
f.log.V(5).Info("Finished reading file") | ||
} | ||
hash = h.Sum(nil) | ||
} | ||
offsetHash := OffsetHash{ | ||
Offset: offset, | ||
Hash: h.Sum(nil), | ||
Hash: hash, | ||
} | ||
f.res <- offsetHash | ||
return nil | ||
|
@@ -210,6 +239,7 @@ func (f *FileHasher) SerializeHashes(w io.Writer) error { | |
if err := binary.Write(w, binary.LittleEndian, int64(f.blockSize)); err != nil { | ||
return err | ||
} | ||
|
||
length := len(f.hashes) | ||
f.log.V(5).Info("Number of blocks", "size", length) | ||
if err := binary.Write(w, binary.LittleEndian, int64(length)); err != nil { | ||
|
@@ -225,8 +255,8 @@ func (f *FileHasher) SerializeHashes(w io.Writer) error { | |
if err := binary.Write(w, binary.LittleEndian, k); err != nil { | ||
return err | ||
} | ||
if len(f.hashes[k]) != 64 { | ||
return errors.New("invalid hash length") | ||
if len(f.hashes[k]) != blake3HashLength { | ||
return fmt.Errorf("invalid hash length %d", len(f.hashes[k])) | ||
} | ||
if n, err := w.Write(f.hashes[k]); err != nil { | ||
return err | ||
|
@@ -248,6 +278,7 @@ func (f *FileHasher) DeserializeHashes(r io.Reader) (int64, map[int64][]byte, er | |
if err := binary.Read(r, binary.LittleEndian, &blockSize); err != nil { | ||
return 0, nil, err | ||
} | ||
f.log.V(5).Info("Block size", "size", blockSize) | ||
var length int64 | ||
if err := binary.Read(r, binary.LittleEndian, &length); err != nil { | ||
return 0, nil, err | ||
|
@@ -263,7 +294,7 @@ func (f *FileHasher) DeserializeHashes(r io.Reader) (int64, map[int64][]byte, er | |
if offset < 0 || offset > length*blockSize { | ||
return 0, nil, fmt.Errorf("invalid offset %d", offset) | ||
} | ||
hash := make([]byte, 64) | ||
hash := make([]byte, blake3HashLength) | ||
if n, err := io.ReadFull(r, hash); err != nil { | ||
return 0, nil, err | ||
} else { | ||
|
@@ -275,6 +306,22 @@ func (f *FileHasher) DeserializeHashes(r io.Reader) (int64, map[int64][]byte, er | |
return blockSize, hashes, nil | ||
} | ||
|
||
func (f *FileHasher) CompareHashOfHash(rw io.ReadWriter) (bool, error) { | ||
f.log.V(5).Info("Comparing hash of hashes", "hash", base64.StdEncoding.EncodeToString(f.hashHash)) | ||
if n, err := rw.Write(f.hashHash); err != nil { | ||
return false, err | ||
} else { | ||
f.log.V(5).Info("Wrote hash of hashes", "bytes", n) | ||
} | ||
hashHash := make([]byte, blake3HashLength) | ||
if n, err := io.ReadFull(rw, hashHash); err != nil { | ||
return false, err | ||
} else { | ||
f.log.V(5).Info("Read hash of hashes", "bytes", n, "hash", base64.StdEncoding.EncodeToString(hashHash)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same reason the else exists. |
||
} | ||
return bytes.Equal(hashHash, f.hashHash), nil | ||
} | ||
|
||
func (f *FileHasher) BlockSize() int64 { | ||
return f.blockSize | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: no need for an
else
statement hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I do need the else here, because I am referencing
n
which only exists in the scope of the if statement.