Skip to content

Add advisory file locking API #111

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

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions Sources/System/FileControl.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
This source file is part of the Swift System open source project

Copyright (c) 2021 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
*/

// Strongly typed, Swifty interfaces to the most common and useful `fcntl`
// commands.

extension FileDescriptor {
internal func _fcntl(
_ cmd: Command, _ lock: inout FileDescriptor.FileLock,
retryOnInterrupt: Bool
) -> Result<(), Errno> {
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
withUnsafeMutablePointer(to: &lock) {
system_fcntl(self.rawValue, cmd.rawValue, $0)
}
}
}
}
63 changes: 63 additions & 0 deletions Sources/System/FileControlRaw.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
This source file is part of the Swift System open source project

Copyright (c) 2021 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
*/

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(Android)
import Glibc
#elseif os(Windows)
// Nothing
#else
#error("Unsupported Platform")
#endif


#if !os(Windows)

// - MARK: Commands

// TODO: Make below API as part of broader `fcntl` support.
extension FileDescriptor {
/// Commands (and various constants) to pass to `fcntl`.
internal struct Command: RawRepresentable, Hashable {
internal let rawValue: CInt

internal init(rawValue: CInt) { self.rawValue = rawValue }

private init(_ raw: CInt) { self.init(rawValue: raw) }

/// Get open file description record locking information.
///
/// The corresponding C constant is `F_GETLK`.
internal static var getOFDLock: Command { Command(_F_OFD_GETLK) }

/// Set open file description record locking information.
///
/// The corresponding C constant is `F_SETLK`.
internal static var setOFDLock: Command { Command(_F_OFD_SETLK) }

/// Set open file description record locking information and wait until
/// the request can be completed.
///
/// The corresponding C constant is `F_SETLKW`.
internal static var setOFDLockWait: Command { Command(_F_OFD_SETLKW) }

#if !os(Linux)
/// Set open file description record locking information and wait until
/// the request can be completed, returning on timeout.
///
/// The corresponding C constant is `F_SETLKWTIMEOUT`.
internal static var setOFDLockWaitTimout: Command {
Command(_F_OFD_SETLKWTIMEOUT)
}
#endif

}
}
#endif
Loading