Skip to content

Commit

Permalink
posix: fs: Fixes stat command to return file information
Browse files Browse the repository at this point in the history
Fixes zephyrproject-rtos#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 <v.van.beveren@nikhef.nl>
  • Loading branch information
vvanbeveren committed Jun 15, 2023
1 parent c9f8b8b commit d9f6a48
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions lib/posix/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit d9f6a48

Please sign in to comment.