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

Method write_file

atomic-repository/src/repository/switch.rs:367–409  ·  view source on GitHub ↗
(&mut self, path: &str, content: &[u8])

Source from the content-addressed store, hash-verified

365 }
366
367 fn write_file(&mut self, path: &str, content: &[u8]) -> Result<bool, RepositoryError> {
368 let content_hash = Hash::of(content);
369 if self
370 .effect_for_expected_new(path, |value| {
371 matches!(
372 value,
373 EffectValue::File(state)
374 if state.kind != FileKind::Directory && state.content == content_hash
375 )
376 })
377 .is_none()
378 {
379 // An unleased entry is verified in place. A symlink is compared
380 // by its target, never by reading through it: following a link to
381 // a directory (the `.agents/skills/<name>` layout) failed every
382 // switch with "Is a directory".
383 let absolute = self.repository.root().join(path);
384 let current = match std::fs::symlink_metadata(&absolute) {
385 Ok(metadata) if metadata.file_type().is_symlink() => link_target_bytes(&absolute),
386 Ok(metadata) if metadata.is_dir() => {
387 return Err(RepositoryError::InvalidOperation {
388 message: format!("unleased directory entry blocks file '{path}'"),
389 });
390 }
391 Ok(_) => std::fs::read(&absolute),
392 Err(error) => Err(error),
393 };
394 return match current {
395 Ok(current) if current == content => Ok(false),
396 Ok(_) => Err(RepositoryError::InvalidOperation {
397 message: format!("unleased filesystem content changed at '{path}'"),
398 }),
399 Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
400 Err(RepositoryError::InvalidOperation {
401 message: format!("switch operation omitted file lease for '{path}'"),
402 })
403 }
404 Err(error) => Err(RepositoryError::Io(error)),
405 };
406 }
407 let pending = self.execute_file(path, content)?;
408 self.record(pending)
409 }
410
411 fn remove_path(&mut self, path: &str) -> Result<bool, RepositoryError> {
412 // The materializer also discovers historical FILE_INDEX rows. A path

Calls 9

link_target_bytesFunction · 0.85
is_noneMethod · 0.80
is_dirMethod · 0.80
execute_fileMethod · 0.80
readFunction · 0.50
rootMethod · 0.45
kindMethod · 0.45
recordMethod · 0.45

Tested by

no test coverage detected