You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2021/09/12 03:36:08 UTC

[incubator-nuttx] branch master updated: lib_fread: return the number of bytes actually read when error occurs.

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 3439709  lib_fread: return the number of bytes actually read when error occurs.
3439709 is described below

commit 34397094d776cc9eda7bd68bd388c23a91405613
Author: Jiuzhu Dong <do...@xiaomi.com>
AuthorDate: Mon Aug 30 18:24:03 2021 +0800

    lib_fread: return the number of bytes actually read when error occurs.
    
    Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
 libs/libc/stdio/lib_libfread.c | 70 +++++++++++++++++++++++++++++-------------
 1 file changed, 49 insertions(+), 21 deletions(-)

diff --git a/libs/libc/stdio/lib_libfread.c b/libs/libc/stdio/lib_libfread.c
index bd41e6f..e4b218f 100644
--- a/libs/libc/stdio/lib_libfread.c
+++ b/libs/libc/stdio/lib_libfread.c
@@ -57,7 +57,8 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
 
   if (!stream || (stream->fs_oflags & O_RDOK) == 0)
     {
-      set_errno(EBADF);
+      _NX_SETERRNO(EBADF);
+      return ERROR;
     }
   else
     {
@@ -97,8 +98,15 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
           ret = lib_wrflush(stream);
           if (ret < 0)
             {
-              lib_give_semaphore(stream);
-              return ret;
+              if (count - remaining > 0)
+                {
+                  goto shortread;
+                }
+              else
+                {
+                  _NX_SETERRNO(ret);
+                  goto errout_with_errno;
+                }
             }
 
           /* Now get any other needed chars from the buffer or the file. */
@@ -148,10 +156,17 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
                       bytes_read = _NX_READ(stream->fs_fd, dest, remaining);
                       if (bytes_read < 0)
                         {
-                          /* An error occurred on the read. */
-
-                          _NX_SETERRNO(bytes_read);
-                          goto errout_with_errno;
+                          if (count - remaining > 0)
+                            {
+                              goto shortread;
+                            }
+                          else
+                            {
+                              /* An error occurred on the read. */
+
+                              _NX_SETERRNO(bytes_read);
+                              goto errout_with_errno;
+                            }
                         }
                       else if (bytes_read == 0)
                         {
@@ -186,12 +201,19 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
                                             buffer_available);
                       if (bytes_read < 0)
                         {
-                          /* An error occurred on the read.  The error code
-                           * is in the 'errno' variable.
-                           */
-
-                          _NX_SETERRNO(bytes_read);
-                          goto errout_with_errno;
+                          if (count - remaining > 0)
+                            {
+                              goto shortread;
+                            }
+                          else
+                            {
+                              /* An error occurred on the read.  The error
+                               * code is in the 'errno' variable.
+                               */
+
+                              _NX_SETERRNO(bytes_read);
+                              goto errout_with_errno;
+                            }
                         }
                       else if (bytes_read == 0)
                         {
@@ -222,12 +244,19 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
               bytes_read = _NX_READ(stream->fs_fd, dest, remaining);
               if (bytes_read < 0)
                 {
-                  /* An error occurred on the read.  The error code is
-                   * in the 'errno' variable.
-                   */
+                  if (count - remaining > 0)
+                    {
+                      break;
+                    }
+                  else
+                    {
+                      /* An error occurred on the read.  The error code is
+                       * in the 'errno' variable.
+                       */
 
-                  _NX_SETERRNO(bytes_read);
-                  goto errout_with_errno;
+                      _NX_SETERRNO(bytes_read);
+                      goto errout_with_errno;
+                    }
                 }
               else if (bytes_read == 0)
                 {
@@ -270,14 +299,13 @@ shortread:
         }
 
       lib_give_semaphore(stream);
+      return count - remaining;
     }
 
-  return count - remaining;
-
   /* Error exits */
 
 errout_with_errno:
   stream->fs_flags |= __FS_FLAG_ERROR;
   lib_give_semaphore(stream);
-  return -get_errno();
+  return ERROR;
 }