| 1 | use std::ffi::{CString, OsString}; |
| 2 | use std::io; |
| 3 | use std::os::unix::ffi::OsStringExt; |
| 4 | use std::path::PathBuf; |
| 5 | |
| 6 | pub(crate) fn create_temporary_dir() -> io::Result<PathBuf> { |
| 7 | let template = c"/tmp/sudoers-XXXXXX".to_owned(); |
| 8 | |
| 9 | // SAFETY: mkdtemp is passed a valid null-terminated C string |
| 10 | let ptr = unsafe { libc::mkdtemp(template.into_raw()) }; |
| 11 | |
| 12 | if ptr.is_null() { |
| 13 | return Err(io::Error::last_os_error()); |
| 14 | } |
| 15 | |
| 16 | // SAFETY: ptr is the same pointer produced by into_raw() above, and it |
| 17 | // is still pointing to a zero-terminated C string |
| 18 | let path = OsString::from_vec(unsafe { CString::from_raw(ptr) }.into_bytes()).into(); |
| 19 | |
| 20 | Ok(path) |
| 21 | } |