MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / matches_canonical_entry

Function matches_canonical_entry

libatomic/src/daemon/services.rs:1313–1380  ·  view source on GitHub ↗
(
    root: &std::path::Path,
    entry: &atomic_repository::ViewEntry,
)

Source from the content-addressed store, hash-verified

1311}
1312
1313fn 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 {

Callers 1

Calls 11

parentMethod · 0.80
componentsMethod · 0.80
is_dirMethod · 0.80
repository_errorFunction · 0.70
readFunction · 0.50
into_iterMethod · 0.45
pushMethod · 0.45
is_fileMethod · 0.45
modeMethod · 0.45
permissionsMethod · 0.45
as_bytesMethod · 0.45

Tested by

no test coverage detected