operations: make rcat obey --ignore-checksum

This commit is contained in:
Nick Craig-Wood 2020-02-14 12:47:11 +00:00
parent c41fbc0f90
commit 3893c14889
2 changed files with 40 additions and 16 deletions

View File

@ -1314,17 +1314,29 @@ func Rcat(ctx context.Context, fdst fs.Fs, dstFileName string, in io.ReadCloser,
}()
in = tr.Account(in).WithBuffer()
hashes := hash.NewHashSet(fdst.Hashes().GetOne()) // just pick one hash
hashOption := &fs.HashesOption{Hashes: hashes}
hash, err := hash.NewMultiHasherTypes(hashes)
if err != nil {
return nil, err
}
readCounter := readers.NewCountingReader(in)
trackingIn := io.TeeReader(readCounter, hash)
var trackingIn io.Reader
var hasher *hash.MultiHasher
var options []fs.OpenOption
if !fs.Config.IgnoreChecksum {
hashes := hash.NewHashSet(fdst.Hashes().GetOne()) // just pick one hash
hashOption := &fs.HashesOption{Hashes: hashes}
options = append(options, hashOption)
hasher, err = hash.NewMultiHasherTypes(hashes)
if err != nil {
return nil, err
}
trackingIn = io.TeeReader(readCounter, hasher)
} else {
trackingIn = readCounter
}
compare := func(dst fs.Object) error {
src := object.NewStaticObjectInfo(dstFileName, modTime, int64(readCounter.BytesRead()), false, hash.Sums(), fdst)
var sums map[hash.Type]string
if hasher != nil {
sums = hasher.Sums()
}
src := object.NewStaticObjectInfo(dstFileName, modTime, int64(readCounter.BytesRead()), false, sums, fdst)
if !Equal(ctx, src, dst) {
err = errors.Errorf("corrupted on transfer")
err = fs.CountError(err)
@ -1373,7 +1385,7 @@ func Rcat(ctx context.Context, fdst fs.Fs, dstFileName string, in io.ReadCloser,
}
objInfo := object.NewStaticObjectInfo(dstFileName, modTime, -1, false, nil, nil)
if dst, err = fStreamTo.Features().PutStream(ctx, in, objInfo, hashOption); err != nil {
if dst, err = fStreamTo.Features().PutStream(ctx, in, objInfo, options...); err != nil {
return dst, err
}
if err = compare(dst); err != nil {

View File

@ -1443,14 +1443,21 @@ func TestGetFsInfo(t *testing.T) {
}
func TestRcat(t *testing.T) {
checkSumBefore := fs.Config.CheckSum
defer func() { fs.Config.CheckSum = checkSumBefore }()
check := func(withChecksum, ignoreChecksum bool) {
checksumBefore, ignoreChecksumBefore := fs.Config.CheckSum, fs.Config.IgnoreChecksum
fs.Config.CheckSum, fs.Config.IgnoreChecksum = withChecksum, ignoreChecksum
defer func() {
fs.Config.CheckSum, fs.Config.IgnoreChecksum = checksumBefore, ignoreChecksumBefore
}()
check := func(withChecksum bool) {
fs.Config.CheckSum = withChecksum
prefix := "no_checksum_"
var prefix string
if withChecksum {
prefix = "with_checksum_"
} else {
prefix = "no_checksum_"
}
if ignoreChecksum {
prefix = "ignore_checksum_"
}
r := fstest.NewRun(t)
@ -1486,8 +1493,13 @@ func TestRcat(t *testing.T) {
fstest.CheckItems(t, r.Fremote, file1, file2)
}
check(true)
check(false)
for i := 0; i < 4; i++ {
withChecksum := (i & 1) != 0
ignoreChecksum := (i & 2) != 0
t.Run(fmt.Sprintf("withChecksum=%v,ignoreChecksum=%v", withChecksum, ignoreChecksum), func(t *testing.T) {
check(withChecksum, ignoreChecksum)
})
}
}
func TestRcatSize(t *testing.T) {