Fingerprint a file without following its final symlink. `None` means deleted. Reject directories and paths outside the working tree.
(root: &Path, path: &str)
| 77 | /// Fingerprint a file without following its final symlink. `None` means deleted. |
| 78 | /// Reject directories and paths outside the working tree. |
| 79 | pub 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. |
no test coverage detected