Skip to content

Commit c14d7d6

Browse files
authored
More changes to support building with Musl. (#2628)
Use direct syscall for `renameat2` because Musl doesn't include a wrapper for it. Similarly, define the constants ourselves. Add Musl imports in relevant places.
1 parent a0e0c68 commit c14d7d6

File tree

13 files changed

+87
-31
lines changed

13 files changed

+87
-31
lines changed

Sources/CNIOLinux/shim.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void CNIOLinux_i_do_nothing_just_working_around_a_darwin_toolchain_bug(void) {}
2727
#include <sched.h>
2828
#include <stdio.h>
2929
#include <sys/prctl.h>
30+
#include <sys/syscall.h>
3031
#include <sys/utsname.h>
3132
#include <unistd.h>
3233
#include <assert.h>
@@ -189,13 +190,19 @@ int CNIOLinux_system_info(struct utsname* uname_data) {
189190
const unsigned long CNIOLinux_IOCTL_VM_SOCKETS_GET_LOCAL_CID = IOCTL_VM_SOCKETS_GET_LOCAL_CID;
190191

191192
const char* CNIOLinux_dirent_dname(struct dirent* ent) {
192-
return ent->d_name;
193+
return ent->d_name;
193194
}
194195

195196
int CNIOLinux_renameat2(int oldfd, const char* old, int newfd, const char* newName, unsigned int flags) {
196-
return renameat2(oldfd, old, newfd, newName, flags);
197+
// Musl doesn't have renameat2, so we make the raw system call directly
198+
return syscall(SYS_renameat2, oldfd, old, newfd, newName, flags);
197199
}
198200

201+
// Musl also doesn't define the flags for renameat2, so we will do so.
202+
// Again, we may as well do this unconditionally.
203+
#define RENAME_NOREPLACE 1
204+
#define RENAME_EXCHANGE 2
205+
199206
const int CNIOLinux_O_TMPFILE = O_TMPFILE;
200207
const unsigned int CNIOLinux_RENAME_NOREPLACE = RENAME_NOREPLACE;
201208
const unsigned int CNIOLinux_RENAME_EXCHANGE = RENAME_EXCHANGE;

Sources/NIOFileSystem/FileInfo.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import SystemPackage
1818
import Darwin
1919
#elseif canImport(Glibc)
2020
import Glibc
21+
#elseif canImport(Musl)
22+
import Musl
2123
#endif
2224

2325
/// Information about a file system object.
@@ -80,7 +82,7 @@ public struct FileInfo: Hashable, Sendable {
8082
self.lastAccessTime = Timespec(platformSpecificStatus.st_atimespec)
8183
self.lastDataModificationTime = Timespec(platformSpecificStatus.st_mtimespec)
8284
self.lastStatusChangeTime = Timespec(platformSpecificStatus.st_ctimespec)
83-
#elseif canImport(Glibc)
85+
#elseif canImport(Glibc) || canImport(Musl)
8486
self.lastAccessTime = Timespec(platformSpecificStatus.st_atim)
8587
self.lastDataModificationTime = Timespec(platformSpecificStatus.st_mtim)
8688
self.lastStatusChangeTime = Timespec(platformSpecificStatus.st_ctim)
@@ -190,7 +192,7 @@ private struct Stat: Hashable {
190192
hasher.combine(FileInfo.Timespec(stat.st_birthtimespec))
191193
hasher.combine(stat.st_flags)
192194
hasher.combine(stat.st_gen)
193-
#elseif canImport(Glibc)
195+
#elseif canImport(Glibc) || canImport(Musl)
194196
hasher.combine(FileInfo.Timespec(stat.st_atim))
195197
hasher.combine(FileInfo.Timespec(stat.st_mtim))
196198
hasher.combine(FileInfo.Timespec(stat.st_ctim))
@@ -231,7 +233,7 @@ private struct Stat: Hashable {
231233
== FileInfo.Timespec(rStat.st_birthtimespec)
232234
isEqual = isEqual && lStat.st_flags == rStat.st_flags
233235
isEqual = isEqual && lStat.st_gen == rStat.st_gen
234-
#elseif canImport(Glibc)
236+
#elseif canImport(Glibc) || canImport(Musl)
235237
isEqual = isEqual && FileInfo.Timespec(lStat.st_atim) == FileInfo.Timespec(rStat.st_atim)
236238
isEqual = isEqual && FileInfo.Timespec(lStat.st_mtim) == FileInfo.Timespec(rStat.st_mtim)
237239
isEqual = isEqual && FileInfo.Timespec(lStat.st_ctim) == FileInfo.Timespec(rStat.st_ctim)

Sources/NIOFileSystem/FileSystem.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import NIOCore
2020
import Darwin
2121
#elseif canImport(Glibc)
2222
import Glibc
23+
#elseif canImport(Musl)
24+
import Musl
2325
#endif
2426

2527
/// A file system which interacts with the local system. The file system uses a thread pool to
@@ -1146,7 +1148,7 @@ extension FileSystem {
11461148
location: .here()
11471149
)
11481150
}
1149-
#elseif canImport(Glibc)
1151+
#elseif canImport(Glibc) || canImport(Musl)
11501152
var offset = 0
11511153

11521154
while offset < sourceInfo.size {

Sources/NIOFileSystem/FileSystemError+Syscall.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import SystemPackage
1818
import Darwin
1919
#elseif canImport(Glibc)
2020
import Glibc
21+
#elseif canImport(Musl)
22+
import Musl
2123
#endif
2224

2325
extension FileSystemError {

Sources/NIOFileSystem/FileType.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import SystemPackage
1818
import Darwin
1919
#elseif canImport(Glibc)
2020
import Glibc
21+
#elseif canImport(Musl)
22+
import Musl
2123
#endif
2224

2325
/// The type of a file system object.
@@ -133,7 +135,7 @@ extension FileType {
133135
/// Initializes a file type from the `d_type` from `dirent`.
134136
@_spi(Testing)
135137
public init(direntType: UInt8) {
136-
#if canImport(Darwin)
138+
#if canImport(Darwin) || canImport(Musl)
137139
let value = Int32(direntType)
138140
#elseif canImport(Glibc)
139141
let value = Int(direntType)

Sources/NIOFileSystem/Internal/Concurrency Primitives/ThreadPosix.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
#if canImport(Glibc)
15+
#if canImport(Glibc) || canImport(Musl)
1616
import CNIOLinux
1717

1818
private let sys_pthread_getname_np = CNIOLinux_pthread_getname_np
@@ -41,8 +41,12 @@ private func sysPthread_create(
4141
) -> CInt {
4242
#if canImport(Darwin)
4343
return pthread_create(handle, nil, destructor, args)
44-
#elseif canImport(Glibc)
44+
#elseif canImport(Glibc) || canImport(Musl)
45+
#if canImport(Glibc)
4546
var handleLinux = pthread_t()
47+
#else
48+
var handleLinux = pthread_t(bitPattern: 0)
49+
#endif
4650
let result = pthread_create(
4751
&handleLinux,
4852
nil,
@@ -61,7 +65,7 @@ enum ThreadOpsPosix: ThreadOps {
6165
typealias ThreadSpecificKey = pthread_key_t
6266
#if canImport(Darwin)
6367
typealias ThreadSpecificKeyDestructor = @convention(c) (UnsafeMutableRawPointer) -> Void
64-
#elseif canImport(Glibc)
68+
#elseif canImport(Glibc) || canImport(Musl)
6569
typealias ThreadSpecificKeyDestructor = @convention(c) (UnsafeMutableRawPointer?) -> Void
6670
#endif
6771

@@ -100,7 +104,7 @@ enum ThreadOpsPosix: ThreadOps {
100104

101105
if let name = name {
102106
let maximumThreadNameLength: Int
103-
#if canImport(Glibc)
107+
#if canImport(Glibc) || canImport(Musl)
104108
maximumThreadNameLength = 15
105109
#elseif canImport(Darwin)
106110
maximumThreadNameLength = .max

Sources/NIOFileSystem/Internal/System Calls/CInterop.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import CNIODarwin
2020
#elseif canImport(Glibc)
2121
import Glibc
2222
import CNIOLinux
23+
#elseif canImport(Musl)
24+
import Musl
25+
import CNIOLinux
2326
#endif
2427

2528
/// Aliases for platform-dependent types used for system calls.
@@ -28,6 +31,8 @@ extension CInterop {
2831
public typealias Stat = Darwin.stat
2932
#elseif canImport(Glibc)
3033
public typealias Stat = Glibc.stat
34+
#elseif canImport(Musl)
35+
public typealias Stat = Musl.stat
3136
#endif
3237

3338
#if canImport(Darwin)
@@ -36,24 +41,29 @@ extension CInterop {
3641
#elseif canImport(Glibc)
3742
@_spi(Testing)
3843
public static let maxPathLength = Glibc.PATH_MAX
44+
#elseif canImport(Musl)
45+
@_spi(Testing)
46+
public static let maxPathLength = Musl.PATH_MAX
3947
#endif
4048

4149
#if canImport(Darwin)
4250
typealias DirPointer = UnsafeMutablePointer<Darwin.DIR>
43-
#elseif canImport(Glibc)
51+
#elseif canImport(Glibc) || canImport(Musl)
4452
typealias DirPointer = OpaquePointer
4553
#endif
4654

4755
#if canImport(Darwin)
4856
typealias DirEnt = Darwin.dirent
4957
#elseif canImport(Glibc)
5058
typealias DirEnt = Glibc.dirent
59+
#elseif canImport(Musl)
60+
typealias DirEnt = Musl.dirent
5161
#endif
5262

5363
#if canImport(Darwin)
5464
typealias FTS = CNIODarwin.FTS
5565
typealias FTSEnt = CNIODarwin.FTSENT
56-
#elseif canImport(Glibc)
66+
#elseif canImport(Glibc) || canImport(Musl)
5767
typealias FTS = CNIOLinux.FTS
5868
typealias FTSEnt = CNIOLinux.FTSENT
5969
#endif

Sources/NIOFileSystem/Internal/System Calls/Errno.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import SystemPackage
1818
import Darwin
1919
#elseif canImport(Glibc)
2020
import Glibc
21+
#elseif canImport(Musl)
22+
import Musl
2123
#endif
2224

2325
extension Errno {
@@ -28,13 +30,17 @@ extension Errno {
2830
return Errno(rawValue: Darwin.errno)
2931
#elseif canImport(Glibc)
3032
return Errno(rawValue: Glibc.errno)
33+
#elseif canImport(Musl)
34+
return Errno(rawValue: Musl.errno)
3135
#endif
3236
}
3337
set {
3438
#if canImport(Darwin)
3539
Darwin.errno = newValue.rawValue
3640
#elseif canImport(Glibc)
3741
Glibc.errno = newValue.rawValue
42+
#elseif canImport(Musl)
43+
Musl.errno = newValue.rawValue
3844
#endif
3945
}
4046
}
@@ -44,6 +50,8 @@ extension Errno {
4450
Darwin.errno = 0
4551
#elseif canImport(Glibc)
4652
Glibc.errno = 0
53+
#elseif canImport(Musl)
54+
Musl.errno = 0
4755
#endif
4856
}
4957
}

Sources/NIOFileSystem/Internal/System Calls/FileDescriptor+Syscalls.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import Darwin
2020
#elseif canImport(Glibc)
2121
import Glibc
2222
import CNIOLinux
23+
#elseif canImport(Musl)
24+
import Musl
25+
import CNIOLinux
2326
#endif
2427

2528
extension FileDescriptor {
@@ -305,7 +308,7 @@ extension FileDescriptor {
305308
}
306309
}
307310

308-
#if canImport(Glibc)
311+
#if canImport(Glibc) || canImport(Musl)
309312
extension FileDescriptor.OpenOptions {
310313
static var temporaryFile: Self {
311314
Self(rawValue: CNIOLinux_O_TMPFILE)

Sources/NIOFileSystem/Internal/System Calls/Mocking.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import Darwin
2828
#elseif canImport(Glibc)
2929
import Glibc
3030
import CNIOLinux
31+
#elseif canImport(Musl)
32+
import Musl
33+
import CNIOLinux
3134
#endif
3235

3336
// Syscall mocking support.
@@ -279,6 +282,11 @@ internal var system_errno: CInt {
279282
get { Glibc.errno }
280283
set { Glibc.errno = newValue }
281284
}
285+
#elseif canImport(Musl)
286+
internal var system_errno: CInt {
287+
get { Musl.errno }
288+
set { Musl.errno = newValue }
289+
}
282290
#endif
283291

284292
// MARK: C stdlib decls

0 commit comments

Comments
 (0)