Skip to content

Commit dc9c45d

Browse files
3405691582theMomax
authored andcommitted
OpenBSD support. (#1126)
* Advise porter on where to make necessary change. In #1075 the change was already made for BSD (thank you!); my working edit had this guidance to ensure future porters get an error directing them where to make a necessary change. Otherwise, the FoundationEssentials build will fail and complain these variables are not defined but not have guidance as to where they are sourced from. * OpenBSD does not support extended attributes. * OpenBSD does not have secure_getenv. * Remaining OpenBSD changes. * OpenBSD also needs `pthread_mutex_t?`. * Originally I followed Darwin's check with `d_namlen`, but this should work too. * Correct statvfs type casts for OpenBSD. On OpenBSD, fsblkcnt_t -- the type of f_blocks -- is a UInt64; therefore, so must `blockSize` be. Ultimately, both sides of the `totalSizeBytes` multiplication should probably be type cast for all platforms, but that's a more significant functional change for another time. * Default activeProcessorCount to 1, not 0. 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. * Use sysconf for activeProcessorCount. This is what Dispatch does in some places for OpenBSD anyway, so do likewise here.
1 parent 5fa5607 commit dc9c45d

11 files changed

+25
-12
lines changed

Sources/FoundationEssentials/Data/Data+Reading.swift

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ func _fgetxattr(_ fd: Int32, _ name: UnsafePointer<CChar>!, _ value: UnsafeMutab
4040
return fgetxattr(fd, name, value, size, position, options)
4141
#elseif os(FreeBSD)
4242
return extattr_get_fd(fd, EXTATTR_NAMESPACE_USER, name, value, size)
43+
#elseif os(OpenBSD)
44+
return -1
4345
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
4446
return fgetxattr(fd, name, value, size)
4547
#else

Sources/FoundationEssentials/Data/Data+Writing.swift

+2
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,8 @@ private func writeExtendedAttributes(fd: Int32, attributes: [String : Data]) {
639639
_ = fsetxattr(fd, key, valueBuf.baseAddress!, valueBuf.count, 0, 0)
640640
#elseif os(FreeBSD)
641641
_ = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, key, valueBuf.baseAddress!, valueBuf.count)
642+
#elseif os(OpenBSD)
643+
return
642644
#elseif canImport(Glibc) || canImport(Musl)
643645
_ = fsetxattr(fd, key, valueBuf.baseAddress!, valueBuf.count, 0)
644646
#endif

Sources/FoundationEssentials/FileManager/FileManager+Directories.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ extension _FileManagerImpl {
201201
}
202202
}
203203
return results
204-
#elseif os(WASI)
204+
#elseif os(WASI) || os(OpenBSD)
205205
// wasi-libc does not support FTS for now
206206
throw CocoaError.errorWithFilePath(.featureUnsupported, path)
207207
#else

Sources/FoundationEssentials/FileManager/FileManager+Files.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ extension _FileManagerImpl {
485485
#endif
486486
}
487487

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

648648
var attributes = statAtPath.fileAttributes
649649
try? Self._catInfo(for: URL(filePath: path, directoryHint: .isDirectory), statInfo: statAtPath, into: &attributes)
650-
#if !os(WASI) // WASI does not support extended attributes
650+
#if !os(WASI) && !os(OpenBSD)
651651
if let extendedAttrs = try? _extendedAttributes(at: fsRep, followSymlinks: false) {
652652
attributes[._extendedAttributes] = extendedAttrs
653653
}
@@ -736,6 +736,9 @@ extension _FileManagerImpl {
736736
#if canImport(Darwin)
737737
let fsNumber = result.f_fsid.val.0
738738
let blockSize = UInt64(result.f_bsize)
739+
#elseif os(OpenBSD)
740+
let fsNumber = result.f_fsid
741+
let blockSize = UInt64(result.f_bsize)
739742
#else
740743
let fsNumber = result.f_fsid
741744
let blockSize = UInt(result.f_frsize)
@@ -950,7 +953,7 @@ extension _FileManagerImpl {
950953
try Self._setCatInfoAttributes(attributes, path: path)
951954

952955
if let extendedAttrs = attributes[.init("NSFileExtendedAttributes")] as? [String : Data] {
953-
#if os(WASI)
956+
#if os(WASI) || os(OpenBSD)
954957
// WASI does not support extended attributes
955958
throw CocoaError.errorWithFilePath(.featureUnsupported, path)
956959
#elseif canImport(Android)

Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ extension _FileManagerImpl {
178178
#endif
179179
}
180180

181-
#if !os(Windows) && !os(WASI)
181+
#if !os(Windows) && !os(WASI) && !os(OpenBSD)
182182
static func _setAttribute(_ key: UnsafePointer<CChar>, value: Data, at path: UnsafePointer<CChar>, followSymLinks: Bool) throws {
183183
try value.withUnsafeBytes { buffer in
184184
#if canImport(Darwin)

Sources/FoundationEssentials/FileManager/FileOperations+Enumeration.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,8 @@ struct _POSIXDirectoryContentsSequence: Sequence {
354354
continue
355355
}
356356
#endif
357-
#if os(FreeBSD)
357+
358+
#if os(FreeBSD) || os(OpenBSD)
358359
guard dent.pointee.d_fileno != 0 else {
359360
continue
360361
}
@@ -363,6 +364,7 @@ struct _POSIXDirectoryContentsSequence: Sequence {
363364
continue
364365
}
365366
#endif
367+
366368
// Use name
367369
let fileName: String
368370
#if os(WASI)

Sources/FoundationEssentials/FileManager/FileOperations.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ enum _FileOperations {
921921
}
922922
var current: off_t = 0
923923

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

959959
#if !canImport(Darwin)
960960
private static func _copyDirectoryMetadata(srcFD: CInt, srcPath: @autoclosure () -> String, dstFD: CInt, dstPath: @autoclosure () -> String, delegate: some LinkOrCopyDelegate) throws {
961-
#if !os(WASI) && !os(Android)
961+
#if !os(WASI) && !os(Android) && !os(OpenBSD)
962962
// Copy extended attributes
963963
#if os(FreeBSD)
964964
// FreeBSD uses the `extattr_*` calls for setting extended attributes. Unlike like, the namespace for the extattrs are not determined by prefix of the attribute

Sources/FoundationEssentials/LockedState.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ package struct LockedState<State> {
3131
private struct _Lock {
3232
#if canImport(os)
3333
typealias Primitive = os_unfair_lock
34-
#elseif os(FreeBSD)
34+
#elseif os(FreeBSD) || os(OpenBSD)
3535
typealias Primitive = pthread_mutex_t?
3636
#elseif canImport(Bionic) || canImport(Glibc) || canImport(Musl)
3737
typealias Primitive = pthread_mutex_t

Sources/FoundationEssentials/Platform.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ extension Platform {
224224
// MARK: - Environment Variables
225225
extension Platform {
226226
static func getEnvSecure(_ name: String) -> String? {
227-
#if canImport(Glibc)
227+
#if canImport(Glibc) && !os(OpenBSD)
228228
if let value = secure_getenv(name) {
229229
return String(cString: value)
230230
} else {

Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ extension _ProcessInfo {
469469
return 0
470470
}
471471
return Int(count)
472-
#elseif os(Linux) || os(FreeBSD) || canImport(Android)
472+
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || canImport(Android)
473473
#if os(Linux)
474474
if let fsCount = Self.fsCoreCount() {
475475
return fsCount
@@ -481,7 +481,7 @@ extension _ProcessInfo {
481481
GetSystemInfo(&sysInfo)
482482
return sysInfo.dwActiveProcessorMask.nonzeroBitCount
483483
#else
484-
return 0
484+
return 1
485485
#endif
486486
}
487487

Sources/_FoundationCShims/include/_CStdlib.h

+4
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@
156156
#ifndef TZDEFAULT
157157
#define TZDEFAULT "/etc/localtime"
158158
#endif /* !defined TZDEFAULT */
159+
#elif TARGET_OS_WINDOWS
160+
/* not required */
161+
#else
162+
#error "possibly define TZDIR and TZDEFAULT for this platform"
159163
#endif /* TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD */
160164

161165
#endif

0 commit comments

Comments
 (0)