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 <lukasa@apple.com>
This commit is contained in:
Saleem AbdulrasoolandCory Benfield authored and GitHub committed 2022-06-07 05:44:36 -07:00
1 parent a8bb008516
commit bb2aae798f
1 file changed
+13 -2
+13 -2
View File
@@ -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)
}