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

Function directory_content_hash

atomic-repository/src/repository/operation.rs:4945–4974  ·  view source on GitHub ↗
(path: &Path)

Source from the content-addressed store, hash-verified

4943}
4944
4945fn directory_content_hash(path: &Path) -> Result<Hash, RepositoryError> {
4946 let mut entries = fs::read_dir(path)?.collect::<Result<Vec<_>, _>>()?;
4947 entries.sort_by(|left, right| {
4948 os_str_bytes(left.file_name().as_os_str()).cmp(&os_str_bytes(right.file_name().as_os_str()))
4949 });
4950 let mut canonical = b"atomic:operation-directory:v1\0".to_vec();
4951 for entry in entries {
4952 let name = os_str_bytes(entry.file_name().as_os_str());
4953 let value = observe_path(&entry.path(), true)?;
4954 let EffectValue::File(state) = value else {
4955 return Err(RepositoryError::InvalidOperation {
4956 message: format!(
4957 "directory entry '{}' disappeared while hashing",
4958 entry.path().display()
4959 ),
4960 });
4961 };
4962 canonical.extend_from_slice(&(name.len() as u64).to_le_bytes());
4963 canonical.extend_from_slice(&name);
4964 canonical.push(match state.kind {
4965 FileKind::Regular => 1,
4966 FileKind::Directory => 2,
4967 FileKind::Symlink => 3,
4968 FileKind::Gitlink => 4,
4969 });
4970 canonical.extend_from_slice(&state.mode.to_le_bytes());
4971 canonical.extend_from_slice(state.content.as_bytes());
4972 }
4973 Ok(Hash::of(&canonical))
4974}
4975
4976fn copy_entry(source: &Path, destination: &Path) -> Result<(), RepositoryError> {
4977 let metadata = fs::symlink_metadata(source)?;

Callers 1

observe_pathFunction · 0.85

Calls 9

os_str_bytesFunction · 0.85
to_le_bytesMethod · 0.80
observe_pathFunction · 0.70
cmpMethod · 0.45
file_nameMethod · 0.45
pathMethod · 0.45
lenMethod · 0.45
pushMethod · 0.45
as_bytesMethod · 0.45

Tested by

no test coverage detected