use std::env::temp_dir;
use std::ffi::OsStr;
use std::fs;
use std::fs::File;
use std::path::{Path, PathBuf};
use libc;
use crate::errno::{errno_result, Error, Result};
#[derive(Debug)]
pub struct TempFile {
path: PathBuf,
file: Option<File>,
}
impl TempFile {
#[cfg(unix)]
pub fn new_with_prefix<P: AsRef<OsStr>>(prefix: P) -> Result<TempFile> {
use std::ffi::CString;
use std::os::unix::{ffi::OsStrExt, io::FromRawFd};
let mut os_fname = prefix.as_ref().to_os_string();
os_fname.push("XXXXXX");
let c_tempname = CString::new(os_fname.as_bytes()).map_err(|_| Error::new(libc::EINVAL))?;
let raw_tempname = c_tempname.into_raw();
let ret = unsafe { libc::mkstemp(raw_tempname) };
let c_tempname = unsafe { CString::from_raw(raw_tempname) };
let fd = match ret {
-1 => return errno_result(),
_ => ret,
};
let os_tempname = OsStr::from_bytes(c_tempname.as_bytes());
let file = unsafe { File::from_raw_fd(fd) };
Ok(TempFile {
path: PathBuf::from(os_tempname),
file: Some(file),
})
}
#[cfg(windows)]
pub fn new_with_prefix<P: AsRef<OsStr>>(prefix: P) -> Result<TempFile> {
use crate::rand::rand_alphanumerics;
use std::fs::OpenOptions;
let file_path_str = format!(
"{}{}",
prefix.as_ref().to_str().unwrap_or_default(),
rand_alphanumerics(6).to_str().unwrap_or_default()
);
let file_path_buf = PathBuf::from(&file_path_str);
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(file_path_buf.as_path())?;
Ok(TempFile {
path: file_path_buf,
file: Some(file),
})
}
pub fn new_in(path: &Path) -> Result<Self> {
let mut path_buf = path.canonicalize().unwrap();
path_buf.push("");
let temp_file = TempFile::new_with_prefix(path_buf.as_path())?;
Ok(temp_file)
}
pub fn new() -> Result<Self> {
let in_tmp_dir = temp_dir();
let temp_file = TempFile::new_in(in_tmp_dir.as_path())?;
Ok(temp_file)
}
pub fn remove(&mut self) -> Result<()> {
fs::remove_file(&self.path).map_err(Error::from)
}
pub fn as_path(&self) -> &Path {
&self.path
}
pub fn as_file(&self) -> &File {
self.file.as_ref().unwrap()
}
pub fn into_file(mut self) -> File {
self.file.take().unwrap()
}
}
impl Drop for TempFile {
fn drop(&mut self) {
let _ = self.remove();
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::{Read, Write};
#[test]
fn test_create_file_with_prefix() {
fn between(lower: u8, upper: u8, to_check: u8) -> bool {
(to_check >= lower) && (to_check <= upper)
}
let mut prefix = temp_dir();
prefix.push("asdf");
let t = TempFile::new_with_prefix(&prefix).unwrap();
let path = t.as_path().to_owned();
assert!(path.is_file());
assert!(path.starts_with(temp_dir()));
assert_eq!(path.file_name().unwrap().to_string_lossy().len(), 10);
for n in path.file_name().unwrap().to_string_lossy().bytes() {
assert!(between(b'0', b'9', n) || between(b'a', b'z', n) || between(b'A', b'Z', n));
}
let mut f = t.as_file();
f.write_all(b"hello world").unwrap();
f.sync_all().unwrap();
assert_eq!(f.metadata().unwrap().len(), 11);
}
#[test]
fn test_create_file_new() {
let t = TempFile::new().unwrap();
let path = t.as_path().to_owned();
assert!(path.starts_with(temp_dir().canonicalize().unwrap()));
}
#[test]
fn test_create_file_new_in() {
let t = TempFile::new_in(temp_dir().as_path()).unwrap();
let path = t.as_path().to_owned();
assert!(path.is_file());
assert!(path.starts_with(temp_dir().canonicalize().unwrap()));
let t = TempFile::new_in(temp_dir().as_path()).unwrap();
let path = t.as_path().to_owned();
assert!(path.starts_with(temp_dir().canonicalize().unwrap()));
}
#[test]
fn test_remove_file() {
let mut prefix = temp_dir();
prefix.push("asdf");
let mut t = TempFile::new_with_prefix(prefix).unwrap();
let path = t.as_path().to_owned();
assert!(t.remove().is_ok());
assert!(!path.exists());
let path_2 = t.as_path().to_owned();
assert_eq!(path, path_2);
assert!(t.remove().is_err());
}
#[test]
fn test_drop_file() {
let mut prefix = temp_dir();
prefix.push("asdf");
let t = TempFile::new_with_prefix(prefix).unwrap();
let path = t.as_path().to_owned();
assert!(path.starts_with(temp_dir()));
drop(t);
assert!(!path.exists());
}
#[test]
fn test_into_file() {
let mut prefix = temp_dir();
prefix.push("asdf");
let text = b"hello world";
let temp_file = TempFile::new_with_prefix(prefix).unwrap();
let path = temp_file.as_path().to_owned();
fs::write(path, text).unwrap();
let mut file = temp_file.into_file();
let mut buf: Vec<u8> = Vec::new();
file.read_to_end(&mut buf).unwrap();
assert_eq!(buf, text);
}
}