Skip to content

Android: a save commit updates the save folder in place (#485) - #732

Open
vhoxha08 wants to merge 1 commit into
igawa6:mainfrom
vhoxha08:fix/485-save-commit-in-place
Open

vhoxha08 wants to merge 1 commit into
igawa6:mainfrom
vhoxha08:fix/485-save-commit-in-place

Conversation

@vhoxha08

Copy link
Copy Markdown

What and why

Fixes #485. On Android a save commit swapped <id>.new and <id> with renameat2(RENAME_EXCHANGE) where the filesystem has it (app-private ext4/f2fs). That gives the save folder a new inode. A sync tool such as Syncthing watches the folder with inotify, so its watch stayed on the replaced folder (which the commit then deletes) and it missed every save until its next full rescan (hourly by default).

plat_rename_swap (dm/host/plat/plat_android.c) no longer exchanges folders: two lines removed. Every commit now uses the file-level roll-forward journal that shared storage (FUSE) already used (.pend1, delete what the new version lacks, .pend2, rename each new file over the old one, remove the marker). The folder keeps its inode and each file arrives as a rename into it. A crash after .pend1 is finished at the next mount (plat_swap_recover), so the save is still all old or all new. The launcher's restore (SaveStore.java) already uses the same format. save_sync.c, which relies on a real swap, is built only for the Mac.

Trade-off: on app-private storage a commit now costs a few marker writes and fsyncs plus one rename per file instead of one syscall. That is cheap for small saves but not for saves with many files (Breath of the Wild has 509). A follow-up PR makes commits copy and move only the changed files.

Not fixed here: on shared storage (FUSE), Android's media provider puts the old file's modification time back about two seconds after a file is renamed over it, so a changed file of the same size keeps its old time and a sync tool's full rescan can still skip it (#571). With this PR the inotify watch sees every commit; the follow-up PR fixes the times.

Reference entry: Commits keep the Android save folder itself; a line in the Link's Awakening note.

Checked

  • Python tests: python3 -m unittest test_android_saves (in dm/tools/tests) passes, including the launcher's JVM tests (JDK 25). The new HostSwap.test_commit_keeps_the_save_folder commits a version with a removed file and a new subfolder and checks that the folder keeps its inode, ends up with exactly the new files, and has no marker left. It fails on the old code.
  • NDK 28.2.13676358 syntax check of plat_android.c and hle_fs.c with the host's flags (Link's Awakening profile): no errors, no warnings.
  • On the phone, outside the app: the journal code (as the test extracts it) built with the NDK, run on f2fs (/data/local/tmp, the same filesystem as app-private storage) and on /sdcard, with an inotify watch on the save folder:
    • old code (RENAME_EXCHANGE) on f2fs: new inode, and the watch gets only IN_MOVE_SELF, no file events;
    • with the fix, on both filesystems: same inode, and the watch sees each removed entry, the new subfolder and each file moved in.
  • Phone run on a test app (-PnxApp=peanuts -PnxAppId=peanutstest -PnxGames=01006BB00C6F0000), The Legend of Zelda: Link's Awakening 1.1.0 (01006BB00C6F0000), a copy of an existing save, Turnip T30 (PurpleVK 26.3.0, driver ID 18): played past the file select into the Mysterious Forest at 59-60 fps, no crash, then saved from the System menu. 8 save commits (6 at boot and load with no writes, 2 with the in-game save's writes; savedata0.bin changed). An inotify watch on the save folder (its /sdcard path, as Syncthing watches it), set before the game started, kept the same inode (375458) throughout and received 32 IN_MOVED_TO events, one per file per commit, and no IN_MOVE_SELF, IN_DELETE_SELF or IN_IGNORED. For comparison, the 0.1.7 release's log on the same phone has save commit #1: … swapped atomically into … from the RENAME_EXCHANGE path.
  • Phone, chip, Android version: AYN Odin 2 Portal, Snapdragon 8 Gen 2, Android 13.
  • Not checked: a run on the system driver, Syncthing itself on the test app's folder (the inotify watch stands in for it), the rest of the Python suite, the host unit tests (make -C dm/host/tests fails on main already and does not compile plat_android.c), and lint_c.py tidy.

Docs

  • The finding is in its docs/reference/services.md entry and the game note, and python3 dm/tools/docs_symptoms.py was run (the symptom index)
  • No game files, keys or data extracted from a game in the change or the attachments
  • Source only: no binaries, nothing under dm/third_party/, no change to dmsh_chain.lock; the branch starts from the current main (the android branch named in CONTRIBUTING.md no longer exists)

🤖 Generated with Claude Code

A commit replaced the whole save folder: it swapped <id>.new and <id> with
renameat2(RENAME_EXCHANGE), which gives <id> a new inode. Sync tools such as
Syncthing watch the folder with inotify, so their watch stayed on the replaced
folder and they missed every save until their next full rescan (hourly by
default).

plat_rename_swap no longer swaps folders. Every commit now moves the new files
into the existing folder one by one, using the crash-safe journal that shared
storage already used. The folder stays the same, so sync tools see each new
file. A commit cut short by a crash is finished at the next start, so the save
is never left half old, half new.

Checked: a new test, test_android_saves.py HostSwap, commits a version with a
removed file and a new subfolder and checks that the folder keeps its inode,
that it ends up with exactly the new files, and that no marker stays. It
passes with the fix and fails on the old code. The whole test_android_saves.py
passes, including the launcher's JVM tests (JDK 25). plat_android.c and
hle_fs.c pass an NDK 28.2.13676358 syntax check with the host's flags
(Link's Awakening profile), with no warnings. On an Odin 2 Portal (Snapdragon
8 Gen 2, Android 13), the journal code built with the NDK ran on f2fs
(/data/local/tmp, as app-private storage) and on /sdcard, with an inotify
watch on the save folder. With RENAME_EXCHANGE the folder got a new inode and
the watch saw only IN_MOVE_SELF. With the fix the inode stays and the watch
sees each delete, the new subfolder and each file moved in.

Not checked: game runs on the phone (system driver, Turnip) with Syncthing,
and lint_c.py tidy (no clang-tidy here).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Saving replaces the whole game save dir, which breaks file-watcher sync tools like Syncthing

1 participant