feat(device-daemon): add inotify watcher, adaptive wake uploader, and IPC daemon

This commit is contained in:
2026-11-18 07:39:38 -05:00
parent 69224f3929
commit da4d963919
7 changed files with 2306 additions and 0 deletions

View File

@@ -9,23 +9,34 @@ from .config import (
remove_watched_folder,
save_daemon_config,
)
from .daemon import SyncDaemon
from .ipc import IpcServer, default_sock_path
from .store import (
SyncStore,
compute_sha256,
default_db_path,
is_media_file,
)
from .uploader import Uploader
from .watcher import InotifyWatcher, PollingWatcher, scan_folder
__all__ = [
"DaemonConfig",
"InotifyWatcher",
"IpcServer",
"PollingWatcher",
"SyncDaemon",
"SyncStore",
"Uploader",
"WatchedFolder",
"add_watched_folder",
"compute_sha256",
"default_config_path",
"default_db_path",
"default_sock_path",
"is_media_file",
"load_daemon_config",
"remove_watched_folder",
"save_daemon_config",
"scan_folder",
]

View File

@@ -0,0 +1,307 @@
"""Orchestrator daemon wiring watcher, SQLite store, uploader, and IPC server."""
from __future__ import annotations
import logging
import os
import threading
from dataclasses import replace
from pathlib import Path
from typing import Any
from .config import (
DEFAULT_TARGET_PORT,
VALID_THEMES,
DaemonConfig,
WatchedFolder,
add_watched_folder,
default_config_path,
load_daemon_config,
remove_watched_folder,
save_daemon_config,
)
from .ipc import IpcServer, default_sock_path
from .store import SyncStore, compute_sha256, default_db_path, is_media_file
from .uploader import Uploader
from .watcher import InotifyWatcher, scan_folder
logger = logging.getLogger(__name__)
def _safe_int(value: str | None, default: int = 0) -> int:
if value is None:
return default
try:
return int(value)
except ValueError:
return default
class SyncDaemon:
"""On-device CardputerZero photo sync daemon."""
def __init__(
self,
config_path: Path | None = None,
db_path: Path | None = None,
sock_path: Path | None = None,
probe_timeout: float = 3.0,
watcher_poll_interval: float = 0.5,
) -> None:
self.config_path = Path(config_path).expanduser() if config_path is not None else default_config_path()
self.db_path = Path(db_path).expanduser() if db_path is not None else default_db_path()
self.sock_path = Path(sock_path).expanduser() if sock_path is not None else default_sock_path()
self._config_lock = threading.RLock()
self.config: DaemonConfig = load_daemon_config(self.config_path)
self.store = SyncStore(self.db_path)
self.wake_event = threading.Event()
self.uploader = Uploader(
store=self.store,
get_config=self.get_config,
wake_event=self.wake_event,
probe_timeout=probe_timeout,
)
self.watcher = InotifyWatcher(
on_file_ready=self._on_file_ready,
on_dir_created=self._on_dir_created,
poll_interval=watcher_poll_interval,
)
self.ipc_server = IpcServer(self.sock_path, self.handle_ipc)
self._started = False
def get_config(self) -> DaemonConfig:
"""Return the current DaemonConfig safely across threads."""
with self._config_lock:
return self.config
def scan_all(self) -> int:
"""Recursively scan all enabled watched folders and enqueue unseen media files."""
cfg = self.get_config()
total = 0
for folder in cfg.watched_folders:
total += scan_folder(folder, cfg, self.store)
if total > 0:
self.uploader.notify_new_file()
return total
def _on_file_ready(self, folder: WatchedFolder, file_path: Path) -> None:
cfg = self.get_config()
active = next((wf for wf in cfg.watched_folders if wf.id == folder.id and wf.enabled), None)
if active is None:
return
if not is_media_file(file_path, cfg, store=self.store):
return
root = Path(os.path.expanduser(active.path))
try:
st = file_path.stat()
if st.st_size <= 0:
return
sha256 = compute_sha256(file_path)
rel_path = file_path.relative_to(root).as_posix()
except (OSError, ValueError):
return
outcome = self.store.enqueue_file(
active.id,
str(file_path),
rel_path,
st.st_size,
st.st_mtime_ns,
sha256,
)
if outcome == "enqueued":
self.uploader.notify_new_file()
def _on_dir_created(self, folder: WatchedFolder, _dir_path: Path) -> None:
cfg = self.get_config()
active = next((wf for wf in cfg.watched_folders if wf.id == folder.id and wf.enabled), None)
if active is None:
return
added = scan_folder(active, cfg, self.store)
if added > 0:
self.uploader.notify_new_file()
def start(self) -> None:
"""Start watcher, initial scan, IPC server, and uploader loop."""
if self._started:
return
self._started = True
self.store.log("INFO", "cp0-syncd started")
self.watcher.sync_folders(self.get_config().watched_folders)
self.watcher.start()
self.scan_all()
self.ipc_server.start()
self.uploader.start()
def stop(self) -> None:
"""Stop IPC server, watcher, uploader, and close the SQLite store."""
self.ipc_server.stop()
self.watcher.stop()
self.uploader.stop()
self.store.close()
self._started = False
def run_forever(self, stop_event: threading.Event | None = None) -> None:
"""Run the daemon until stop_event is set."""
waiter = stop_event if stop_event is not None else threading.Event()
self.start()
try:
waiter.wait()
finally:
self.stop()
def handle_ipc(self, req: dict[str, Any]) -> dict[str, Any]:
"""Dispatch a parsed IPC JSON request dict and return a JSON-serializable response."""
cmd = req.get("cmd")
if not isinstance(cmd, str) or not cmd:
return {"ok": False, "error": "Missing 'cmd' field"}
if cmd == "status":
return self._cmd_status()
if cmd == "sync_now":
return self._cmd_sync_now()
if cmd == "add_folder":
return self._cmd_add_folder(req)
if cmd == "remove_folder":
return self._cmd_remove_folder(req)
if cmd == "set_target":
return self._cmd_set_target(req)
if cmd == "set_theme":
return self._cmd_set_theme(req)
if cmd == "retry_failed":
return self._cmd_retry_failed()
return {"ok": False, "error": f"Unknown command: {cmd}"}
def _cmd_status(self) -> dict[str, Any]:
cfg = self.get_config()
snap = self.store.get_status_snapshot(cfg.watched_folders)
meta: dict[str, str] = snap["meta"]
counts: dict[str, int] = snap["counts"]
return {
"ok": True,
"target_host": cfg.target_host,
"target_port": cfg.target_port,
"theme": cfg.theme,
"mac_online": meta.get("mac_online") == "true",
"mac_dest_root": meta.get("mac_dest_root", ""),
"syncing": self.uploader.syncing,
"current_file": self.uploader.current_file,
"pending_count": counts["pending"],
"pending_bytes": counts["pending_bytes"],
"synced_count": counts["synced"],
"failed_count": counts["failed"],
"next_retry_sec": self.uploader.next_retry_sec(),
"last_sync_ts": _safe_int(meta.get("last_sync_ts"), 0),
"last_synced_file": meta.get("last_synced_file", ""),
"last_error": meta.get("last_error", ""),
"folders": [
{
"id": str(item["id"]),
"path": str(item["path"]),
"label": str(item["label"]),
"enabled": bool(item["enabled"]),
"exists": bool(item["exists"]),
"pending": int(item["pending"]),
"synced": int(item["synced"]),
}
for item in snap["folders"]
],
"logs": snap["logs"],
}
def _cmd_sync_now(self) -> dict[str, Any]:
added = self.scan_all()
self.uploader.wake(reset_backoff=True, probe=True)
return {"ok": True, "enqueued": added}
def _cmd_add_folder(self, req: dict[str, Any]) -> dict[str, Any]:
path = req.get("path")
label = req.get("label")
if label is not None and not isinstance(label, str):
return {"ok": False, "error": "Folder label must be a string"}
with self._config_lock:
try:
new_cfg, folder = add_watched_folder(self.config, path, label=label) # type: ignore[arg-type]
except ValueError as exc:
return {"ok": False, "error": str(exc)}
save_daemon_config(new_cfg, self.config_path)
self.config = new_cfg
self.watcher.add_folder(folder)
added = scan_folder(folder, new_cfg, self.store)
self.store.log("INFO", f"Added watched folder: {folder.path} ({folder.label})")
self.uploader.wake(reset_backoff=True, probe=False)
return {
"ok": True,
"folder": {
"id": folder.id,
"path": folder.path,
"label": folder.label,
"enabled": folder.enabled,
},
"enqueued": added,
}
def _cmd_remove_folder(self, req: dict[str, Any]) -> dict[str, Any]:
folder_id = req.get("folder_id")
if not isinstance(folder_id, str) or not folder_id.strip():
return {"ok": False, "error": "Missing or invalid 'folder_id'"}
with self._config_lock:
if not any(wf.id == folder_id for wf in self.config.watched_folders):
return {"ok": False, "error": f"Unknown folder_id: {folder_id}"}
new_cfg = remove_watched_folder(self.config, folder_id)
save_daemon_config(new_cfg, self.config_path)
self.config = new_cfg
self.watcher.remove_folder(folder_id)
purged = self.store.purge_pending_for_folder(folder_id)
self.store.log("INFO", f"Removed watched folder {folder_id} (purged {purged} pending)")
return {"ok": True, "purged": purged}
def _cmd_set_target(self, req: dict[str, Any]) -> dict[str, Any]:
raw_host = req.get("host")
if not isinstance(raw_host, str):
return {"ok": False, "error": "Target host must be a string"}
host = raw_host.strip()
if not host or any(ch.isspace() for ch in host) or "/" in host:
return {"ok": False, "error": "Invalid target host"}
port = req.get("port", DEFAULT_TARGET_PORT)
if not isinstance(port, int) or isinstance(port, bool) or not (1 <= port <= 65535):
return {"ok": False, "error": "Target port must be an integer between 1 and 65535"}
with self._config_lock:
new_cfg = replace(self.config, target_host=host, target_port=port)
save_daemon_config(new_cfg, self.config_path)
self.config = new_cfg
self.store.set_meta("mac_online", "false")
self.store.set_meta("mac_dest_root", "")
self.store.set_meta("last_error", "")
self.store.log("INFO", f"Updated target to {host}:{port}")
self.uploader.wake(reset_backoff=True, probe=True)
return {"ok": True, "target_host": host, "target_port": port}
def _cmd_set_theme(self, req: dict[str, Any]) -> dict[str, Any]:
theme = req.get("theme")
if not isinstance(theme, str) or theme not in VALID_THEMES:
return {"ok": False, "error": "Theme must be 'dark' or 'light'"}
with self._config_lock:
new_cfg = replace(self.config, theme=theme)
save_daemon_config(new_cfg, self.config_path)
self.config = new_cfg
return {"ok": True, "theme": theme}
def _cmd_retry_failed(self) -> dict[str, Any]:
retried = self.store.retry_all_failed()
if retried > 0:
self.store.log("INFO", f"Queued {retried} failed files for retry")
self.uploader.wake(reset_backoff=True, probe=True)
return {"ok": True, "retried": retried}

