-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
std::path::absolute
#91673
std::path::absolute
#91673
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
|
Good point! But I'm slightly wary of the Windows behaviour here. I'm thinking of legacy paths such as E.g. let path = Path::new(r"C:\path").join("D:foo");
// path is "D:foo"
let absolute = absolute(path);
// absolute is like "D:\something\foo"
// actual value depends on the `D` drive's current directory. My point being that the given path is relative but the base directory is still ignored. |
Is that an issue with |
Hm, probably with using them to implement
To be clear, I'm uncertain how much of an issue this is but it does make me wary of suggesting it. |
// SAFETY: `fill_utf16_buf` ensures the `buffer` and `size` are valid. | ||
// `lpfilename` is a pointer to a null terminated string that is not | ||
// invalidated until after `GetFullPathNameW` returns successfully. | ||
|buffer, size| unsafe { c::GetFullPathNameW(lpfilename, size, buffer, ptr::null_mut()) }, |
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.
Does this work for non \\?\
paths bigger than MAX_PATH? It would be nice to be able to do std::path::absolute()
and then prefix \\?\
if necessary to handle long paths.
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.
Indeed it does! The std already does exactly that. Unfortunately the current docs for GetFullPathNameW
suggests otherwise but this was an oversight that's going to be corrected when the online docs are next rebuilt (see the sdk-api source).
In general this seems "fine", though I don't know if it's quite the API we want. Adding it as unstable to start though seems quite reasonable; we can discuss prior to stabilization whether this hits the right point on the API spectrum. Some initial questions I might think about:
Can you file a tracking issue, and then I can r+? |
@Mark-Simulacrum I've filed a tracking issue. I've added your points to the unresolved questions and I'll try to give my thoughts on them soon. |
@bors r+ rollup Thanks! Going to go ahead and approve this PR. |
📌 Commit 0660732 has been approved by |
…imulacrum `std::path::absolute` Implements rust-lang#59117 by adding a `std::path::absolute` function that creates an absolute path without reading the filesystem. This is intended to be a drop-in replacement for [`std::fs::canonicalize`](https://doc.rust-lang.org/std/fs/fn.canonicalize.html) in cases where it isn't necessary to resolve symlinks. It can be used on paths that don't exist or where resolving symlinks is unwanted. It can also be used to avoid circumstances where `canonicalize` might otherwise fail. On Windows this is a wrapper around [`GetFullPathNameW`](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfullpathnamew). On Unix it partially implements the POSIX [pathname resolution](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13) specification, stopping just short of actually resolving symlinks.
…imulacrum `std::path::absolute` Implements rust-lang#59117 by adding a `std::path::absolute` function that creates an absolute path without reading the filesystem. This is intended to be a drop-in replacement for [`std::fs::canonicalize`](https://doc.rust-lang.org/std/fs/fn.canonicalize.html) in cases where it isn't necessary to resolve symlinks. It can be used on paths that don't exist or where resolving symlinks is unwanted. It can also be used to avoid circumstances where `canonicalize` might otherwise fail. On Windows this is a wrapper around [`GetFullPathNameW`](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfullpathnamew). On Unix it partially implements the POSIX [pathname resolution](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13) specification, stopping just short of actually resolving symlinks.
@bors r- Failed in #92785 (comment) The wasm32-unknown-unknown target reuses Let me know if you have any questions about that or how to test it. |
Thanks! I got |
💔 Test failed - checks-actions |
This comment has been minimized.
This comment has been minimized.
fd5bb45
to
c17568e
Compare
#87869 changed the way of creating a const |
This comment has been minimized.
This comment has been minimized.
c17568e
to
81cc3af
Compare
@bors r+ |
📌 Commit 81cc3af has been approved by |
☀️ Test successful - checks-actions |
Finished benchmarking commit (1f4681a): comparison url. Summary: This benchmark run did not return any relevant results. If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf. @rustbot label: -perf-regression |
assert_eq!(absolute("//a/b/c").unwrap(), Path::new("//a/b/c")); | ||
assert_eq!(absolute("///a/b/c").unwrap(), Path::new("/a/b/c")); | ||
assert_eq!(absolute("/a/b/c/").unwrap(), Path::new("/a/b/c/")); | ||
assert_eq!(absolute("/a/./b/../c/.././..").unwrap(), Path::new("/a/b/../c/../..")); |
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.
Unix test without dot prefix.
assert_eq!(absolute("./a").unwrap(), Path::new("/pwd/a")); // return /pwd/./a
assert_eq!(absolute("../a").unwrap(), Path::new("/parent_pwd/a")); // return /pwd/../a
I have tried in darwin. it is wrong
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.
Thank you for reporting this. I'll add more tests and fix the dot prefix case.
Implements #59117 by adding a
std::path::absolute
function that creates an absolute path without reading the filesystem. This is intended to be a drop-in replacement forstd::fs::canonicalize
in cases where it isn't necessary to resolve symlinks. It can be used on paths that don't exist or where resolving symlinks is unwanted. It can also be used to avoid circumstances wherecanonicalize
might otherwise fail.On Windows this is a wrapper around
GetFullPathNameW
. On Unix it partially implements the POSIX pathname resolution specification, stopping just short of actually resolving symlinks.