Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion crates/paths/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,21 @@ macro_rules! path_type {
}

pub fn write(&self, contents: impl AsRef<[u8]>) -> std::io::Result<()> {
use std::io::Write as _;

self.create_parent()?;
std::fs::write(self, contents)
let mut file = std::fs::File::create(self)?;
file.write_all(contents.as_ref())?;
file.sync_all()?;

// In case the file got created, we also need to fsync the
// directory, so that the directory entry becomes durable.
if let Some(parent) = self.0.parent()
&& parent != std::path::Path::new("") {
std::fs::File::open(parent)?.sync_all()?;
}

Ok(())
}

/// Opens a file at this path with the given options, ensuring its parent directory exists.
Expand Down
Loading