(
root: &std::path::Path,
entry: &atomic_repository::ViewEntry,
)
| 1311 | } |
| 1312 | |
| 1313 | fn matches_canonical_entry( |
| 1314 | root: &std::path::Path, |
| 1315 | entry: &atomic_repository::ViewEntry, |
| 1316 | ) -> Result<bool, Status> { |
| 1317 | use atomic_repository::ViewEntryKind; |
| 1318 | let metadata = |path: &std::path::Path| match std::fs::symlink_metadata(path) { |
| 1319 | Ok(metadata) => Ok(Some(metadata)), |
| 1320 | Err(error) |
| 1321 | if matches!( |
| 1322 | error.kind(), |
| 1323 | std::io::ErrorKind::NotFound | std::io::ErrorKind::NotADirectory |
| 1324 | ) => |
| 1325 | { |
| 1326 | Ok(None) |
| 1327 | } |
| 1328 | Err(error) => Err(repository_error(error.into())), |
| 1329 | }; |
| 1330 | let relative = std::path::Path::new(&entry.path); |
| 1331 | let mut full = root.to_path_buf(); |
| 1332 | // Never approve matching bytes reached through a replaced parent symlink. |
| 1333 | for component in relative |
| 1334 | .parent() |
| 1335 | .into_iter() |
| 1336 | .flat_map(|path| path.components()) |
| 1337 | { |
| 1338 | if !matches!(component, std::path::Component::Normal(_)) { |
| 1339 | return Ok(false); |
| 1340 | } |
| 1341 | full.push(component); |
| 1342 | if !metadata(&full)?.is_some_and(|m| m.is_dir() && !m.file_type().is_symlink()) { |
| 1343 | return Ok(false); |
| 1344 | } |
| 1345 | } |
| 1346 | let path = root.join(relative); |
| 1347 | let Some(metadata) = metadata(&path)? else { |
| 1348 | return Ok(false); |
| 1349 | }; |
| 1350 | let kind_matches = match entry.kind { |
| 1351 | ViewEntryKind::File => metadata.is_file(), |
| 1352 | ViewEntryKind::Directory => metadata.is_dir(), |
| 1353 | ViewEntryKind::Symlink => metadata.file_type().is_symlink(), |
| 1354 | }; |
| 1355 | if !kind_matches { |
| 1356 | return Ok(false); |
| 1357 | } |
| 1358 | #[cfg(unix)] |
| 1359 | if entry.kind != ViewEntryKind::Symlink { |
| 1360 | use std::os::unix::fs::PermissionsExt; |
| 1361 | if metadata.permissions().mode() & 0o777 != u32::from(entry.mode) & 0o777 { |
| 1362 | return Ok(false); |
| 1363 | } |
| 1364 | } |
| 1365 | let content = match entry.kind { |
| 1366 | ViewEntryKind::Directory => return Ok(true), |
| 1367 | ViewEntryKind::File => std::fs::read(&path), |
| 1368 | ViewEntryKind::Symlink => { |
| 1369 | #[cfg(unix)] |
| 1370 | { |
no test coverage detected