Fix not transferring files that don't differ in size - fixes #911

Due to a logic error files stored on remotes which support modtime but
not hashes weren't being transferred when updating with a file of the
same size but different modtime.  Instead the modtime of the remote
file was being set to that of the local file.

In practice this affected crypt with all remotes except Amazon Drive
and Dropbox.
This commit is contained in:
Nick Craig-Wood 2016-11-28 17:08:15 +00:00
parent 539853df36
commit 2756900749
2 changed files with 64 additions and 26 deletions

View file

@ -12,7 +12,6 @@ import (
"strings"
"sync"
"sync/atomic"
"time"
"github.com/pkg/errors"
"github.com/spf13/pflag"
@ -121,34 +120,53 @@ func Equal(src, dst Object) bool {
return true
}
var srcModTime time.Time
if !Config.CheckSum {
// Assert: Size is equal or being ignored
// If checking checksum and not modtime
if Config.CheckSum {
// Check the hash
same, hash, _ := CheckHashes(src, dst)
if !same {
Debug(src, "%v differ", hash)
return false
}
if hash == HashNone {
Debug(src, "Size of src and dst objects identical")
} else {
Debug(src, "Size and %v of src and dst objects identical", hash)
}
return true
}
// Sizes the same so check the mtime
if Config.ModifyWindow == ModTimeNotSupported {
Debug(src, "Sizes identical")
return true
}
// Size the same so check the mtime
srcModTime = src.ModTime()
srcModTime := src.ModTime()
dstModTime := dst.ModTime()
dt := dstModTime.Sub(srcModTime)
ModifyWindow := Config.ModifyWindow
if dt >= ModifyWindow || dt <= -ModifyWindow {
Debug(src, "Modification times differ by %s: %v, %v", dt, srcModTime, dstModTime)
} else {
if dt < ModifyWindow && dt > -ModifyWindow {
Debug(src, "Size and modification time the same (differ by %s, within tolerance %s)", dt, ModifyWindow)
return true
}
}
// mtime is unreadable or different but size is the same so
// check the hash
Debug(src, "Modification times differ by %s: %v, %v", dt, srcModTime, dstModTime)
// Check if the hashes are the same
same, hash, _ := CheckHashes(src, dst)
if !same {
Debug(src, "Hash differ")
Debug(src, "%v differ", hash)
return false
}
if hash == HashNone {
// if couldn't check hash, return that they differ
return false
}
if !(Config.CheckSum || Config.NoUpdateModTime) {
// mod time differs but hash is the same to reset mod time if required
if !Config.NoUpdateModTime {
// Size and hash the same but mtime different so update the
// mtime of the dst object here
err := dst.SetModTime(srcModTime)
@ -157,14 +175,10 @@ func Equal(src, dst Object) bool {
return false
} else if err != nil {
Stats.Error()
ErrorLog(dst, "Failed to read set modification time: %v", err)
}
}
if hash == HashNone {
Debug(src, "Size of src and dst objects identical")
ErrorLog(dst, "Failed to set modification time: %v", err)
} else {
Debug(src, "Size and %v of src and dst objects identical", hash)
Debug(src, "Updated modification time in destination")
}
}
return true
}

View file

@ -336,6 +336,30 @@ func TestSyncAfterChangingModtimeOnlyWithNoUpdateModTime(t *testing.T) {
fstest.CheckItems(t, r.fremote, file2)
}
func TestSyncDoesntUpdateModtime(t *testing.T) {
if fs.Config.ModifyWindow == fs.ModTimeNotSupported {
t.Skip("Can't run this test on fs which doesn't support mod time")
}
r := NewRun(t)
defer r.Finalise()
file1 := r.WriteFile("foo", "foo", t2)
file2 := r.WriteObject("foo", "bar", t1)
fstest.CheckItems(t, r.flocal, file1)
fstest.CheckItems(t, r.fremote, file2)
fs.Stats.ResetCounters()
err := fs.Sync(r.fremote, r.flocal)
require.NoError(t, err)
fstest.CheckItems(t, r.flocal, file1)
fstest.CheckItems(t, r.fremote, file1)
// We should have transferred exactly one file, not set the mod time
assert.Equal(t, int64(1), fs.Stats.GetTransfers())
}
func TestSyncAfterAddingAFile(t *testing.T) {
r := NewRun(t)
defer r.Finalise()