Skip to content

Add mkstemp(3) #365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
24 changes: 23 additions & 1 deletion src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use fcntl::{fcntl, OFlag, O_NONBLOCK, O_CLOEXEC, FD_CLOEXEC};
use fcntl::FcntlArg::{F_SETFD, F_SETFL};
use libc::{self, c_char, c_void, c_int, c_uint, size_t, pid_t, off_t, uid_t, gid_t};
use std::mem;
use std::ffi::CString;
use std::ffi::{CString, OsStr};
use std::os::unix::ffi::OsStrExt;
use std::os::unix::io::RawFd;
use std::path::{PathBuf, Path};
use void::Void;

#[cfg(any(target_os = "linux", target_os = "android"))]
Expand Down Expand Up @@ -374,6 +376,26 @@ pub fn sleep(seconds: libc::c_uint) -> c_uint {
unsafe { libc::sleep(seconds) }
}

#[inline]
pub fn mkstemp<P: ?Sized + NixPath>(template: &P) -> Result<(RawFd, PathBuf)> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this too much because it forces an allocation and copy. See #365 (comment) for another option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I don't much like that. Mutating the input parameter feels like a hack and a bit of a safety hazard (though the mut mark makes it a bit less terrifying), to be honest. Plus, CStr input parameters are always kind of messy - I just had reason to use memfd_create, and its signature is kind of a pain.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't be a safety hazard unless you unsafely obtained the mutable reference :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memfd_create is like open so it should be fine?

let res = template.with_nix_path(|path| {
let owned_path = path.to_owned();
let path_ptr = owned_path.into_raw();
unsafe {
(libc::mkstemp(path_ptr), CString::from_raw(path_ptr))
}
});
match res {
Ok((fd, pathname)) => {
try!(Errno::result(fd));
Ok((fd, Path::new(OsStr::from_bytes(pathname.as_bytes())).to_owned()))
}
Err(e) => {
Err(e)
}
}
}

#[cfg(any(target_os = "linux", target_os = "android"))]
mod linux {
use sys::syscall::{syscall, SYSPIVOTROOT};
Expand Down
11 changes: 11 additions & 0 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ fn test_wait() {
}
}

#[test]
fn test_mkstemp() {
let result = mkstemp("/tmp/nix_tempfile.XXXXXXXX");
match result {
Ok((fd, path)) => {
close(fd).unwrap();
unlink(path.as_path()).unwrap();
}
Err(e) => panic!("mkstemp failed: {}", e)
}
}

#[test]
fn test_getpid() {
Expand Down