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

[FreeBSD] Fix FreeBSD build/support #1075

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 @@ -34,6 +34,8 @@ import WASILibc
func _fgetxattr(_ fd: Int32, _ name: UnsafePointer<CChar>!, _ value: UnsafeMutableRawPointer!, _ size: Int, _ position: UInt32, _ options: Int32) -> Int {
#if canImport(Darwin)
return fgetxattr(fd, name, value, size, position, options)
#elseif os(FreeBSD)
return extattr_get_fd(fd, EXTATTR_NAMESPACE_USER, name, value, size)
#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 @@ -606,6 +606,8 @@ private func writeExtendedAttributes(fd: Int32, attributes: [String : Data]) {
// Returns non-zero on error, but we ignore them
#if canImport(Darwin)
_ = 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 canImport(Glibc) || canImport(Musl)
_ = fsetxattr(fd, key, valueBuf.baseAddress!, valueBuf.count, 0)
#endif
Expand Down
6 changes: 3 additions & 3 deletions Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ extension POSIXError {
return .EMULTIHOP
}

#if !os(WASI)
#if !os(WASI) && !os(FreeBSD)
/// No message available on STREAM.
public static var ENODATA: POSIXErrorCode {
return .ENODATA
Expand All @@ -635,7 +635,7 @@ extension POSIXError {
return .ENOLINK
}

#if !os(WASI)
#if !os(WASI) && !os(FreeBSD)
/// No STREAM resources.
public static var ENOSR: POSIXErrorCode {
return .ENOSR
Expand All @@ -653,7 +653,7 @@ extension POSIXError {
return .EPROTO
}

#if !os(OpenBSD) && !os(WASI)
#if !os(OpenBSD) && !os(WASI) && !os(FreeBSD)
/// STREAM ioctl timeout.
public static var ETIME: POSIXErrorCode {
return .ETIME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ extension _FileManagerImpl {
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)
#elseif os(FreeBSD)
var size = (followSymlinks ? extattr_get_file : extattr_get_link)(path, EXTATTR_NAMESPACE_USER, key, nil, 0)
#else
var size = followSymlinks ? getxattr(path, key, nil, 0) : lgetxattr(path, key, nil, 0)
#endif
Expand All @@ -498,6 +500,8 @@ extension _FileManagerImpl {
let buffer = malloc(size)!
#if canImport(Darwin)
size = getxattr(path, key, buffer, size, 0, followSymlinks ? 0 : XATTR_NOFOLLOW)
#elseif os(FreeBSD)
size = (followSymlinks ? extattr_get_file : extattr_get_link)(path, EXTATTR_NAMESPACE_USER, key, buffer, size)
#else
size = followSymlinks ? getxattr(path, key, buffer, size) : lgetxattr(path, key, buffer, size)
#endif
Expand All @@ -516,6 +520,8 @@ extension _FileManagerImpl {
private func _extendedAttributes(at path: UnsafePointer<CChar>, followSymlinks: Bool) throws -> [String : Data]? {
#if canImport(Darwin)
var size = listxattr(path, nil, 0, 0)
#elseif os(FreeBSD)
var size = (followSymlinks ? extattr_list_file : extattr_list_link)(path, EXTATTR_NAMESPACE_USER, nil, 0)
#else
var size = listxattr(path, nil, 0)
#endif
Expand All @@ -524,6 +530,8 @@ extension _FileManagerImpl {
defer { keyList.deallocate() }
#if canImport(Darwin)
size = listxattr(path, keyList.baseAddress!, size, 0)
#elseif os(FreeBSD)
size = (followSymlinks ? extattr_list_file : extattr_list_link)(path, EXTATTR_NAMESPACE_USER, nil, 0)
#else
size = listxattr(path, keyList.baseAddress!, size)
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ extension _FileManagerImpl {
try value.withUnsafeBytes { buffer in
#if canImport(Darwin)
let result = setxattr(path, key, buffer.baseAddress!, buffer.count, 0, followSymLinks ? 0 : XATTR_NOFOLLOW)
#elseif os(FreeBSD)
var result: Int32
if followSymLinks {
result = Int32(extattr_set_file(path, EXTATTR_NAMESPACE_USER, key, buffer.baseAddress!, buffer.count))
} else {
result = Int32(extattr_set_link(path, EXTATTR_NAMESPACE_USER, key, buffer.baseAddress!, buffer.count))
}
#else
var result: Int32
if followSymLinks {
Expand All @@ -191,6 +198,7 @@ extension _FileManagerImpl {
result = setxattr(path, key, buffer.baseAddress!, buffer.count, 0)
}
#endif

#if os(macOS) && FOUNDATION_FRAMEWORK
// if setxaddr failed and its a permission error for a sandbox app trying to set quaratine attribute, ignore it since its not
// permitted, the attribute will be put on the file by the quaratine MAC hook
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,15 @@ struct _POSIXDirectoryContentsSequence: Sequence {
continue
}
#endif
#if os(FreeBSD)
guard dent.pointee.d_fileno != 0 else {
continue
}
#else
guard dent.pointee.d_ino != 0 else {
continue
}
#endif
// Use name
let fileName: String
#if os(WASI)
Expand Down
7 changes: 7 additions & 0 deletions Sources/FoundationEssentials/FileManager/FileOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -900,10 +900,17 @@ enum _FileOperations {
}
#else
while current < total {
#if os(FreeBSD)
guard copy_file_range(srcfd, &current, dstfd, nil, total - Int(current), 0) != -1 else {
try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr))
return
}
#else
guard sendfile(dstfd, srcfd, &current, Swift.min(total - Int(current), chunkSize)) != -1 else {
try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr))
return
}
#endif
}
#endif
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/FoundationEssentials/LockedState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ package struct LockedState<State> {
private struct _Lock {
#if canImport(os)
typealias Primitive = os_unfair_lock
#elseif os(FreeBSD)
typealias Primitive = pthread_mutex_t?
#elseif canImport(Bionic) || canImport(Glibc) || canImport(Musl)
typealias Primitive = pthread_mutex_t
#elseif canImport(WinSDK)
Expand Down
4 changes: 4 additions & 0 deletions Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ final class _ProcessInfo: Sendable {
let time: UInt64 = ullTime
#else
var ts: timespec = timespec()
#if os(FreeBSD) || os(OpenBSD)
clock_gettime(CLOCK_MONOTONIC, &ts)
#else
clock_gettime(CLOCK_MONOTONIC_RAW, &ts)
#endif
let time: UInt64 = UInt64(ts.tv_sec) * 1000000000 + UInt64(ts.tv_nsec)
#endif
let timeString = String(time, radix: 16, uppercase: true)
Expand Down
4 changes: 2 additions & 2 deletions Sources/_FoundationCShims/include/_CStdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@
#include <tzfile.h>
#else

#if TARGET_OS_MAC || TARGET_OS_LINUX
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
#ifndef TZDIR
#define TZDIR "/usr/share/zoneinfo/" /* Time zone object file directory */
#endif /* !defined TZDIR */

#ifndef TZDEFAULT
#define TZDEFAULT "/etc/localtime"
#endif /* !defined TZDEFAULT */
#endif /* TARGET_OS_MAC || TARGET_OS_LINUX */
#endif /* TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD */

#endif

Expand Down