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

Implement File.tempfile in Crystal #12111

Merged

Conversation

straight-shoota
Copy link
Member

This patch implements tempfile creation natively in Crystal, instead of relying on mkstemp on some platforms.
We already used a custom implementation on Windows, but it lacked reliability in case of a name collision and produced overly long file names. So the new implementation is a complete rewrite.

Resolves #12082

@straight-shoota straight-shoota changed the title Extract low-level Crystal::System::File.open variant Implement File.tempfile in Crystal Jun 10, 2022
@straight-shoota straight-shoota self-assigned this Dec 8, 2022
@Blacksmoke16
Copy link
Member

Blacksmoke16 commented Dec 8, 2022

Somewhat related: #12393. I have a draft PR that also revamps some how files are opened on Windows. I'll have to see if this PR fixes that as well, then could just close that other PR ideally.

@@ -10,6 +10,7 @@ lib LibC
FD_CLOEXEC = 1
O_CLOEXEC = 0
O_CREAT = 1_u16 << 12
O_EXCL = 4_u16 << 12
Copy link
Contributor

Choose a reason for hiding this comment

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

Just asking, did you go through the system headers of all supported systems to grab those O_EXCL values?

Copy link
Member Author

@straight-shoota straight-shoota Feb 14, 2023

Choose a reason for hiding this comment

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

I don't recall doing that explicitly. That's the problem with ridiculously long review timespans (poke @beta-ziliani 😏).
But I'm pretty sure I must have. The values are different, and I don't think I rolled a dice 😆
Perhaps I didn't go through all original headers though and grabbed the values from some other implementation in Go instead. Can't say.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is the WASI definition I could find: https://github.com/WebAssembly/wasi-libc/blob/8daaba387ce70a6088afe3916e9e16ed1c2bc630/libc-bottom-half/headers/public/__header_fcntl.h

Then it looks like Linux uses 0200 whereas the BSDs (including Darwin) use 0x0800. So those values are probably correct

spec/std/file/tempfile_spec.cr Outdated Show resolved Hide resolved
src/file.cr Show resolved Hide resolved
@@ -47,6 +47,43 @@ module Crystal::System::File
m | o
end

LOWER_ALPHANUM = "0123456789abcdefghijklmnopqrstuvwxyz".to_slice

def self.mktemp(prefix : String?, suffix : String?, dir : String, random : ::Random = ::Random::DEFAULT) : {LibC::Int, String}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason the name generation logic here needs to be distinct from ::File.tempname? That method also includes the process ID and the current time. Could we include them here? Could we get rid of them there?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I considered the implications for tempname that but it's probably better done in a follow up to keep things simple.

src/crystal/system/file.cr Outdated Show resolved Hide resolved
@straight-shoota straight-shoota merged commit 107970e into crystal-lang:master Feb 23, 2023
@straight-shoota straight-shoota deleted the feature/native-mktemp branch February 23, 2023 10:25
@@ -47,6 +47,43 @@ module Crystal::System::File
m | o
end

LOWER_ALPHANUM = "0123456789abcdefghijklmnopqrstuvwxyz".to_slice
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be private?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's in the Crystal::System namespace, so inherently non-public.

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

Successfully merging this pull request may close these issues.

Crystal implementation of File#tempfile
4 participants