use std::fs::{File, OpenOptions};
use std::path::Path;
#[cfg(unix)]
pub fn open_append_private(path: &Path) -> std::io::Result<File> {
use std::os::unix::fs::OpenOptionsExt;
OpenOptions::new()
.create(true)
.append(true)
.mode(0o600)
.open(path)
}
#[cfg(not(unix))]
pub fn open_append_private(path: &Path) -> std::io::Result<File> {
OpenOptions::new().create(true).append(true).open(path)
}
#[cfg(all(test, unix))]
mod tests {
use super::*;
use std::os::unix::fs::PermissionsExt;
#[test]
fn creates_file_with_0600() {
let dir = std::env::temp_dir().join(format!(
"vallum_fsutil_{}",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("f.log");
let _ = open_append_private(&path).unwrap();
let mode = std::fs::metadata(&path).unwrap().permissions().mode();
assert_eq!(mode & 0o777, 0o600);
let _ = std::fs::remove_dir_all(&dir);
}
}