use std::fs::File;
use std::io;
use std::os::fd::AsRawFd;
use std::path::Path;
pub use super::unix::{
allocated_bytes, apply_mode, create_new_no_follow, create_symlink, link_count, local_time,
name_problem, open_exclusive, open_existing_no_follow, open_folder_no_follow, path_len,
same_filesystem, sync_dir, unlock_folder,
};
pub const MAX_PATH_LEN: usize = 4095 - ".hatchzip-part".len();
pub const SYNC_EACH_FILE: bool = false;
pub fn sync_batch(output: &Path) -> io::Result<()> {
let f = File::open(output)?;
if unsafe { libc::syncfs(f.as_raw_fd()) } != 0 {
return Err(io::Error::last_os_error());
}
Ok(())
}
pub fn punch_hole(file: &File, offset: u64, len: u64) -> io::Result<bool> {
if len == 0 {
return Ok(true);
}
let (Ok(off), Ok(len)) = (i64::try_from(offset), i64::try_from(len)) else {
return Err(io::ErrorKind::InvalidInput.into());
};
loop {
let mode = libc::FALLOC_FL_PUNCH_HOLE | libc::FALLOC_FL_KEEP_SIZE;
if unsafe { libc::fallocate(file.as_raw_fd(), mode, off, len) } == 0 {
return Ok(true);
}
let e = io::Error::last_os_error();
match e.raw_os_error() {
Some(libc::EINTR) => continue,
Some(libc::EOPNOTSUPP | libc::ENOSYS) => return Ok(false),
_ => return Err(e),
}
}
}
const BTRFS_MAGIC: i64 = 0x9123_683E;
const ZFS_MAGIC: i64 = 0x2FC1_2FC1;
pub fn punch_granule(dir: &Path) -> u64 {
let Ok(f) = File::open(dir) else {
return 128 << 20;
};
let mut st: libc::statfs = unsafe { std::mem::zeroed() };
if unsafe { libc::fstatfs(f.as_raw_fd(), &mut st) } != 0 {
return 128 << 20;
}
match st.f_type as i64 {
BTRFS_MAGIC => 128 << 20,
ZFS_MAGIC => 16 << 20,
_ => (st.f_bsize as u64).max(64 << 10),
}
}
pub const NORMALIZES_NAMES: bool = false;
pub fn case_sensitive(dir: &Path) -> bool {
use std::os::unix::fs::OpenOptionsExt;
const EXT: u32 = 0xEF53;
const F2FS: u32 = 0xF2F5_2010;
const TMPFS: u32 = 0x0102_1994;
const XFS: u32 = 0x5846_5342;
let Ok(f) = std::fs::OpenOptions::new()
.read(true)
.custom_flags(libc::O_DIRECTORY)
.open(dir)
else {
return false;
};
let mut st: libc::statfs = unsafe { std::mem::zeroed() };
if unsafe { libc::fstatfs(f.as_raw_fd(), &mut st) } != 0 {
return false;
}
match st.f_type as u32 {
magic if magic == BTRFS_MAGIC as u32 => true,
EXT | F2FS | TMPFS => folder_ignores_case(&f) == Some(false),
XFS => xfs_ignores_case(&f) == Some(false),
_ => false,
}
}
fn folder_ignores_case(dir: &File) -> Option<bool> {
const FS_CASEFOLD_FL: libc::c_uint = 0x4000_0000;
let mut flags: libc::c_uint = 0;
let got = unsafe { libc::ioctl(dir.as_raw_fd(), libc::FS_IOC_GETFLAGS, &mut flags) };
(got == 0).then_some(flags & FS_CASEFOLD_FL != 0)
}
fn xfs_ignores_case(any: &File) -> Option<bool> {
#[repr(C)]
struct Geometry {
_sizes: [u32; 8],
_blocks: [u64; 4],
_uuid: [u8; 16],
_stripes_and_version: [u32; 3],
flags: u32,
_more_sizes: [u32; 3],
}
const FSGEOMETRY_V1: libc::Ioctl = libc::_IOR::<Geometry>('X' as u32, 100);
const FLAGS_DIRV2CI: u32 = 1 << 12;
let mut geometry: Geometry = unsafe { std::mem::zeroed() };
let got = unsafe { libc::ioctl(any.as_raw_fd(), FSGEOMETRY_V1, &mut geometry) };
(got == 0).then_some(geometry.flags & FLAGS_DIRV2CI != 0)
}
#[cfg(feature = "rar")]
pub struct RarName(std::ffi::CString);
#[cfg(feature = "rar")]
impl RarName {
pub fn new(path: &Path) -> io::Result<Self> {
use std::os::unix::ffi::OsStrExt;
std::ffi::CString::new(path.as_os_str().as_bytes())
.map(Self)
.map_err(|_| io::ErrorKind::InvalidInput.into())
}
pub fn open_data(&self, mode: std::ffi::c_uint) -> unrar_ng_sys::OpenArchiveDataEx {
unrar_ng_sys::OpenArchiveDataEx::new(self.0.as_ptr(), mode)
}
}