Skip to content

OpenBSD support. #1126

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 7 commits into from
Apr 7, 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: 2 additions & 0 deletions Sources/FoundationEssentials/Data/Data+Reading.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func _fgetxattr(_ fd: Int32, _ name: UnsafePointer<CChar>!, _ value: UnsafeMutab
return fgetxattr(fd, name, value, size, position, options)
#elseif os(FreeBSD)
return extattr_get_fd(fd, EXTATTR_NAMESPACE_USER, name, value, size)
#elseif os(OpenBSD)
return -1
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
return fgetxattr(fd, name, value, size)
#else
Expand Down
2 changes: 2 additions & 0 deletions Sources/FoundationEssentials/Data/Data+Writing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,8 @@ private func writeExtendedAttributes(fd: Int32, attributes: [String : Data]) {
_ = fsetxattr(fd, key, valueBuf.baseAddress!, valueBuf.count, 0, 0)
#elseif os(FreeBSD)
_ = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, key, valueBuf.baseAddress!, valueBuf.count)
#elseif os(OpenBSD)
return
#elseif canImport(Glibc) || canImport(Musl)
_ = fsetxattr(fd, key, valueBuf.baseAddress!, valueBuf.count, 0)
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ extension _FileManagerImpl {
}
}
return results
#elseif os(WASI)
#elseif os(WASI) || os(OpenBSD)
// wasi-libc does not support FTS for now
throw CocoaError.errorWithFilePath(.featureUnsupported, path)
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ extension _FileManagerImpl {
#endif
}

