|
| 1 | +use crate::ffi::OsString; |
| 2 | +use crate::os::unix::ffi::OsStringExt; |
| 3 | +use crate::path::{Path, PathBuf}; |
| 4 | +use crate::sys::common::small_c_string::run_path_with_cstr; |
| 5 | +use crate::sys::cvt; |
| 6 | +use crate::{io, ptr}; |
| 7 | + |
| 8 | +#[inline] |
| 9 | +pub fn is_sep_byte(b: u8) -> bool { |
| 10 | + b == b'/' || b == b'\\' |
| 11 | +} |
| 12 | + |
| 13 | +/// Cygwin allways prefers `/` over `\`, and it always converts all `/` to `\` |
| 14 | +/// internally when calling Win32 APIs. Therefore, the server component of path |
| 15 | +/// `\\?\UNC\localhost/share` is `localhost/share` on Win32, but `localhost` |
| 16 | +/// on Cygwin. |
| 17 | +#[inline] |
| 18 | +pub fn is_verbatim_sep(b: u8) -> bool { |
| 19 | + b == b'/' || b == b'\\' |
| 20 | +} |
| 21 | + |
| 22 | +pub use super::windows_prefix::parse_prefix; |
| 23 | + |
| 24 | +pub const MAIN_SEP_STR: &str = "/"; |
| 25 | +pub const MAIN_SEP: char = '/'; |
| 26 | + |
| 27 | +unsafe extern "C" { |
| 28 | + // Doc: https://cygwin.com/cygwin-api/func-cygwin-conv-path.html |
| 29 | + // Src: https://github.com/cygwin/cygwin/blob/718a15ba50e0d01c79800bd658c2477f9a603540/winsup/cygwin/path.cc#L3902 |
| 30 | + // Safety: |
| 31 | + // * `what` should be `CCP_WIN_A_TO_POSIX` here |
| 32 | + // * `from` is null-terminated UTF-8 path |
| 33 | + // * `to` is buffer, the buffer size is `size`. |
| 34 | + // |
| 35 | + // Converts a path to an absolute POSIX path, no matter the input is Win32 path or POSIX path. |
| 36 | + fn cygwin_conv_path( |
| 37 | + what: libc::c_uint, |
| 38 | + from: *const libc::c_char, |
| 39 | + to: *mut u8, |
| 40 | + size: libc::size_t, |
| 41 | + ) -> libc::ssize_t; |
| 42 | +} |
| 43 | + |
| 44 | +const CCP_WIN_A_TO_POSIX: libc::c_uint = 2; |
| 45 | + |
| 46 | +/// Make a POSIX path absolute. |
| 47 | +pub(crate) fn absolute(path: &Path) -> io::Result<PathBuf> { |
| 48 | + run_path_with_cstr(path, &|path| { |
| 49 | + let conv = CCP_WIN_A_TO_POSIX; |
| 50 | + let size = cvt(unsafe { cygwin_conv_path(conv, path.as_ptr(), ptr::null_mut(), 0) })?; |
| 51 | + // If success, size should not be 0. |
| 52 | + debug_assert!(size >= 1); |
| 53 | + let size = size as usize; |
| 54 | + let mut buffer = Vec::with_capacity(size); |
| 55 | + cvt(unsafe { cygwin_conv_path(conv, path.as_ptr(), buffer.as_mut_ptr(), size) })?; |
| 56 | + unsafe { |
| 57 | + buffer.set_len(size - 1); |
| 58 | + } |
| 59 | + Ok(PathBuf::from(OsString::from_vec(buffer))) |
| 60 | + }) |
| 61 | + .map(|path| { |
| 62 | + if path.prefix().is_some() { |
| 63 | + return path; |
| 64 | + } |
| 65 | + |
| 66 | + // From unix.rs |
| 67 | + let mut components = path.components(); |
| 68 | + let path_os = path.as_os_str().as_encoded_bytes(); |
| 69 | + |
| 70 | + let mut normalized = if path_os.starts_with(b"//") && !path_os.starts_with(b"///") { |
| 71 | + components.next(); |
| 72 | + PathBuf::from("//") |
| 73 | + } else { |
| 74 | + PathBuf::new() |
| 75 | + }; |
| 76 | + normalized.extend(components); |
| 77 | + |
| 78 | + if path_os.ends_with(b"/") { |
| 79 | + normalized.push(""); |
| 80 | + } |
| 81 | + |
| 82 | + normalized |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +pub(crate) fn is_absolute(path: &Path) -> bool { |
| 87 | + if path.as_os_str().as_encoded_bytes().starts_with(b"\\") { |
| 88 | + path.has_root() && path.prefix().is_some() |
| 89 | + } else { |
| 90 | + path.has_root() |
| 91 | + } |
| 92 | +} |
0 commit comments