acassis commented on code in PR #3406:
URL: https://github.com/apache/nuttx-apps/pull/3406#discussion_r2842846118


##########
nshlib/nsh_fscmds.c:
##########
@@ -44,9 +44,12 @@
 #include <libgen.h>
 #include <errno.h>
 #include <debug.h>
-
 #include "nsh.h"
 
+#ifdef CONFIG_ARCH_SIM
+#  include <time.h>
+#endif

Review Comment:
   @Biancaa-R this could should be generic, so seems like you was guided 
incorrectly to fix it only for arch SIM. So, please remove this #ifdef check 



##########
nshlib/nsh_fscmds.c:
##########
@@ -364,7 +367,126 @@ static int ls_handler(FAR struct nsh_vtbl_s *vtbl, FAR 
const char *dirpath,
 
       memset(&buf, 0, sizeof(struct stat));
 
-      /* stat the file */
+      /* If entryp is provided, listing a directory and need to
+       * construct the full path to stat the file. Otherwise, dirpath
+       * is the target itself. (no separate file name as entryp)
+       */
+
+      if (entryp != NULL)
+        {
+          FAR char *fullpath = nsh_getdirpath(vtbl, dirpath, entryp->d_name);
+          ret = stat(fullpath, &buf);
+          free(fullpath);
+        }
+      else
+        {
+          ret = stat(dirpath, &buf);
+        }
+
+#ifdef CONFIG_CLOCK_TIMEKEEPING
+      /* manual epoch time to date calculation to reduce extra memory
+       * by using includes For boards with minimal flash.
+       */
+
+#  ifdef CONFIG_ARCH_SIM
+      struct timespec ts;
+
+      /* This pulls the ACTUAL current time from your Linux Host */
+
+      /* Sometime defaults to 2008 */
+
+      if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
+        {
+          clock_settime(CLOCK_REALTIME, &ts);
+        }
+#  endif
+
+      /* for referencing /data : if not mounted the reference will fail. */
+
+#  if defined(CONFIG_ARCH_SIM) && defined(CONFIG_FS_HOSTFS)
+      static bool g_time_synced = false;
+
+      if (!g_time_synced)
+        {
+          struct stat hstat;
+
+          /* Data section always maintains the current time value. */
+
+          if (stat("/data", &hstat) == 0)
+            {
+              struct timespec ts_sync;
+
+              ts_sync.tv_sec  = hstat.st_mtime;
+              ts_sync.tv_nsec = 0;
+
+              /* This is the magic line that sets the system clock */
+
+              if (clock_settime(CLOCK_REALTIME, &ts_sync) == 0)
+                {
+                  g_time_synced = true;
+                }
+            }
+        }
+#  endif

Review Comment:
   I tested this hack on sim:nsh (that has CONFIG_FS_HOSTFS enabled by default) 
and it worked.
   
   In fact my test was very simple, I modified the hello example to:
   
   ```
   #include <time.h>
   #include <sys/stat.h>
   
   ....
   
   int main(int argc, FAR char *argv[])
   {
     struct stat hstat;
        
     /* Data section always maintains the current time value. */
        
     if (stat("/data", &hstat) == 0)
       {
          struct timespec ts_sync;
        
          ts_sync.tv_sec  = hstat.st_mtime;
          ts_sync.tv_nsec = 0;
   
          /* This is the magic line that sets the system clock */
   
         if (clock_settime(CLOCK_REALTIME, &ts_sync) == 0)
           {
                  return 0;
           }
       }
   
        return -1;
   }
   ```
   
   Then I ran ./nuttx and tested this way:
   
   ```
   NuttShell (NSH) NuttX-12.12.0
   nsh> date
   Sun, Jun 01 00:00:01 2008
   nsh> hello
   nsh> echo $?
   0
   nsh> date
   Mon, Feb 23 20:15:11 2026
   nsh>
   ```
   
   So, this hack should be in the #else of CONFIG_CLOCK_TIMEKEEPING



##########
nshlib/nsh_fscmds.c:
##########
@@ -364,7 +367,126 @@ static int ls_handler(FAR struct nsh_vtbl_s *vtbl, FAR 
const char *dirpath,
 
       memset(&buf, 0, sizeof(struct stat));
 
-      /* stat the file */
+      /* If entryp is provided, listing a directory and need to
+       * construct the full path to stat the file. Otherwise, dirpath
+       * is the target itself. (no separate file name as entryp)
+       */
+
+      if (entryp != NULL)
+        {
+          FAR char *fullpath = nsh_getdirpath(vtbl, dirpath, entryp->d_name);
+          ret = stat(fullpath, &buf);
+          free(fullpath);
+        }
+      else
+        {
+          ret = stat(dirpath, &buf);
+        }
+
+#ifdef CONFIG_CLOCK_TIMEKEEPING
+      /* manual epoch time to date calculation to reduce extra memory
+       * by using includes For boards with minimal flash.
+       */
+
+#  ifdef CONFIG_ARCH_SIM
+      struct timespec ts;
+
+      /* This pulls the ACTUAL current time from your Linux Host */
+
+      /* Sometime defaults to 2008 */
+
+      if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
+        {
+          clock_settime(CLOCK_REALTIME, &ts);
+        }
+#  endif

Review Comment:
   I think this part is the same for SIM and for native boards. Could you 
please confirm it in some ESP32 or STM32 board? If that is true we don't need 
this #ifdef CONFIG_ARCH_SIM



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to