-
Notifications
You must be signed in to change notification settings - Fork 127
Implement FilePath.getCurrentWorkingDirectory() #71
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,6 +62,18 @@ public struct FilePath { | |
|
|
||
| // @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) | ||
| extension FilePath { | ||
|
|
||
| /// Returns the current working directory of this process. | ||
| /// | ||
| /// - Warning: This value is global and care should be taken to make sure it does not unexpectedly change. | ||
| public static func getCurrentWorkingDirectoryForProcess() throws -> FilePath { | ||
| guard let cwd = system_getcwd(nil, 0) else { | ||
|
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. This will allocate twice -- wouldn't it be better if we supplied this with an appropriately sized FilePath buffer and only resize it if we guessed wrong? If desired, we can also eliminate allocations altogether by adding a variant that takes an inout FilePath. (I don't think we need that.) 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. This might be better but given all the discussion about how this shouldn't be used often, I preferred the simpler variant rather than adding an infrequently taken second codepath. 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.
|
||
| throw Errno.current | ||
| } | ||
| defer { system_free(cwd) } | ||
| return FilePath(platformString: cwd) | ||
| } | ||
|
|
||
|
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. Way back when I was thinking this would be hosted on some kind of current process type, but I think this approach makes more sense. It's also available for typename elision IIUC: func foo(_: FilePath)
let x = foo(.getCurrentWorkingDirectory())I feel like making this a function rather than a throwing var makes sense (ignoring tools version concerns) as it will make a syscall. |
||
| /// The length of the file path, excluding the null terminator. | ||
| public var length: Int { _storage.length } | ||
| } | ||
|
|
||
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.
We should lose the
getprefix -- per the Swift API design guidelines, functions without side effects should read as noun phrases.I know you specifically added this in response to discussions, but I would like to also remove the
ForProcesssuffix. There are two reasons for this:First, I don't think it makes sense to try documenting the precise semantics of these functions through their naming -- it seems futile to me, and it leads to an unwieldy naming scheme.
Far, far more importantly, while the default semantics of these syscalls have been pretty stable, there is no reason an OS cannot switch to different behavior in certain circumstances. For instance, on Linux and macOS at least we have non-portable ways to set up a thread-local working directory.