(workspace: &Result<Option<PathBuf>, SourceError>)
| 184 | } |
| 185 | |
| 186 | pub(crate) fn collect(workspace: &Result<Option<PathBuf>, SourceError>) -> RepositoryMetrics { |
| 187 | let root = match workspace { |
| 188 | Ok(Some(root)) => root, |
| 189 | Ok(None) => { |
| 190 | return RepositoryMetrics::NotApplicable { |
| 191 | reason: "not inside an Atomic workspace", |
| 192 | } |
| 193 | } |
| 194 | Err(_) => { |
| 195 | return RepositoryMetrics::Unavailable { |
| 196 | reason: "workspace cannot be determined", |
| 197 | database_bytes: None, |
| 198 | } |
| 199 | } |
| 200 | }; |
| 201 | let dot = match atomic_repository::Repository::canonical_dot_dir(root) { |
| 202 | Ok(dot) => dot, |
| 203 | Err(_) => { |
| 204 | return RepositoryMetrics::Unavailable { |
| 205 | reason: "repository database location unavailable", |
| 206 | database_bytes: None, |
| 207 | } |
| 208 | } |
| 209 | }; |
| 210 | let path = dot.join(atomic_repository::DATABASE_FILE); |
| 211 | let size = std::fs::metadata(&path).ok().map(|m| m.len()); |
| 212 | // A legacy layout (including an interrupted merge) cannot be interpreted |
| 213 | // as the authoritative current database without the migration protocol. |
| 214 | match std::fs::metadata(dot.join(atomic_repository::LEGACY_PRISTINE_FILE)) { |
| 215 | Ok(_) => { |
| 216 | return RepositoryMetrics::Unavailable { |
| 217 | reason: "legacy repository layout requires migration", |
| 218 | database_bytes: size, |
| 219 | }; |
| 220 | } |
| 221 | Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} |
| 222 | Err(_) => { |
| 223 | return RepositoryMetrics::Unavailable { |
| 224 | reason: "repository layout cannot be verified", |
| 225 | database_bytes: size, |
| 226 | }; |
| 227 | } |
| 228 | } |
| 229 | let database = match redb::ReadOnlyDatabase::open(&path) { |
| 230 | Ok(database) => database, |
| 231 | Err(_) => { |
| 232 | return RepositoryMetrics::Unavailable { |
| 233 | reason: "repository database cannot be read or is in use", |
| 234 | database_bytes: size, |
| 235 | } |
| 236 | } |
| 237 | }; |
| 238 | let txn = match database.begin_read() { |
| 239 | Ok(txn) => txn, |
| 240 | Err(_) => { |
| 241 | return RepositoryMetrics::Unavailable { |
| 242 | reason: "repository database cannot be read", |
| 243 | database_bytes: size, |
no test coverage detected