-
Notifications
You must be signed in to change notification settings - Fork 689
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
Add mkstemp(3) #365
Conversation
} | ||
})); | ||
Errno::result(res) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably move the newline to before the final expression rather than after it.
Looks good to me. I agree that the placement within unistd is a bit odd. I'll let others take a look before merging to decide if we want something like a Right now, I would lean toward creating that module versus putting it in |
Same. One thing that's slightly concerning: we are unable to communicate the actual file name back to the caller: because of how Pinging #221 #230 since this use case might matter for them. |
Aside: for temporary files, I usually use either the tempdir or tempfile crates depending on the exact use case. tempfile's focus is on creating an unnamed file that is not linked into the filesystem. Most often I use tempdir and create files in there. |
Test failures are for Rust < 1.4, which didn't have Ping #238. |
Thanks a lot for the comments! Those are some excellent points (and agreed to a new module)! My use case is specifically for a fresh file descriptor (not a While I'm not sure what one would use the filename of an unlinked file for, losing the value isn't great either - we could either require a I'm going to remove the depentency on |
I thought that /me checks |
Yeah, mkstemp results in a linked file:
|
Non-portably, the best approach on Linux is to use
|
ugh strings and nix are so annoying. this is not ideal because /me goes to finish setting up the RFCs repo so we can get past #221. |
Ahaha, eep, I totally missed the non-unlinkedness. You're completely right (and gosh, I should have taken a look at my |
let res = try!(template.with_nix_path(|path| { | ||
let mut path_copy = path.to_bytes_with_nul().to_owned(); | ||
let c_template: *mut c_char = path_copy.as_mut_ptr() as *mut c_char; | ||
pub fn mkstemp<P: ?Sized + NixPath>(template: &P) -> Result<(RawFd, PathBuf)> { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :-)
There was a problem hiding this comment.
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?
As unfortunate as the type is, it does fit best... There's no way to create one though, even
that will be very tricky, especially if we continue to support fallible one-way conversions... |
Huh, that's unfortunate. I'll take a look at adding at least the It would be nice to have an RFC like rust-lang/rfcs#1309 for
Maybe a different trait for the mutable argument case? |
At the same time maybe try adding some (oh god these long names though, why couldn't we have just named them
I'm not sure this is necessary, since the conversion from |
☔ The latest upstream changes (presumably #416) made this pull request unmergeable. Please resolve the merge conflicts. |
Since I also wanted to implement mkstemp in unistd I found this open PR. I fixed it. This is the working diff: nix-mkstemp.diff A few remarks
|
You can create a fresh PR that builds on antifuchs's work:
(I'm sorry, if I bore you with things you already know). |
@fiveop thanks for the explanation. No, I didn't know this already. What's the advantage of doing it the way you suggested against just doing a 100% fresh PR with his |
His commits will show up in the git history (instead of just his code). |
Ahh, sorry for letting this sit so long & thanks for working on this, @philippkeller! I'll close this PR now in favor of yours! |
@fiveop @philippkeller an improvement on the get-the-PR workflow:
:-) |
thanks :) |
Add Mkstemp (fixed for rust 1.2) I fixed @antifuchs addition of `mkstmp` in #365 by making it compile in Rust 1.2 and adding documentation. A few remarks: - made it working with Rust 1.2 which needed `to_bytes_with_nul()`. I think the implementation is memory safe but would like to have a pair of eyes checking that - replaced Path by PathBuf so it's more in line with getcwd - I didn't move it into another module. If this still the consensus then I would like to do that but in a separate PR (probably moving all stdio methods out) - it's true that unistd doesn't need mkstmp since there is the crate tempdir and tempfile, but I'd love to see this here for completeness
I've missed this call in a project that uses nix, so I added it to the
unistd
module; the placement is only a half-truth (as the header isstdlib.h
on most systems, but some related functions are documented to live inunistd.h
on BSD-derivatives), so I'm not sure my PR is completely right. But then, there is nostdlib
module yet! Any guidance here is much appreciated! (-: