Repository navigation
Conversation
Adding the third symptom variant: the remote file is deleted, not zeroedWhile preparing the state-consistency report I found that this defect can also produce a different, more alarming symptom than the "revert / truncate / zero byte" family: the file disappears from the remote entirely. The save pattern that does it is the one many editors (and the foobar2000 case in #9943) use:
Step 3 is not deferred: Measured on Windows 11 + WinFsp 2.2.26215, SFTP mount with
So this PR covers that variant too. |
On Windows os.Rename (MoveFileEx) cannot replace a destination that is open, even when the open handle shares delete, and with --vfs-cache-mode full the target of an atomic temp-file rename keeps its cache file open (its Item holds the fd for the handle cache grace period). The cache rename then failed with Access is denied. File.Rename only logged that at Info level and returned success, so the application saw the save succeed while the cache kept both files: the destination's stale data stayed at the destination name and the newly written data stayed at the temporary name. The re-targeted write back then uploaded the destination's stale (or empty) data over the remote, silently losing the user's new content (truncated or reverted file). See rclone#9943. Fail the rename instead: * vfs/file.go: propagate the cache rename error to the caller and to the application, and put the file's directory/leaf back so the node stays consistent with the unchanged directory entry. * vfs/vfscache/item.go: perform the cache file renames before mutating any item state so that a failure leaves the item's name, its cache files and its pending write back target untouched (the pending data is then uploaded under its own name rather than overwriting the destination). The real fix for atomic saves over an open cache file is a Windows POSIX semantics rename (see PR rclone#9574); this change makes the failure safe in the meantime.
Windows os.Rename uses MoveFileEx which fails with Access is denied when either path is open, even when those handles were opened with FILE_SHARE_DELETE (measured), and it cannot be fixed with share flags. The VFS cache deliberately keeps cache files open (file.OpenFile opens with FILE_SHARE_DELETE and the Item keeps the fd for the handle cache grace period), so an application doing the usual atomic save (write a temporary file then rename it over the target - editors, browsers, tools such as foobar2000) fails in the cache rename; before the previous commit that failure was hidden and silently lost the new data (issue rclone#9943). Add file.Rename: on Windows it renames with SetFileInformationByHandle and FileRenameInfoEx using FILE_RENAME_REPLACE_IF_EXISTS|FILE_RENAME_POSIX_SEMANTICS which can replace a destination which has open handles, falling back to os.Rename when that is not supported. On other platforms it is os.Rename. Use it for the cache rename in vfs/vfscache (the only os.Rename there). A destination held open by a handle without FILE_SHARE_DELETE (eg some antivirus scanners) still cannot be replaced, but that now surfaces as an error rather than silent data loss. New tests: lib/file/file_windows_test.go covers renaming over an open destination and an open source (both must succeed) and the no-share-delete case (must fail, leaving the destination untouched).
323fa3a to
46992a1
Compare
|
Rebased onto current The lint failure in the earlier run — Local checks after the rebase: The workflow runs for this push are |
|
Hi @Kurptas, thanks for rebasing, and for the extra write-up of the unlink-then-rename variant. Thanks also for offering to rebase onto #9574: yes please. We'll land #9574 first. Then please drop the duplicated The earlier review points still apply to those parts:
Thanks again for the careful work on this. |
What this fixes (on top of #9574 / for #9943)
This is the same class of bug as #9943 (Windows: an editor's atomic save = write temp + rename over the target silently loses or truncates data). PR #9574 fixes the two central pieces: the failure in
vfs/file.gois now returned instead of logged, andlib/filegained a WindowsRenamewith POSIX semantics that can replace a destination held open withFILE_SHARE_DELETE.This PR adds the state-consistency parts that the error path still needs, plus one unrelated defect found while reproducing.
1.
vfs/vfscache/item.go: the cache files must be renamed before any state is mutated.Today
item.renamesetsitem.name,item.oand the fingerprint first, then renames the data/metadata files, and the failure path re-targets the pending write-back (writeback.Rename(id, newName)) even when the file move failed.Cache.renamealso returns before moving the map key. So after a failed rename the item claims a name whose files do not exist: the next write-back resolves the destination by name (_store→fremote.NewObject/fcache.NewObject) and uploads the destination's stale content over the remote, which is how the data loss is produced (unpatched log:File.Rename failed in Cache: ... Access is denied→Copied (new)→upload succeeded try #1, remote unchanged). This commit renames the files first and returns the error withitem.name,item.o, the map key and the write-back target all untouched.2.
vfs/file.go: roll the node back when the rename fails.The node adopts
destDir/dPath/newNamebefore the risky part, so even when the error is returned the node and the directory entry disagree. Capturef.d/f.dPath/f.leafand restore them on failure.3. Independent defect: a pending edit is dropped when an existing cache is re-walked —
walk()passes an OS-encoded path toCache.get(), soreload()cannot find dirty items and never resumes them (repro and fix in the first commit of this branch; happy to split it into its own PR if you prefer).Verification
Windows 11 + WinFsp 2.2.26215, SFTP remote (
rclone serve sftp --local-encoding Rawas a name-preserving stand-in for a NAS), WinFsp mount,--vfs-cache-mode full; the save action is exactly what an editor does (read→ write temp →os.replaceover the target).vfscommits (lib/filenot touched)EIO), target unchanged, user's bytes preservedlib/filePOSIX rename (= #9574 commit 2)Also reproduced on current master.
go build,go vet,gofmtandgo test ./vfs/... ./lib/file/... ./lib/encoder/...are clean.Relationship to #9574
Commits 2–3 of this branch are equivalent in intent to #9574's two commits (which I could not fetch from the machine where this was developed, hence the independent implementation); the parts you will not find there are the item/node state consistency above and the reload fix. I'm happy to rebase this on #9574 once it lands and drop whatever it already contains, or to reduce this to a review suggestion if that is easier.