Implement posix_getdents

This commit is contained in:
Wildan Mubarok
2025-09-23 16:57:13 +00:00
committed by Jeremy Soller
parent dc7491387b
commit 7416ba6ff3
6 changed files with 118 additions and 8 deletions
+29 -3
View File
@@ -1,4 +1,3 @@
use crate::{header::errno::EOPNOTSUPP, io::Write, out::Out};
use core::{arch::asm, ptr};
use super::{types::*, Pal, ERRNO};
@@ -6,15 +5,16 @@ use crate::{
c_str::CStr,
header::{
dirent::dirent,
errno::EINVAL,
errno::{EINVAL, EIO, EOPNOTSUPP},
fcntl::{AT_EMPTY_PATH, AT_FDCWD, AT_REMOVEDIR, AT_SYMLINK_NOFOLLOW},
signal::SIGCHLD,
sys_resource::{rlimit, rusage},
sys_stat::{stat, S_IFIFO},
sys_statvfs::statvfs,
sys_time::{timeval, timezone},
unistd::SEEK_SET,
unistd::{SEEK_CUR, SEEK_SET},
},
io::Write,
};
// use header::sys_times::tms;
use crate::{
@@ -558,6 +558,32 @@ impl Pal for Sys {
e_raw(unsafe { syscall!(PIPE2, fildes.as_mut_ptr(), flags) }).map(|_| ())
}
fn posix_getdents(fildes: c_int, buf: &mut [u8]) -> Result<usize> {
let current_offset = Self::lseek(fildes, 0, SEEK_CUR)? as u64;
let bytes_read = Self::getdents(fildes, buf, current_offset)?;
if bytes_read == 0 {
return Ok(0);
}
let mut bytes_processed = 0;
let mut next_offset = current_offset;
while bytes_processed < bytes_read {
let remaining_slice = &buf[bytes_processed..];
let (reclen, opaque_next) =
unsafe { Self::dent_reclen_offset(remaining_slice, bytes_processed) }
.ok_or(Errno(EIO))?;
if reclen == 0 {
return Err(Errno(EIO));
}
bytes_processed += reclen as usize;
next_offset = opaque_next;
}
Self::lseek(fildes, next_offset as off_t, SEEK_SET)?;
Ok(bytes_read)
}
#[cfg(target_arch = "x86_64")]
unsafe fn rlct_clone(stack: *mut usize) -> Result<crate::pthread::OsTid> {
let flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD;