Something went wrong. Try again.
Chris’s PDS implementation
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156//! 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)); } }}