Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(platform): 网络挂载掉线报读取失败,不再误报目录不存在或没有权限

longmeidao2c2f5f0847d8
diff --git a/src/peach/platform.py b/src/peach/platform.py
index 869bdb70f..d0e1f0a3c 100644
--- a/src/peach/platform.py
+++ b/src/peach/platform.py
@@ -110,6 +110,22 @@ def resolve_location(
return location, tail
+#: 设备未就绪与网络路径断开的 Win32 错误码。Python 把 21 映射成 `PermissionError`、
+#: 53/67 映射成 `FileNotFoundError`,只看异常类型会把掉线挂载报成没权限或目录不存在。
+_UNREACHABLE_WINERRORS = frozenset({
+ 21, # ERROR_NOT_READY
+ 51, # ERROR_REM_NOT_LIST
+ 53, # ERROR_BAD_NETPATH
+ 55, # ERROR_DEV_NOT_EXIST
+ 59, # ERROR_UNEXP_NET_ERR
+ 64, # ERROR_NETNAME_DELETED
+ 67, # ERROR_BAD_NET_NAME
+ 1167, # ERROR_DEVICE_NOT_CONNECTED
+ 1222, # ERROR_NO_NETWORK
+ 1231, # ERROR_NETWORK_UNREACHABLE
+})
+
+
def root_status(root: Path) -> str:
"""以目录首条读取判断挂载状态;空目录可读也算在线。"""
try:
@@ -119,13 +135,15 @@ def root_status(root: Path) -> str:
return 'ok'
with os.scandir(root) as entries:
next(iter(entries), None)
- except PermissionError:
- return "permission_denied"
- except FileNotFoundError:
- return "missing"
- except NotADirectoryError:
- return "not_directory"
- except OSError:
+ except OSError as error:
+ if getattr(error, "winerror", None) in _UNREACHABLE_WINERRORS:
+ return "unavailable"
+ if isinstance(error, PermissionError):
+ return "permission_denied"
+ if isinstance(error, FileNotFoundError):
+ return "missing"
+ if isinstance(error, NotADirectoryError):
+ return "not_directory"
return "unavailable"
return "ok"
diff --git a/tests/test_platform.py b/tests/test_platform.py
index 87a20cbca..7c36dcaee 100644
--- a/tests/test_platform.py
+++ b/tests/test_platform.py
@@ -334,6 +334,14 @@ class MountReachabilityTests(unittest.TestCase):
with patch("peach.platform.os.scandir", side_effect=error):
self.assertEqual(root_status(self.root), expected)
+ def test_disconnected_network_mount_is_unreachable_not_missing_or_denied(self):
+ # Windows 把网络路径断开(53/67)映射成 FileNotFoundError,设备未就绪(21)映射成 PermissionError。
+ for error_type, winerror in ((FileNotFoundError, 53), (FileNotFoundError, 67), (PermissionError, 21)):
+ error = error_type(2, "network")
+ error.winerror = winerror
+ with self.subTest(winerror=winerror), patch("peach.platform.os.scandir", side_effect=error):
+ self.assertEqual(root_status(self.root), "unavailable")
+
def test_failure_while_reading_the_first_entry_is_not_online(self):
class Entries:
def __enter__(self): return self