diff --git a/fs/vfs/fs_fstat.c b/fs/vfs/fs_fstat.c index c0d17dd22a..08d7188792 100644 --- a/fs/vfs/fs_fstat.c +++ b/fs/vfs/fs_fstat.c @@ -222,6 +222,26 @@ int file_fstat(FAR struct file *filep, FAR struct stat *buf) return ret; } +int nx_fstat(int fd, FAR struct stat *buf) +{ + FAR struct file *filep; + int ret; + + /* First, get the file structure. Note that on failure, + * fs_getfilep() will return the errno. + */ + + ret = fs_getfilep(fd, &filep); + if (ret >= 0) + { + /* Perform the fstat operation */ + + return file_fstat(filep, buf); + } + + return ret; +} + /**************************************************************************** * Name: fstat * @@ -246,33 +266,14 @@ int file_fstat(FAR struct file *filep, FAR struct stat *buf) int fstat(int fd, FAR struct stat *buf) { - FAR struct file *filep; int ret; - /* First, get the file structure. Note that on failure, - * fs_getfilep() will return the errno. - */ - - ret = fs_getfilep(fd, &filep); + ret = nx_fstat(fd, buf); if (ret < 0) { - goto errout; + set_errno(-ret); + ret = ERROR; } - /* Perform the fstat operation */ - - ret = file_fstat(filep, buf); - - /* Check if the fstat operation was successful */ - - if (ret >= 0) - { - /* Successfully fstat'ed the file */ - - return OK; - } - -errout: - set_errno(-ret); - return ERROR; + return ret; } diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h index 17410a5ba6..a9012eb1a2 100644 --- a/include/nuttx/fs/fs.h +++ b/include/nuttx/fs/fs.h @@ -74,7 +74,7 @@ # define _NX_READ(f,b,s) nx_read(f,b,s) # define _NX_WRITE(f,b,s) nx_write(f,b,s) # define _NX_SEEK(f,o,w) nx_seek(f,o,w) -# define _NX_STAT(p,s) nx_stat(p,s,1) +# define _NX_STAT(f,s) nx_fstat(f,s) # define _NX_GETERRNO(r) (-(r)) # define _NX_SETERRNO(r) set_errno(-(r)) # define _NX_GETERRVAL(r) (r) @@ -84,7 +84,7 @@ # define _NX_READ(f,b,s) read(f,b,s) # define _NX_WRITE(f,b,s) write(f,b,s) # define _NX_SEEK(f,o,w) lseek(f,o,w) -# define _NX_STAT(p,s) stat(p,s) +# define _NX_STAT(f,s) fstat(f,s) # define _NX_GETERRNO(r) errno # define _NX_SETERRNO(r) ((void)(r)) # define _NX_GETERRVAL(r) (-errno) @@ -1458,6 +1458,23 @@ int file_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup); int file_fstat(FAR struct file *filep, FAR struct stat *buf); +/**************************************************************************** + * Name: nx_fstat + * + * Description: + * nx_fstat() is similar to the standard 'fstat' interface except that is + * not a cancellation point and it does not modify the errno variable. + * + * nx_fstat() is an internal NuttX interface and should not be called from + * applications. + * + * Returned Value: + * Zero is returned on success; a negated value is returned on any failure. + * + ****************************************************************************/ + +int nx_fstat(int fd, FAR struct stat *buf); + /**************************************************************************** * Name: nx_stat * diff --git a/libs/libc/modlib/modlib_init.c b/libs/libc/modlib/modlib_init.c index d26efca8c1..e8cd643bea 100644 --- a/libs/libc/modlib/modlib_init.c +++ b/libs/libc/modlib/modlib_init.c @@ -71,15 +71,14 @@ * ****************************************************************************/ -static inline int modlib_filelen(FAR struct mod_loadinfo_s *loadinfo, - FAR const char *filename) +static inline int modlib_filelen(FAR struct mod_loadinfo_s *loadinfo) { struct stat buf; int ret; /* Get the file stats */ - ret = _NX_STAT(filename, &buf); + ret = _NX_STAT(loadinfo->filfd, &buf); if (ret < 0) { int errval = _NX_GETERRNO(ret); @@ -95,10 +94,6 @@ static inline int modlib_filelen(FAR struct mod_loadinfo_s *loadinfo, return -ENOENT; } - /* TODO: Verify that the file is readable. Not really important because - * we will detect this when we try to open the file read-only. - */ - /* Return the size of the file in the loadinfo structure */ loadinfo->filelen = buf.st_size; @@ -134,15 +129,6 @@ int modlib_initialize(FAR const char *filename, memset(loadinfo, 0, sizeof(struct mod_loadinfo_s)); loadinfo->filfd = -1; - /* Get the length of the file. */ - - ret = modlib_filelen(loadinfo, filename); - if (ret < 0) - { - berr("ERROR: modlib_filelen failed: %d\n", ret); - return ret; - } - /* Open the binary file for reading (only) */ loadinfo->filfd = _NX_OPEN(filename, O_RDONLY); @@ -153,6 +139,15 @@ int modlib_initialize(FAR const char *filename, return -errval; } + /* Get the length of the file. */ + + ret = modlib_filelen(loadinfo); + if (ret < 0) + { + berr("ERROR: modlib_filelen failed: %d\n", ret); + return ret; + } + /* Read the ELF ehdr from offset 0 */ ret = modlib_read(loadinfo, (FAR uint8_t *)&loadinfo->ehdr,