View File

@@ -0,0 +1,137 @@
"""Newline-delimited JSON Unix domain socket IPC server for cp0-syncd."""
from __future__ import annotations
import json
import logging
import os
import socket
import threading
from pathlib import Path
from typing import Any, Callable
logger = logging.getLogger(__name__)
ENV_SOCK_PATH = "CP0_SYNC_SOCK"
def default_sock_path() -> Path:
"""Return the default Unix socket path, honoring CP0_SYNC_SOCK then XDG_RUNTIME_DIR."""
override = os.environ.get(ENV_SOCK_PATH, "").strip()
if override:
return Path(override).expanduser()
runtime_dir = os.environ.get("XDG_RUNTIME_DIR", "").strip() or "/tmp"
return Path(runtime_dir) / "cp0-sync.sock"
class IpcServer:
"""AF_UNIX stream server speaking newline-delimited JSON commands."""
def __init__(
self,
sock_path: Path,
handler: Callable[[dict[str, Any]], dict[str, Any]],
) -> None:
self.sock_path = Path(sock_path).expanduser()
self._handler = handler
self._stop_event = threading.Event()
self._sock: socket.socket | None = None
self._accept_thread: threading.Thread | None = None
def start(self) -> None:
"""Unlink any stale socket, bind AF_UNIX socket, and start accept loop."""
if self._accept_thread is not None and self._accept_thread.is_alive():
return
self.sock_path.parent.mkdir(parents=True, exist_ok=True)
try:
os.unlink(self.sock_path)
except FileNotFoundError:
pass
srv_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
srv_sock.bind(str(self.sock_path))
os.chmod(self.sock_path, 0o600)
srv_sock.listen(16)
srv_sock.settimeout(0.2)
self._sock = srv_sock
self._stop_event.clear()
self._accept_thread = threading.Thread(
target=self._accept_loop,
name="cp0-ipc-server",
daemon=True,
)
self._accept_thread.start()
def stop(self) -> None:
"""Stop the IPC accept loop, close socket, and unlink socket path."""
self._stop_event.set()
if self._sock is not None:
try:
self._sock.close()
except OSError:
pass
self._sock = None
if self._accept_thread is not None and self._accept_thread is not threading.current_thread():
self._accept_thread.join(timeout=2.0)
self._accept_thread = None
try:
os.unlink(self.sock_path)
except FileNotFoundError:
pass
def _accept_loop(self) -> None:
while not self._stop_event.is_set() and self._sock is not None:
try:
conn, _ = self._sock.accept()
except socket.timeout:
continue
except OSError:
if self._stop_event.is_set():
break
continue
client_thread = threading.Thread(
target=self._serve_client,
args=(conn,),
name="cp0-ipc-client",
daemon=True,
)
client_thread.start()
def _serve_client(self, conn: socket.socket) -> None:
conn.settimeout(2.0)
try:
with conn:
with conn.makefile("r", encoding="utf-8", errors="replace") as reader:
for raw_line in reader:
line = raw_line.strip()
if not line:
continue
response = self._dispatch_line(line)
payload = (json.dumps(response, separators=(",", ":")) + "\n").encode("utf-8")
conn.sendall(payload)
except (OSError, TimeoutError):
pass
def _dispatch_line(self, line: str) -> dict[str, Any]:
try:
req = json.loads(line)
except json.JSONDecodeError as exc:
return {"ok": False, "error": f"Invalid JSON: {exc}"}
if not isinstance(req, dict):
return {"ok": False, "error": "Request must be a JSON object"}
try:
resp = self._handler(req)
except Exception as exc:
logger.exception("IPC handler raised exception")
return {"ok": False, "error": str(exc)}
if not isinstance(resp, dict):
return {"ok": False, "error": "Internal handler returned invalid response"}
return resp

