Return `.atomic/atomic.redb`, merging a legacy layout into it first. # Errors - `DatabaseBusy` while another process holds a database file; retry. - `LegacyChangeStoreBusy` when an owner from an older atomic holds `changes.redb`. - `InvalidRepository` when both layouts exist and no interrupted merge explains it.
(dot_dir: &Path)
| 79 | /// - `InvalidRepository` when both layouts exist and no interrupted merge |
| 80 | /// explains it. |
| 81 | pub fn ensure_database(dot_dir: &Path) -> Result<PathBuf, RepositoryError> { |
| 82 | let database = dot_dir.join(DATABASE_FILE); |
| 83 | if !dot_dir.join(LEGACY_PRISTINE_FILE).is_file() { |
| 84 | return existing(database, dot_dir); |
| 85 | } |
| 86 | |
| 87 | // Lock the legacy graph before deciding anything, so concurrent opens of |
| 88 | // one repository queue behind a single merge instead of racing it. |
| 89 | let mut legacy = LegacyDatabases::new(); |
| 90 | if let Err(error) = legacy.add(&dot_dir.join(LEGACY_PRISTINE_FILE)) { |
| 91 | // Another open finished the merge and moved the file away meanwhile. |
| 92 | if is_not_found(&error) { |
| 93 | return existing(database, dot_dir); |
| 94 | } |
| 95 | return Err(error.into()); |
| 96 | } |
| 97 | add_change_store(&mut legacy, dot_dir)?; |
| 98 | if database.is_file() { |
| 99 | finish_retiring_legacy(dot_dir, &legacy)?; |
| 100 | } else { |
| 101 | merge_legacy(dot_dir, &legacy)?; |
| 102 | } |
| 103 | Ok(database) |
| 104 | } |
| 105 | |
| 106 | fn existing(database: PathBuf, dot_dir: &Path) -> Result<PathBuf, RepositoryError> { |
| 107 | if database.is_file() { |