# Sync Database Module `sync_db.rs` (839 lines) — SQLite persistence layer for Proton Drive sync metadata. ## Purpose Tracks the state of every synced file so the app can: - Resume sync after restart without re-scanning - Detect conflicts between local and remote state - Track retry counts for failed operations - Map local files to their remote Proton Drive counterparts ## Schema The database uses two main tables: ### `sync_roots` Tracks sync folder registrations: | Column | Type | Description | |---|---|---| | `id` | TEXT PK | Hash of root path | | `root_path_hash` | TEXT | SHA-256 hash of root path | | `remote_scope` | TEXT | `computers`, `my_files`, or `unmapped` | | `created_at_ns` | INTEGER | Creation timestamp (nanoseconds) | | `updated_at_ns` | INTEGER | Last update timestamp | ### `sync_items` Per-file sync state: | Column | Type | Description | |---|---|---| | `root_id` | TEXT FK | References `sync_roots.id` | | `relative_path_hash` | TEXT | Hash of path relative to root | | `local_kind` | TEXT | File or directory | | `local_size` | INTEGER? | File size in bytes | | `local_mtime_ns` | INTEGER? | Modification time (ns) | | `content_hash` | TEXT? | SHA-256 of file content | | `remote_volume_id_hash` | TEXT? | Proton volume ID (hashed) | | `remote_share_id_hash` | TEXT? | Proton share ID (hashed) | | `remote_link_id_hash` | TEXT? | Proton link ID (hashed) | | `remote_parent_id_hash` | TEXT? | Parent folder ID (hashed) | | `remote_revision_hash` | TEXT? | Latest revision hash | | `state` | TEXT | One of: `synced`, `local_pending`, `remote_pending`, `conflict`, `tombstone` | | `retry_count` | INTEGER | Consecutive failure count | | `last_error_code` | TEXT? | Last error for diagnostics | ## Sync Item States | State | Meaning | |---|---| | `synced` | Local and remote are identical | | `local_pending` | Local change not yet uploaded | | `remote_pending` | Remote change not yet applied locally | | `conflict` | Both sides changed — needs resolution | | `tombstone` | Deleted but record kept for tracking | ## Security - All Proton Drive IDs are stored as **SHA-256 hashes**, never in plaintext - The database file is created with 0600 permissions - Schema version is tracked (`SCHEMA_VERSION = 3`) for migrations - WAL journal mode and foreign keys are enabled on open