From 9380daed0d54c50456c013f314332df78b4c8936 Mon Sep 17 00:00:00 2001 From: Neil Cawse <8085959+neilcawse@users.noreply.github.com> Date: Fri, 25 Sep 2026 17:55:00 -0400 Subject: [PATCH] vfs: stop setting a file's modtime from discarding its cached data Setting the modtime of a closed file refreshed the cache item's fingerprint before the new modtime had been applied to the remote object. The next open then saw a fingerprint mismatch, logged "removed cache file as stale (remote is different)" and downloaded the whole file again, even though only the modtime had changed. Refresh the cache fingerprint again once the remote modtime has been set. --- vfs/file.go | 5 +++++ vfs/file_test.go | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/vfs/file.go b/vfs/file.go index 69a7afcd1..11d63d803 100644 --- a/vfs/file.go +++ b/vfs/file.go @@ -495,6 +495,11 @@ func (f *File) _applyPendingModTime() error { switch err { case nil: fs.Debugf(f.o, "Applied pending mod time %v OK", f.pendingModTime) + // The cache fingerprint predates the new modtime, so refresh + // it or the next open discards the cached data as stale. + if f.d.vfs.cache != nil && f.d.vfs.cache.Exists(f._cachePath()) { + f.d.vfs.cache.SetModTime(f._cachePath(), f.pendingModTime) + } case fs.ErrorCantSetModTime, fs.ErrorCantSetModTimeWithoutDelete: // do nothing, in order to not break "touch somefile" if it exists already default: diff --git a/vfs/file_test.go b/vfs/file_test.go index ff561d2b0..0c6452154 100644 --- a/vfs/file_test.go +++ b/vfs/file_test.go @@ -13,6 +13,7 @@ import ( "github.com/rclone/rclone/fstest" "github.com/rclone/rclone/fstest/mockfs" "github.com/rclone/rclone/fstest/mockobject" + "github.com/rclone/rclone/lib/ranges" "github.com/rclone/rclone/vfs/vfscommon" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -163,6 +164,22 @@ func TestFileSetModTime(t *testing.T) { } } +// Touching a closed, fully cached file must keep its cached data: the +// remote modtime change it makes is not a change to the content. +func TestFileSetModTimeKeepsCache(t *testing.T) { + _, vfs, file, _ := fileCreate(t, vfscommon.CacheModeFull) + fileCheckContents(t, file) + whole := ranges.Range{Pos: 0, Size: int64(len("file1 contents"))} + item := vfs.cache.Item(file.Path()) + require.True(t, item.HasRange(whole)) + + require.NoError(t, file.SetModTime(t2)) + + require.NoError(t, item.Open(file.getObject())) + defer func() { require.NoError(t, item.Close(nil)) }() + assert.True(t, item.HasRange(whole), "touch discarded the cached data") +} + func fileCheckContents(t *testing.T, file *File) { fd, err := file.Open(os.O_RDONLY) require.NoError(t, err)