Skip to content

Implement File.tempfile in Crystal#12111

Merged
straight-shoota merged 8 commits intocrystal-lang:masterfrom
straight-shoota:feature/native-mktemp
Feb 23, 2023
Merged

Implement File.tempfile in Crystal#12111
straight-shoota merged 8 commits intocrystal-lang:masterfrom
straight-shoota:feature/native-mktemp

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.

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


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.

straight-shoota and others added 2 commits February 14, 2023 18:14
Co-authored-by: Quinton Miller <nicetas.c@gmail.com>
@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
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