Skip to content

Commit 4db3c7a

Browse files
Guard out user/group related code on WASI
This change guards out the user/group related code on WASI, as WASI does not have the concept of users or groups.
1 parent c0a485e commit 4db3c7a

File tree

6 files changed

+11
-3
lines changed

6 files changed

+11
-3
lines changed

Sources/FoundationEssentials/Data/Data+Writing.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint
514514

515515
cleanupTemporaryDirectory(at: temporaryDirectoryPath)
516516

517+
#if !os(WASI) // WASI does not support fchmod for now
517518
if let mode {
518519
// Try to change the mode if the path has not changed. Do our best, but don't report an error.
519520
#if FOUNDATION_FRAMEWORK
@@ -537,6 +538,7 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint
537538
fchmod(fd, mode)
538539
#endif
539540
}
541+
#endif // os(WASI)
540542
}
541543
}
542544
}

Sources/FoundationEssentials/FileManager/FileManager+Files.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,7 @@ extension _FileManagerImpl {
905905
let group = attributes[.groupOwnerAccountName] as? String
906906
let groupID = _readFileAttributePrimitive(attributes[.groupOwnerAccountID], as: UInt.self)
907907

908+
#if !os(WASI) // WASI does not have the concept of users or groups
908909
if user != nil || userID != nil || group != nil || groupID != nil {
909910
// Bias toward userID & groupID - try to prevent round trips to getpwnam if possible.
910911
var leaveUnchanged: UInt32 { UInt32(bitPattern: -1) }
@@ -914,6 +915,7 @@ extension _FileManagerImpl {
914915
throw CocoaError.errorWithFilePath(path, errno: errno, reading: false)
915916
}
916917
}
918+
#endif
917919

918920
try Self._setCatInfoAttributes(attributes, path: path)
919921

Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ extension _FileManagerImpl {
267267
}
268268
#endif
269269

270-
#if !os(Windows)
270+
#if !os(Windows) && !os(WASI)
271271
static func _userAccountNameToNumber(_ name: String) -> uid_t? {
272272
name.withCString { ptr in
273273
getpwnam(ptr)?.pointee.pw_uid

Sources/FoundationEssentials/FileManager/FileOperations.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,12 +855,14 @@ enum _FileOperations {
855855
}
856856
defer { close(dstfd) }
857857

858+
#if !os(WASI) // WASI doesn't have fchmod for now
858859
// Set the file permissions using fchmod() instead of when open()ing to avoid umask() issues
859860
let permissions = fileInfo.st_mode & ~S_IFMT
860861
guard fchmod(dstfd, permissions) == 0 else {
861862
try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr))
862863
return
863864
}
865+
#endif
864866

865867
if fileInfo.st_size == 0 {
866868
// no copying required

Sources/FoundationEssentials/Platform.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private let _cachedUGIDs: (uid_t, gid_t) = {
113113
}()
114114
#endif
115115

116-
#if !os(Windows)
116+
#if !os(Windows) && !os(WASI)
117117
extension Platform {
118118
private static var ROOT_USER: UInt32 { 0 }
119119
static func getUGIDs(allowEffectiveRootUID: Bool = true) -> (uid: UInt32, gid: UInt32) {
@@ -174,7 +174,7 @@ extension Platform {
174174
// FIXME: bionic implements this as `return 0;` and does not expose the
175175
// function via headers. We should be able to shim this and use the call
176176
// if it is available.
177-
#if !os(Android)
177+
#if !os(Android) && !os(WASI)
178178
guard issetugid() == 0 else { return nil }
179179
#endif
180180
if let value = getenv(name) {

Sources/FoundationEssentials/String/String+Path.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ extension String {
450450
return envVar.standardizingPath
451451
}
452452

453+
#if !os(WASI) // WASI does not have user concept
453454
// Next, attempt to find the home directory via getpwnam/getpwuid
454455
var pass: UnsafeMutablePointer<passwd>?
455456
if let user {
@@ -463,6 +464,7 @@ extension String {
463464
if let dir = pass?.pointee.pw_dir {
464465
return String(cString: dir).standardizingPath
465466
}
467+
#endif
466468

467469
// Fallback to HOME for the current user if possible
468470
if user == nil, let home = getenv("HOME") {

0 commit comments

Comments
 (0)