graphrt_core/
private_fs.rs1use std::fs::{DirBuilder, File, OpenOptions};
15use std::io::{self, Write};
16use std::path::{Path, PathBuf};
17use std::sync::atomic::{AtomicU64, Ordering};
18
19pub const PRIVATE_DIR_MODE: u32 = 0o700;
21pub const PRIVATE_FILE_MODE: u32 = 0o600;
23
24pub fn create_dir_all(path: &Path) -> io::Result<()> {
32 private_dir_builder().recursive(true).create(path)
33}
34
35pub fn create_fresh_temp_dir(prefix: &str) -> io::Result<PathBuf> {
46 static COUNTER: AtomicU64 = AtomicU64::new(0);
47 let nanos =
48 std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or_default();
49 let unique = format!("{}-{}-{nanos}", std::process::id(), COUNTER.fetch_add(1, Ordering::Relaxed));
50 let dir = std::env::temp_dir().join(format!("{prefix}-{unique}"));
51 private_dir_builder().create(&dir)?;
52 Ok(dir)
53}
54
55pub fn create_file(path: &Path) -> io::Result<File> {
59 private_open_options().write(true).create(true).truncate(true).open(path)
60}
61
62pub fn append_file(path: &Path) -> io::Result<File> {
66 private_open_options().append(true).create(true).open(path)
67}
68
69pub fn write(path: &Path, bytes: impl AsRef<[u8]>) -> io::Result<()> {
73 create_file(path)?.write_all(bytes.as_ref())
74}
75
76fn private_dir_builder() -> DirBuilder {
77 #[cfg_attr(not(unix), allow(unused_mut))] let mut builder = DirBuilder::new();
79 #[cfg(unix)]
80 std::os::unix::fs::DirBuilderExt::mode(&mut builder, PRIVATE_DIR_MODE);
81 builder
82}
83
84fn private_open_options() -> OpenOptions {
85 #[cfg_attr(not(unix), allow(unused_mut))] let mut options = OpenOptions::new();
87 #[cfg(unix)]
88 std::os::unix::fs::OpenOptionsExt::mode(&mut options, PRIVATE_FILE_MODE);
89 options
90}
91
92#[cfg(test)]
93#[allow(clippy::unwrap_used)]
94mod tests {
95 use super::*;
96
97 #[cfg(unix)]
98 fn mode_of(path: &Path) -> u32 {
99 use std::os::unix::fs::PermissionsExt;
100 std::fs::metadata(path).unwrap().permissions().mode() & 0o777
101 }
102
103 #[test]
104 fn fresh_temp_dirs_are_new_and_owner_only() {
105 let first = create_fresh_temp_dir("graphrt-private-fs-test").unwrap();
106 let second = create_fresh_temp_dir("graphrt-private-fs-test").unwrap();
107 assert_ne!(first, second);
108 #[cfg(unix)]
109 assert_eq!(mode_of(&first), PRIVATE_DIR_MODE);
110 for dir in [first, second] {
111 std::fs::remove_dir_all(dir).unwrap();
112 }
113 }
114
115 #[test]
116 fn files_and_nested_dirs_are_owner_only() {
117 let root = create_fresh_temp_dir("graphrt-private-fs-test").unwrap();
118 let nested = root.join("a/b");
119 create_dir_all(&nested).unwrap();
120 let file = nested.join("data.json");
121 write(&file, b"{}").unwrap();
122 append_file(&nested.join("log.jsonl")).unwrap().write_all(b"x\n").unwrap();
123 assert_eq!(std::fs::read(&file).unwrap(), b"{}");
124 #[cfg(unix)]
125 {
126 assert_eq!(mode_of(&root.join("a")), PRIVATE_DIR_MODE);
127 assert_eq!(mode_of(&nested), PRIVATE_DIR_MODE);
128 assert_eq!(mode_of(&file), PRIVATE_FILE_MODE);
129 assert_eq!(mode_of(&nested.join("log.jsonl")), PRIVATE_FILE_MODE);
130 }
131 std::fs::remove_dir_all(root).unwrap();
132 }
133}