//! Synchronous durable writes for private storage snapshots. #[cfg(unix)] use std::os::unix::fs::{DirBuilderExt as _, OpenOptionsExt as _}; use std::{io::Write as _, path::Path}; use crate::error::Result; pub(crate) fn parent_or_current(path: &Path) -> &Path { path.parent() .filter(|parent| !parent.as_os_str().is_empty()) .unwrap_or_else(|| Path::new(".")) } pub(crate) fn create_directory(path: &Path) -> Result<()> { #[cfg_attr(not(unix), allow(unused_mut))] let mut builder = std::fs::DirBuilder::new(); #[cfg(unix)] builder.mode(0o700); builder.create(path).map_err(Into::into) } pub(crate) fn write_file(path: &Path, bytes: &[u8]) -> Result<()> { let mut options = std::fs::OpenOptions::new(); options.write(true).create_new(true); #[cfg(unix)] options.mode(0o600); let mut file = options.open(path)?; file.write_all(bytes)?; file.sync_all().map_err(Into::into) } pub(crate) fn secure_file(path: &Path) -> Result<()> { #[cfg(unix)] { use std::os::unix::fs::PermissionsExt as _; std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))?; } std::fs::File::open(path)?.sync_all().map_err(Into::into) } pub(crate) fn sync_directory(path: &Path) -> Result<()> { if cfg!(unix) { std::fs::File::open(path)?.sync_all()?; } Ok(()) } #[cfg(test)] mod tests { use super::*; #[test] fn directory_sync_preserves_platform_io_and_errors() { let directory = tempfile::tempdir().unwrap(); let file = directory.path().join("file"); std::fs::write(&file, b"contents").unwrap(); for path in [ directory.path().to_owned(), file.clone(), directory.path().join("missing"), file.join("not-a-directory"), ] { let expected: Result<()> = (|| { #[cfg(unix)] std::fs::File::open(&path)?.sync_all()?; Ok(()) })(); assert_eq!( format!("{:?}", sync_directory(&path)), format!("{expected:?}") ); } assert_eq!(std::fs::read(file).unwrap(), b"contents"); } #[test] fn private_snapshot_writes_never_replace_existing_paths() { let directory = tempfile::tempdir().unwrap(); let snapshot = directory.path().join("snapshot"); create_directory(&snapshot).unwrap(); let file = snapshot.join("data"); write_file(&file, b"original").unwrap(); for result in [ create_directory(&snapshot), write_file(&file, b"replacement"), ] { let crate::error::Error::Io(error) = result.unwrap_err() else { panic!("existing snapshot paths must produce an I/O error"); }; assert_eq!(error.kind(), std::io::ErrorKind::AlreadyExists); } assert_eq!(std::fs::read(file).unwrap(), b"original"); } #[cfg(unix)] #[test] fn private_snapshot_modes_and_symlink_collisions_are_preserved() { use std::os::unix::fs::{PermissionsExt as _, symlink}; let directory = tempfile::tempdir().unwrap(); let snapshot = directory.path().join("snapshot"); create_directory(&snapshot).unwrap(); let file = snapshot.join("data"); write_file(&file, b"original").unwrap(); for (path, mode) in [(&snapshot, 0o700), (&file, 0o600)] { assert_eq!( std::fs::metadata(path).unwrap().permissions().mode() & 0o777, mode ); } let linked_file = directory.path().join("linked-file"); let linked_directory = directory.path().join("linked-directory"); symlink(&file, &linked_file).unwrap(); symlink(&snapshot, &linked_directory).unwrap(); for result in [ write_file(&linked_file, b"replacement"), create_directory(&linked_directory), ] { let crate::error::Error::Io(error) = result.unwrap_err() else { panic!("symlink collisions must produce an I/O error"); }; assert_eq!(error.kind(), std::io::ErrorKind::AlreadyExists); } assert_eq!(std::fs::read(file).unwrap(), b"original"); assert!( std::fs::symlink_metadata(linked_file) .unwrap() .file_type() .is_symlink() ); assert!( std::fs::symlink_metadata(linked_directory) .unwrap() .file_type() .is_symlink() ); } #[test] fn storage_parent_preserves_relative_and_root_path_behavior() { for (path, expected) in [ ("", "."), ("backup", "."), ("./backup", "."), ("storage/backup", "storage"), ("/", "."), ("/backup", "/"), ] { assert_eq!(parent_or_current(Path::new(path)), Path::new(expected)); } } }