Skip to content

The agent that grows with you

fix(backup): skip non-regular filesystem entries

mrwanstudio947e027f610f
diff --git a/hermes_cli/backup.py b/hermes_cli/backup.py
index 834dafceb9b..f3ab2bcc9f4 100644
--- a/hermes_cli/backup.py
+++ b/hermes_cli/backup.py
@@ -228,11 +228,23 @@ def _iter_external_files(base: Path) -> List[Path]:
for dirpath, dirnames, filenames in os.walk(base, followlinks=False):
dirnames[:] = [d for d in dirnames if d not in _EXCLUDED_DIRS]
files.extend(fp for fp in (Path(dirpath) / f for f in filenames)
- if not (fp.is_symlink() or fp.name in _EXCLUDED_NAMES
+ if not (_is_non_regular_path(fp) or fp.name in _EXCLUDED_NAMES
or fp.name.endswith(_EXCLUDED_SUFFIXES)))
return files
+def _is_non_regular_path(path: Path) -> bool:
+ """True for symlinks, sockets, devices, and other non-regular filesystem entries.
+
+ A failed ``lstat`` is not treated as an exclusion: the archive writer must see the path and
+ report the read failure instead of silently claiming a complete backup.
+ """
+ try:
+ return not stat.S_ISREG(path.lstat().st_mode)
+ except OSError:
+ return False
+
+
def _should_exclude(rel_path: Path) -> bool:
"""Return True if *rel_path* (relative to hermes root) should be skipped."""
parts = rel_path.parts
@@ -267,7 +279,7 @@ def _iter_backup_files(hermes_root: Path, out_path: Path, skipped_dirs: Optional
fpath = hermes_root / rel
# zipfile.write() follows file symlinks, so skip links before any archive write can
# copy data from outside HERMES_HOME; never archive the output zip into itself.
- if _should_exclude(rel) or fpath.is_symlink():
+ if _should_exclude(rel) or _is_non_regular_path(fpath):
continue
with suppress(OSError, ValueError):
if fpath.resolve() == out_path.resolve():
diff --git a/tests/hermes_cli/test_backup.py b/tests/hermes_cli/test_backup.py
index 405fb1aa5a7..9a1cc9c4718 100644
--- a/tests/hermes_cli/test_backup.py
+++ b/tests/hermes_cli/test_backup.py
@@ -2,6 +2,7 @@
import json
import os
+import socket
import sqlite3
import stat
import zipfile
@@ -277,6 +278,20 @@ class TestIterBackupFiles:
assert "models" in skipped
assert "hermes-agent" in skipped
+ @pytest.mark.linux_only
+ def test_skips_unix_sockets(self, tmp_path):
+ from hermes_cli.backup import _iter_backup_files
+
+ root = tmp_path / ".hermes"
+ root.mkdir()
+ socket_path = root / "gateway.sock"
+ with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as gateway_socket:
+ gateway_socket.bind(str(socket_path))
+
+ selected = {str(rel) for _, rel in _iter_backup_files(root, tmp_path / "out.zip")}
+
+ assert "gateway.sock" not in selected
+
# ---------------------------------------------------------------------------
# Backup tests