Skip to content

macOS: NFD-vs-NFC file_locations + FTS5 duplication on re-scan (phase-9 hardening) #113

Open

Description

@utof

Context

After the 2026-04-20 path-consolidation work (lib-audit L5),
MediaPath::new no longer NFC-normalizes its input. Production
MediaPaths are built downstream of fs::canonicalize. The
per-platform filesystem semantics:

  • macOS HFS+: stores NFD; realpath returns NFD.
  • macOS APFS: stores caller-form-preserving; dirent lookup
    uses hash-of-normalized (per Apple APFS FAQ); realpath returns
    the stored dirent name (whichever form the file was written
    under).
  • NTFS (Windows): Win32 layer applies normalization; storage
    is UTF-16 as given. Behavior across NT versions is complex.
  • ext4 (Linux): byte-exact — no Unicode equivalence.

The macOS user impact

A user with an existing perima DB populated pre-L5 has
NFC-normalized relative_path bytes in file_locations. On a
macOS HFS+ machine, a fresh scan post-L5 produces NFD bytes. The
DB ends up with two file_locations rows for the same physical
file: one NFC (pre-L5), one NFD (post-L5). APFS defaults since
macOS 10.13 (2017), so HFS+ impact is narrow — but possible for
any early adopter who migrated from HFS+ to APFS while retaining
the original dirent forms.

FTS5 secondary impact

crates/db/src/search_repo.rs indexes relative_path into FTS5
for filename tokenization. With duplicate rows in file_locations,
the FTS5 content has BOTH NFC + NFD tokenized. macOS users typing
NFC queries (macOS keyboard default) hit only the pre-L5 row; the
NFD rows become effectively unsearchable via NFC queries. The
migration below MUST rebuild the FTS5 content after collapsing
duplicates — simply deleting duplicate file_locations rows
without refreshing FTS5 leaves stale tokens.

Acceptance criteria

  1. Detect duplicate-by-hash file_locations rows whose
    relative_path differs only by Unicode normalization form.
  2. Canonicalize the absolute path via fs::canonicalize; rewrite
    the winning row's relative_path to match the canonicalized
    form; soft-delete the losing row (per CRDT-ready soft-delete
    discipline).
  3. Rebuild the FTS5 virtual-table content (INSERT INTO fts5(fts5) VALUES('rebuild')) OR surgically delete-and-re-insert affected
    rows.
  4. Ship the migration as part of Phase 9 hardening — DB versioning
    handles multi-step migrations; add the step to the migration
    pipeline.

Labels

phase-9-hardening — hardening + cert procurement phase.
library-audit-followup — L5 work surfaced this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions