diff options
| -rw-r--r-- | tests/test_world_build_output_lock.py | 14 | ||||
| -rw-r--r-- | tools/world_build.py | 10 |
2 files changed, 21 insertions, 3 deletions
diff --git a/tests/test_world_build_output_lock.py b/tests/test_world_build_output_lock.py index 7b461a2f7..52f11014c 100644 --- a/tests/test_world_build_output_lock.py +++ b/tests/test_world_build_output_lock.py | |||
| @@ -105,6 +105,20 @@ class WorldBuildOutputLockTests(unittest.TestCase): | |||
| 105 | self.assertFalse(os.path.exists(os.path.join(out, "world.json"))) | 105 | self.assertFalse(os.path.exists(os.path.join(out, "world.json"))) |
| 106 | self.assertFalse(os.path.exists(os.path.join(out, "chunks"))) | 106 | self.assertFalse(os.path.exists(os.path.join(out, "chunks"))) |
| 107 | 107 | ||
| 108 | def test_case_and_unicode_variants_share_a_lock(self): | ||
| 109 | with tempfile.TemporaryDirectory() as tmp: | ||
| 110 | out = os.path.join(tmp, "Wörld") | ||
| 111 | alias = os.path.join(tmp, "wo\u0308RLD") | ||
| 112 | unrelated = os.path.join(tmp, "other") | ||
| 113 | with _lock_held_by_child(tmp, out): | ||
| 114 | with self.assertRaisesRegex(RuntimeError, "another world_build is using"): | ||
| 115 | with wb._exclusive_output(alias): | ||
| 116 | pass | ||
| 117 | with wb._exclusive_output(unrelated): | ||
| 118 | pass | ||
| 119 | with wb._exclusive_output(alias): | ||
| 120 | pass | ||
| 121 | |||
| 108 | def test_short_name_spelling_of_a_held_output_is_refused(self): | 122 | def test_short_name_spelling_of_a_held_output_is_refused(self): |
| 109 | if os.name != "nt": | 123 | if os.name != "nt": |
| 110 | self.skipTest("8.3 short names are Windows only") | 124 | self.skipTest("8.3 short names are Windows only") |
diff --git a/tools/world_build.py b/tools/world_build.py index 894c212fc..c67beed1d 100644 --- a/tools/world_build.py +++ b/tools/world_build.py | |||
| @@ -38,6 +38,7 @@ import stat | |||
| 38 | import statistics | 38 | import statistics |
| 39 | import sys | 39 | import sys |
| 40 | import tempfile | 40 | import tempfile |
| 41 | import unicodedata | ||
| 41 | 42 | ||
| 42 | try: | 43 | try: |
| 43 | import fcntl | 44 | import fcntl |
| @@ -1272,9 +1273,12 @@ def _exclusive_output(out_dir: str): | |||
| 1272 | # and a successful one must contain only chunks/ and world.json. Key it on the resolved | 1273 | # and a successful one must contain only chunks/ and world.json. Key it on the resolved |
| 1273 | # path so every spelling of one folder (8.3 names, junctions, symlinks) shares a lock; for | 1274 | # path so every spelling of one folder (8.3 names, junctions, symlinks) shares a lock; for |
| 1274 | # a folder that does not exist yet, realpath resolves the part that does and appends the rest. | 1275 | # a folder that does not exist yet, realpath resolves the part that does and appends the rest. |
| 1275 | key = hashlib.sha256( | 1276 | # Fold conservatively on every platform: the output volume may ignore case or |
| 1276 | os.path.normcase(os.path.realpath(out_dir)).encode("utf-8", "surrogatepass") | 1277 | # Unicode normalization even when os.path.normcase does not (APFS, for example). |
| 1277 | ).hexdigest()[:20] | 1278 | # Distinct folders that fold alike can contend, but cannot publish concurrently. |
| 1279 | resolved = os.path.normcase(os.path.realpath(out_dir)) | ||
| 1280 | lock_name = unicodedata.normalize("NFD", unicodedata.normalize("NFD", resolved).casefold()) | ||
| 1281 | key = hashlib.sha256(lock_name.encode("utf-8", "surrogatepass")).hexdigest()[:20] | ||
| 1278 | lock_path = os.path.join(tempfile.gettempdir(), _WORLD_BUILD_LOCK + key) | 1282 | lock_path = os.path.join(tempfile.gettempdir(), _WORLD_BUILD_LOCK + key) |
| 1279 | with open(lock_path, "a+b") as fh: | 1283 | with open(lock_path, "a+b") as fh: |
| 1280 | fd = fh.fileno() | 1284 | fd = fh.fileno() |