diff --git a/docs/launcher.md b/docs/launcher.md index 6e6998cf..9cd48919 100644 --- a/docs/launcher.md +++ b/docs/launcher.md @@ -378,6 +378,10 @@ A mod goes in as it came. Play first puts the folder in order (`mods.prepare`): that folder is there already, and the archive stays; a mod folder whose files sit one or more folders down (an archive that held the mod's own folder) gets them moved up; a `romfs`, `romfslite`, `exefs` or `cheats` folder put straight into the mods folder becomes a mod of its own, *Unnamed mod*. An archive that would write outside its folder is refused. +So is, on Windows, one that holds a file Windows cannot keep under its name (`mods.windows_name_problem`): a device's +name such as `CON.pchtxt`, a name ending in a dot or a space, or one with a colon or another character Windows does +not allow. Unpacked anyway such a file is lost, renamed or a hidden stream, and the game misses it without a word; the +refusal names the file. Nothing is deleted. Add a Mod and Add a Mod Folder do the same for an archive or folder the player picks, which is unpacked or copied into the mods folder under a free name; Open Mods Folder opens it. The picked mod is put together in a folder beside the mods folder (`.adding-mod`) and moves in with one rename once it is complete, so a copy that diff --git a/launcher/mods.py b/launcher/mods.py index 8eb8893f..d1536757 100644 --- a/launcher/mods.py +++ b/launcher/mods.py @@ -387,11 +387,39 @@ def unwrap(folder): return True -def _safe_members(names): +# Names Windows keeps for devices, with or without an extension and in any case: a file called so is the device. +WINDOWS_DEVICES = frozenset(('CON', 'PRN', 'AUX', 'NUL', *(f'{d}{n}' for d in ('COM', 'LPT') for n in '123456789¹²³'))) + + +def windows_name_problem(name): + """Why Windows cannot keep a file or folder under name, '' when it can: a device's name (CON, NUL.txt), a dot + or a space at its end (Windows drops them, so the file gets another name than the one the game asks for), or + a character Windows does not allow (a colon makes a hidden stream of another file).""" + if name.split('.')[0].rstrip(' ').upper() in WINDOWS_DEVICES: + return 'Windows keeps that name for a device' + if name != name.rstrip('. '): + return 'Windows drops a dot or a space at the end of a name' + bad = sorted({c for c in name if c in '<>:"|?*' or ord(c) < 32}) + if bad: + return 'Windows allows no ' + ' '.join(c if ord(c) >= 32 else f'control character {ord(c)}' for c in bad) + ' in a name' + return '' + + +def _safe_members(names, windows=None): + """ModError when a file of an archive (its member names) would land outside the archive's own folder, or, on + Windows (windows: None asks the system), under another name than its own or not at all (windows_name_problem): + unpacked all the same, such a file is missing where the game looks for it, with no word about it. The game + for Linux reads such names as they are, so there they pass.""" + if windows is None: + windows = os.name == 'nt' for n in names: p = n.replace('\\', '/') if p.startswith('/') or re.match(r'^[A-Za-z]:', p) or '..' in p.split('/'): raise ModError(f'it holds a file outside its own folder ({n})') + for part in p.split('/') if windows else (): + why = windows_name_problem(part) + if why: + raise ModError(f'it holds a file Windows cannot keep under its name ({n}): {why}') def _tar(): @@ -451,6 +479,10 @@ def prepare(game_dir, log=None): for a in sorted(p for p in folder.iterdir() if p.is_file() and p.suffix.lower() in ARCHIVES): name = a.stem.strip().rstrip('.') target = folder / name + why = windows_name_problem(name) if os.name == 'nt' and name else '' + if why: # before exists(): Windows says a device's name exists in every folder + note(f'{a.name} was not unpacked: no folder can be called {name} ({why}); rename the archive') + continue if not name or target.exists(): continue try: @@ -539,6 +571,9 @@ def add(game_dir, src, log=None): name = src.stem.strip().rstrip('.') or UNNAMED else: raise ModError('it is no archive (.zip, .7z or .rar) and no folder') + why = windows_name_problem(name) if os.name == 'nt' else '' + if why: + raise ModError(f'no mod folder can be called {name} ({why}): rename it first') stage = _staging(folder) try: made = stage / 'mod' diff --git a/tests/launcher_test.py b/tests/launcher_test.py index 1a2d8a1c..58a0a0fa 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -5395,6 +5395,43 @@ class ModsTest(Tmp): self.assertEqual([(m.name, m.enabled) for m in found], [('beta', True), (' Lead', False), ('Alpha', True)]) self.assertEqual(self.mods.overlaps(found), {'beta': [('Alpha', 1, True)], 'Alpha': [('beta', 1, False)]}) + def test_names_windows_cannot_keep_are_refused_in_an_archive(self): + """A file of a mod's archive called after a device (CON.pchtxt), ending in a dot or a space, or holding a + colon cannot be kept under its name on Windows: unpacked anyway it is lost, renamed or a hidden stream of + another file, and the game misses it without a word. The archive is refused with the file's name; on + Linux, whose game reads such names, it passes.""" + problem = self.mods.windows_name_problem + for name in ('CON', 'con.pchtxt', 'NUL.tar.gz', 'Com1', 'LPT9.txt', 'aux .txt', 'trailing.', 'trailing ', + 'file.txt:stream', 'what?.bfres', 'tab\there'): + self.assertTrue(problem(name), name) + for name in ('console.txt', 'COM10.txt', 'a.b.c', '.hidden', 'nul_tex.bntx.zs', 'Actor', 'my mod (2)', + 'Ünïcödé'): + self.assertEqual(problem(name), '', name) + safe = self.mods._safe_members + safe(['romfs/Actor/a.bfres', 'romfs/Actor/', 'readme.txt'], windows=True) + for bad in ('exefs/CON.pchtxt', 'romfs/nul/a.txt', 'romfs/Actor/foo.', 'romfs/Actor /a.txt', + 'romfs/file.txt:stream', 'romfs\\AUX.bin'): + with self.assertRaises(self.mods.ModError) as refused: + safe(['romfs/ok.txt', bad], windows=True) + self.assertIn(f'({bad})', str(refused.exception)) + safe(['romfs/ok.txt', bad], windows=False) # names as any other elsewhere + with self.assertRaises(self.mods.ModError): # leaving the folder is refused everywhere + safe(['../outside.txt'], windows=False) + with zipfile.ZipFile(self.folder / 'Device.zip', 'w') as z: + z.writestr('exefs/CON.pchtxt', '@stop\n') + z.writestr('romfs/a.txt', 'a') + done = self.mods.prepare(self.game) + if os.name == 'nt': + self.assertEqual(len(done), 1) + self.assertIn('Device.zip was not unpacked: it holds a file Windows cannot keep under its name ' + '(exefs/CON.pchtxt): Windows keeps that name for a device', done[0]) + self.assertEqual(self.tree(), ['Device.zip']) # nothing of it was unpacked + with self.assertRaises(self.mods.ModError): # Add a Mod refuses the same archive + self.mods.add(self.game, self.folder / 'Device.zip') + self.assertEqual(self.tree(), ['Device.zip']) + else: + self.assertEqual(self.tree(), ['Device.zip', 'Device/exefs/CON.pchtxt', 'Device/romfs/a.txt']) + def test_a_picked_archive_or_folder_is_added(self): src = self.tmp / 'Downloads' src.mkdir()