Skip to content

vfs: preserve shrinking truncate on uncached files - #10063

Merged
ncw merged 2 commits into
rclone:masterfrom
flcrom:vfs-truncate-shrink-uncached
Oct 15, 2026
Merged

ncw merged 2 commits into
rclone:masterfrom
flcrom:vfs-truncate-shrink-uncached

Conversation

@flcrom

@flcrom flcrom commented Oct 10, 2026

Copy link
Copy Markdown

What does this change do?

If an uncached file is shrunk with Truncate (what a mount does for truncate(2) with no open handle), the cache file is cut to the new size, but the kept part of the file still has to be fetched on close. The downloader wrote the whole remote object into the cache file, which grew it back to its old size, so the upload replaced the object with the original contents.

This stops the downloader writing past the end of the item. Added tests at the vfscache level and the vfs level (--vfs-cache-mode writes and full).

Linked issue

Fixes #10047

Checklist

  • This change is trivial OR it has been discussed and agreed in the linked issue.
  • I have read the contribution guidelines.
  • (If I used AI tools to help write this code) I have read and understood the AI-assisted contributions guidance, and I have tested and take ownership of this change myself.
  • I have added tests for all changes in this PR if appropriate.
  • I have added documentation for the changes if appropriate.
  • All commit messages are in house style.
  • (Backend changes only) test_all passes for this backend and if submitting a new backend can provide a test account for the integration tester - see CONTRIBUTING.md.
  • This Pull Request is ready for review.

I used an AI coding assistant to help write this. I built it and ran make quicktest; the only failure was cmd/gitannex, which needs rclone on the PATH and passes when it is.

If a file that hadn't been read was shrunk with Truncate, the cache
file was cut to the new size but the kept part of the file still had
to be fetched on close. The downloader wrote the whole remote object
into the cache file, which grew it back to its old size, so the upload
replaced the object with the original contents.

Stop the downloader writing past the end of the item, and add tests at
the vfscache and vfs levels.

Fixes rclone#10047
@flcrom
flcrom requested a review from ncw as a code owner October 10, 2026 06:52

@ncw ncw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice fix - thank you :-)

I put some nits inline for you to look at.

Comment thread vfs/read_write_test.go Outdated
require.NoError(t, err)
require.NoError(t, node.(*File).Truncate(5))

vfs.WaitForWriters(10 * time.Second)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use the waitForWritersDelay constant rather than 10 * time.Second? These tests run against every backend on the integration tester and some of them are slow.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 793a4b0.

Comment thread vfs/read_write_test.go

obj, err := r.Fremote.NewObject(context.Background(), "file1")
require.NoError(t, err)
assert.Equal(t, int64(5), obj.Size())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to check the contents of the object as well as its size here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 793a4b0.

Comment thread vfs/vfscache/item.go
}
}
item.mu.Unlock()
if err == nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment on WriteAtNoOverwrite should mention that bytes past the end of the item are counted as skipped.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 793a4b0.

Use waitForWritersDelay instead of a literal timeout, check the
uploaded object's contents as well as its size, and document that
bytes past the end of the item are counted as skipped.

@ncw ncw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you - looks great now :-)

@ncw
ncw merged commit 78be707 into rclone:master Oct 15, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

vfs: shrinking truncate of an uncached file is silently undone on close (--vfs-cache-mode writes/full)

2 participants