View File

@@ -0,0 +1,374 @@
"""Adaptive wake-probe HTTP uploader for CardputerZero photos."""
from __future__ import annotations
import hashlib
import http.client
import json
import logging
import math
import os
import threading
import time
from http import HTTPStatus
from pathlib import Path
from typing import Callable
from urllib.parse import quote
from .config import DaemonConfig, WatchedFolder
from .store import SyncStore
logger = logging.getLogger(__name__)
BACKOFF_SCHEDULE = (5, 10, 20, 40, 60)
CHUNK_SIZE = 65536
PERMANENT_FAIL_STATUSES = frozenset({400, 413, 422})
def _extract_error_message(body: bytes) -> str:
if not body:
return ""
try:
parsed = json.loads(body.decode("utf-8"))
if isinstance(parsed, dict) and "error" in parsed:
return str(parsed["error"])
except (UnicodeDecodeError, json.JSONDecodeError):
pass
return body.decode("utf-8", errors="replace").strip()[:200]
def _hash_open_file(handle: object) -> str:
digest = hashlib.sha256()
handle.seek(0) # type: ignore[attr-defined]
while True:
chunk = handle.read(CHUNK_SIZE) # type: ignore[attr-defined]
if not chunk:
break
digest.update(chunk)
handle.seek(0) # type: ignore[attr-defined]
return digest.hexdigest()
class Uploader:
"""Sequential FIFO photo uploader with /health wake-probe and adaptive backoff."""
def __init__(
self,
store: SyncStore,
get_config: Callable[[], DaemonConfig],
wake_event: threading.Event,
probe_timeout: float = 3.0,
upload_timeout: float = 30.0,
) -> None:
self._store = store
self._get_config = get_config
self._wake_event = wake_event
self._probe_timeout = float(probe_timeout)
self._upload_timeout = float(upload_timeout)
self._state_lock = threading.Lock()
self._process_lock = threading.Lock()
self._stop_event = threading.Event()
self._thread: threading.Thread | None = None
self._syncing = False
self._current_file = ""
self._backoff_index = 0
self._current_delay = 0
self._next_retry_deadline = 0.0
self._probe_requested = False
self._logged_no_target = False
@property
def syncing(self) -> bool:
"""Return True while actively uploading or probing."""
with self._state_lock:
return self._syncing
@property
def current_file(self) -> str:
"""Return the relative path of the file currently being uploaded, or ''."""
with self._state_lock:
return self._current_file
def next_retry_sec(self) -> int:
"""Return whole seconds remaining until the next backoff retry, or 0."""
with self._state_lock:
if self._next_retry_deadline <= 0.0:
return 0
remaining = self._next_retry_deadline - time.monotonic()
return max(0, int(math.ceil(remaining))) if remaining > 0 else 0
def notify_new_file(self) -> None:
"""Reset retry backoff and wake the uploader when a new photo arrives."""
self.wake(reset_backoff=True, probe=False)
def wake(self, reset_backoff: bool = True, probe: bool = False) -> None:
"""Interrupt backoff sleep immediately and optionally trigger a /health probe."""
with self._state_lock:
if reset_backoff:
self._backoff_index = 0
self._current_delay = 0
self._next_retry_deadline = 0.0
if probe:
self._probe_requested = True
if reset_backoff:
self._store.set_meta("next_retry_ts", "0")
self._wake_event.set()
def start(self) -> None:
"""Start the background upload loop thread."""
if self._thread is not None and self._thread.is_alive():
return
self._stop_event.clear()
cfg = self._get_config()
if cfg.target_host.strip():
with self._state_lock:
self._probe_requested = True
self._wake_event.set()
self._thread = threading.Thread(target=self._run_loop, name="cp0-uploader", daemon=True)
self._thread.start()
def stop(self) -> None:
"""Stop the background upload loop cleanly."""
self._stop_event.set()
self._wake_event.set()
if self._thread is not None and self._thread is not threading.current_thread():
self._thread.join(timeout=5.0)
self._thread = None
def _run_loop(self) -> None:
while not self._stop_event.is_set():
with self._state_lock:
probe = self._probe_requested
self._probe_requested = False
self._wake_event.clear()
has_pending = self._store.next_pending() is not None
if has_pending or probe:
result = self.process_once(probe_when_empty=probe)
else:
result = "idle"
if self._stop_event.is_set():
break
if self._wake_event.is_set():
continue
if result in ("offline", "paused"):
with self._state_lock:
wait_sec = max(1, self._current_delay)
self._wake_event.wait(timeout=wait_sec)
else:
self._wake_event.wait(timeout=60.0)
def _reset_backoff(self) -> None:
with self._state_lock:
self._backoff_index = 0
self._current_delay = 0
self._next_retry_deadline = 0.0
self._store.set_meta("next_retry_ts", "0")
def _record_backoff(self) -> int:
with self._state_lock:
delay = BACKOFF_SCHEDULE[min(self._backoff_index, len(BACKOFF_SCHEDULE) - 1)]
if self._backoff_index < len(BACKOFF_SCHEDULE) - 1:
self._backoff_index += 1
self._current_delay = delay
self._next_retry_deadline = time.monotonic() + delay
next_ms = int(time.time() * 1000) + delay * 1000
self._store.set_meta("next_retry_ts", str(next_ms))
return delay
def _set_syncing(self, syncing: bool, current_file: str = "") -> None:
with self._state_lock:
self._syncing = syncing
self._current_file = current_file
def _probe_health(self, host: str, port: int) -> tuple[bool, str]:
conn: http.client.HTTPConnection | None = None
try:
conn = http.client.HTTPConnection(host, port, timeout=self._probe_timeout)
conn.request("GET", "/health", headers={"Connection": "close"})
resp = conn.getresponse()
body = resp.read()
except (OSError, http.client.HTTPException, TimeoutError) as exc:
return (False, str(exc) or exc.__class__.__name__)
finally:
if conn is not None:
conn.close()
if resp.status != HTTPStatus.OK:
detail = _extract_error_message(body)
return (False, f"HTTP {resp.status}: {detail}" if detail else f"HTTP {resp.status}")
try:
data = json.loads(body.decode("utf-8"))
except (UnicodeDecodeError, json.JSONDecodeError) as exc:
return (False, f"Invalid /health JSON: {exc}")
if not isinstance(data, dict) or data.get("status") != "ok":
return (False, "Unexpected /health payload")
dest_root = data.get("dest_root", "")
return (True, str(dest_root) if isinstance(dest_root, str) else "")
def process_once(self, probe_when_empty: bool = False) -> str:
"""Probe Mac /health if needed and upload queued PENDING files sequentially."""
with self._process_lock:
cfg = self._get_config()
host = cfg.target_host.strip()
if not host:
self._set_syncing(False, "")
self._reset_backoff()
self._store.set_meta("mac_online", "false")
if not self._logged_no_target:
self._store.log("INFO", "No target host configured")
self._logged_no_target = True
return "no_target"
self._logged_no_target = False
first_row = self._store.next_pending()
if first_row is None and not probe_when_empty:
self._reset_backoff()
return "idle"
ok, detail = self._probe_health(host, cfg.target_port)
self._store.set_meta("last_probe_ts", str(int(time.time() * 1000)))
if not ok:
self._set_syncing(False, "")
self._store.set_meta("mac_online", "false")
self._store.set_meta("last_error", detail)
self._record_backoff()
return "offline"
self._reset_backoff()
self._store.set_meta("mac_online", "true")
self._store.set_meta("mac_dest_root", detail)
if first_row is None:
self._store.set_meta("last_error", "")
return "online"
try:
while not self._stop_event.is_set():
cfg = self._get_config()
host = cfg.target_host.strip()
if not host:
return "no_target"
folders_by_id = {wf.id: wf for wf in cfg.watched_folders}
row = self._store.next_pending()
if row is None:
return "synced"
folder = folders_by_id.get(str(row["folder_id"]))
if folder is None:
self._store.purge_pending_for_folder(str(row["folder_id"]))
continue
outcome = self._upload_row(row, folder, host, cfg.target_port)
if outcome in ("paused", "offline"):
return outcome
return "stopped"
finally:
self._set_syncing(False, "")
def _upload_row(
self,
row: dict[str, object],
folder: WatchedFolder,
host: str,
port: int,
) -> str:
file_id = int(row["id"]) # type: ignore[arg-type]
rel_path = str(row["rel_path"])
abs_path = Path(str(row["abs_path"]))
try:
handle = open(abs_path, "rb")
except FileNotFoundError:
self._store.delete_file(file_id)
self._store.log("WARN", f"Removed missing queued file: {rel_path}")
return "missing"
except OSError as exc:
err = f"Read error for {rel_path}: {exc}"
self._store.mark_retry(file_id, err)
self._store.set_meta("last_error", err)
self._store.log("WARN", err)
self._record_backoff()
return "paused"
with handle:
st = os.fstat(handle.fileno())
if st.st_size <= 0:
self._store.delete_file(file_id)
self._store.log("WARN", f"Removed 0-byte queued file: {rel_path}")
return "missing"
if st.st_size != int(row["size_bytes"]) or st.st_mtime_ns != int(row["mtime_ns"]): # type: ignore[arg-type]
sha256 = _hash_open_file(handle)
else:
sha256 = str(row["sha256"])
size_bytes = st.st_size
mtime_sec = st.st_mtime_ns / 1_000_000_000
self._set_syncing(True, rel_path)
self._store.mark_uploading(file_id)
conn: http.client.HTTPConnection | None = None
try:
conn = http.client.HTTPConnection(host, port, timeout=self._upload_timeout)
conn.putrequest("POST", "/upload")
conn.putheader("Content-Length", str(size_bytes))
conn.putheader("X-CP0-Folder-Label", quote(folder.label, safe=""))
conn.putheader("X-CP0-Rel-Path", quote(rel_path, safe="/"))
conn.putheader("X-CP0-SHA256", sha256)
conn.putheader("X-CP0-Mtime", f"{mtime_sec:.6f}")
conn.putheader("Connection", "close")
conn.endheaders()
sent = 0
while sent < size_bytes:
chunk = handle.read(min(CHUNK_SIZE, size_bytes - sent))
if not chunk:
raise OSError("File truncated while streaming upload")
conn.send(chunk)
sent += len(chunk)
resp = conn.getresponse()
body = resp.read()
except (OSError, http.client.HTTPException, TimeoutError) as exc:
err = f"Upload network error ({rel_path}): {exc}"
self._store.mark_retry(file_id, err)
self._store.set_meta("mac_online", "false")
self._store.set_meta("last_error", err)
self._store.log("WARN", err)
self._record_backoff()
return "offline"
finally:
if conn is not None:
conn.close()
if 200 <= resp.status < 300:
self._store.mark_synced(file_id)
self._store.set_meta("last_sync_ts", str(int(time.time() * 1000)))
self._store.set_meta("last_synced_file", rel_path)
self._store.set_meta("last_error", "")
self._reset_backoff()
self._store.log("INFO", f"Synced {folder.label}/{rel_path}")
return "synced"
detail = _extract_error_message(body)
err = f"HTTP {resp.status}: {detail}" if detail else f"HTTP {resp.status}"
if resp.status in PERMANENT_FAIL_STATUSES:
self._store.mark_failed(file_id, err)
self._store.set_meta("last_error", err)
self._store.log("ERROR", f"Rejected {rel_path}: {err}")
return "failed"
self._store.mark_retry(file_id, err)
self._store.set_meta("last_error", err)
self._store.log("ERROR", f"Paused on {rel_path}: {err}")
self._record_backoff()
return "paused"

