A secondary deletes its own stale write-ahead logs, and the sync leaves no stray files (#994)

* A secondary deletes its own stale write-ahead logs, and the sync leaves no stray files

Found by the first sync between two real machines:

- The primary's unfinished files (SQLite logs, partial downloads) were excluded from the copy, and an excluded path
  is never deleted on the receiving side.  So a secondary kept its own write-ahead log beside a map search
  database the primary had replaced, and SQLite would apply that log to the new database.  These patterns are now
  only hidden on the primary's side, so the secondary's stale ones are deleted (to the trash).
- Reading the incoming snapshot to check it made a -wal and -shm beside it, which stayed behind when the snapshot
  was moved into place.  It is now opened immutable.
- The primary's info, written by the Controller (root), belonged to root.  Files the Controller writes in the media
  directory now belong to its owner, like the directories it makes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* With deletes turned off, a secondary still drops its stale unfinished files

Review found that the hide rules of the previous commit only take effect through --delete, which is not given when
delete_after_days is null (never delete).  A secondary then kept its own write-ahead log beside a database the
primary had replaced, and SQLite applied it to the new database.

Without a trash, --delete is now given with every path protected except the unfinished patterns (and what is
inside an unfinished folder), so only those are removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-11-19 14:19:09 -07:00 • committed by GitHub
parent ea7cc4dec2
commit 6772762ce9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 87 additions and 15 deletions

View file

@ -82,8 +82,12 @@ LOCAL_PATHS = (
f"/{SYNC_DIRECTORY}",
f"/{TRASH_DIRECTORY}", "/lost+found",
)
# Anywhere in the library: files still being written on the primary.
UNFINISHED_PATTERNS = (PARTIAL_DIRECTORY, ".partial", "*.part", "*.aria2", "*.ytdl", "*.db-wal", "*.db-shm")
# Anywhere in the library: files still being written on the primary, which are not copied. Hidden from the
# primary's side only (rsync's "hide"), not excluded: the secondary's own are stale, and must go. An excluded
# path is never deleted, so a secondary kept the write-ahead log of a map search database whose database the
# primary had replaced, and SQLite would apply that log to the new database. (rsync keeps its own partial
# directory itself.)
UNFINISHED_PATTERNS = (".partial", "*.part", "*.aria2", "*.ytdl", "*.db-wal", "*.db-shm")
class SyncError(Exception):
@ -171,11 +175,14 @@ def checked_path(path: Path) -> Path:
def write_file(file: Path, text: str):
"""Write `file` in the media directory without following a link: to a new file of its own, renamed into
place."""
place. It belongs to the media directory's owner, like everything else in the library."""
checked_path(file)
owner = get_media_directory().stat()
temporary = file.with_name(f".{file.name}.{uuid.uuid4().hex}.tmp")
descriptor = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644)
with os.fdopen(descriptor, "w") as fh:
with contextlib.suppress(PermissionError):
os.fchown(fh.fileno(), owner.st_uid, owner.st_gid)
fh.write(text)
os.replace(temporary, file)
@ -252,7 +259,16 @@ def build_files_command(primary_url: str, media_directory: Path, trash: Optional
for playlists in dict.fromkeys(str(i or "").strip("/") for i in playlists_destinations):
if playlists:
arguments.append(f"--exclude=/{rsync_literal(playlists)}")
arguments.extend(f"--exclude={i}" for i in UNFINISHED_PATTERNS)
arguments.extend(f"--filter=H {i}" for i in UNFINISHED_PATTERNS)
if trash is None:
# Nothing of the library is deleted, but the secondary's stale unfinished files must still go: a log left
# beside a database the primary replaced would be applied to it. Only those are at risk (R); everything
# else is protected (P) from the --delete that removes them.
arguments.append("--delete")
for pattern in UNFINISHED_PATTERNS:
# The pattern itself, and what is inside it when it is a folder (".partial").
arguments.extend([f"--filter=R {pattern}", f"--filter=R {pattern}/**"])
arguments.append("--filter=P *")
# Folders the owner chose not to copy.
for directory in excluded_directories or []:
if directory := str(directory).strip("/"):
@ -516,7 +532,8 @@ def replace_database(media_directory: Path):
database complete."""
database = checked_path(media_directory / "config/wrolpi.db")
incoming = checked_path(media_directory / INCOMING_DATABASE)
with contextlib.closing(sqlite3.connect(f"file:{incoming}?mode=ro", uri=True)) as connection:
# Immutable: read without making a -wal and -shm beside it, which would be left behind when it is moved.
with contextlib.closing(sqlite3.connect(f"file:{incoming}?mode=ro&immutable=1", uri=True)) as connection:
if connection.execute("PRAGMA schema_version").fetchone()[0] == 0:
raise SyncError("The snapshot from the primary is not a database")
if database.is_file():