Skip to content
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

OpenBSD support. #1126

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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 @@ -36,6 +36,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 @@ -612,6 +612,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 @@ -482,7 +482,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 @@ -644,7 +644,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 @@ -733,6 +733,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 @@ -945,7 +948,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
var size = flistxattr(srcFD, nil, 0)
if size > 0 {
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
2 changes: 2 additions & 0 deletions Sources/_FoundationCShims/include/_CStdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@
#ifndef TZDEFAULT
#define TZDEFAULT "/etc/localtime"
#endif /* !defined TZDEFAULT */
#else
#error "define TZDIR and TZDEFAULT for this platform
#endif /* TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD */

#endif
Expand Down