Skip to content

Guard out user/group related code on WASI #783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/FoundationEssentials/Data/Data+Writing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint

cleanupTemporaryDirectory(at: temporaryDirectoryPath)

#if !os(WASI) // WASI does not support fchmod for now
if let mode {
// Try to change the mode if the path has not changed. Do our best, but don't report an error.
#if FOUNDATION_FRAMEWORK
Expand All @@ -537,6 +538,7 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint
fchmod(fd, mode)
#endif
}
#endif // os(WASI)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -906,13 +906,18 @@ extension _FileManagerImpl {
let groupID = _readFileAttributePrimitive(attributes[.groupOwnerAccountID], as: UInt.self)

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

try Self._setCatInfoAttributes(attributes, path: path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ extension _FileManagerImpl {
}
#endif

#if !os(Windows)
#if !os(Windows) && !os(WASI)
static func _userAccountNameToNumber(_ name: String) -> uid_t? {
name.withCString { ptr in
getpwnam(ptr)?.pointee.pw_uid
Expand Down
2 changes: 2 additions & 0 deletions Sources/FoundationEssentials/FileManager/FileOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -855,12 +855,14 @@ enum _FileOperations {
}
defer { close(dstfd) }

#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
guard fchmod(dstfd, permissions) == 0 else {
try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr))
return
}
#endif

if fileInfo.st_size == 0 {
// no copying required
Expand Down
4 changes: 2 additions & 2 deletions Sources/FoundationEssentials/Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private let _cachedUGIDs: (uid_t, gid_t) = {
}()
#endif

#if !os(Windows)
#if !os(Windows) && !os(WASI)
extension Platform {
private static var ROOT_USER: UInt32 { 0 }
static func getUGIDs(allowEffectiveRootUID: Bool = true) -> (uid: UInt32, gid: UInt32) {
Expand Down Expand Up @@ -174,7 +174,7 @@ extension Platform {
// FIXME: bionic implements this as `return 0;` and does not expose the
// function via headers. We should be able to shim this and use the call
// if it is available.
#if !os(Android)
#if !os(Android) && !os(WASI)
guard issetugid() == 0 else { return nil }
#endif
if let value = getenv(name) {
Expand Down
2 changes: 2 additions & 0 deletions Sources/FoundationEssentials/String/String+Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ extension String {
return envVar.standardizingPath
}

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

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