Skip to content

Commit da3bb7d

Browse files
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 4db3c7a commit da3bb7d

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

Sources/FoundationEssentials/FileManager/FileManager+Files.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,17 +905,20 @@ 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
909908
if user != nil || userID != nil || group != nil || groupID != nil {
909+
#if os(WASI)
910+
// WASI does not have the concept of users or groups
911+
throw CocoaError.errorWithFilePath(.featureUnsupported, path)
912+
#else
910913
// Bias toward userID & groupID - try to prevent round trips to getpwnam if possible.
911914
var leaveUnchanged: UInt32 { UInt32(bitPattern: -1) }
912915
let rawUserID = userID.flatMap(uid_t.init) ?? user.flatMap(Self._userAccountNameToNumber) ?? leaveUnchanged
913916
let rawGroupID = groupID.flatMap(gid_t.init) ?? group.flatMap(Self._groupAccountNameToNumber) ?? leaveUnchanged
914917
if chown(fileSystemRepresentation, rawUserID, rawGroupID) != 0 {
915918
throw CocoaError.errorWithFilePath(path, errno: errno, reading: false)
916919
}
920+
#endif
917921
}
918-
#endif
919922

920923
try Self._setCatInfoAttributes(attributes, path: path)
921924

0 commit comments

Comments
 (0)