about summary refs log tree commit diff
path: root/library/std/src/path.rs
diff options
context:
space:
mode:
author王宇逸 <Strawberry_Str@hotmail.com>2025-06-01 23:12:35 +0800
committer王宇逸 <Strawberry_Str@hotmail.com>2025-06-16 09:24:07 +0800
commit3cb0cba054d9d1871f3a10345d5c30cfc7ac214c (patch)
tree8e8c225bf85c5c9afc3afe80cdb7b5ebf49140ba /library/std/src/path.rs
parent015c7770ec0ffdba9ff03f1861144a827497f8ca (diff)
downloadrust-3cb0cba054d9d1871f3a10345d5c30cfc7ac214c.tar.gz
rust-3cb0cba054d9d1871f3a10345d5c30cfc7ac214c.zip
Handle win32 separator & prefixes for cygwin paths
Diffstat (limited to 'library/std/src/path.rs')
-rw-r--r--library/std/src/path.rs17
1 files changed, 16 insertions, 1 deletions
diff --git a/library/std/src/path.rs b/library/std/src/path.rs
index 826d9f0f39d..be8d9f1b054 100644
--- a/library/std/src/path.rs
+++ b/library/std/src/path.rs
@@ -1316,8 +1316,17 @@ impl PathBuf {
             need_sep = false
         }
 
+        let need_clear = if cfg!(target_os = "cygwin") {
+            // If path is absolute and its prefix is none, it is like `/foo`,
+            // and will be handled below.
+            path.prefix().is_some()
+        } else {
+            // On Unix: prefix is always None.
+            path.is_absolute() || path.prefix().is_some()
+        };
+
         // absolute `path` replaces `self`
-        if path.is_absolute() || path.prefix().is_some() {
+        if need_clear {
             self.inner.truncate(0);
 
         // verbatim paths need . and .. removed
@@ -3616,6 +3625,11 @@ impl Error for NormalizeError {}
 /// paths, this is currently equivalent to calling
 /// [`GetFullPathNameW`][windows-path].
 ///
+/// On Cygwin, this is currently equivalent to calling [`cygwin_conv_path`][cygwin-path]
+/// with mode `CCP_WIN_A_TO_POSIX`, and then being processed like other POSIX platforms.
+/// If a Windows path is given, it will be converted to an absolute POSIX path without
+/// keeping `..`.
+///
 /// Note that these [may change in the future][changes].
 ///
 /// # Errors
@@ -3673,6 +3687,7 @@ impl Error for NormalizeError {}
 /// [changes]: io#platform-specific-behavior
 /// [posix-semantics]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
 /// [windows-path]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfullpathnamew
+/// [cygwin-path]: https://cygwin.com/cygwin-api/func-cygwin-conv-path.html
 #[stable(feature = "absolute_path", since = "1.79.0")]
 pub fn absolute<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
     let path = path.as_ref();