aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-node/src/meshbay_node/transport/wire.py
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-node/src/meshbay_node/transport/wire.py')
-rw-r--r--packages/meshbay-node/src/meshbay_node/transport/wire.py35
1 files changed, 27 insertions, 8 deletions
diff --git a/packages/meshbay-node/src/meshbay_node/transport/wire.py b/packages/meshbay-node/src/meshbay_node/transport/wire.py
index 77c5d62..75d27c1 100644
--- a/packages/meshbay-node/src/meshbay_node/transport/wire.py
+++ b/packages/meshbay-node/src/meshbay_node/transport/wire.py
@@ -32,6 +32,7 @@ from pathlib import Path
from meshbay_common import MNP_VERSION
from meshbay_common.groupbox import PURPOSE_INDEX, seal
+from meshbay_common.paths import long_path
from meshbay_common.protocol import MNP, index_entry_wire
from meshbay_node.roots import RootSet, off_disk
@@ -75,22 +76,40 @@ def _dir_roots(roots: RootSet) -> list[tuple[str, Path, bool]]:
def _walk_dirs(dir_roots: list[tuple[str, Path, bool]]) -> list[str]:
- """Blocking. Directories only: `os.walk` reads each one once and never
- stats a file, which `rglob("*")` and an `is_dir()` per entry did."""
+ """Blocking. Directories only: each is read once and no file is stat'ed,
+ which `rglob("*")` and an `is_dir()` per entry did. What `os.walk` did,
+ except that each directory is listed under its long form: `os.walk` skipped
+ whatever lay past Windows' MAX_PATH, without a word."""
out: list[str] = []
for name, path, available in dir_roots:
out.append(name)
if not available:
continue
- for top, subdirs, _files in os.walk(path):
- # Pruned in place: nothing under a hidden directory is listed either.
- subdirs[:] = [d for d in subdirs if not d.startswith(".")]
- rel = Path(top).relative_to(path)
- for d in subdirs:
- out.append(f"{name}/{(rel / d).as_posix()}")
+ pending = [path]
+ while pending:
+ top = pending.pop()
+ try:
+ with os.scandir(long_path(top)) as listing:
+ # Nothing under a hidden directory is listed either.
+ subdirs = [e for e in listing
+ if not e.name.startswith(".") and _is_dir(e)]
+ except OSError:
+ continue
+ rel = top.relative_to(path)
+ for entry in subdirs:
+ out.append(f"{name}/{(rel / entry.name).as_posix()}")
+ if not entry.is_symlink(): # listed, not entered, as os.walk did
+ pending.append(top / entry.name)
return sorted(out)[:MAX_DIRS]
+def _is_dir(entry: os.DirEntry) -> bool:
+ try:
+ return entry.is_dir()
+ except OSError:
+ return False
+
+
def index_sync_message(index, roots: RootSet | None, hidden=(),
dirs: list[str] | None = None) -> dict:
"""