Skip to content

Keep an atomically saved file under the name it was saved as - #52

Merged
franzjeger merged 1 commit into
mainfrom
claude/fix-rename
Aug 8, 2026
Merged

franzjeger merged 1 commit into
mainfrom
claude/fix-rename

Conversation

@franzjeger

Copy link
Copy Markdown
Owner

Editing a file on the mount made it disappear: README.md was gone and README.md.tmp.194149.5089d5eff10a was left holding the content.

That is the atomic save — write a temp file, rename it over the target — which is how vim, VS Code and most build tools save. The normal case, not an edge case.

What actually happened

rename() was not the culprit. It applies the rename locally, correctly. The problem is upstream of it:

Uploaded (small) README.md.tmp.194149.5089d5eff10a -> id=01UHM7UUXA7XDR…
upload complete: README.md (4955 bytes)

Those two lines are the whole bug. The upload started under the temp name and was still in flight when the rename landed, so OneDrive ended up holding the file as README.md.tmp.…. The next delta then dragged that name back down over the local one — the target vanished and the temp file appeared.

No bytes were lost, and the reporter recovered everything from the temp file. But it reads exactly like data loss, and a cleanup of "orphaned" temp files would have turned it into the real thing.

Fix: the row already carries the name the file ended up with, so the upload compares and corrects the remote (PATCH, i.e. move_item) before writing the row back.

Second, smaller, and more dangerous

The _local_* branch of rename() deleted the row before re-inserting it:

let _ = self.db.delete_item(&item.id).await;   // removed
if let Err(e) = self.db.upsert_item(&updated).await { … }

upsert_item already evicts whatever holds the new path and updates in place, so the delete bought nothing — and it opened a window in which the row did not exist at all. An upload of this very file is typically in flight during an atomic save, and since #51 a completing upload reads "no row" as "deleted while uploading" and removes the file from OneDrive. Two correct-looking changes that combine into deletion.

Testing

A fourth mount round trip performing a real atomic save: create temp, write, close, rename over the target. The mock holds the temp file's upload open for 2s so the rename lands mid-flight, as it does in practice, rather than depending on machine speed.

Asserts the target exists with its content and the temp name is gone immediately after the rename; then that the remote was corrected, the row carries the final name, and neither reverts once the upload settles.

Confirmed to fail before the fix — the assertion that fires is the one naming the consequence:

the upload started under the temp name and was never corrected, so OneDrive holds the file as notes.md.tmp.194149 — the next delta renames the local file back to that and the saved file disappears

cargo fmt, cargo clippy --workspace --all-targets -- -D warnings, and ONEDRIVE_REQUIRE_FUSE=1 cargo test --workspace pass.


Generated by Claude Code

Editing a file on the mount made it disappear: `README.md` was gone and
`README.md.tmp.194149.5089d5eff10a` was left holding the content. That is
the atomic save — write a temp file, rename it over the target — which is
how vim, VS Code and most build tools save. The normal case, not an edge
case.

rename() was not the culprit; it applies the rename locally. The upload
of the temp file was still in flight, having started under the temp name,
so OneDrive ended up holding the file under that name. The next delta
then dragged the temp name back down over the local one: the target
vanished and the temp file appeared. No bytes were lost, but it reads
exactly like data loss — and a cleanup of "orphaned" temp files would
have turned it into the real thing.

The row already carries the name the file ended up with, so the upload
now compares and corrects the remote to match before writing the row
back.

Second, smaller, and more dangerous: the `_local_*` branch of rename()
deleted the row before re-inserting it. `upsert_item` already evicts
whatever holds the new path and updates in place, so the delete bought
nothing — and it opened a window in which the row did not exist. An
upload of this very file is typically in flight during an atomic save,
and one completing inside that window would read "no row" as "deleted
while uploading" and remove the file from OneDrive.

Tests: a fourth mount round trip performing a real atomic save, with the
mock holding the temp file's upload open so the rename lands mid-flight
as it does in practice. Asserts the target exists with its content, the
temp name is gone, the remote was corrected, and neither reverts once the
upload settles. Confirmed to fail before the fix.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XfrYQTqjBaP4gDF77Ux1wZ
@franzjeger
franzjeger merged commit aae9171 into main Aug 8, 2026
3 checks passed
franzjeger pushed a commit that referenced this pull request Aug 8, 2026
…not use

