Skip to content

TSCLibc: add an implementation of mkdtemp for Windows #110

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

Merged
merged 1 commit into from
Aug 18, 2020
Merged
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
37 changes: 36 additions & 1 deletion Sources/TSCLibc/libc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,45 @@ public func realpath(
}

// char *mkdtemp(char *template);
// NOTE(compnerd) this is unsafe! This assumes that the template is *ASCII*.
public func mkdtemp(
_ template: UnsafeMutablePointer<CChar>?
) -> UnsafeMutablePointer<CChar>? {
fatalError("mkdtemp is unimplemented")
Copy link
Contributor

Choose a reason for hiding this comment

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

was this just not used before?

Copy link
Member Author

@compnerd compnerd Aug 11, 2020

Choose a reason for hiding this comment

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

It "was" in the sense that s-p-m never got to the point of building stuff previously. I've had this change laying around for ages (well had a version of it that worked well enough). Just draining changes that will allow s-p-m to get further :) I think that this might be better to implement using Foundation.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah gotcha

// Although the signature of the function is `char *(*)(char *)`, the C
// library treats it as `char *(*)(char * _Nonull)`. Most implementations
// will simply use and trigger a segmentation fault on x86 (and similar faults
// on other architectures) when the memory is accessed. This roughly emulates
// that by terminating in the case even though it is possible for us to return
// an error.
guard let template = template else { fatalError() }

let length: Int = strlen(template)

// Validate the precondition: the template must terminate with 6 `X` which
// will be filled in to generate a unique directory.
guard length >= 6, memcmp(template + length - 6, "XXXXXX", 6) == 0 else {
_set_errno(EINVAL)
return nil
}

let stampSuffix = { (buffer: UnsafeMutablePointer<CChar>) in
let alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
_ = (0 ..< 6).map { index in
buffer[index] = CChar(alpha.shuffled().randomElement()!.utf8.first!)
}
}

// Attempt to create the directory
var retries: Int = 100
repeat {
stampSuffix(template + length - 6)
if _mkdir(template) == 0 {
return template
}
retries = retries - 1
} while retries > 0

return nil
}

// int mkstemps(char *template, int suffixlen);
Expand Down