-
Notifications
You must be signed in to change notification settings - Fork 16
/
swift-android-foundation-release.patch
649 lines (596 loc) · 29.8 KB
/
swift-android-foundation-release.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
diff --git a/swift/stdlib/public/Platform/android.modulemap b/swift/stdlib/public/Platform/android.modulemap
index 78e7bda9976..dc25e5c3668 100644
--- a/swift/stdlib/public/Platform/android.modulemap
+++ b/swift/stdlib/public/Platform/android.modulemap
@@ -556,6 +556,12 @@ module _bits_sa_family_t [system] {
header "bits/sa_family_t.h"
export *
}
+
+module _bits_sockaddr_storage [system] {
+ header "bits/sockaddr_storage.h"
+ export *
+}
+
module _bits_stdatomic [system] {
// Note: this module is not part of 'stdatomic'
// as it depends on libc++ and forcing it to
diff --git a/swift-corelibs-foundation/Package.swift b/swift-corelibs-foundation/Package.swift
--- a/swift-corelibs-foundation/Package.swift
+++ b/swift-corelibs-foundation/Package.swift
@@ -176,7 +176,8 @@
"BlockRuntime",
"CMakeLists.txt"
],
- cSettings: coreFoundationBuildSettings
+ cSettings: coreFoundationBuildSettings,
+ linkerSettings: [.linkedLibrary("log", .when(platforms: [.android]))]
),
.target(
name: "_CFXMLInterface",
diff --git a/swift-corelibs-foundation/Sources/CoreFoundation/include/ForSwiftFoundationOnly.h b/swift-corelibs-foundation/Sources/CoreFoundation/include/ForSwiftFoundationOnly.h
index a2ba56cd..91a312dc 100644
--- a/swift-corelibs-foundation/Sources/CoreFoundation/include/ForSwiftFoundationOnly.h
+++ b/swift-corelibs-foundation/Sources/CoreFoundation/include/ForSwiftFoundationOnly.h
@@ -69,6 +69,13 @@
#include <sys/stat.h>
#include <sys/syscall.h>
#include <termios.h>
+#include <linux/fcntl.h>
+#ifdef __swift__
+// The linux/stat header is private in the Android modulemap.
+#pragma clang module import posix_filesystem.linux_stat
+#else
+#include <linux/stat.h>
+#endif
#elif TARGET_OS_WASI
#include <fcntl.h>
#include <sys/stat.h>
diff --git a/swift-corelibs-foundation/Sources/Foundation/CGFloat.swift b/swift-corelibs-foundation/Sources/Foundation/CGFloat.swift
index ffe3a6c6..c59977f8 100644
--- a/swift-corelibs-foundation/Sources/Foundation/CGFloat.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/CGFloat.swift
@@ -7,6 +7,10 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
+#if canImport(Android)
+import Android
+#endif
+
@frozen
public struct CGFloat: Sendable {
#if arch(i386) || arch(arm) || arch(wasm32)
diff --git a/swift-corelibs-foundation/Sources/Foundation/FileHandle.swift b/swift-corelibs-foundation/Sources/Foundation/FileHandle.swift
index b07c49ac..b540e284 100644
--- a/swift-corelibs-foundation/Sources/Foundation/FileHandle.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/FileHandle.swift
@@ -34,6 +34,11 @@ import WASILibc
fileprivate let _read = WASILibc.read(_:_:_:)
fileprivate let _write = WASILibc.write(_:_:_:)
fileprivate let _close = WASILibc.close(_:)
+#elseif canImport(Android)
+import Android
+fileprivate let _read = Android.read(_:_:_:)
+fileprivate let _write = Android.write(_:_:_:)
+fileprivate let _close = Android.close(_:)
#endif
#if canImport(WinSDK)
@@ -321,10 +326,15 @@ open class FileHandle : NSObject, @unchecked Sendable {
if options.contains(.alwaysMapped) {
// Filesizes are often 64bit even on 32bit systems
let mapSize = min(length, Int(clamping: statbuf.st_size))
+ #if canImport(Android)
+ // Bionic mmap() now returns _Nonnull, so the force unwrap below isn't needed.
let data = mmap(nil, mapSize, PROT_READ, MAP_PRIVATE, _fd, 0)
+ #else
+ let data = mmap(nil, mapSize, PROT_READ, MAP_PRIVATE, _fd, 0)!
+ #endif
// Swift does not currently expose MAP_FAILURE
if data != UnsafeMutableRawPointer(bitPattern: -1) {
- return NSData.NSDataReadResult(bytes: data!, length: mapSize) { buffer, length in
+ return NSData.NSDataReadResult(bytes: data, length: mapSize) { buffer, length in
munmap(buffer, length)
}
}
diff --git a/swift-corelibs-foundation/Sources/Foundation/FileManager+POSIX.swift b/swift-corelibs-foundation/Sources/Foundation/FileManager+POSIX.swift
index e89b3bf6..a82fe1c2 100644
--- a/swift-corelibs-foundation/Sources/Foundation/FileManager+POSIX.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/FileManager+POSIX.swift
@@ -7,6 +7,10 @@
//
#if !os(Windows)
+#if canImport(Android)
+import Android
+#endif
+
#if os(Android) && (arch(i386) || arch(arm)) // struct stat.st_mode is UInt32
internal func &(left: UInt32, right: mode_t) -> mode_t {
return mode_t(left) & right
@@ -398,13 +402,13 @@ extension FileManager {
_current = fts_read(stream)
while let current = _current {
- let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path, length: Int(current.pointee.fts_pathlen))
+ let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path!, length: Int(current.pointee.fts_pathlen))
switch Int32(current.pointee.fts_info) {
case FTS_D:
let (showFile, skipDescendants) = match(filename: filename, to: _options, isDir: true)
if skipDescendants {
- fts_set(_stream, _current, FTS_SKIP)
+ fts_set(stream, current, FTS_SKIP)
}
if showFile {
return URL(fileURLWithPath: filename, isDirectory: true)
@@ -578,7 +582,7 @@ extension FileManager {
let finalErrno = originalItemURL.withUnsafeFileSystemRepresentation { (originalFS) -> Int32? in
return newItemURL.withUnsafeFileSystemRepresentation { (newItemFS) -> Int32? in
// This is an atomic operation in many OSes, but is not guaranteed to be atomic by the standard.
- if rename(newItemFS, originalFS) == 0 {
+ if rename(newItemFS!, originalFS!) == 0 {
return nil
} else {
return errno
diff --git a/swift-corelibs-foundation/Sources/Foundation/FileManager.swift b/swift-corelibs-foundation/Sources/Foundation/FileManager.swift
index a5d75820..a19464d7 100644
--- a/swift-corelibs-foundation/Sources/Foundation/FileManager.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/FileManager.swift
@@ -21,6 +21,8 @@ import WinSDK
#if os(WASI)
import WASILibc
+#elseif canImport(Bionic)
+import Bionic
#endif
#if os(Windows)
diff --git a/swift-corelibs-foundation/Sources/Foundation/Host.swift b/swift-corelibs-foundation/Sources/Foundation/Host.swift
index 6c4f5291..fb130631 100644
--- a/swift-corelibs-foundation/Sources/Foundation/Host.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/Host.swift
@@ -12,8 +12,9 @@
import WinSDK
#endif
-#if os(Android)
- // Android Glibc differs a little with respect to the Linux Glibc.
+#if canImport(Android)
+ import Android
+ // Android Bionic differs a little with respect to the Linux Glibc.
// IFF_LOOPBACK is part of the enumeration net_device_flags, which needs to
// convert to UInt32.
@@ -24,8 +25,8 @@ import WinSDK
}
// getnameinfo uses size_t for its 4th and 6th arguments.
- private func getnameinfo(_ addr: UnsafePointer<sockaddr>?, _ addrlen: socklen_t, _ host: UnsafeMutablePointer<Int8>?, _ hostlen: socklen_t, _ serv: UnsafeMutablePointer<Int8>?, _ servlen: socklen_t, _ flags: Int32) -> Int32 {
- return Glibc.getnameinfo(addr, addrlen, host, Int(hostlen), serv, Int(servlen), flags)
+ private func getnameinfo(_ addr: UnsafePointer<sockaddr>, _ addrlen: socklen_t, _ host: UnsafeMutablePointer<Int8>?, _ hostlen: socklen_t, _ serv: UnsafeMutablePointer<Int8>?, _ servlen: socklen_t, _ flags: Int32) -> Int32 {
+ return Android.getnameinfo(addr, addrlen, host, Int(hostlen), serv, Int(servlen), flags)
}
// getifaddrs and freeifaddrs are not available in Android 6.0 or earlier, so call these functions dynamically.
diff --git a/swift-corelibs-foundation/Sources/Foundation/NSData.swift b/swift-corelibs-foundation/Sources/Foundation/NSData.swift
index ae54f971..65eb0d93 100644
--- a/swift-corelibs-foundation/Sources/Foundation/NSData.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/NSData.swift
@@ -11,6 +11,9 @@
#if !os(WASI)
import Dispatch
#endif
+#if canImport(Android)
+import Android
+#endif
extension NSData {
public typealias ReadingOptions = Data.ReadingOptions
@@ -469,6 +472,8 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
let createMode = Int(Musl.S_IRUSR) | Int(Musl.S_IWUSR) | Int(Musl.S_IRGRP) | Int(Musl.S_IWGRP) | Int(Musl.S_IROTH) | Int(Musl.S_IWOTH)
#elseif canImport(WASILibc)
let createMode = Int(WASILibc.S_IRUSR) | Int(WASILibc.S_IWUSR) | Int(WASILibc.S_IRGRP) | Int(WASILibc.S_IWGRP) | Int(WASILibc.S_IROTH) | Int(WASILibc.S_IWOTH)
+#elseif canImport(Android)
+ let createMode = Int(Android.S_IRUSR) | Int(Android.S_IWUSR) | Int(Android.S_IRGRP) | Int(Android.S_IWGRP) | Int(Android.S_IROTH) | Int(Android.S_IWOTH)
#endif
guard let fh = FileHandle(path: path, flags: flags, createMode: createMode) else {
throw _NSErrorWithErrno(errno, reading: false, path: path)
diff --git a/swift-corelibs-foundation/Sources/Foundation/NSError.swift b/swift-corelibs-foundation/Sources/Foundation/NSError.swift
index 6f21d3a0..dcd6f3f1 100644
--- a/swift-corelibs-foundation/Sources/Foundation/NSError.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/NSError.swift
@@ -16,6 +16,8 @@ import Darwin
import Glibc
#elseif canImport(CRT)
import CRT
+#elseif canImport(Android)
+import Android
#endif
@_implementationOnly import CoreFoundation
diff --git a/swift-corelibs-foundation/Sources/Foundation/NSLock.swift b/swift-corelibs-foundation/Sources/Foundation/NSLock.swift
index fe1d08b7..9d0fadc9 100644
--- a/swift-corelibs-foundation/Sources/Foundation/NSLock.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/NSLock.swift
@@ -11,6 +11,8 @@
#if canImport(Glibc)
import Glibc
+#elseif canImport(Bionic)
+import Bionic
#endif
#if os(Windows)
diff --git a/swift-corelibs-foundation/Sources/Foundation/NSPlatform.swift b/swift-corelibs-foundation/Sources/Foundation/NSPlatform.swift
index a1809026..5424f5bb 100644
--- a/swift-corelibs-foundation/Sources/Foundation/NSPlatform.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/NSPlatform.swift
@@ -10,6 +10,9 @@
#if os(macOS) || os(iOS)
fileprivate let _NSPageSize = Int(vm_page_size)
#elseif os(Linux) || os(Android) || os(OpenBSD)
+#if canImport(Android)
+import Android
+#endif
fileprivate let _NSPageSize = Int(getpagesize())
#elseif os(Windows)
import WinSDK
diff --git a/swift-corelibs-foundation/Sources/Foundation/NSSwiftRuntime.swift b/swift-corelibs-foundation/Sources/Foundation/NSSwiftRuntime.swift
index 03176c17..1509c31d 100644
--- a/swift-corelibs-foundation/Sources/Foundation/NSSwiftRuntime.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/NSSwiftRuntime.swift
@@ -19,6 +19,8 @@ internal import Synchronization
@_exported import Glibc
#elseif canImport(Musl)
@_exported import Musl
+#elseif canImport(Bionic)
+@_exported import Bionic
#elseif os(WASI)
@_exported import WASILibc
#elseif os(Windows)
diff --git a/swift-corelibs-foundation/Sources/Foundation/NSURL.swift b/swift-corelibs-foundation/Sources/Foundation/NSURL.swift
index 6af73f16..9e6f20d0 100644
--- a/swift-corelibs-foundation/Sources/Foundation/NSURL.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/NSURL.swift
@@ -22,6 +22,8 @@ import Darwin
import Glibc
#elseif canImport(Musl)
import Musl
+#elseif canImport(Bionic)
+import Bionic
#endif
// NOTE: this represents PLATFORM_PATH_STYLE
diff --git a/swift-corelibs-foundation/Sources/Foundation/Port.swift b/swift-corelibs-foundation/Sources/Foundation/Port.swift
index c4ed8282..e71f591b 100644
--- a/swift-corelibs-foundation/Sources/Foundation/Port.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/Port.swift
@@ -107,7 +107,7 @@ fileprivate let FOUNDATION_SOCK_STREAM = SOCK_STREAM
fileprivate let FOUNDATION_IPPROTO_TCP = IPPROTO_TCP
#endif
-#if canImport(Glibc) && !os(Android) && !os(OpenBSD)
+#if canImport(Glibc) && !os(OpenBSD)
import Glibc
fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM.rawValue)
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
@@ -119,14 +119,19 @@ fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM)
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
#endif
-#if canImport(Glibc) && os(Android) || os(OpenBSD)
+#if canImport(Glibc) && os(OpenBSD)
import Glibc
fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM)
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
fileprivate let INADDR_ANY: in_addr_t = 0
-#if os(OpenBSD)
fileprivate let INADDR_LOOPBACK = 0x7f000001
#endif
+
+#if canImport(Android)
+import Android
+fileprivate let FOUNDATION_SOCK_STREAM = Int32(Android.SOCK_STREAM)
+fileprivate let FOUNDATION_IPPROTO_TCP = Int32(Android.IPPROTO_TCP)
+fileprivate let INADDR_ANY: in_addr_t = 0
#endif
diff --git a/swift-corelibs-foundation/Sources/Foundation/Process.swift b/swift-corelibs-foundation/Sources/Foundation/Process.swift
index ee35c23a..5e8cfbaa 100644
--- a/swift-corelibs-foundation/Sources/Foundation/Process.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/Process.swift
@@ -18,6 +18,8 @@ import struct WinSDK.HANDLE
#if canImport(Darwin)
import Darwin
+#elseif canImport(Android)
+import Android
#endif
internal import Synchronization
diff --git a/swift-corelibs-foundation/Sources/Foundation/Thread.swift b/swift-corelibs-foundation/Sources/Foundation/Thread.swift
index 5e79579c..0985a482 100644
--- a/swift-corelibs-foundation/Sources/Foundation/Thread.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/Thread.swift
@@ -17,6 +17,8 @@ import WinSDK
import Glibc
#elseif canImport(Musl)
import Musl
+#elseif canImport(Bionic)
+import Bionic
#endif
// WORKAROUND_SR9811
diff --git a/swift-corelibs-foundation/Sources/FoundationNetworking/HTTPCookie.swift b/swift-corelibs-foundation/Sources/FoundationNetworking/HTTPCookie.swift
index e0d1cbbd..237c1daf 100644
--- a/swift-corelibs-foundation/Sources/FoundationNetworking/HTTPCookie.swift
+++ b/swift-corelibs-foundation/Sources/FoundationNetworking/HTTPCookie.swift
@@ -15,6 +15,8 @@ import Foundation
#if os(Windows)
import WinSDK
+#elseif canImport(Android)
+import Android
#endif
public struct HTTPCookiePropertyKey : RawRepresentable, Equatable, Hashable, Sendable {
diff --git a/swift-corelibs-foundation/Sources/plutil/main.swift b/swift-corelibs-foundation/Sources/plutil/main.swift
index 29316d16..29584596 100644
--- a/swift-corelibs-foundation/Sources/plutil/main.swift
+++ b/swift-corelibs-foundation/Sources/plutil/main.swift
@@ -15,6 +15,9 @@ import Glibc
#elseif canImport(Musl)
import Foundation
import Musl
+#elseif canImport(Bionic)
+import Foundation
+import Bionic
#elseif canImport(CRT)
import Foundation
import CRT
diff --git a/swift-corelibs-foundation/Sources/xdgTestHelper/main.swift b/swift-corelibs-foundation/Sources/xdgTestHelper/main.swift
index d515a63f..fb037e24 100644
--- a/swift-corelibs-foundation/Sources/xdgTestHelper/main.swift
+++ b/swift-corelibs-foundation/Sources/xdgTestHelper/main.swift
@@ -19,6 +19,8 @@ import FoundationNetworking
#endif
#if os(Windows)
import WinSDK
+#elseif canImport(Android)
+import Android
#endif
enum HelperCheckStatus : Int32 {
diff --git a/swift-corelibs-foundation/Sources/Foundation/NSPathUtilities.swift b/swift-corelibs-foundation/Sources/Foundation/NSPathUtilities.swift
--- a/swift-corelibs-foundation/Sources/Foundation/NSPathUtilities.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/NSPathUtilities.swift
@@ -19,6 +19,8 @@
get { WASILibc.errno }
set { WASILibc.errno = newValue }
}
+#elseif os(Android)
+import Android
#endif
#if os(Windows)
diff --git a/swift-corelibs-foundation/Sources/Foundation/Process.swift b/swift-corelibs-foundation/Sources/Foundation/Process.swift
index 758dd1df..02970992 100644
--- a/swift-corelibs-foundation/Sources/Foundation/Process.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/Process.swift
@@ -927,8 +927,8 @@ open class Process: NSObject, @unchecked Sendable {
}
let useFallbackChdir: Bool
if let dir = currentDirectoryURL?.path {
- let chdirResult = _CFPosixSpawnFileActionsChdir(fileActions, dir)
- useFallbackChdir = chdirResult == ENOSYS
+ // let chdirResult = _CFPosixSpawnFileActionsChdir(fileActions, dir)
+ useFallbackChdir = true ; let chdirResult = ENOSYS
if !useFallbackChdir {
try _throwIfPosixError(chdirResult)
}
From ab001004196b249af359d6152e5c2d06c11bee56
From: Alex Lorenz <[email protected]>
Date: Tue, 29 Oct 2024 13:00:11 -0700
Subject: [PATCH] [android] fix the android build (#999)
Platform.swift regressed after 71eefee7fca08ecbeacc89098b74ab80319baa6b
---
diff --git a/swift-foundation/Sources/FoundationEssentials/Platform.swift b/swift-foundation/Sources/FoundationEssentials/Platform.swift
index d8b8fe6f..15a1362f 100644
--- a/swift-foundation/Sources/FoundationEssentials/Platform.swift
+++ b/swift-foundation/Sources/FoundationEssentials/Platform.swift
@@ -29,8 +29,7 @@ fileprivate let _pageSize: Int = {
// WebAssembly defines a fixed page size
fileprivate let _pageSize: Int = 65_536
#elseif os(Android)
-import Bionic
-import unistd
+import Android
fileprivate let _pageSize: Int = Int(getpagesize())
#elseif canImport(Glibc)
import Glibc
@@ -142,7 +141,7 @@ extension Platform {
typealias Operation<Input, Output> = (Input, UnsafeMutablePointer<Output>, UnsafeMutablePointer<CChar>, Int, UnsafeMutablePointer<UnsafeMutablePointer<Output>?>) -> Int32
#endif
- private static func withUserGroupBuffer<Input, Output, R>(_ input: Input, _ output: Output, sizeProperty: Int32, operation: Operation<Input, Output>, block: (Output) throws -> R) rethrows -> R? {
+ private static func withUserGroupBuffer<Input, Output, R>(_ input: Input, _ output: Output, sizeProperty: Int32, operation: Operation<Input, Output>, block: (Output) throws -> R?) rethrows -> R? {
var bufferLen = sysconf(sizeProperty)
if bufferLen == -1 {
bufferLen = 4096 // Generous default size estimate
@@ -172,31 +171,51 @@ extension Platform {
static func name(forUID uid: uid_t) -> String? {
withUserGroupBuffer(uid, passwd(), sizeProperty: Int32(_SC_GETPW_R_SIZE_MAX), operation: getpwuid_r) {
- String(cString: $0.pw_name)
+ // Android's pw_name `char *`` is nullable when it should be non-null.
+ // FIXME: avoid the coerce cast workaround once https://github.com/android/ndk/issues/2098 is fixed.
+ let pw_name: UnsafeMutablePointer<CChar>? = $0.pw_name
+ return pw_name.flatMap { String(cString: $0) }
}
}
static func fullName(forUID uid: uid_t) -> String? {
withUserGroupBuffer(uid, passwd(), sizeProperty: Int32(_SC_GETPW_R_SIZE_MAX), operation: getpwuid_r) {
- String(cString: $0.pw_gecos)
+#if os(Android) && _pointerBitWidth(_32)
+ // pw_gecos isn't available on 32-bit Android.
+ let pw_gecos: UnsafeMutablePointer<CChar>? = nil
+#else
+ // Android's pw_gecos `char *`` is nullable, so always coerce to a nullable pointer
+ // in order to be compatible with Android.
+ let pw_gecos: UnsafeMutablePointer<CChar>? = $0.pw_gecos
+#endif
+ return pw_gecos.flatMap { String(cString: $0) }
}
}
static func name(forGID gid: gid_t) -> String? {
withUserGroupBuffer(gid, group(), sizeProperty: Int32(_SC_GETGR_R_SIZE_MAX), operation: getgrgid_r) {
- String(cString: $0.gr_name)
+ // Android's gr_name `char *`` is nullable when it should be non-null.
+ // FIXME: avoid the coerce cast workaround once https://github.com/android/ndk/issues/2098 is fixed.
+ let gr_name: UnsafeMutablePointer<CChar>? = $0.gr_name
+ return gr_name.flatMap { String(cString: $0) }
}
}
static func homeDirectory(forUserName userName: String) -> String? {
withUserGroupBuffer(userName, passwd(), sizeProperty: Int32(_SC_GETPW_R_SIZE_MAX), operation: getpwnam_r) {
- String(cString: $0.pw_dir)
+ // Android's pw_dir `char *`` is nullable when it should be non-null.
+ // FIXME: avoid the coerce cast workaround once https://github.com/android/ndk/issues/2098 is fixed.
+ let pw_dir: UnsafeMutablePointer<CChar>? = $0.pw_dir
+ return pw_dir.flatMap { String(cString: $0) }
}
}
static func homeDirectory(forUID uid: uid_t) -> String? {
withUserGroupBuffer(uid, passwd(), sizeProperty: Int32(_SC_GETPW_R_SIZE_MAX), operation: getpwuid_r) {
- String(cString: $0.pw_dir)
+ // Android's pw_dir `char *`` is nullable when it should be non-null.
+ // FIXME: avoid the coerce cast workaround once https://github.com/android/ndk/issues/2098 is fixed.
+ let pw_dir: UnsafeMutablePointer<CChar>? = $0.pw_dir
+ return pw_dir.flatMap { String(cString: $0) }
}
}
}
commit ad6ca71b4eef90e3ae69b130e3cc989a21192020
Author: Alex Lorenz <[email protected]>
Date: Wed Aug 14 10:56:25 2024 -0700
[android] fix the LP32 armv7/i686 android build (#846)
* [android] fix the LP32 armv7/i686 android build
* Update Sources/FoundationEssentials/Android+Extensions.swift
Co-authored-by: Jeremy Schonfeld <[email protected]>
* drop the android Lp32 specific operator &
---------
Co-authored-by: Jeremy Schonfeld <[email protected]>
diff --git a/swift-foundation/Sources/FoundationEssentials/Data/Data+Reading.swift b/swift-foundation/Sources/FoundationEssentials/Data/Data+Reading.swift
index 2540b14..a779837 100644
--- a/swift-foundation/Sources/FoundationEssentials/Data/Data+Reading.swift
+++ b/swift-foundation/Sources/FoundationEssentials/Data/Data+Reading.swift
@@ -325,7 +325,7 @@ internal func readBytesFromFile(path inPath: PathOrURL, reportProgress: Bool, ma
}
let fileSize = min(Int(clamping: filestat.st_size), maxLength ?? Int.max)
- let fileType = filestat.st_mode & S_IFMT
+ let fileType = mode_t(filestat.st_mode) & S_IFMT
#if !NO_FILESYSTEM
let shouldMap = shouldMapFileDescriptor(fd, path: inPath, options: options)
#else
diff --git a/swift-foundation/Sources/FoundationEssentials/FileManager/FileManager+Basics.swift b/swift-foundation/Sources/FoundationEssentials/FileManager/FileManager+Basics.swift
index 991c5e8..d3e6de3 100644
--- a/swift-foundation/Sources/FoundationEssentials/FileManager/FileManager+Basics.swift
+++ b/swift-foundation/Sources/FoundationEssentials/FileManager/FileManager+Basics.swift
@@ -221,7 +221,7 @@ internal struct _FileManagerImpl {
var statBuf = stat()
let fd = open(path, 0, 0)
guard fd >= 0 else { return nil }
- if fstat(fd, &statBuf) < 0 || statBuf.st_mode & S_IFMT == S_IFDIR {
+ if fstat(fd, &statBuf) < 0 || mode_t(statBuf.st_mode) & S_IFMT == S_IFDIR {
close(fd)
return nil
}
@@ -240,7 +240,7 @@ internal struct _FileManagerImpl {
}
/* check for being same type */
- if myInfo.st_mode & S_IFMT != otherInfo.st_mode & S_IFMT {
+ if mode_t(myInfo.st_mode) & S_IFMT != mode_t(otherInfo.st_mode) & S_IFMT {
return false
}
diff --git a/swift-foundation/Sources/FoundationEssentials/FileManager/FileManager+Files.swift b/swift-foundation/Sources/FoundationEssentials/FileManager/FileManager+Files.swift
index b8cd50a..bee9fb3 100644
--- a/swift-foundation/Sources/FoundationEssentials/FileManager/FileManager+Files.swift
+++ b/swift-foundation/Sources/FoundationEssentials/FileManager/FileManager+Files.swift
@@ -175,7 +175,8 @@ extension stat {
}
fileprivate var fileAttributes: [FileAttributeKey : Any] {
- let fileType = st_mode.fileType
+ // On 32 bit Android, st_mode is UInt32.
+ let fileType = mode_t(st_mode).fileType
var result: [FileAttributeKey : Any] = [
.size : _writeFileAttributePrimitive(st_size, as: UInt.self),
.modificationDate : modificationDate,
@@ -400,7 +401,7 @@ extension _FileManagerImpl {
guard stat(rep, &fileInfo) == 0 else {
return (false, false)
}
- let isDir = (fileInfo.st_mode & S_IFMT) == S_IFDIR
+ let isDir = (mode_t(fileInfo.st_mode) & S_IFMT) == S_IFDIR
return (true, isDir)
}
#endif
@@ -479,7 +480,7 @@ extension _FileManagerImpl {
return false
}
- if ((dirInfo.st_mode & S_ISVTX) != 0) && fileManager.fileExists(atPath: path) {
+ if ((mode_t(dirInfo.st_mode) & S_ISVTX) != 0) && fileManager.fileExists(atPath: path) {
// its sticky so verify that we own the file
// otherwise we answer YES on the principle that if
// we create files we can delete them
diff --git a/swift-foundation/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift b/swift-foundation/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift
index 9bac967..e531cb5 100644
--- a/swift-foundation/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift
+++ b/swift-foundation/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift
@@ -49,19 +49,19 @@ extension FILETIME {
#if !os(Windows)
extension stat {
var isDirectory: Bool {
- (self.st_mode & S_IFMT) == S_IFDIR
+ (mode_t(self.st_mode) & S_IFMT) == S_IFDIR
}
var isRegular: Bool {
- (self.st_mode & S_IFMT) == S_IFREG
+ (mode_t(self.st_mode) & S_IFMT) == S_IFREG
}
var isSymbolicLink: Bool {
- (self.st_mode & S_IFMT) == S_IFLNK
+ (mode_t(self.st_mode) & S_IFMT) == S_IFLNK
}
var isSpecial: Bool {
- let type = self.st_mode & S_IFMT
+ let type = mode_t(self.st_mode) & S_IFMT
return type == S_IFBLK || type == S_IFCHR
}
}
diff --git a/swift-foundation/Sources/FoundationEssentials/FileManager/FileOperations+Enumeration.swift b/swift-foundation/Sources/FoundationEssentials/FileManager/FileOperations+Enumeration.swift
index 2c9a02f..500da1d 100644
--- a/swift-foundation/Sources/FoundationEssentials/FileManager/FileOperations+Enumeration.swift
+++ b/swift-foundation/Sources/FoundationEssentials/FileManager/FileOperations+Enumeration.swift
@@ -367,7 +367,7 @@ struct _POSIXDirectoryContentsSequence: Sequence {
let statDir = directoryPath + "/" + fileName
if stat(statDir, &statBuf) == 0 {
// #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
- if (statBuf.st_mode & S_IFMT) == S_IFDIR {
+ if (mode_t(statBuf.st_mode) & S_IFMT) == S_IFDIR {
isDirectory = true
}
}
diff --git a/swift-foundation/Sources/FoundationEssentials/FileManager/FileOperations.swift b/swift-foundation/Sources/FoundationEssentials/FileManager/FileOperations.swift
index 03adcc6..92e609f 100644
--- a/swift-foundation/Sources/FoundationEssentials/FileManager/FileOperations.swift
+++ b/swift-foundation/Sources/FoundationEssentials/FileManager/FileOperations.swift
@@ -867,7 +867,7 @@ enum _FileOperations {
#if !os(WASI) // WASI doesn't have fchmod for now
// Set the file permissions using fchmod() instead of when open()ing to avoid umask() issues
- let permissions = fileInfo.st_mode & ~S_IFMT
+ let permissions = mode_t(fileInfo.st_mode) & ~S_IFMT
guard fchmod(dstfd, permissions) == 0 else {
try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr))
return
diff --git a/swift-foundation/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift b/swift-foundation/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift
index 2e809fa..d01ca3f 100644
--- a/swift-foundation/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift
+++ b/swift-foundation/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift
@@ -198,7 +198,10 @@ final class _ProcessInfo: Sendable {
}
var fullUserName: String {
-#if canImport(Darwin) || os(Android) || canImport(Glibc) || canImport(Musl)
+#if os(Android) && (arch(i386) || arch(arm))
+ // On LP32 Android, pw_gecos doesn't exist and is presumed to be NULL.
+ return ""
+#elseif canImport(Darwin) || os(Android) || canImport(Glibc) || canImport(Musl)
let (euid, _) = Platform.getUGIDs()
if let fullName = Platform.fullName(forUID: euid) {
return fullName
diff --git a/swift-foundation/Sources/FoundationEssentials/String/String+Path.swift b/swift-foundation/Sources/FoundationEssentials/String/String+Path.swift
index 477d5d3..1ce75d6 100644
--- a/swift-foundation/Sources/FoundationEssentials/String/String+Path.swift
+++ b/swift-foundation/Sources/FoundationEssentials/String/String+Path.swift
@@ -737,7 +737,7 @@ extension String {
if lstat(buffer.baseAddress!, &statBuf) < 0 {
return nil
}
- if statBuf.st_mode & S_IFMT == S_IFLNK {
+ if mode_t(statBuf.st_mode) & S_IFMT == S_IFLNK {
/* Examples:
* fspath == /foo/bar0baz/quux/froboz
* linkx == /tic/tac/toe