From: Tulio A M Mendes Date: Thu, 12 Feb 2026 02:58:41 +0000 (-0300) Subject: feat: truncate/ftruncate syscalls (78/79) + ulibc wrappers X-Git-Url: https://projects.tadryanom.me/?a=commitdiff_plain;h=97e566f7b770df14b58af058ca3e735a72f8ae76;p=AdrOS.git feat: truncate/ftruncate syscalls (78/79) + ulibc wrappers --- diff --git a/include/syscall.h b/include/syscall.h index b6418d71..adee9486 100644 --- a/include/syscall.h +++ b/include/syscall.h @@ -110,6 +110,11 @@ enum { SYSCALL_UMASK = 75, SYSCALL_SETUID = 76, SYSCALL_SETGID = 77, + SYSCALL_TRUNCATE = 78, + SYSCALL_FTRUNCATE = 79, + SYSCALL_SIGSUSPEND = 80, + SYSCALL_READV = 81, + SYSCALL_WRITEV = 82, }; #endif diff --git a/src/kernel/syscall.c b/src/kernel/syscall.c index 8fab4e38..949bd5e0 100644 --- a/src/kernel/syscall.c +++ b/src/kernel/syscall.c @@ -2238,7 +2238,8 @@ void syscall_handler(struct registers* regs) { } if (syscall_no == SYSCALL_PREAD || syscall_no == SYSCALL_PWRITE || - syscall_no == SYSCALL_ACCESS) { + syscall_no == SYSCALL_ACCESS || syscall_no == SYSCALL_TRUNCATE || + syscall_no == SYSCALL_FTRUNCATE) { posix_ext_syscall_dispatch(regs, syscall_no); return; } @@ -2339,6 +2340,32 @@ static void posix_ext_syscall_dispatch(struct registers* regs, uint32_t syscall_ return; } + if (syscall_no == SYSCALL_FTRUNCATE) { + int fd = (int)regs->ebx; + uint32_t length = regs->ecx; + struct file* f = fd_get(fd); + if (!f || !f->node) { regs->eax = (uint32_t)-EBADF; return; } + if (!(f->node->flags & FS_FILE)) { regs->eax = (uint32_t)-EINVAL; return; } + f->node->length = length; + regs->eax = 0; + return; + } + + if (syscall_no == SYSCALL_TRUNCATE) { + const char* user_path = (const char*)regs->ebx; + uint32_t length = regs->ecx; + if (!user_path) { regs->eax = (uint32_t)-EFAULT; return; } + char path[128]; + int prc = path_resolve_user(user_path, path, sizeof(path)); + if (prc < 0) { regs->eax = (uint32_t)prc; return; } + fs_node_t* node = vfs_lookup(path); + if (!node) { regs->eax = (uint32_t)-ENOENT; return; } + if (!(node->flags & FS_FILE)) { regs->eax = (uint32_t)-EISDIR; return; } + node->length = length; + regs->eax = 0; + return; + } + regs->eax = (uint32_t)-ENOSYS; } diff --git a/user/ulibc/include/syscall.h b/user/ulibc/include/syscall.h index 8404b67d..f43ab027 100644 --- a/user/ulibc/include/syscall.h +++ b/user/ulibc/include/syscall.h @@ -69,6 +69,11 @@ enum { SYS_UMASK = 75, SYS_SETUID = 76, SYS_SETGID = 77, + SYS_TRUNCATE = 78, + SYS_FTRUNCATE = 79, + SYS_SIGSUSPEND = 80, + SYS_READV = 81, + SYS_WRITEV = 82, }; /* Raw syscall wrappers — up to 5 args via INT 0x80 */ diff --git a/user/ulibc/include/unistd.h b/user/ulibc/include/unistd.h index 356780c9..af7f24df 100644 --- a/user/ulibc/include/unistd.h +++ b/user/ulibc/include/unistd.h @@ -49,6 +49,8 @@ int pwrite(int fd, const void* buf, size_t count, int offset); int access(const char* path, int mode); int setuid(int uid); int setgid(int gid); +int truncate(const char* path, int length); +int ftruncate(int fd, int length); void* brk(void* addr); void _exit(int status) __attribute__((noreturn)); diff --git a/user/ulibc/src/unistd.c b/user/ulibc/src/unistd.c index 8e2e0d29..f1c04483 100644 --- a/user/ulibc/src/unistd.c +++ b/user/ulibc/src/unistd.c @@ -123,6 +123,14 @@ int setgid(int gid) { return __syscall_ret(_syscall1(SYS_SETGID, gid)); } +int truncate(const char* path, int length) { + return __syscall_ret(_syscall2(SYS_TRUNCATE, (int)path, length)); +} + +int ftruncate(int fd, int length) { + return __syscall_ret(_syscall2(SYS_FTRUNCATE, fd, length)); +} + void* brk(void* addr) { return (void*)_syscall1(SYS_BRK, (int)addr); }