-
Notifications
You must be signed in to change notification settings - Fork 109
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
Implement FilePath.getCurrentWorkingDirectory() #71
Changes from all 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 to the proess and care should be taken to make sure it does not unexpectedly change. | ||
public static func currentWorkingDirectory() throws -> FilePath { | ||
guard let cwd = system_getcwd(nil, 0) else { | ||
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.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
FilePath
used to have a with-style internal init that used max-path-length, but it turns out there are multiple notions of such a thing and it's probably too complicated as-is (and wastes space). I agree that it doesn't matter for now; in the future we'd like to do it with something like an init onFilePath
, which can make the space/speed tradeoffs.