Skip to content
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

Extend mktemp to support file suffixes. #13969

Closed
wants to merge 6 commits into from
Closed

Extend mktemp to support file suffixes. #13969

wants to merge 6 commits into from

Conversation

maleadt
Copy link
Member

@maleadt maleadt commented Nov 13, 2015

This PR extends mktemp to support file suffixes using the mkstemps function:

mktemp(suffix=".txt")
("/tmp/tmprFezJ1.txt",IOStream(<fd 15>))

Additionally, I've also changed the ccall signature from Ptr{UInt8} to Cstring. My understanding is that Cstring is meant to be used when a string is NULL-terminated, and according to this document the template parameter to mkstemp and others is supposed to be terminated as such.
Am I reading this correctly? If so, I think other functions (tempnam, mktempdir) might need adjusting too.

@tkelman
Copy link
Contributor

tkelman commented Nov 13, 2015

needs tests, docs, and if possible a windows implementation (if not, an error)

edit: and the do-block form below with mktemp(fn, parent) should also probably be allowed to use this kwarg accordingly

@maleadt
Copy link
Member Author

maleadt commented Nov 13, 2015

Updated. I force-pushed, because I reverted the change from Ptr{UInt8} to Cstring, as I'm going to keep that for another PR.

@vtjnash
Copy link
Member

vtjnash commented Nov 13, 2015

Can you explain the purpose of this PR? The Win32 API doesn't support suffixes (https://msdn.microsoft.com/en-us/library/windows/desktop/aa364991%28v=vs.85%29.aspx) so that would need to be reimplemented from scratch, and suffixes are not used much on unix systems so they should not have much value there.

@maleadt
Copy link
Member Author

maleadt commented Nov 14, 2015

I was using Julia to interface with an application whose behaviour depends on the file extension, hence I needed to create temporary files with a specific suffix. Didn't seem unreasonable to me, and I had used Python's tempfile.TemporaryFile before which does support such suffixes (cpython completely reimplements this logic, not relying on OS APIs).

I botched the Windows version though, not understanding tempname creates the file on Windows. One option would be to rename the file, replacing the .tmp it is guaranteed to contain to the requested suffix. But then the file isn't guaranteed to be unique; just add a check, or really reimplement from scratch?

Or I just close this PR and don't add support for suffixes...

@tkelman
Copy link
Contributor

tkelman commented Nov 14, 2015

ref #9053, still need to fix that

close(f)
@test isfile(p) == true
@test endswith(p, ".foo") == true
rm(p)
Copy link
Member

Choose a reason for hiding this comment

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

You can use do block in mktemp to avoid both close and rm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants