Skip to content

OpenBSD support. #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

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ let package = Package(
"SWBCSupport",
"SWBLibc",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Crypto", package: "swift-crypto", condition: .when(platforms: [.linux, .android])),
.product(name: "Crypto", package: "swift-crypto", condition: .when(platforms: [.linux, .openbsd, .android])),
.product(name: "SystemPackage", package: "swift-system", condition: .when(platforms: [.linux, .android, .windows])),
],
exclude: ["CMakeLists.txt"],
Expand Down
8 changes: 4 additions & 4 deletions Sources/SWBUtil/FSProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ class LocalFS: FSProxy, @unchecked Sendable {
let UTIME_OMIT = 1073741822
#endif
let atime = timespec(tv_sec: 0, tv_nsec: Int(UTIME_OMIT))
let mtime = timespec(tv_sec: timestamp, tv_nsec: 0)
let mtime = timespec(tv_sec: time_t(timestamp), tv_nsec: 0)
guard utimensat(AT_FDCWD, path.str, [atime, mtime], 0) == 0 else {
throw POSIXError(errno, context: "utimensat", "AT_FDCWD", path.str, String(timestamp))
}
Expand Down Expand Up @@ -1390,7 +1390,7 @@ public class PseudoFS: FSProxy, @unchecked Sendable {
#if os(Windows)
info.st_mtimespec = timespec(tv_sec: Int64(node.timestamp), tv_nsec: 0)
#else
info.st_mtimespec = timespec(tv_sec: node.timestamp, tv_nsec: 0)
info.st_mtimespec = timespec(tv_sec: time_t(node.timestamp), tv_nsec: 0)
#endif
info.st_size = off_t(contents.bytes.count)
info.st_dev = node.device
Expand All @@ -1403,7 +1403,7 @@ public class PseudoFS: FSProxy, @unchecked Sendable {
info.st_mtimespec = timespec(tv_sec: Int64(node.timestamp), tv_nsec: 0)
#else
info.st_mode = S_IFDIR
info.st_mtimespec = timespec(tv_sec: node.timestamp, tv_nsec: 0)
info.st_mtimespec = timespec(tv_sec: time_t(node.timestamp), tv_nsec: 0)
#endif
info.st_size = off_t(dir.contents.count)
info.st_dev = node.device
Expand All @@ -1416,7 +1416,7 @@ public class PseudoFS: FSProxy, @unchecked Sendable {
info.st_mtimespec = timespec(tv_sec: Int64(node.timestamp), tv_nsec: 0)
#else
info.st_mode = S_IFLNK
info.st_mtimespec = timespec(tv_sec: node.timestamp, tv_nsec: 0)
info.st_mtimespec = timespec(tv_sec: time_t(node.timestamp), tv_nsec: 0)
#endif
info.st_size = off_t(0)
info.st_dev = node.device
Expand Down
3 changes: 3 additions & 0 deletions Sources/SWBUtil/Lock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public final class Lock: @unchecked Sendable {
#if os(Windows)
@usableFromInline
let mutex: UnsafeMutablePointer<SRWLOCK> = UnsafeMutablePointer.allocate(capacity: 1)
#elseif os(OpenBSD)
@usableFromInline
let mutex: UnsafeMutablePointer<pthread_mutex_t?> = UnsafeMutablePointer.allocate(capacity: 1)
Copy link
Collaborator

@jakepetroules jakepetroules Mar 31, 2025

Choose a reason for hiding this comment

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

@3405691582 Do you know why there's this difference between BSDs and Darwin/Linux? I needed it for FreeBSD as well.

Can we safely just use the Optional variant on all platforms?

Copy link
Member Author

Choose a reason for hiding this comment

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

This happens because OpenBSD's value of pthread_t and subsidiary types like pthread_mutex_t is a pointer, whereas on Linux for example, it's typedef'd to an int (and pthread_mutex_t to a union). Of course, there's no nullability annotations on the 'BSDs, so Swift is interpreting the type as an optional like it would any other nullable pointer.

After thinking about it briefly I think you have an incongruity either way, so I don't think you can use Optional always: you either have to declare, for example, mutex as Optional here and then conditionally check the optional or not depending on platform at the callsite to pthread_mutex_init, or you do what we do here, and conditionally declare mutex as Optional or not.

#else
@usableFromInline
let mutex: UnsafeMutablePointer<pthread_mutex_t> = UnsafeMutablePointer.allocate(capacity: 1)
Expand Down