View File

@@ -0,0 +1,660 @@
"""Recursive startup scanner, Linux ctypes inotify watcher, and polling fallback."""
from __future__ import annotations
import ctypes
import ctypes.util
import errno
import logging
import os
import select
import struct
import sys
import threading
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Callable
from .config import DaemonConfig, WatchedFolder
from .store import SyncStore, compute_sha256, is_media_file
logger = logging.getLogger(__name__)
# Linux <sys/inotify.h> event masks
IN_CLOSE_WRITE = 0x00000008
IN_MOVED_TO = 0x00000080
IN_CREATE = 0x00000100
IN_DELETE = 0x00000200
IN_DELETE_SELF = 0x00000400
IN_MOVE_SELF = 0x00000800
IN_IGNORED = 0x00008000
IN_ISDIR = 0x40000000
_DIR_WATCH_MASK = IN_CLOSE_WRITE | IN_MOVED_TO | IN_CREATE | IN_DELETE_SELF | IN_MOVE_SELF
_ANCESTOR_WATCH_MASK = IN_CREATE | IN_MOVED_TO | IN_DELETE_SELF | IN_MOVE_SELF
_EVENT_HEADER = struct.Struct("=iIII")
_READ_BUFFER_SIZE = 65536
@dataclass(frozen=True)
class InotifyEvent:
"""Decoded Linux struct inotify_event record."""
wd: int
mask: int
cookie: int
name: str
def parse_inotify_events(data: bytes) -> list[InotifyEvent]:
"""Decode zero or more packed Linux struct inotify_event entries from raw bytes."""
events: list[InotifyEvent] = []
offset = 0
total = len(data)
header_size = _EVENT_HEADER.size
while offset + header_size <= total:
wd, mask, cookie, name_len = _EVENT_HEADER.unpack_from(data, offset)
offset += header_size
if name_len > 0:
end = min(offset + name_len, total)
raw_name = data[offset:end].split(b"\x00", 1)[0]
name = os.fsdecode(raw_name)
offset += name_len
else:
name = ""
events.append(InotifyEvent(wd=wd, mask=mask, cookie=cookie, name=name))
return events
def scan_folder(folder: WatchedFolder, cfg: DaemonConfig, store: SyncStore) -> int:
"""Recursively scan an enabled watched folder and enqueue new media files."""
if not folder.enabled:
return 0
root = Path(os.path.expanduser(folder.path))
if not root.is_dir():
return 0
enqueued = 0
ignore_prefixes = [prefix for prefix in cfg.ignore_prefixes if prefix]
for dirpath, dirnames, filenames in os.walk(root, followlinks=False):
dirnames[:] = sorted(
name
for name in dirnames
if not any(name.startswith(prefix) for prefix in ignore_prefixes)
)
current_dir = Path(dirpath)
for filename in sorted(filenames):
full_path = current_dir / filename
if not is_media_file(full_path, cfg, store=store):
continue
try:
st = full_path.stat()
if st.st_size <= 0:
continue
sha256 = compute_sha256(full_path)
rel_path = full_path.relative_to(root).as_posix()
except (OSError, ValueError):
continue
outcome = store.enqueue_file(
folder.id,
str(full_path),
rel_path,
st.st_size,
st.st_mtime_ns,
sha256,
)
if outcome == "enqueued":
enqueued += 1
return enqueued
def _load_linux_libc() -> Any | None:
"""Load Linux libc.so.6 with inotify symbols, or return None on unsupported OS."""
if not sys.platform.startswith("linux"):
return None
candidates = ["libc.so.6"]
found = ctypes.util.find_library("c")
if found and found not in candidates:
candidates.append(found)
for candidate in candidates:
try:
libc = ctypes.CDLL(candidate, use_errno=True)
if (
hasattr(libc, "inotify_init1")
and hasattr(libc, "inotify_add_watch")
and hasattr(libc, "inotify_rm_watch")
):
libc.inotify_init1.argtypes = [ctypes.c_int]
libc.inotify_init1.restype = ctypes.c_int
libc.inotify_add_watch.argtypes = [
ctypes.c_int,
ctypes.c_char_p,
ctypes.c_uint32,
]
libc.inotify_add_watch.restype = ctypes.c_int
libc.inotify_rm_watch.argtypes = [ctypes.c_int, ctypes.c_int]
libc.inotify_rm_watch.restype = ctypes.c_int
return libc
except OSError:
continue
return None
def _nearest_existing_ancestor(target: Path) -> Path:
"""Return target or its nearest existing ancestor directory."""
candidate = target
while True:
if candidate.is_dir():
return candidate
parent = candidate.parent
if parent == candidate:
return Path("/")
candidate = parent
def _iter_subdirs(root: Path) -> list[Path]:
"""Yield root and all non-symlink subdirectories beneath root."""
dirs: list[Path] = []
if not root.is_dir():
return dirs
for dirpath, dirnames, _ in os.walk(root, followlinks=False):
dirnames[:] = sorted(d for d in dirnames if not d.startswith("."))
dirs.append(Path(dirpath))
return dirs
class PollingWatcher:
"""Portable mtime/size polling watcher fallback for non-Linux hosts."""
def __init__(
self,
on_file_ready: Callable[[WatchedFolder, Path], None],
on_dir_created: Callable[[WatchedFolder, Path], None] | None = None,
poll_interval: float = 0.5,
) -> None:
self._on_file_ready = on_file_ready
self._on_dir_created = on_dir_created
self._poll_interval = max(0.02, float(poll_interval))
self._lock = threading.RLock()
self._stop_event = threading.Event()
self._thread: threading.Thread | None = None
self._folders: dict[str, WatchedFolder] = {}
self._snapshots: dict[str, dict[str, Any]] = {}
def sync_folders(self, folders: list[WatchedFolder]) -> None:
with self._lock:
target_ids = {wf.id for wf in folders if wf.enabled}
for existing_id in list(self._folders.keys()):
if existing_id not in target_ids:
self._folders.pop(existing_id, None)
self._snapshots.pop(existing_id, None)
for wf in folders:
if wf.enabled:
if wf.id not in self._folders or self._folders[wf.id].path != wf.path:
self._folders[wf.id] = wf
self._snapshots[wf.id] = self._build_baseline(wf)
else:
self._folders[wf.id] = wf
def add_folder(self, folder: WatchedFolder) -> None:
with self._lock:
if not folder.enabled:
return
self._folders[folder.id] = folder
self._snapshots[folder.id] = self._build_baseline(folder)
def remove_folder(self, folder_id: str) -> None:
with self._lock:
self._folders.pop(folder_id, None)
self._snapshots.pop(folder_id, None)
def _build_baseline(self, folder: WatchedFolder) -> dict[str, Any]:
root = Path(os.path.expanduser(folder.path))
if not root.is_dir():
return {"present": False, "dirs": set(), "files": {}}
dirs, files = self._scan_tree(root)
return {"present": True, "dirs": dirs, "files": files}
@staticmethod
def _scan_tree(root: Path) -> tuple[set[Path], dict[Path, tuple[int, int]]]:
dirs: set[Path] = set()
files: dict[Path, tuple[int, int]] = {}
for dirpath, dirnames, filenames in os.walk(root, followlinks=False):
dirnames[:] = sorted(d for d in dirnames if not d.startswith("."))
cur = Path(dirpath)
dirs.add(cur)
for name in filenames:
fpath = cur / name
try:
st = fpath.stat()
except OSError:
continue
files[fpath] = (st.st_size, st.st_mtime_ns)
return dirs, files
def poll_once(self) -> None:
events_dirs: list[tuple[WatchedFolder, Path]] = []
events_files: list[tuple[WatchedFolder, Path]] = []
with self._lock:
items = list(self._folders.items())
for folder_id, folder in items:
root = Path(os.path.expanduser(folder.path))
is_present = root.is_dir()
with self._lock:
snap = self._snapshots.get(folder_id)
if snap is None:
continue
was_present = bool(snap["present"])
if not is_present:
snap["present"] = False
snap["dirs"] = set()
snap["files"] = {}
continue
dirs, files = self._scan_tree(root)
prev_dirs: set[Path] = snap["dirs"]
prev_files: dict[Path, tuple[int, int]] = snap["files"]
if not was_present:
events_dirs.append((folder, root))
for new_dir in sorted(dirs - prev_dirs):
if new_dir != root:
events_dirs.append((folder, new_dir))
for fpath, sig in sorted(files.items()):
if prev_files.get(fpath) != sig:
events_files.append((folder, fpath))
snap["present"] = True
snap["dirs"] = dirs
snap["files"] = files
if self._on_dir_created is not None:
for folder, dir_path in events_dirs:
self._on_dir_created(folder, dir_path)
for folder, file_path in events_files:
self._on_file_ready(folder, file_path)
def start(self) -> None:
if self._thread is not None and self._thread.is_alive():
return
self._stop_event.clear()
self._thread = threading.Thread(target=self._run_loop, name="cp0-polling-watcher", daemon=True)
self._thread.start()
def stop(self) -> None:
self._stop_event.set()
if self._thread is not None and self._thread is not threading.current_thread():
self._thread.join(timeout=2.0)
self._thread = None
def _run_loop(self) -> None:
while not self._stop_event.is_set():
try:
self.poll_once()
except Exception:
logger.exception("PollingWatcher tick failed")
self._stop_event.wait(timeout=self._poll_interval)
class InotifyWatcher:
"""Linux ctypes inotify watcher with ancestor tracking and PollingWatcher fallback."""
def __init__(
self,
on_file_ready: Callable[[WatchedFolder, Path], None],
on_dir_created: Callable[[WatchedFolder, Path], None] | None = None,
poll_interval: float = 0.5,
libc: Any | None = None,
) -> None:
self._on_file_ready = on_file_ready
self._on_dir_created = on_dir_created
self._poll_interval = max(0.05, float(poll_interval))
self._lock = threading.RLock()
self._stop_event = threading.Event()
self._thread: threading.Thread | None = None
self._owns_real_fd = libc is None
self._libc = libc if libc is not None else _load_linux_libc()
self._polling: PollingWatcher | None = None
self._fd: int = -1
self._folders: dict[str, WatchedFolder] = {}
self._wd_paths: dict[int, Path] = {}
self._path_wds: dict[Path, int] = {}
self._active_by_wd: dict[int, tuple[WatchedFolder, Path]] = {}
self._ancestors_by_wd: dict[int, dict[str, tuple[WatchedFolder, Path]]] = {}
self._folder_wds: dict[str, set[int]] = {}
self._unmounted: dict[str, tuple[WatchedFolder, Path]] = {}
if self._libc is not None:
fd = int(self._libc.inotify_init1(0))
if fd < 0:
err = ctypes.get_errno()
logger.warning("inotify_init1 failed (errno=%s); falling back to polling", err)
self._libc = None
else:
self._fd = fd
if self._owns_real_fd:
os.set_inheritable(self._fd, False)
os.set_blocking(self._fd, False)
if self._libc is None:
self._polling = PollingWatcher(
on_file_ready=on_file_ready,
on_dir_created=on_dir_created,
poll_interval=self._poll_interval,
)
@property
def is_using_polling(self) -> bool:
"""Return True if running with the PollingWatcher fallback."""
return self._polling is not None
def sync_folders(self, folders: list[WatchedFolder]) -> None:
"""Synchronize active watches with the given WatchedFolder list."""
if self._polling is not None:
self._polling.sync_folders(folders)
return
with self._lock:
enabled_map = {wf.id: wf for wf in folders if wf.enabled}
for existing_id in list(self._folders.keys()):
if existing_id not in enabled_map:
self._detach_folder_locked(existing_id)
for wf in folders:
if not wf.enabled:
continue
prev = self._folders.get(wf.id)
if prev is None or prev.path != wf.path:
self._detach_folder_locked(wf.id)
self._attach_folder_locked(wf)
else:
self._folders[wf.id] = wf
def add_folder(self, folder: WatchedFolder) -> None:
"""Attach watches for a single folder (or its nearest existing ancestor)."""
if self._polling is not None:
self._polling.add_folder(folder)
return
with self._lock:
if not folder.enabled:
return
self._detach_folder_locked(folder.id)
self._attach_folder_locked(folder)
def remove_folder(self, folder_id: str) -> None:
"""Detach all watches associated with folder_id."""
if self._polling is not None:
self._polling.remove_folder(folder_id)
return
with self._lock:
self._detach_folder_locked(folder_id)
def _add_watch_locked(self, dir_path: Path, mask: int) -> int | None:
assert self._libc is not None
existing = self._path_wds.get(dir_path)
if existing is not None:
return existing
wd = int(self._libc.inotify_add_watch(self._fd, os.fsencode(str(dir_path)), mask))
if wd < 0:
err = ctypes.get_errno()
logger.warning("inotify_add_watch failed for %s (errno=%s)", dir_path, err)
return None
self._wd_paths[wd] = dir_path
self._path_wds[dir_path] = wd
return wd
def _maybe_rm_watch_locked(self, wd: int) -> None:
if wd in self._active_by_wd:
return
if self._ancestors_by_wd.get(wd):
return
self._ancestors_by_wd.pop(wd, None)
dir_path = self._wd_paths.pop(wd, None)
if dir_path is not None and self._path_wds.get(dir_path) == wd:
self._path_wds.pop(dir_path, None)
if self._libc is not None and self._fd >= 0:
self._libc.inotify_rm_watch(self._fd, wd)
def _attach_folder_locked(self, folder: WatchedFolder) -> None:
self._folders[folder.id] = folder
root = Path(os.path.expanduser(folder.path))
if root.is_dir():
self._unmounted.pop(folder.id, None)
self._attach_tree_locked(folder, root)
else:
self._watch_ancestor_locked(folder, root)
def _attach_tree_locked(self, folder: WatchedFolder, root: Path) -> None:
wds = self._folder_wds.setdefault(folder.id, set())
for subdir in _iter_subdirs(root):
wd = self._add_watch_locked(subdir, _DIR_WATCH_MASK)
if wd is not None:
self._active_by_wd[wd] = (folder, subdir)
wds.add(wd)
def _watch_ancestor_locked(self, folder: WatchedFolder, target_root: Path) -> None:
ancestor = _nearest_existing_ancestor(target_root)
self._unmounted[folder.id] = (folder, target_root)
wd = self._add_watch_locked(ancestor, _ANCESTOR_WATCH_MASK)
if wd is not None:
self._ancestors_by_wd.setdefault(wd, {})[folder.id] = (folder, target_root)
self._folder_wds.setdefault(folder.id, set()).add(wd)
def _detach_folder_locked(self, folder_id: str) -> None:
self._folders.pop(folder_id, None)
self._unmounted.pop(folder_id, None)
wds = self._folder_wds.pop(folder_id, set())
for wd in list(wds):
active = self._active_by_wd.get(wd)
if active is not None and active[0].id == folder_id:
self._active_by_wd.pop(wd, None)
anc = self._ancestors_by_wd.get(wd)
if anc is not None:
anc.pop(folder_id, None)
self._maybe_rm_watch_locked(wd)
def _recheck_unmounted_mounts(self) -> None:
mounted_callbacks: list[tuple[WatchedFolder, Path]] = []
with self._lock:
for folder_id, (folder, target_root) in list(self._unmounted.items()):
if target_root.is_dir():
self._detach_folder_locked(folder_id)
self._attach_folder_locked(folder)
mounted_callbacks.append((folder, target_root))
continue
# If an intermediate mountpoint appeared, tighten ancestor watch closer to target_root.
best_ancestor = _nearest_existing_ancestor(target_root)
watched_ancestors = {
self._wd_paths[wd]
for wd in self._folder_wds.get(folder_id, set())
if wd in self._wd_paths
}
if best_ancestor not in watched_ancestors:
self._detach_folder_locked(folder_id)
self._attach_folder_locked(folder)
if self._on_dir_created is not None:
for folder, target_root in mounted_callbacks:
self._on_dir_created(folder, target_root)
def process_inotify_bytes(self, data: bytes) -> None:
"""Parse raw inotify buffer bytes and dispatch callbacks."""
file_callbacks: list[tuple[WatchedFolder, Path]] = []
dir_callbacks: list[tuple[WatchedFolder, Path]] = []
events = parse_inotify_events(data)
with self._lock:
for ev in events:
watched_dir = self._wd_paths.get(ev.wd)
if ev.mask & IN_IGNORED:
self._cleanup_ignored_wd_locked(ev.wd)
continue
if watched_dir is None:
continue
if ev.mask & (IN_DELETE_SELF | IN_MOVE_SELF):
self._handle_self_removed_locked(ev.wd, watched_dir)
continue
if ev.mask & IN_ISDIR:
if ev.name and (ev.mask & (IN_CREATE | IN_MOVED_TO)):
created_dir = watched_dir / ev.name
self._handle_dir_created_locked(
ev.wd, created_dir, dir_callbacks
)
continue
# File event: react ONLY to IN_CLOSE_WRITE or IN_MOVED_TO (never IN_CREATE).
if ev.name and (ev.mask & (IN_CLOSE_WRITE | IN_MOVED_TO)):
active = self._active_by_wd.get(ev.wd)
if active is not None:
folder, base_dir = active
file_callbacks.append((folder, base_dir / ev.name))
if self._on_dir_created is not None:
for folder, dir_path in dir_callbacks:
self._on_dir_created(folder, dir_path)
for folder, file_path in file_callbacks:
self._on_file_ready(folder, file_path)
def _cleanup_ignored_wd_locked(self, wd: int) -> None:
dir_path = self._wd_paths.pop(wd, None)
if dir_path is not None and self._path_wds.get(dir_path) == wd:
self._path_wds.pop(dir_path, None)
active = self._active_by_wd.pop(wd, None)
if active is not None:
folder, _ = active
self._folder_wds.get(folder.id, set()).discard(wd)
root = Path(os.path.expanduser(folder.path))
if dir_path == root and folder.id in self._folders:
self._detach_folder_locked(folder.id)
self._attach_folder_locked(folder)
anc = self._ancestors_by_wd.pop(wd, None)
if anc is not None:
affected = list(anc.keys())
for fid in affected:
self._folder_wds.get(fid, set()).discard(wd)
folder = self._folders.get(fid)
if folder is not None:
self._detach_folder_locked(fid)
self._attach_folder_locked(folder)
def _handle_self_removed_locked(self, wd: int, watched_dir: Path) -> None:
active = self._active_by_wd.get(wd)
if active is not None:
folder, _ = active
root = Path(os.path.expanduser(folder.path))
if watched_dir == root:
self._detach_folder_locked(folder.id)
self._attach_folder_locked(folder)
return
self._active_by_wd.pop(wd, None)
self._folder_wds.get(folder.id, set()).discard(wd)
anc = self._ancestors_by_wd.get(wd)
if anc is not None:
affected = list(anc.values())
for folder, target_root in affected:
self._detach_folder_locked(folder.id)
self._attach_folder_locked(folder)
return
self._maybe_rm_watch_locked(wd)
def _handle_dir_created_locked(
self,
wd: int,
created_dir: Path,
dir_callbacks: list[tuple[WatchedFolder, Path]],
) -> None:
active = self._active_by_wd.get(wd)
if active is not None:
folder, _ = active
if created_dir.is_dir():
self._attach_tree_locked(folder, created_dir)
dir_callbacks.append((folder, created_dir))
anc = self._ancestors_by_wd.get(wd)
if anc is not None:
for folder_id, (folder, target_root) in list(anc.items()):
if created_dir == target_root or created_dir in target_root.parents:
self._detach_folder_locked(folder_id)
self._attach_folder_locked(folder)
if target_root.is_dir():
dir_callbacks.append((folder, target_root))
def start(self) -> None:
"""Start background filesystem watching thread."""
if self._polling is not None:
self._polling.start()
return
if self._thread is not None and self._thread.is_alive():
return
self._stop_event.clear()
self._thread = threading.Thread(target=self._run_loop, name="cp0-inotify-watcher", daemon=True)
self._thread.start()
def stop(self) -> None:
"""Stop background watcher thread and close watch resources."""
if self._polling is not None:
self._polling.stop()
return
self._stop_event.set()
if self._thread is not None and self._thread is not threading.current_thread():
self._thread.join(timeout=2.0)
self._thread = None
with self._lock:
for folder_id in list(self._folders.keys()):
self._detach_folder_locked(folder_id)
if self._owns_real_fd and self._fd >= 0:
try:
os.close(self._fd)
except OSError:
pass
self._fd = -1
def _run_loop(self) -> None:
while not self._stop_event.is_set():
fd = self._fd
if fd < 0:
break
try:
readable, _, _ = select.select([fd], [], [], self._poll_interval)
except (OSError, ValueError):
if self._stop_event.is_set():
break
continue
if readable:
while not self._stop_event.is_set():
try:
chunk = os.read(fd, _READ_BUFFER_SIZE)
except BlockingIOError:
break
except OSError as exc:
if exc.errno in (errno.EAGAIN, errno.EWOULDBLOCK):
break
if not self._stop_event.is_set():
logger.warning("inotify read failed: %s", exc)
break
if not chunk:
break
self.process_inotify_bytes(chunk)
if not self._stop_event.is_set():
self._recheck_unmounted_mounts()