From 3893c14889b997c0ad273d01b8cd4baf63ed09b4 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 14 Feb 2020 12:47:11 +0000 Subject: [PATCH] operations: make rcat obey --ignore-checksum --- fs/operations/operations.go | 30 +++++++++++++++++++++--------- fs/operations/operations_test.go | 26 +++++++++++++++++++------- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/fs/operations/operations.go b/fs/operations/operations.go index 9caa8f473..c04b5ab77 100644 --- a/fs/operations/operations.go +++ b/fs/operations/operations.go @@ -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 { diff --git a/fs/operations/operations_test.go b/fs/operations/operations_test.go index 29912763d..ddefac822 100644 --- a/fs/operations/operations_test.go +++ b/fs/operations/operations_test.go @@ -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) {