lib: posix: fs: fix errno handling

ftruncate should return -1 on errors and it needs to
set appropriate error values in errno

Signed-off-by: Karthikeyan Krishnasamy <karthikeyan@linumiz.com>
This commit is contained in:
Karthikeyan Krishnasamy 2024-04-16 10:47:53 +05:30 committed by Carles Cufí
commit cc06c2b21f

View file

@ -423,11 +423,18 @@ int mkdir(const char *path, mode_t mode)
*/
int ftruncate(int fd, off_t length)
{
int rc;
struct posix_fs_desc *ptr = NULL;
ptr = z_get_fd_obj(fd, NULL, EBADF);
if (!ptr)
return -1;
return fs_truncate(&ptr->file, length);
rc = fs_truncate(&ptr->file, length);
if (rc < 0) {
errno = -rc;
return -1;
}
return 0;
}