Fix Android: publish current mtime after the atomic-write rename

Edits on the phone were not picked up by syncthing for minutes to hours
("edited a file, but it never synced to my other devices").

Reproduced on device (Pixel 9 Pro, API 35, Syncthing-Fork, shared
storage /storage/emulated/0):

- The app's temp+rename save lands the new content (inode changes,
  bytes are correct), yet the file's mtime served through the FUSE
  layer reverts to the PREVIOUS file's mtime -- exactly, to the
  nanosecond. MediaProvider's media-scan DB row for the path is not
  updated when the staging file is renamed into place (logcat:
  "Database update failed while renaming .<name>.tmp.<pid>.<seq>")
  and the stale row wins. This reproduces for ANY writer, not just
  the app (shell temp+rename included).
- The fork's inotify watcher ignores these rename-based writes for a
  long time (a 15:48 edit reached the peer only at 16:39; five
  consecutive app writes over 6 minutes were never propagated), but
  it acts on an explicit timestamp update immediately (a touch
  synced in exactly the 10 s fsWatcherDelayS). Inotify events
  themselves are delivered fine (verified with an on-device inotify
  watcher: IN_MOVED_TO arrives).

Fix: after the rename in RealFileSystem.WriteFileAtomic, set the
file's atime/mtime to now (os.Chtimes, best-effort). utimensat
sticks through the FUSE layer and is the update the watcher reacts
to. Verified on device after the fix: edit -> peer in ~11-12 s,
mtime stays current.

Also pins the timestamp contract in
TestWriteFileAtomic_PublishesCurrentMtime (mtime is current after a
write and advances on rewrite) and documents the invariant in
architecture.md 6.5.
This commit is contained in:
gmp
2026-09-02 17:12:54 -04:00
parent 06b1444207
commit 5e40378072
3 changed files with 64 additions and 0 deletions
+8
View File
@@ -472,6 +472,14 @@ Only the visible byte range is shaped and drawn each frame:
(crash leftovers, plus the legacy deterministic "`.<name>.tmp`" name).
Unique temp names make same-file interleaving structurally impossible even
if the serialization regressed.
- **Post-rename timestamp:** immediately after the rename, `WriteFileAtomic`
sets the file's atime/mtime to now (`os.Chtimes`, best-effort). On Android
shared storage the FUSE/MediaProvider layer serves the **previous** file's
mtime after a rename (its media-scan DB row is not updated on rename), and
stat-comparing change detectors (syncthing) then miss the new content
until the next full rescan. The explicit utimensat publishes the current
mtime and is the update the watcher reacts to immediately; it is also the
timestamp contract `TestWriteFileAtomic_PublishesCurrentMtime` pins.
- Write failures are tracked per file (`writeFailed`, `retryAttempts`) and
retried via `retryChan` (exponential backoff 1 s … 30 s; the timer sends a
non-blocking token and the owner re-snapshots at fire time). There is no