Skip to content

Commit 0b3974d

Browse files
Guard out user/group related code on WASI (#783)
* 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. * Throw explicit unsupported error if trying to set user or group on WASI Instead of implicitly ignoring user-given values, we should throw exception to make it clear that those values cannot be set on WASI.
1 parent fab7195 commit 0b3974d

File tree

6 files changed

+14
-3
lines changed

6 files changed

+14
-3
lines changed

Sources/FoundationEssentials/Data/Data+Writing.swift

+2
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint
519519

520520
cleanupTemporaryDirectory(at: temporaryDirectoryPath)
521521

522+
#if !os(WASI) // WASI does not support fchmod for now
522523
if let mode {
523524
// Try to change the mode if the path has not changed. Do our best, but don't report an error.
524525
#if FOUNDATION_FRAMEWORK
@@ -542,6 +543,7 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint
542543
fchmod(fd, mode)
543544
#endif
544545
}
546+
#endif // os(WASI)
545547
}
546548
}
547549
}

Sources/FoundationEssentials/FileManager/FileManager+Files.swift

+5
Original file line numberDiff line numberDiff line change
@@ -922,13 +922,18 @@ extension _FileManagerImpl {
922922
let groupID = _readFileAttributePrimitive(attributes[.groupOwnerAccountID], as: UInt.self)
923923

924924
if user != nil || userID != nil || group != nil || groupID != nil {
925+
#if os(WASI)
926+
// WASI does not have the concept of users or groups
927+
throw CocoaError.errorWithFilePath(.featureUnsupported, path)
928+
#else
925929
// Bias toward userID & groupID - try to prevent round trips to getpwnam if possible.
926930
var leaveUnchanged: UInt32 { UInt32(bitPattern: -1) }
927931
let rawUserID = userID.flatMap(uid_t.init) ?? user.flatMap(Self._userAccountNameToNumber) ?? leaveUnchanged
928932
let rawGroupID = groupID.flatMap(gid_t.init) ?? group.flatMap(Self._groupAccountNameToNumber) ?? leaveUnchanged
929933
if chown(fileSystemRepresentation, rawUserID, rawGroupID) != 0 {
930934
throw CocoaError.errorWithFilePath(path, errno: errno, reading: false)
931935
}
936+
#endif
932937
}
933938

934939
try Self._setCatInfoAttributes(attributes, path: path)

Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ extension _FileManagerImpl {
271271
}
272272
#endif
273273

274-
#if !os(Windows)
274+
#if !os(Windows) && !os(WASI)
275275
static func _userAccountNameToNumber(_ name: String) -> uid_t? {
276276
name.withCString { ptr in
277277
getpwnam(ptr)?.pointee.pw_uid

Sources/FoundationEssentials/FileManager/FileOperations.swift

+2
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

+2-2
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

+2
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)