kernel+libc: Add truncate and ftruncate
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-08-03 08:47:37 +02:00
parent 53d9f5c6fc
commit c5e24e478f
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 49 additions and 1 deletions

View File

@ -229,3 +229,32 @@ Result<u64> sys_umask(Registers*, SyscallArgs args)
return old_umask; return old_umask;
} }
Result<u64> sys_truncate(Registers*, SyscallArgs args)
{
auto path = TRY(MemoryManager::strdup_from_user(args[0]));
size_t length = (size_t)args[1];
auto* current = Scheduler::current();
auto inode = TRY(VFS::resolve_path(path.chars(), current->auth, current->current_directory));
if (!VFS::can_write(inode, current->auth)) return err(EACCES);
TRY(inode->truncate(length));
return 0;
}
Result<u64> sys_ftruncate(Registers*, SyscallArgs args)
{
int fd = (int)args[0];
size_t length = (size_t)args[1];
auto* current = Scheduler::current();
auto description = TRY(current->resolve_fd(fd))->description;
if (!(description->flags & O_WRONLY)) return err(EBADF);
TRY(description->inode->truncate(length));
return 0;
}

View File

@ -184,6 +184,12 @@ extern "C"
/* Set the process group associated with a terminal file descriptor. */ /* Set the process group associated with a terminal file descriptor. */
int tcsetpgrp(int fd, pid_t pgrp); int tcsetpgrp(int fd, pid_t pgrp);
/* Truncate a file to a specific length. */
int truncate(const char* path, size_t size);
/* Truncate a file to a specific length. */
int ftruncate(int fd, size_t size);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -498,4 +498,16 @@ extern "C"
{ {
return ioctl(fd, TIOCSPGRP, &pgid); return ioctl(fd, TIOCSPGRP, &pgid);
} }
int truncate(const char* path, size_t size)
{
long rc = syscall(SYS_truncate, path, size);
__errno_return(rc, int);
}
int ftruncate(int fd, size_t size)
{
long rc = syscall(SYS_ftruncate, fd, size);
__errno_return(rc, int);
}
} }

View File

@ -7,7 +7,8 @@
_e(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(dup2) _e(pipe) _e(mount) \ _e(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(dup2) _e(pipe) _e(mount) \
_e(umount) _e(pstat) _e(getrusage) _e(symlinkat) _e(readlinkat) _e(umask) _e(linkat) _e(faccessat) \ _e(umount) _e(pstat) _e(getrusage) _e(symlinkat) _e(readlinkat) _e(umask) _e(linkat) _e(faccessat) \
_e(pivot_root) _e(sigreturn) _e(sigaction) _e(kill) _e(sigprocmask) _e(setpgid) _e(isatty) \ _e(pivot_root) _e(sigreturn) _e(sigaction) _e(kill) _e(sigprocmask) _e(setpgid) _e(isatty) \
_e(getpgid) _e(socket) _e(bind) _e(connect) _e(listen) _e(accept) _e(poll) _e(msync) _e(getpgid) _e(socket) _e(bind) _e(connect) _e(listen) _e(accept) _e(poll) _e(msync) \
_e(truncate) _e(ftruncate)
enum Syscalls enum Syscalls
{ {