use super::{DirEntry, DirScan, EntryFailure, EnumPlan, FileId};
use crate::predicate::EntryType;
use crate::syntax::decode;
use rustix::fs::{self, AtFlags, Dir, FileType, Mode, OFlags, Statx, StatxFlags};
use std::ffi::CStr;
use std::io;
use std::path::Path;
pub fn enumerate_dir_native(path: &Path, plan: EnumPlan) -> io::Result<DirScan> {
let dirfd = fs::open(
path,
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
Mode::empty(),
)?;
let mut names: Vec<(std::ffi::CString, FileType)> = Vec::new();
let mut entry_errors = Vec::new();
let dir = Dir::read_from(&dirfd)?;
for entry in dir {
let entry = match entry {
Ok(e) => e,
Err(err) => {
if names.is_empty() {
return Err(io::Error::from(err));
}
entry_errors.push(EntryFailure {
name: None,
source: io::Error::from(err),
});
break;
}
};
let name = entry.file_name();
if is_dot_entry(name) {
continue;
}
names.push((name.to_owned(), entry.file_type()));
}
let mask = StatxFlags::TYPE
| StatxFlags::SIZE
| StatxFlags::INO
| StatxFlags::ATIME
| StatxFlags::MTIME
| StatxFlags::CTIME
| StatxFlags::BTIME;
let mut entries = Vec::with_capacity(names.len());
for (name, dtype) in &names {
let need_stat = plan.want_stat
|| plan.wants_file_id_for(matches!(dtype, FileType::Symlink))
|| matches!(dtype, FileType::Unknown);
if !need_stat {
entries.push(make_entry_from_dtype(name, *dtype));
continue;
}
match fs::statx(&dirfd, name.as_c_str(), AtFlags::SYMLINK_NOFOLLOW, mask) {
Ok(st) => entries.push(make_entry(name, &st)),
Err(err) => entry_errors.push(EntryFailure {
name: Some(decode::decode_bytes(name.to_bytes())),
source: io::Error::from(err),
}),
}
}
Ok(DirScan {
entries,
entry_errors,
})
}
fn classify(file_type: FileType) -> (EntryType, bool) {
match file_type {
FileType::Directory => (EntryType::Dir, false),
FileType::RegularFile => (EntryType::File, false),
FileType::Symlink => (EntryType::Other, true),
_ => (EntryType::Other, false),
}
}
fn make_entry_from_dtype(name: &CStr, dtype: FileType) -> DirEntry {
let (entry_type, is_reparse) = classify(dtype);
DirEntry {
name: decode::decode_bytes(name.to_bytes()),
entry_type,
is_reparse,
reparse_tag: 0,
attributes: 0,
size: 0,
btime: 0,
mtime: 0,
atime: 0,
ctime: 0,
file_id: FileId { volume: 0, id: 0 },
}
}
fn make_entry(name: &CStr, st: &Statx) -> DirEntry {
let (entry_type, is_reparse) = classify(FileType::from_raw_mode(u32::from(st.stx_mode)));
let volume = rustix::fs::makedev(st.stx_dev_major, st.stx_dev_minor);
DirEntry {
name: decode::decode_bytes(name.to_bytes()),
entry_type,
is_reparse,
reparse_tag: 0,
attributes: 0,
size: st.stx_size,
btime: statx_time(st, StatxFlags::BTIME, st.stx_btime),
mtime: statx_time(st, StatxFlags::MTIME, st.stx_mtime),
atime: statx_time(st, StatxFlags::ATIME, st.stx_atime),
ctime: statx_time(st, StatxFlags::CTIME, st.stx_ctime),
file_id: if StatxFlags::from_bits_truncate(st.stx_mask).contains(StatxFlags::INO) {
FileId {
volume,
id: u128::from(st.stx_ino),
}
} else {
FileId { volume: 0, id: 0 }
},
}
}
fn statx_time(st: &Statx, field: StatxFlags, ts: rustix::fs::StatxTimestamp) -> i64 {
if !StatxFlags::from_bits_truncate(st.stx_mask).contains(field) {
return 0;
}
ts.tv_sec
.saturating_mul(1_000_000_000)
.saturating_add(i64::from(ts.tv_nsec))
}
fn is_dot_entry(name: &CStr) -> bool {
matches!(name.to_bytes(), b"." | b"..")
}