Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/safeposix/syscalls/fs_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3496,16 +3496,42 @@ impl Cage {
}

//------------------GETDENTS SYSCALL------------------
/// ## `getdents_syscall`
///
/// ### Description
/// This function implements the `getdents` system call, which reads directory entries from a directory file descriptor
/// and returns them in a buffer. Reading directory entries using multiple read calls can be less efficient because it
/// involves reading the data in smaller chunks and then parsing it.
/// getdents can often be faster by reading directory entries in a more optimized way.
///
/// ### Function Arguments
/// * `fd`: A file descriptor representing the directory to read.
/// * `dirp`: A pointer to a buffer where the directory entries will be written.
/// * `bufsize`: The size of the buffer in bytes.
///
/// ### Returns
/// * The number of bytes written to the buffer on success.
///
/// ### Errors and Panics
/// * `EINVAL(22)`: If the buffer size is too small or if the file descriptor is invalid.
/// * `ENOTDIR(20)`: If the file descriptor does not refer to a existing directory.
/// * `ESPIPE(29)`: If the file descriptor does not refer to a file.

pub fn getdents_syscall(&self, fd: i32, dirp: *mut u8, bufsize: u32) -> i32 {
let mut vec: Vec<(interface::ClippedDirent, Vec<u8>)> = Vec::new();

// make sure bufsize is at least greater than size of a ClippedDirent struct
// ClippedDirent is a simplified version of the traditional dirent structure used in POSIX systems
// By using a simpler structure, SafePosix can store and retrieve directory entries more efficiently,
// potentially improving performance compared to using the full dirent structure.
if bufsize <= interface::CLIPPED_DIRENT_SIZE {
return syscall_error(Errno::EINVAL, "getdents", "Result buffer is too small.");
}

let checkedfd = self.get_filedescriptor(fd).unwrap();
let checkedfd = match self.get_filedescriptor(fd) {
Ok(fd) => fd,
Err(_) => return syscall_error(Errno::EBADF, "getdents", "Invalid file descriptor."),
};
Comment thread
rupeshkoushik07 marked this conversation as resolved.
let mut unlocked_fd = checkedfd.write();
if let Some(filedesc_enum) = &mut *unlocked_fd {
match filedesc_enum {
Expand Down Expand Up @@ -4364,7 +4390,7 @@ impl Cage {
// Cloning and dropping the original reference lets us modify the value without deadlocking the dashmap.
drop(sementry);
// Acquire the semaphore. This operation will block the calling process until the
Comment thread
rupeshkoushik07 marked this conversation as resolved.
Outdated
///semaphore becomes available. The`lock` method internally decrements the semaphore value.
// semaphore becomes available. The`lock` method internally decrements the semaphore value.
// The lock fun is located in misc.rs
semaphore.lock();
} else {
Expand Down
86 changes: 86 additions & 0 deletions src/tests/fs_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,92 @@ pub mod fs_tests {
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}
#[test]
fn ut_lind_fs_getdents_invalid_fd() {
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);

let bufsize = 50;
let mut vec = vec![0u8; bufsize as usize];
let baseptr: *mut u8 = &mut vec[0];

// Create a directory
assert_eq!(cage.mkdir_syscall("/getdents", S_IRWXA), 0);

// Open the directory
let fd = cage.open_syscall("/getdents", O_RDWR, S_IRWXA);

// Attempt to call `getdents_syscall` with an invalid file descriptor
Comment thread
rupeshkoushik07 marked this conversation as resolved.
let result = cage.getdents_syscall(-1, baseptr, bufsize as u32);

// Assert that the return value is EBADF (errno for "Bad file descriptor")
assert_eq!(result, -(Errno::EBADF as i32));

// Close the directory
assert_eq!(cage.close_syscall(fd), 0);

assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}

#[test]
fn ut_lind_fs_getdents_bufsize_too_small() {
let _thelock = setup::lock_and_init();
let cage = interface::cagetable_getref(1);

let bufsize = interface::CLIPPED_DIRENT_SIZE - 1; // Buffer size smaller than CLIPPED_DIRENT_SIZE
let mut vec = vec![0u8; bufsize as usize];
let baseptr: *mut u8 = &mut vec[0];

// Create a directory
assert_eq!(cage.mkdir_syscall("/getdents", S_IRWXA), 0);

// Open the directory
let fd = cage.open_syscall("/getdents", O_RDWR, S_IRWXA);

// Attempt to call `getdents_syscall` with a buffer size smaller than CLIPPED_DIRENT_SIZE
let result = cage.getdents_syscall(fd, baseptr, bufsize as u32);

// Assert that the return value is EINVAL (errno for "Invalid argument")
assert_eq!(result, -(Errno::EINVAL as i32));

// Close the directory
assert_eq!(cage.close_syscall(fd), 0);

assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}

#[test]
fn ut_lind_fs_getdents_non_directory_fd() {
// Acquire a lock on TESTMUTEX to prevent other tests from running concurrently,
// and also perform clean environment setup.
let _thelock = setup::lock_and_init();

let cage = interface::cagetable_getref(1);

// Create a regular file
let filepath = "/regularfile";
let fd = cage.open_syscall(filepath, O_CREAT | O_WRONLY, S_IRWXA);
assert_ne!(fd, -(Errno::ENOENT as i32));
Comment thread
rupeshkoushik07 marked this conversation as resolved.
Outdated

// Allocate a buffer to store directory entries
let bufsize = 1024;
let mut vec = vec![0u8; bufsize as usize];
let baseptr: *mut u8 = &mut vec[0];
println!("Buffer contents: {:?}", vec);
Comment thread
rupeshkoushik07 marked this conversation as resolved.
Outdated

// Attempt to call getdents_syscall on the regular file descriptor
let result = cage.getdents_syscall(fd, baseptr, bufsize as u32);
println!("Syscall result: {}", result);
// Verify that it returns ENOTDIR
assert_eq!(result, -(Errno::ENOTDIR as i32));

// Clean up: Close the file descriptor and finalize the test environment
assert_eq!(cage.close_syscall(fd), 0);
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
lindrustfinalize();
}

#[test]
pub fn ut_lind_fs_dir_chdir_getcwd() {
Expand Down