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/nuttx-apps.git

commit e3400b25552f01d2b709ada70ec992b817e91acc
Author: wangjianyu3 <[email protected]>
AuthorDate: Wed Jan 24 21:09:50 2024 +0800

    Reduce the number of open() and close() to improve performance
    
    Signed-off-by: wangjianyu3 <[email protected]>
---
 system/fastboot/fastboot.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/system/fastboot/fastboot.c b/system/fastboot/fastboot.c
index 1d29a9def..80e8aa8a7 100644
--- a/system/fastboot/fastboot.c
+++ b/system/fastboot/fastboot.c
@@ -107,6 +107,7 @@ struct fastboot_ctx_s
 {
   int usbdev_in;
   int usbdev_out;
+  int flash_fd;
   size_t download_max;
   size_t download_size;
   size_t download_offset;
@@ -398,18 +399,20 @@ static void fastboot_flash(FAR struct fastboot_ctx_s 
*context,
                            FAR const char *arg)
 {
   char blkdev[PATH_MAX];
-  int fd;
 
   snprintf(blkdev, PATH_MAX, FASTBOOT_BLKDEV, arg);
 
-  fd = fastboot_flash_open(blkdev);
-  if (fd < 0)
+  if (context->flash_fd < 0)
     {
-      fastboot_fail(context, "Flash open failure");
-      return;
+      context->flash_fd = fastboot_flash_open(blkdev);
+      if (context->flash_fd < 0)
+        {
+          fastboot_fail(context, "Flash open failure");
+          return;
+        }
     }
 
-  if (fastboot_flash_program(context, fd) < 0)
+  if (fastboot_flash_program(context, context->flash_fd) < 0)
     {
       fastboot_fail(context, "Image flash failure");
     }
@@ -418,7 +421,11 @@ static void fastboot_flash(FAR struct fastboot_ctx_s 
*context,
       fastboot_okay(context, "");
     }
 
-  fastboot_flash_close(fd);
+  if (context->total_imgsize == 0)
+    {
+      fastboot_flash_close(context->flash_fd);
+      context->flash_fd = -1;
+    }
 }
 
 static void fastboot_erase(FAR struct fastboot_ctx_s *context,
@@ -715,6 +722,7 @@ int main(int argc, FAR char **argv)
       goto err_with_in;
     }
 
+  context.flash_fd = -1;
   context.download_buffer = buffer;
   context.download_size   = 0;
   context.download_offset = 0;

Reply via email to