Skip to content

Commit 4f36710

Browse files
rauhulmilseman
authored andcommitted
Expose O_DIRECTORY as api
- Adds a `directory` OpenOptions as a type-safe version of O_DIRECTORY. Allows clients to only open a FileDescriptor for FilePaths that refer to directories.
1 parent 591ff41 commit 4f36710

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

Sources/System/FileDescriptor.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ extension FileDescriptor {
234234
@_alwaysEmitIntoClient
235235
@available(*, unavailable, renamed: "noFollow")
236236
public static var O_NOFOLLOW: OpenOptions { noFollow }
237+
238+
/// Indicates that opening the file only succeeds if the file is a directory.
239+
///
240+
/// If you specify this option and the file path you pass to
241+
/// <doc:System/FileDescriptor/open(_:_:options:permissions:)-10dcs>
242+
/// is a not a directory, then that open operation fails.
243+
///
244+
/// The corresponding C constant is `O_DIRECTORY`.
245+
@_alwaysEmitIntoClient
246+
public static var directory: OpenOptions { OpenOptions(_O_DIRECTORY) }
247+
248+
@_alwaysEmitIntoClient
249+
@available(*, unavailable, renamed: "directory")
250+
public static var O_DIRECTORY: OpenOptions { directory }
237251
#endif
238252

239253
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)

Sources/System/Internals/Constants.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,6 @@ internal var _O_EVTONLY: CInt { O_EVTONLY }
498498
@_alwaysEmitIntoClient
499499
internal var _O_NOCTTY: CInt { O_NOCTTY }
500500

501-
// TODO: API?
502501
@_alwaysEmitIntoClient
503502
internal var _O_DIRECTORY: CInt { O_DIRECTORY }
504503
#endif

0 commit comments

Comments
 (0)