|
| 1 | +use crate::ffi::{OsStr, OsString}; |
| 2 | +use crate::os::unix::ffi::OsStringExt; |
| 3 | +use crate::path::{Path, PathBuf, Prefix}; |
| 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 | +#[inline] |
| 14 | +pub fn is_verbatim_sep(b: u8) -> bool { |
| 15 | + b == b'/' || b == b'\\' |
| 16 | +} |
| 17 | + |
| 18 | +#[inline] |
| 19 | +pub fn parse_prefix(_: &OsStr) -> Option<Prefix<'_>> { |
| 20 | + None |
| 21 | +} |
| 22 | + |
| 23 | +pub const MAIN_SEP_STR: &str = "/"; |
| 24 | +pub const MAIN_SEP: char = '/'; |
| 25 | + |
| 26 | +unsafe extern "C" { |
| 27 | + // Doc: https://cygwin.com/cygwin-api/func-cygwin-conv-path.html |
| 28 | + // Src: https://github.com/cygwin/cygwin/blob/718a15ba50e0d01c79800bd658c2477f9a603540/winsup/cygwin/path.cc#L3902 |
| 29 | + // Safety: |
| 30 | + // * `what` should be `CCP_WIN_A_TO_POSIX` here |
| 31 | + // * `from` is null-terminated UTF-8 path |
| 32 | + // * `to` is buffer, the buffer size is `size`. |
| 33 | + // |
| 34 | + // Converts a path to an absolute POSIX path, no matter the input is Win32 path or POSIX path. |
| 35 | + fn cygwin_conv_path( |
| 36 | + what: libc::c_uint, |
| 37 | + from: *const libc::c_char, |
| 38 | + to: *mut u8, |
| 39 | + size: libc::size_t, |
| 40 | + ) -> libc::ssize_t; |
| 41 | +} |
| 42 | + |
| 43 | +const CCP_WIN_A_TO_POSIX: libc::c_uint = 2; |
| 44 | + |
| 45 | +/// Make a POSIX path absolute. |
| 46 | +pub(crate) fn absolute(path: &Path) -> io::Result<PathBuf> { |
| 47 | + run_path_with_cstr(path, &|path| { |
| 48 | + let size = cvt(unsafe { |
| 49 | + cygwin_conv_path(CCP_WIN_A_TO_POSIX, path.as_ptr(), ptr::null_mut(), 0) |
| 50 | + })?; |
| 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 { |
| 56 | + cygwin_conv_path(CCP_WIN_A_TO_POSIX, path.as_ptr(), buffer.as_mut_ptr(), size) |
| 57 | + })?; |
| 58 | + unsafe { |
| 59 | + buffer.set_len(size - 1); |
| 60 | + } |
| 61 | + Ok(PathBuf::from(OsString::from_vec(buffer))) |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +pub(crate) fn is_absolute(path: &Path) -> bool { |
| 66 | + path.has_root() |
| 67 | +} |
0 commit comments