forked from TrueCloudLab/rclone
operations: make rcat obey --ignore-checksum
This commit is contained in:
parent
c41fbc0f90
commit
3893c14889
2 changed files with 40 additions and 16 deletions
|
@ -1314,17 +1314,29 @@ func Rcat(ctx context.Context, fdst fs.Fs, dstFileName string, in io.ReadCloser,
|
||||||
}()
|
}()
|
||||||
in = tr.Account(in).WithBuffer()
|
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)
|
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 {
|
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) {
|
if !Equal(ctx, src, dst) {
|
||||||
err = errors.Errorf("corrupted on transfer")
|
err = errors.Errorf("corrupted on transfer")
|
||||||
err = fs.CountError(err)
|
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)
|
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
|
return dst, err
|
||||||
}
|
}
|
||||||
if err = compare(dst); err != nil {
|
if err = compare(dst); err != nil {
|
||||||
|
|
|
@ -1443,14 +1443,21 @@ func TestGetFsInfo(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRcat(t *testing.T) {
|
func TestRcat(t *testing.T) {
|
||||||
checkSumBefore := fs.Config.CheckSum
|
check := func(withChecksum, ignoreChecksum bool) {
|
||||||
defer func() { fs.Config.CheckSum = checkSumBefore }()
|
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) {
|
var prefix string
|
||||||
fs.Config.CheckSum = withChecksum
|
|
||||||
prefix := "no_checksum_"
|
|
||||||
if withChecksum {
|
if withChecksum {
|
||||||
prefix = "with_checksum_"
|
prefix = "with_checksum_"
|
||||||
|
} else {
|
||||||
|
prefix = "no_checksum_"
|
||||||
|
}
|
||||||
|
if ignoreChecksum {
|
||||||
|
prefix = "ignore_checksum_"
|
||||||
}
|
}
|
||||||
|
|
||||||
r := fstest.NewRun(t)
|
r := fstest.NewRun(t)
|
||||||
|
@ -1486,8 +1493,13 @@ func TestRcat(t *testing.T) {
|
||||||
fstest.CheckItems(t, r.Fremote, file1, file2)
|
fstest.CheckItems(t, r.Fremote, file1, file2)
|
||||||
}
|
}
|
||||||
|
|
||||||
check(true)
|
for i := 0; i < 4; i++ {
|
||||||
check(false)
|
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) {
|
func TestRcatSize(t *testing.T) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue