From 6c10528cbd0d7e85f590bb1021e832038129c249 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 5 Jul 2023 22:27:16 +0530 Subject: [PATCH] Make the hasher var private in the rsync API --- tools/rsync/algorithm.go | 23 ++++++++++++++++------- tools/rsync/api.go | 15 ++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/tools/rsync/algorithm.go b/tools/rsync/algorithm.go index 896b7933b..59a47f75d 100644 --- a/tools/rsync/algorithm.go +++ b/tools/rsync/algorithm.go @@ -156,9 +156,14 @@ type RSync struct { BlockSize int // This must be non-nil before using any functions - UniqueHasher hash.Hash + hasher hash.Hash + hasher_constructor func() hash.Hash + buffer []byte +} - buffer []byte +func (r *RSync) SetHasher(c func() hash.Hash) { + r.hasher_constructor = c + r.hasher = c() } // If the target length is known the number of hashes in the @@ -525,8 +530,8 @@ func (r *RSync) CreateDiff(source io.Reader, signature []BlockHash) func() (*Ope ans := &diff{ block_size: r.BlockSize, buffer: make([]byte, 0, (r.BlockSize * 8)), hash_lookup: make(map[uint32][]BlockHash, len(signature)), - source: source, hasher: r.UniqueHasher, - hash_buf: make([]byte, 0, r.UniqueHasher.Size()), + source: source, hasher: r.hasher_constructor(), + hash_buf: make([]byte, 0, r.hasher.Size()), } for _, h := range signature { key := h.WeakHash @@ -552,11 +557,15 @@ func (r *RSync) CreateDelta(source io.Reader, signature []BlockHash, ops Operati // Use a more unique way to identify a set of bytes. func (r *RSync) hash(v []byte) []byte { - r.UniqueHasher.Reset() - r.UniqueHasher.Write(v) - return r.UniqueHasher.Sum(nil) + r.hasher.Reset() + r.hasher.Write(v) + return r.hasher.Sum(nil) } +func (r *RSync) HashSize() int { return r.hasher.Size() } +func (r *RSync) HashBlockSize() int { return r.hasher.BlockSize() } +func (r *RSync) HasHasher() bool { return r.hasher != nil } + // Searches for a given strong hash among all strong hashes in this bucket. func find_hash(hh []BlockHash, hv []byte) (uint64, bool) { if len(hv) == 0 { diff --git a/tools/rsync/api.go b/tools/rsync/api.go index e82ab8e12..c2da62575 100644 --- a/tools/rsync/api.go +++ b/tools/rsync/api.go @@ -4,6 +4,7 @@ package rsync import ( "fmt" + "hash" "io" "math" @@ -59,7 +60,7 @@ func (self *Api) read_signature_header(data []byte) (consumed int, err error) { switch strong_hash := StrongHashType(bin.Uint16(data[4:])); strong_hash { case XXH3: self.Strong_hash_type = strong_hash - self.rsync.UniqueHasher = xxh3.New() + self.rsync.SetHasher(func() hash.Hash { return xxh3.New() }) default: return consumed, fmt.Errorf("Invalid strong_hash in signature header: %d", strong_hash) } @@ -83,7 +84,7 @@ func (self *Api) read_signature_header(data []byte) (consumed int, err error) { } func (self *Api) read_signature_blocks(data []byte) (consumed int) { - hash_size := self.rsync.UniqueHasher.Size() + hash_size := self.rsync.HashSize() block_hash_size := hash_size + 12 for ; len(data) >= block_hash_size; data = data[block_hash_size:] { bl := BlockHash{} @@ -99,7 +100,7 @@ func (self *Differ) finish_signature_data() (err error) { return fmt.Errorf("There were %d leftover bytes in the signature data", len(self.unconsumed_signature_data)) } self.unconsumed_signature_data = nil - if self.rsync.UniqueHasher == nil { + if !self.rsync.HasHasher() { return fmt.Errorf("No header was found in the signature data") } return @@ -211,7 +212,7 @@ func (self *Differ) CreateDelta(src io.Reader) DeltaIterator { // Add more external signature data func (self *Differ) AddSignatureData(data []byte) (err error) { self.unconsumed_signature_data = append(self.unconsumed_signature_data, data...) - if self.rsync.UniqueHasher == nil { + if !self.rsync.HasHasher() { consumed, err := self.read_signature_header(self.unconsumed_signature_data) if err != nil { if consumed < 0 { @@ -240,10 +241,10 @@ func NewPatcher(expected_input_size int64) (ans *Patcher) { } ans = &Patcher{} ans.rsync.BlockSize = utils.Min(bs, MaxBlockSize) - ans.rsync.UniqueHasher = xxh3.New() + ans.rsync.SetHasher(func() hash.Hash { return xxh3.New() }) - if ans.rsync.UniqueHasher.BlockSize() > 0 && ans.rsync.UniqueHasher.BlockSize() < ans.rsync.BlockSize { - ans.rsync.BlockSize = (ans.rsync.BlockSize / ans.rsync.UniqueHasher.BlockSize()) * ans.rsync.UniqueHasher.BlockSize() + if ans.rsync.HashBlockSize() > 0 && ans.rsync.HashBlockSize() < ans.rsync.BlockSize { + ans.rsync.BlockSize = (ans.rsync.BlockSize / ans.rsync.HashBlockSize()) * ans.rsync.HashBlockSize() } ans.expected_input_size_for_signature_generation = sz