From 5176fb917a64e0657798cf7367817bd1c33af18c Mon Sep 17 00:00:00 2001 From: fangxinyong Date: Fri, 9 Jun 2023 21:13:21 +0800 Subject: [PATCH] fs/vfs: ftruncate produce EINVAL if fd is not open for writing As posix spec, ftruncate should produce EBADF or EINVAL error, if the file descriptor fd is not open for writing. Change the behavior to be similar to Linux and pass ltp open_posix test: testcases/open_posix_testsuite/conformance/interfaces/shm_open/20-1.c https://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html https://man7.org/linux/man-pages/man2/ftruncate.2.html Signed-off-by: fangxinyong --- fs/vfs/fs_truncate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/vfs/fs_truncate.c b/fs/vfs/fs_truncate.c index 91bbd8e5a0..2f11c17ec9 100644 --- a/fs/vfs/fs_truncate.c +++ b/fs/vfs/fs_truncate.c @@ -57,7 +57,7 @@ int file_truncate(FAR struct file *filep, off_t length) if ((filep->f_oflags & O_WROK) == 0) { fwarn("WARNING: Cannot truncate a file opened read-only\n"); - return -EBADF; + return -EINVAL; } /* Is this inode a registered mountpoint? Does it support the