mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-18 02:39:57 -04:00
Put a finished save commit back when Import Saves cannot swap the account folder.
The import moved save-tx's journal into the backup before it moved the account folder aside. When that move failed (a file in the folder held open by a virus scanner or a sync client), the player was told nothing was imported, yet the newest finished commit was gone from save-tx and the game loaded the older account folder. The journal now goes back into save-tx when the swap fails, and the staged copy is removed. A save copied from read-only media also kept the read-only attribute in the game's folder, and the replaced save's read-only files stayed behind in save-tx/replaced: imported files are made writable and the old ones are deleted with the attribute cleared. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -18,9 +18,22 @@ account folder inside).
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import stat
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def _rmtree(path):
|
||||
"""Delete a folder, read-only files in it too (shutil.rmtree stops at them on Windows); what still fails stays."""
|
||||
def writable(func, p, _exc):
|
||||
try:
|
||||
os.chmod(p, stat.S_IREAD | stat.S_IWRITE)
|
||||
func(p)
|
||||
except OSError:
|
||||
pass
|
||||
if Path(path).exists():
|
||||
shutil.rmtree(path, onexc=writable)
|
||||
|
||||
README = '''NX2navi saves folder
|
||||
|
||||
To import a save, put it here and select Import Saves in the NX2navi launcher (next to Game Folder):
|
||||
@@ -210,9 +223,12 @@ def import_save(game, folder=None, log=print):
|
||||
for rel in files:
|
||||
(stage / rel).parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(Path(src) / rel, stage / rel)
|
||||
# a save copied from read-only media keeps the attribute through copy2; the game rewrites its files
|
||||
os.chmod(stage / rel, stat.S_IREAD | stat.S_IWRITE)
|
||||
# The journal of the replaced save goes out of save-tx (the game would load a finished commit first, or complete an
|
||||
# interrupted one): what holds files moves into the backup, beside the save it was copied from, so a copy the
|
||||
# backup's own choice missed (an interrupted commit's other half) is kept too.
|
||||
moved = []
|
||||
for d in list(tx.iterdir()):
|
||||
if d == stage:
|
||||
continue
|
||||
@@ -222,22 +238,34 @@ def import_save(game, folder=None, log=print):
|
||||
backup.mkdir()
|
||||
(backup / 'save-tx').mkdir(exist_ok=True)
|
||||
shutil.move(str(d), str(backup / 'save-tx' / d.name))
|
||||
moved.append(d)
|
||||
else:
|
||||
shutil.rmtree(d, ignore_errors=True) if d.is_dir() else d.unlink(missing_ok=True)
|
||||
_rmtree(d) if d.is_dir() else d.unlink(missing_ok=True)
|
||||
account.parent.mkdir(parents=True, exist_ok=True)
|
||||
# The imported save goes into place before the old one is deleted; a failed rename puts the old one back.
|
||||
# The imported save goes into place before the old one is deleted; a failed rename puts the old one back, and the
|
||||
# journal too: a finished commit waiting in save-tx is newer than the account folder, and without it the game
|
||||
# would load the older save although nothing was imported.
|
||||
gone = None
|
||||
if account.exists():
|
||||
gone = _fresh(tx, 'replaced')
|
||||
os.replace(account, gone)
|
||||
try:
|
||||
os.replace(stage, account)
|
||||
if account.exists():
|
||||
gone = _fresh(tx, 'replaced')
|
||||
os.replace(account, gone)
|
||||
try:
|
||||
os.replace(stage, account)
|
||||
except OSError:
|
||||
if gone is not None:
|
||||
os.replace(gone, account)
|
||||
raise
|
||||
except OSError:
|
||||
if gone is not None:
|
||||
os.replace(gone, account)
|
||||
for d in moved:
|
||||
try:
|
||||
shutil.move(str(backup / 'save-tx' / d.name), str(d))
|
||||
except OSError as e:
|
||||
log(f'saves: could not put {d.name} back into {tx}: {e}; it is in {backup / "save-tx"}')
|
||||
_rmtree(stage)
|
||||
raise
|
||||
if gone is not None:
|
||||
shutil.rmtree(gone, ignore_errors=True)
|
||||
_rmtree(gone)
|
||||
if from_saves: # what was put into saves/ moves out whole, so the next import does not take any of it again
|
||||
done = _fresh(saves, f'imported {stamp}')
|
||||
done.mkdir()
|
||||
|
||||
@@ -23,6 +23,7 @@ import json
|
||||
import os
|
||||
import shutil
|
||||
import ssl
|
||||
import stat
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
@@ -1191,6 +1192,43 @@ class SavesTest(unittest.TestCase):
|
||||
saves.os.replace = real
|
||||
self.assertEqual((acct / 'save.dat').read_bytes(), b'old')
|
||||
|
||||
def test_a_failed_swap_keeps_the_finished_commit(self):
|
||||
# the account folder cannot be moved aside (a file in it held open): the newer finished commit stays in
|
||||
# save-tx, where the game loads it first, instead of going into the backup
|
||||
acct = saves.account_dir(self.game)
|
||||
self.files(acct, {'save.dat': b'older'})
|
||||
tx = self.game / 'profile' / 'save-tx'
|
||||
self.files(tx, {'ready-4/save.dat': b'newest'})
|
||||
src = self.files(self.tmp / 'mine', {'save.dat': b'imported'})
|
||||
real = saves.os.replace
|
||||
|
||||
def replace(a, b):
|
||||
if Path(a) == acct:
|
||||
raise PermissionError(32, 'The process cannot access the file')
|
||||
return real(a, b)
|
||||
saves.os.replace = replace
|
||||
try:
|
||||
with self.assertRaises(PermissionError):
|
||||
saves.import_save(self.game, src, log=lambda m: None)
|
||||
finally:
|
||||
saves.os.replace = real
|
||||
self.assertEqual((tx / 'ready-4' / 'save.dat').read_bytes(), b'newest')
|
||||
self.assertEqual((acct / 'save.dat').read_bytes(), b'older')
|
||||
self.assertFalse((tx / 'import').exists())
|
||||
|
||||
def test_read_only_files_import_writable(self):
|
||||
acct = saves.account_dir(self.game)
|
||||
self.files(acct, {'save.dat': b'old'})
|
||||
src = self.files(self.tmp / 'mine', {'save.dat': b'imported'})
|
||||
os.chmod(src / 'save.dat', stat.S_IREAD)
|
||||
os.chmod(acct / 'save.dat', stat.S_IREAD) # the save it replaces: deleted after the swap all the same
|
||||
try:
|
||||
saves.import_save(self.game, src, log=lambda m: None)
|
||||
self.assertTrue(os.access(acct / 'save.dat', os.W_OK))
|
||||
self.assertEqual([p.name for p in (self.game / 'profile' / 'save-tx').iterdir()], [])
|
||||
finally:
|
||||
os.chmod(src / 'save.dat', stat.S_IREAD | stat.S_IWRITE)
|
||||
|
||||
def test_another_installs_finished_commit_is_its_save(self):
|
||||
other = self.files(self.tmp / 'other', {'profile/save/' + 'b' * 32 + '/save.dat': b'older',
|
||||
'profile/save-tx/ready-2/save.dat': b'newest'})
|
||||
|
||||
Reference in New Issue
Block a user