From d9f6a48d35967ddc8e7bfc5f8fffed17f545f4b9 Mon Sep 17 00:00:00 2001 From: Vincent van Beveren Date: Thu, 15 Jun 2023 15:38:36 +0200 Subject: [PATCH] posix: fs: Fixes stat command to return file information Fixes #58911. Previously the stat command returned information on the filesystem, but not the file itself. Because block size is still set this function is backwards compatible with the previous faulty behavior. Signed-off-by: Vincent van Beveren --- lib/posix/fs.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/posix/fs.c b/lib/posix/fs.c index bc48ee7fbc6d223..a190cff7e53fc7c 100644 --- a/lib/posix/fs.c +++ b/lib/posix/fs.c @@ -349,22 +349,45 @@ int unlink(const char *path) int stat(const char *path, struct stat *buf) { int rc; - struct fs_statvfs stat; + struct fs_statvfs stat_vfs; + struct fs_dirent stat_fil if (buf == NULL) { errno = EBADF; return -1; } - rc = fs_statvfs(path, &stat); + rc = fs_statvfs(path, &stat_vfs); if (rc < 0) { errno = -rc; return -1; } - buf->st_size = stat.f_bsize * stat.f_blocks; - buf->st_blksize = stat.f_bsize; - buf->st_blocks = stat.f_blocks; + rc = fs_stat(path, &stat_file); + if (rc < 0) { + errno = -rc; + return -1; + } + + memset(buf, 0, sizeof(buf)); + + switch (stat_file.type) { + case FS_DIR_ENTRY_FILE: + buf->st_mode = S_IFREG; + break; + case FS_DIR_ENTRY_DIR: + buf->st_mode = S_IFDIR; + break; + default: + /* what about this? */ + break; + } + buf->st_size = stat_file.size; + buf->st_blksize = stat_vfs.f_bsize; + /* big if */ + buf->st_blocks = (stat_file.size + stat_vfs.f_bsize - 1) / stat_vfs.f_bsize; + + /* stat has many more fields, like time, gui, uid how to retrieve??? */ return 0; }