Add part count to checksum (#17035)

This commit is contained in:
Klaus Post 2023-04-14 09:44:45 -07:00 committed by GitHub
parent a9269cee29
commit c133979b8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 4 deletions

View File

@ -1137,8 +1137,9 @@ func (er erasureObjects) CompleteMultipartUpload(ctx context.Context, bucket str
}
}
if checksumType.IsSet() {
checksumType |= hash.ChecksumMultipart
cs := hash.NewChecksumFromData(checksumType, checksumCombined)
fi.Checksum = cs.AppendTo(nil)
fi.Checksum = cs.AppendTo(nil, len(fi.Parts))
if opts.EncryptFn != nil {
fi.Checksum = opts.EncryptFn("object-checksum", fi.Checksum)
}

View File

@ -1081,7 +1081,7 @@ func (er erasureObjects) putObject(ctx context.Context, bucket string, object st
}
fi.DataDir = mustGetUUID()
fi.Checksum = opts.WantChecksum.AppendTo(nil)
fi.Checksum = opts.WantChecksum.AppendTo(nil, 0)
if opts.EncryptFn != nil {
fi.Checksum = opts.EncryptFn("object-checksum", fi.Checksum)
}

View File

@ -22,6 +22,7 @@ import (
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"fmt"
"hash"
"hash/crc32"
"net/http"
@ -53,6 +54,8 @@ const (
ChecksumCRC32C
// ChecksumInvalid indicates an invalid checksum.
ChecksumInvalid
// ChecksumMultipart indicates the checksum is from a multipart upload.
ChecksumMultipart
// ChecksumNone indicates no checksum.
ChecksumNone ChecksumType = 0
@ -194,8 +197,17 @@ func ReadCheckSums(b []byte) map[string]string {
if length == 0 || len(b) < length {
break
}
res[typ.String()] = base64.StdEncoding.EncodeToString(b[:length])
cs := base64.StdEncoding.EncodeToString(b[:length])
b = b[length:]
if typ.Is(ChecksumMultipart) {
t, n := binary.Uvarint(b)
if n < 0 {
break
}
cs = fmt.Sprintf("%s-%d", cs, t)
b = b[n:]
}
res[typ.String()] = cs
}
if len(res) == 0 {
res = nil
@ -225,8 +237,9 @@ func NewChecksumString(alg, value string) *Checksum {
}
// AppendTo will append the checksum to b.
// 'parts' is used when checksum has ChecksumMultipart set.
// ReadCheckSums reads the values back.
func (c *Checksum) AppendTo(b []byte) []byte {
func (c *Checksum) AppendTo(b []byte, parts int) []byte {
if c == nil {
return nil
}
@ -238,6 +251,13 @@ func (c *Checksum) AppendTo(b []byte) []byte {
}
b = append(b, tmp[:n]...)
b = append(b, crc...)
if c.Type.Is(ChecksumMultipart) {
if parts < 0 {
parts = 0
}
n := binary.PutUvarint(tmp[:], uint64(parts))
b = append(b, tmp[:n]...)
}
return b
}