#if !os(Windows) && !os(WASI)
#if !os(Windows) && !os(WASI) && !os(OpenBSD)
private func _extendedAttribute(_ key: UnsafePointer<CChar>, at path: UnsafePointer<CChar>, followSymlinks: Bool) throws -> Data? {
#if canImport(Darwin)
var size = getxattr(path, key, nil, 0, 0, followSymlinks ? 0 : XATTR_NOFOLLOW)
Expand Down Expand Up @@ -647,7 +647,7 @@ extension _FileManagerImpl {

var attributes = statAtPath.fileAttributes
try? Self._catInfo(for: URL(filePath: path, directoryHint: .isDirectory), statInfo: statAtPath, into: &attributes)
#if !os(WASI) // WASI does not support extended attributes
#if !os(WASI) && !os(OpenBSD)
if let extendedAttrs = try? _extendedAttributes(at: fsRep, followSymlinks: false) {
attributes[._extendedAttributes] = extendedAttrs
}
Expand Down Expand Up @@ -736,6 +736,9 @@ extension _FileManagerImpl {
#if canImport(Darwin)
let fsNumber = result.f_fsid.val.0
let blockSize = UInt64(result.f_bsize)
#elseif os(OpenBSD)
let fsNumber = result.f_fsid
let blockSize = UInt64(result.f_bsize)
#else
let fsNumber = result.f_fsid
let blockSize = UInt(result.f_frsize)
Expand Down Expand Up @@ -950,7 +953,7 @@ extension _FileManagerImpl {
try Self._setCatInfoAttributes(attributes, path: path)

if let extendedAttrs = attributes[.init("NSFileExtendedAttributes")] as? [String : Data] {
#if os(WASI)
#if os(WASI) || os(OpenBSD)
// WASI does not support extended attributes
throw CocoaError.errorWithFilePath(.featureUnsupported, path)
#elseif canImport(Android)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ extension _FileManagerImpl {
#endif
}

#if !os(Windows) && !os(WASI)
#if !os(Windows) && !os(WASI) && !os(OpenBSD)
static func _setAttribute(_ key: UnsafePointer<CChar>, value: Data, at path: UnsafePointer<CChar>, followSymLinks: Bool) throws {
try value.withUnsafeBytes { buffer in
#if canImport(Darwin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ struct _POSIXDirectoryContentsSequence: Sequence {
continue
}
#endif
#if os(FreeBSD)

#if os(FreeBSD) || os(OpenBSD)
guard dent.pointee.d_fileno != 0 else {
continue
}
Expand All @@ -363,6 +364,7 @@ struct _POSIXDirectoryContentsSequence: Sequence {
continue
}
#endif

// Use name
let fileName: String
#if os(WASI)
Expand Down
4 changes: 2 additions & 2 deletions Sources/FoundationEssentials/FileManager/FileOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ enum _FileOperations {
}
var current: off_t = 0

#if os(WASI)
#if os(WASI) || os(OpenBSD)
// WASI doesn't have sendfile, so we need to do it in user space with read/write
try withUnsafeTemporaryAllocation(of: UInt8.self, capacity: chunkSize) { buffer in
while current < total {
Expand Down Expand Up @@ -958,7 +958,7 @@ enum _FileOperations {

#if !canImport(Darwin)
private static func _copyDirectoryMetadata(srcFD: CInt, srcPath: @autoclosure () -> String, dstFD: CInt, dstPath: @autoclosure () -> String, delegate: some LinkOrCopyDelegate) throws {
#if !os(WASI) && !os(Android)
#if !os(WASI) && !os(Android) && !os(OpenBSD)
// Copy extended attributes
#if os(FreeBSD)
// FreeBSD uses the `extattr_*` calls for setting extended attributes. Unlike like, the namespace for the extattrs are not determined by prefix of the attribute
Expand Down
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/LockedState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ package struct LockedState<State> {
private struct _Lock {
#if canImport(os)
typealias Primitive = os_unfair_lock
#elseif os(FreeBSD)
#elseif os(FreeBSD) || os(OpenBSD)
typealias Primitive = pthread_mutex_t?
#elseif canImport(Bionic) || canImport(Glibc) || canImport(Musl)
typealias Primitive = pthread_mutex_t
Expand Down
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ extension Platform {
// MARK: - Environment Variables
extension Platform {
static func getEnvSecure(_ name: String) -> String? {
#if canImport(Glibc)
#if canImport(Glibc) && !os(OpenBSD)
if let value = secure_getenv(name) {
return String(cString: value)
} else {
Expand Down
4 changes: 2 additions & 2 deletions Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ extension _ProcessInfo {
return 0
}
return Int(count)
#elseif os(Linux) || os(FreeBSD) || canImport(Android)
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || canImport(Android)
#if os(Linux)
if let fsCount = Self.fsCoreCount() {
return fsCount
Expand All @@ -481,7 +481,7 @@ extension _ProcessInfo {
GetSystemInfo(&sysInfo)
return sysInfo.dwActiveProcessorMask.nonzeroBitCount
#else
return 0
return 1
Copy link
Member

Choose a reason for hiding this comment

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

Why change the default?

Copy link
Member Author

@3405691582 3405691582 Apr 6, 2025

Choose a reason for hiding this comment

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

Discussed in the commit message, which I didn't transpose here, sorry:

After a rather tedious debugging session trying to figure out why
swiftpm-bootstrap appeared to be deadlocked, this turned out to be the
culprit. Perhaps this should be #error instead, but for now, set a
sensible default.

spm's bootstrap sets up an operation queue with activeProcessorCount. If this isn't twiddled, this will be 0, so you have an operation queue that doesn't process, and isn't immediately clear this is the effect unless you dig very deep. It doesn't make sense to have the active processor count to be 0 as I understand it, hence the default change.

Copy link
Member

Choose a reason for hiding this comment

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

OK, I didn't read all the commit messages, just looked at the final patch. I guess that's up to the Foundation people, just wait till they chime in before merging.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense to me.

#endif
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/_FoundationCShims/include/_CStdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@
#ifndef TZDEFAULT
#define TZDEFAULT "/etc/localtime"
#endif /* !defined TZDEFAULT */
#elif TARGET_OS_WINDOWS
/* not required */
#else
#error "possibly define TZDIR and TZDEFAULT for this platform"
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we going to cause a new error on any other platforms with this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Prior to this change, if there are other platforms that require TZDIR/TZDEFAULT that aren't TARGET_OS_MAC, TARGET_OS_LINUX, TARGET_OS_BSD or TARGET_OS_WINDOWS, this manifests as a missing symbol error where they are referenced, so there should be no new errors.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good

#endif /* TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD */

#endif
Expand Down