macOS / Tests (AppleSilicon) (push) Canceled after 0s
macOS / Tests (Intel) (push) Canceled after 0s
macOS / Build (AppleSilicon) (push) Canceled after 0s
macOS / Build (Intel) (push) Canceled after 0s
macOS / Publish the GitHub Release (push) Canceled after 0s
132 lines
4.3 KiB
Python
132 lines
4.3 KiB
Python
"""Who wrote the copy Syncthing set aside.
|
|
|
|
A conflict file is named ``<base>.sync-conflict-<date>-<time>-<DEVICE>.<ext>``,
|
|
where DEVICE is the first seven characters of the Syncthing device ID that last
|
|
wrote the copy being renamed. The merge report used to call one copy "this
|
|
machine" and the other "the other machine" purely from *which filename* it had
|
|
— and that is a guess Syncthing does not honor: it decides which copy keeps the
|
|
plain name, and it routinely sets the local one aside instead. The token in the
|
|
filename is the only real evidence about authorship, so this module turns it
|
|
into a name a person recognizes.
|
|
|
|
Everything here is best-effort and read-only: no Syncthing, no config, an
|
|
unreadable certificate or an unknown token all mean "we don't know", and the
|
|
report falls back to describing the two copies by their edit times alone.
|
|
"""
|
|
|
|
import base64
|
|
import hashlib
|
|
import os
|
|
import ssl
|
|
from pathlib import Path
|
|
|
|
from lintunes import paths
|
|
|
|
# Where Syncthing keeps config.xml + cert.pem. Newer builds use XDG state, older
|
|
# ones ~/.config; the last two are for people who never migrated.
|
|
_CONFIG_DIRS = (
|
|
"$XDG_STATE_HOME/syncthing",
|
|
"~/.local/state/syncthing",
|
|
"~/.config/syncthing",
|
|
"~/.syncthing",
|
|
)
|
|
|
|
# macOS: Syncthing's own default, which the syncthing-macos app and
|
|
# Homebrew's syncthing both use. STCONFDIR / STHOMEDIR are how Syncthing is
|
|
# pointed elsewhere (docs.syncthing.net/users/config.html), so they come
|
|
# first; the Unix locations last, for a Syncthing started with old habits.
|
|
_MAC_CONFIG_DIRS = (
|
|
"$STCONFDIR",
|
|
"$STHOMEDIR",
|
|
"~/Library/Application Support/Syncthing",
|
|
) + _CONFIG_DIRS
|
|
|
|
_cache: dict | None = None
|
|
|
|
|
|
def config_dirs() -> tuple[str, ...]:
|
|
"""The candidate directories, unexpanded, in the order they're tried."""
|
|
return _MAC_CONFIG_DIRS if paths.IS_MAC else _CONFIG_DIRS
|
|
|
|
|
|
def _config_dir() -> Path | None:
|
|
for raw in config_dirs():
|
|
text = os.path.expandvars(raw)
|
|
if not text.strip(): # a variable set to nothing
|
|
continue
|
|
expanded = Path(text).expanduser()
|
|
if "$" in str(expanded): # unset XDG_STATE_HOME / STCONFDIR
|
|
continue
|
|
if (expanded / "config.xml").is_file():
|
|
return expanded
|
|
return None
|
|
|
|
|
|
def _read_device_names(config_dir: Path) -> dict[str, str]:
|
|
"""{7-char ID prefix: device name} for every device in the config."""
|
|
import xml.etree.ElementTree as ET
|
|
names = {}
|
|
try:
|
|
root = ET.parse(config_dir / "config.xml").getroot()
|
|
except Exception:
|
|
return names
|
|
for device in root.iter("device"):
|
|
device_id = (device.get("id") or "").strip()
|
|
name = (device.get("name") or "").strip()
|
|
if device_id and name:
|
|
names[device_id[:7].upper()] = name
|
|
return names
|
|
|
|
|
|
def _read_self_id(config_dir: Path) -> str | None:
|
|
"""This machine's own device ID prefix.
|
|
|
|
config.xml lists every device including ourselves and doesn't say which one
|
|
we are, so derive it the way Syncthing does: a device ID is the base32 of
|
|
the SHA-256 of its certificate in DER form.
|
|
"""
|
|
try:
|
|
der = ssl.PEM_cert_to_DER_cert((config_dir / "cert.pem").read_text())
|
|
except Exception:
|
|
return None
|
|
digest = hashlib.sha256(der).digest()
|
|
return base64.b32encode(digest).decode("ascii").rstrip("=")[:7]
|
|
|
|
|
|
def _identity() -> dict:
|
|
global _cache
|
|
if _cache is None:
|
|
config_dir = _config_dir()
|
|
if config_dir is None:
|
|
_cache = {"names": {}, "self": None}
|
|
else:
|
|
_cache = {"names": _read_device_names(config_dir),
|
|
"self": _read_self_id(config_dir)}
|
|
return _cache
|
|
|
|
|
|
def reset_cache():
|
|
"""Forget what we read (tests point HOME somewhere else mid-run)."""
|
|
global _cache
|
|
_cache = None
|
|
|
|
|
|
def device_names() -> dict[str, str]:
|
|
return dict(_identity()["names"])
|
|
|
|
|
|
def self_device_id() -> str | None:
|
|
return _identity()["self"]
|
|
|
|
|
|
def label_for(token: str | None) -> str | None:
|
|
"""A human name for a conflict filename's device token, or None."""
|
|
if not token:
|
|
return None
|
|
token = token.upper()[:7]
|
|
identity = _identity()
|
|
name = identity["names"].get(token)
|
|
if identity["self"] and token == identity["self"]:
|
|
return f"{name} (this machine)" if name else "this machine"
|
|
return name
|