-
Notifications
You must be signed in to change notification settings - Fork 107
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
[Sketch] openat, fsync, and sync #43
Draft
milseman
wants to merge
1
commit into
apple:main
Choose a base branch
from
milseman:open_at_sync
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -371,3 +371,109 @@ extension FileDescriptor { | |||||
fatalError("Not implemented") | ||||||
} | ||||||
} | ||||||
|
||||||
extension FileDescriptor { | ||||||
/// Opens or creates a file for reading or writing. | ||||||
/// | ||||||
/// - Parameters: | ||||||
/// - path: The location of the file to open. | ||||||
/// - at: if `path` is relative, treat it as relative to this file descriptor | ||||||
/// rather than relative to the current working directory | ||||||
/// - mode: The read and write access to use. | ||||||
/// - options: The behavior for opening the file. | ||||||
/// - permissions: The file permissions to use for created files. | ||||||
/// - retryOnInterrupt: Whether to retry the open operation | ||||||
/// if it throws ``Errno/interrupted``. | ||||||
/// The default is `true`. | ||||||
/// Pass `false` to try only once and throw an error upon interruption. | ||||||
/// - Returns: A file descriptor for the open file | ||||||
/// | ||||||
/// The corresponding C function is `openat`. | ||||||
@_alwaysEmitIntoClient | ||||||
public static func open( | ||||||
_ path: FilePath, | ||||||
at: FileDescriptor, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
(etc, throughout this PR.) |
||||||
_ mode: FileDescriptor.AccessMode, | ||||||
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(), | ||||||
permissions: FilePermissions? = nil, | ||||||
retryOnInterrupt: Bool = true | ||||||
) throws -> FileDescriptor { | ||||||
try path.withPlatformString { | ||||||
try FileDescriptor._open( | ||||||
$0, at: at, mode, options: options, permissions: permissions, | ||||||
retryOnInterrupt: retryOnInterrupt | ||||||
).get() | ||||||
} | ||||||
} | ||||||
|
||||||
@usableFromInline | ||||||
internal static func _open( | ||||||
_ path: UnsafePointer<CInterop.PlatformChar>, | ||||||
at: FileDescriptor, | ||||||
_ mode: FileDescriptor.AccessMode, | ||||||
options: FileDescriptor.OpenOptions, | ||||||
permissions: FilePermissions?, | ||||||
retryOnInterrupt: Bool | ||||||
) -> Result<FileDescriptor, Errno> { | ||||||
let oFlag = mode.rawValue | options.rawValue | ||||||
let descOrError: Result<CInt, Errno> = valueOrErrno(retryOnInterrupt: retryOnInterrupt) { | ||||||
if let permissions = permissions { | ||||||
return system_openat(at.rawValue, path, oFlag, permissions.rawValue) | ||||||
} | ||||||
precondition(!options.contains(.create), | ||||||
"Create must be given permissions") | ||||||
return system_openat(at.rawValue, path, oFlag) | ||||||
} | ||||||
return descOrError.map { FileDescriptor(rawValue: $0) } | ||||||
} | ||||||
|
||||||
@_alwaysEmitIntoClient | ||||||
@available(*, unavailable, renamed: "open") | ||||||
public static func openat() throws -> FileDescriptor { | ||||||
fatalError("Not implemented") | ||||||
} | ||||||
|
||||||
/// Synchronize a file's state with that on disk. | ||||||
/// | ||||||
/// Note that while this will flush all data from the host to the drive | ||||||
/// (i.e. the "permanent storage device"), the drive itself may not | ||||||
/// physically write the data to the platters for quite some time and it | ||||||
/// may be written in an out-of-order sequence. | ||||||
/// | ||||||
/// For applications that require tighter guarantees about the integrity of | ||||||
/// their data, Mac OS X provides the F_FULLFSYNC fcntl. | ||||||
/// | ||||||
/// The corresponding C function is `fsync`. | ||||||
@_alwaysEmitIntoClient | ||||||
public func sync(retryOnInterrupt: Bool = true) throws { | ||||||
try _sync(retryOnInterrupt: retryOnInterrupt).get() } | ||||||
|
||||||
@usableFromInline | ||||||
internal func _sync(retryOnInterrupt: Bool) -> Result<(), Errno> { | ||||||
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) { | ||||||
system_fsync(self.rawValue) | ||||||
} | ||||||
} | ||||||
|
||||||
@_alwaysEmitIntoClient | ||||||
@available(*, unavailable, renamed: "sync") | ||||||
public static func fsync() throws -> FileDescriptor { | ||||||
fatalError("Not implemented") | ||||||
} | ||||||
|
||||||
// TODO: Figure out the deal with fdatasync. Darwin does not have it available. | ||||||
|
||||||
} | ||||||
|
||||||
/// A namespace for global, file-system operations | ||||||
public | ||||||
enum FileSystem {} | ||||||
|
||||||
extension FileSystem { | ||||||
/// Synchronize disk block status with that on disk. | ||||||
/// | ||||||
/// The corresponding C function is `sync`. | ||||||
public static func sync() { | ||||||
system_sync() | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: we should not use prepositions as variable names.
Or we could just call it
fd
.