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

Function fingerprint

atomic-agent/src/record/scope.rs:79–107  ·  view source on GitHub ↗

Fingerprint a file without following its final symlink. `None` means deleted. Reject directories and paths outside the working tree.

(root: &Path, path: &str)

Source from the content-addressed store, hash-verified

77/// Fingerprint a file without following its final symlink. `None` means deleted.
78/// Reject directories and paths outside the working tree.
79pub fn fingerprint(root: &Path, path: &str) -> Result<Option<String>, String> {
80 let full = checked_path(root, path)?;
81 let meta = match std::fs::symlink_metadata(&full) {
82 Ok(m) => m,
83 Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
84 Err(e) => return Err(e.to_string()),
85 };
86 if meta.file_type().is_symlink() {
87 let target = std::fs::read_link(full).map_err(|e| e.to_string())?;
88 return Ok(Some(format!(
89 "link:{}",
90 hex(&Sha256::digest(target.as_os_str().as_encoded_bytes()))
91 )));
92 }
93 if !meta.is_file() {
94 return Err(format!("not a regular scoped file: {path}"));
95 }
96 #[cfg(unix)]
97 let executable = {
98 use std::os::unix::fs::PermissionsExt;
99 meta.permissions().mode() & 0o111 != 0
100 };
101 #[cfg(not(unix))]
102 let executable = false;
103 let mut file = std::fs::File::open(full).map_err(|e| e.to_string())?;
104 let mut hash = Sha256::new();
105 std::io::copy(&mut file, &mut DigestWriter(&mut hash)).map_err(|e| e.to_string())?;
106 Ok(Some(format!("file:{}:{executable}", hex(&hash.finalize()))))
107}
108
109/// Return fingerprints for dirty files and explicitly requested prior candidates.
110/// The protocol version also lets plugins refuse an older, unscoped CLI.

Callers 2

snapshotFunction · 0.70
validateFunction · 0.70

Calls 7

checked_pathFunction · 0.85
openFunction · 0.85
DigestWriterClass · 0.85
kindMethod · 0.45
is_fileMethod · 0.45
modeMethod · 0.45
permissionsMethod · 0.45

Tested by

no test coverage detected