From bb2aae798fb1d7a5bb7abbec4bcae982784c85b7 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Tue, 7 Jun 2022 05:44:36 -0700 Subject: [PATCH] NIOCore: extract and modify POSIX flag constants (#2150) Windows does not have the same set of POSIX flag constants. In particular, there is no equivalent to group permissions as the ACL model on Windows is far richer and requires the full specification of the DACLs. Additionally, isolate `O_CLOEXEC` to aid Windows which does not have this flag. Co-authored-by: Cory Benfield --- Sources/NIOCore/FileHandle.swift | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Sources/NIOCore/FileHandle.swift b/Sources/NIOCore/FileHandle.swift index 0ca264f8e..6baeea212 100644 --- a/Sources/NIOCore/FileHandle.swift +++ b/Sources/NIOCore/FileHandle.swift @@ -128,11 +128,17 @@ extension NIOFileHandle { public static let `default` = Flags(posixMode: 0, posixFlags: 0) +#if os(Windows) + public static let defaultPermissions = _S_IREAD | _S_IWRITE +#else + public static let defaultPermissions = S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH +#endif + /// Allows file creation when opening file for writing. File owner is set to the effective user ID of the process. /// /// - parameters: /// - posixMode: `file mode` applied when file is created. Default permissions are: read and write for fileowner, read for owners group and others. - public static func allowFileCreation(posixMode: NIOPOSIXFileMode = S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH) -> Flags { + public static func allowFileCreation(posixMode: NIOPOSIXFileMode = defaultPermissions) -> Flags { return Flags(posixMode: posixMode, posixFlags: O_CREAT) } @@ -154,7 +160,12 @@ extension NIOFileHandle { /// - mode: Access mode. Default mode is `.read`. /// - flags: Additional POSIX flags. public convenience init(path: String, mode: Mode = .read, flags: Flags = .default) throws { - let fd = try SystemCalls.open(file: path, oFlag: mode.posixFlags | O_CLOEXEC | flags.posixFlags, mode: flags.posixMode) +#if os(Windows) + let fl = mode.posixFlags | flags.posixFlags | _O_NOINHERIT +#else + let fl = mode.posixFlags | flags.posixFlags | O_CLOEXEC +#endif + let fd = try SystemCalls.open(file: path, oFlag: fl, mode: flags.posixMode) self.init(descriptor: fd) }