use std::io;
#[cfg(windows)]
use std::os::windows::io::AsHandle;
use super::dir::Dir;
use crate::backend::VolumeInfo;
#[cfg(unix)]
pub(super) fn volume_info(dir: &Dir, root_dev: Option<u64>) -> io::Result<VolumeInfo> {
let st = rustix::fs::fstatvfs(dir)?;
let id = if st.f_fsid != 0 {
st.f_fsid
} else {
root_dev.unwrap_or(0)
};
Ok(crate::host::volume_info(&st, id))
}
#[cfg(windows)]
pub(super) fn volume_info(dir: &Dir, _root_dev: Option<u64>) -> io::Result<VolumeInfo> {
crate::host::volume_info_of(dir)
}
#[cfg(windows)]
pub(super) fn volume_of(file: &impl AsHandle) -> Option<(u32, String)> {
use std::os::windows::io::AsRawHandle as _;
use windows_sys::Win32::Storage::FileSystem::GetVolumeInformationByHandleW;
let handle = file.as_handle().as_raw_handle();
let mut flags = 0u32;
let mut name = [0u16; 64];
let ok = unsafe {
GetVolumeInformationByHandleW(
handle,
std::ptr::null_mut(),
0,
std::ptr::null_mut(),
std::ptr::null_mut(),
&mut flags,
name.as_mut_ptr(),
name.len() as u32,
)
};
let len = name.iter().position(|&u| u == 0).unwrap_or(name.len());
(ok != 0).then(|| (flags, String::from_utf16_lossy(&name[..len])))
}
#[cfg(target_os = "linux")]
pub(super) fn is_case_sensitive(dir: &Dir) -> bool {
const MSDOS_SUPER_MAGIC: i128 = 0x4d44;
const EXFAT_SUPER_MAGIC: i128 = 0x2011_BAB0;
const FS_CASEFOLD_FL: u32 = 0x4000_0000;
if let Ok(st) = rustix::fs::fstatfs(dir)
&& matches!(i128::from(st.f_type), MSDOS_SUPER_MAGIC | EXFAT_SUPER_MAGIC)
{
return false;
}
match dir.open(".") {
Ok(file) => rustix::fs::ioctl_getflags(&file)
.map_or(true, |flags| flags.bits() & FS_CASEFOLD_FL == 0),
Err(_) => true,
}
}
#[cfg(target_os = "macos")]
pub(super) fn is_case_sensitive(dir: &Dir) -> bool {
use std::os::fd::AsRawFd as _;
unsafe { libc::fpathconf(dir.as_raw_fd(), libc::_PC_CASE_SENSITIVE) == 1 }
}
#[cfg(windows)]
pub(super) fn is_case_sensitive(dir: &Dir) -> bool {
use std::os::windows::io::{AsHandle as _, AsRawHandle as _};
use windows_sys::Win32::Storage::FileSystem::{
FILE_CASE_SENSITIVE_INFO, FileCaseSensitiveInfo, GetFileInformationByHandleEx,
};
const FILE_CS_FLAG_CASE_SENSITIVE_DIR: u32 = 0x1;
let mut info = FILE_CASE_SENSITIVE_INFO::default();
let ok = unsafe {
GetFileInformationByHandleEx(
dir.as_handle().as_raw_handle(),
FileCaseSensitiveInfo,
(&raw mut info).cast(),
size_of::<FILE_CASE_SENSITIVE_INFO>() as u32,
)
};
ok != 0 && info.Flags & FILE_CS_FLAG_CASE_SENSITIVE_DIR != 0
}
#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
pub(super) fn is_case_sensitive(_dir: &Dir) -> bool {
true
}