syscalls: implement ftruncate

This commit is contained in:
Matthew Leach
2025-12-17 22:16:52 +00:00
committed by Ashwin Naren
parent 8bc271c266
commit 5511e28b72
6 changed files with 29 additions and 1 deletions

View File

@@ -46,7 +46,7 @@
| 0x2b (43) | statfs | (const char *pathname, struct statfs *buf) | __arm64_sys_statfs | false |
| 0x2c (44) | fstatfs | (unsigned int fd, struct statfs *buf) | __arm64_sys_fstatfs | false |
| 0x2d (45) | truncate | (const char *path, long length) | __arm64_sys_truncate | false |
| 0x2e (46) | ftruncate | (unsigned int fd, off_t length) | __arm64_sys_ftruncate | false |
| 0x2e (46) | ftruncate | (unsigned int fd, off_t length) | __arm64_sys_ftruncate | true |
| 0x2f (47) | fallocate | (int fd, int mode, loff_t offset, loff_t len) | __arm64_sys_fallocate | false |
| 0x30 (48) | faccessat | (int dfd, const char *filename, int mode) | __arm64_sys_faccessat | true |
| 0x31 (49) | chdir | (const char *filename) | __arm64_sys_chdir | true |

View File

@@ -1,3 +1,4 @@
use crate::fs::syscalls::trunc::sys_ftruncate;
use crate::kernel::power::sys_reboot;
use crate::kernel::rand::sys_getrandom;
use crate::memory::mmap::sys_mprotect;
@@ -91,6 +92,7 @@ pub async fn handle_syscall() {
0x18 => sys_dup3(arg1.into(), arg2.into(), arg3 as _),
0x19 => sys_fcntl(arg1.into(), arg2 as _, arg3 as _).await,
0x1d => sys_ioctl(arg1.into(), arg2 as _, arg3 as _).await,
0x2e => sys_ftruncate(arg1.into(), arg2 as _).await,
0x30 => sys_faccessat(arg1.into(), TUA::from_value(arg2 as _), arg3 as _).await,
0x31 => sys_chdir(TUA::from_value(arg1 as _)).await,
0x38 => {

View File

@@ -84,6 +84,11 @@ pub trait FileOps: Send + Sync {
Err(KernelError::NotATty)
}
/// Truncate a file to a specified length
async fn truncate(&mut self, _ctx: &FileCtx, _new_size: usize) -> Result<()> {
Err(KernelError::InvalidValue)
}
/// Flushes any pending writes to the hardware.
async fn flush(&self, _ctx: &FileCtx) -> Result<()> {
Ok(())

View File

@@ -91,6 +91,10 @@ impl FileOps for RegFile {
Ok(total_bytes_written)
}
async fn truncate(&mut self, _ctx: &FileCtx, new_size: usize) -> Result<()> {
self.inode.truncate(new_size as _).await
}
fn poll_read_ready(&self) -> Pin<Box<dyn Future<Output = Result<()>> + 'static + Send>> {
// For regular files, polling just returns ready.
Box::pin(async { Ok(()) })

View File

@@ -9,3 +9,4 @@ pub mod seek;
pub mod splice;
pub mod stat;
pub mod sync;
pub mod trunc;

16
src/fs/syscalls/trunc.rs Normal file
View File

@@ -0,0 +1,16 @@
use crate::{process::fd_table::Fd, sched::current_task};
use libkernel::{
error::{KernelError, Result},
};
pub async fn sys_ftruncate(fd: Fd, new_size: usize) -> Result<usize> {
let fd = current_task()
.fd_table
.lock_save_irq()
.get(fd)
.ok_or(KernelError::BadFd)?;
let (ops, ctx) = &mut *fd.lock().await;
ops.truncate(ctx, new_size).await.map(|_| 0)
}