1
0
Fork 0
forked from nuttx/nuttx-update

libc/modlib: Replace nx_stat with file_stat

to avoid searching the path twice

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2023-07-04 19:58:04 +08:00 committed by Alan Carvalho de Assis
parent 1b2f37259a
commit 935f43185f
3 changed files with 54 additions and 41 deletions

View file

@ -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;
}

View file

@ -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
*

View file

@ -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,