Three findings from an external conformance suite that states what a cloud
file must answer and checks it through a real mount. Five of its eight
invariants already passed, including the two hardest — the atomic save and
the delete-during-upload races fixed in #52 and #51 both held up under a
test that was not written by whoever fixed them.

**`chmod +x` returned success and changed nothing.** setattr accepted a
mode change and dropped it on the floor, and every attribute answer used a
hardcoded 0o644. OneDrive has nowhere to store a mode, so it has to live
here or not at all — and the exec bit is the one that matters, because
losing it turns a working script into one that will not run with nothing
to point at. Modes now live in their own table, the same way local-only
symlinks already do, and are held in memory because every stat reads them
and a stat must not become a query.

The second half of that was worse and only showed up under the suite. The
mode is keyed by item ID, and a created file's ID changes when the upload
adopts the real OneDrive one. A `chmod +x` made before the upload landed
was orphaned under a `_local_*` key nothing would ever ask about again, so
it applied right up until the file finished uploading and then quietly
stopped. The cache file already moves across adoption; the mode moves with
it now.

**A download that ended early was served as a short file.** The size of a
placeholder comes from the delta pass. If the object changes on OneDrive
before anyone reads it, the stream ends at a different length — and we
took a completed download to mean a correct one. The reader had asked
about a 4096-byte file and got 2048, with no error, and because getattr
answers from the cache file the wrong size then became what stat reported
from then on. A hydration now either brings what the row promised or it
brings nothing: the partial copy is discarded, the row stays a
placeholder so the next open tries again, and the reader gets EIO instead
of a file that is quietly not what it asked for.

**A placeholder claimed disk it was not using.** blocks was computed from
size, so a cloud-only file reported a full allocation. That makes
`du -sh ~/OneDrive` report the whole drive, which means on-demand cannot
be seen to be working even when it is — and the cache-size policy reads
those numbers. blocks now comes from the cached copy, and is zero when
there is not one.

Tests: two mount round trips, one for the mode across an adoption and one
for the short download, which also asserts the placeholder's blocks.
Both confirmed to fail before the corresponding fix. The five existing
round trips and the rest of the suite are unchanged.
franzjeger added a commit that referenced this pull request Aug 8, 2026
…not use (#55)

Three findings from an external conformance suite that states what a cloud
file must answer and checks it through a real mount. Five of its eight
invariants already passed, including the two hardest — the atomic save and
the delete-during-upload races fixed in #52 and #51 both held up under a
test that was not written by whoever fixed them.

**`chmod +x` returned success and changed nothing.** setattr accepted a
mode change and dropped it on the floor, and every attribute answer used a
hardcoded 0o644. OneDrive has nowhere to store a mode, so it has to live
here or not at all — and the exec bit is the one that matters, because
losing it turns a working script into one that will not run with nothing
to point at. Modes now live in their own table, the same way local-only
symlinks already do, and are held in memory because every stat reads them
and a stat must not become a query.

The second half of that was worse and only showed up under the suite. The
mode is keyed by item ID, and a created file's ID changes when the upload
adopts the real OneDrive one. A `chmod +x` made before the upload landed
was orphaned under a `_local_*` key nothing would ever ask about again, so
it applied right up until the file finished uploading and then quietly
stopped. The cache file already moves across adoption; the mode moves with
it now.

**A download that ended early was served as a short file.** The size of a
placeholder comes from the delta pass. If the object changes on OneDrive
before anyone reads it, the stream ends at a different length — and we
took a completed download to mean a correct one. The reader had asked
about a 4096-byte file and got 2048, with no error, and because getattr
answers from the cache file the wrong size then became what stat reported
from then on. A hydration now either brings what the row promised or it
brings nothing: the partial copy is discarded, the row stays a
placeholder so the next open tries again, and the reader gets EIO instead
of a file that is quietly not what it asked for.

**A placeholder claimed disk it was not using.** blocks was computed from
size, so a cloud-only file reported a full allocation. That makes
`du -sh ~/OneDrive` report the whole drive, which means on-demand cannot
be seen to be working even when it is — and the cache-size policy reads
those numbers. blocks now comes from the cached copy, and is zero when
there is not one.

Tests: two mount round trips, one for the mode across an adoption and one
for the short download, which also asserts the placeholder's blocks.
Both confirmed to fail before the corresponding fix. The five existing
round trips and the rest of the suite are unchanged.

Co-authored-by: Frank <support@sybr.no>
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.

2 participants