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

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

commit f7173029d0d373acb38e2d65a8bef81736129810
Author: v-liuchenxu3 <[email protected]>
AuthorDate: Thu Nov 12 23:15:24 2026 +0800

    tmpfs: add configurable timestamp support
    
    tmpfs keeps no times, so stat() always reports st_atime, st_mtime
    and st_ctime as zero.  Add the three times to every tmpfs object, set
    them when the object is created and report them in stat() and fstat().
    
    Update st_atime on a successful read() unless O_NOATIME is set, and
    on mmap() because tmpfs cannot trap the first access to the mapping.
    Add an msync() handler that marks st_mtime and st_ctime of a shared
    mapping.
    
    Signed-off-by: v-liuchenxu3 <[email protected]>
---
 fs/tmpfs/fs_tmpfs.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/tmpfs/fs_tmpfs.h | 18 ++++++++++++
 2 files changed, 103 insertions(+)

diff --git a/fs/tmpfs/fs_tmpfs.c b/fs/tmpfs/fs_tmpfs.c
index d368b479945..abbef461ef4 100644
--- a/fs/tmpfs/fs_tmpfs.c
+++ b/fs/tmpfs/fs_tmpfs.c
@@ -247,6 +247,44 @@ const struct mountpt_operations g_tmpfs_operations =
  * Private Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: tmpfs_update_atime
+ ****************************************************************************/
+
+static inline void tmpfs_update_atime(FAR struct tmpfs_object_s *to)
+{
+  clock_gettime(CLOCK_REALTIME, &to->to_atime);
+}
+
+/****************************************************************************
+ * Name: tmpfs_update_mtime
+ ****************************************************************************/
+
+static inline void tmpfs_update_mtime(FAR struct tmpfs_object_s *to)
+{
+  clock_gettime(CLOCK_REALTIME, &to->to_mtime);
+}
+
+/****************************************************************************
+ * Name: tmpfs_update_ctime
+ ****************************************************************************/
+
+static inline void tmpfs_update_ctime(FAR struct tmpfs_object_s *to)
+{
+  clock_gettime(CLOCK_REALTIME, &to->to_ctime);
+}
+
+/****************************************************************************
+ * Name: tmpfs_init_times
+ ****************************************************************************/
+
+static void tmpfs_init_times(FAR struct tmpfs_object_s *to)
+{
+  tmpfs_update_atime(to);
+  to->to_mtime = to->to_atime;
+  to->to_ctime = to->to_atime;
+}
+
 /****************************************************************************
  * Name: tmpfs_realloc_directory
  ****************************************************************************/
@@ -867,6 +905,8 @@ tmpfs_alloc_file(FAR struct tmpfs_directory_s *parent, 
mode_t mode)
   tfo->tfo_size   = 0;
   tfo->tfo_data   = NULL;
 
+  tmpfs_init_times((FAR struct tmpfs_object_s *)tfo);
+
 #ifdef CONFIG_FS_PERMISSION
   tmpfs_init_object((FAR struct tmpfs_object_s *)tfo, mode);
 #else
@@ -1023,6 +1063,8 @@ tmpfs_alloc_directory(FAR struct tmpfs_directory_s 
*parent, mode_t mode)
   tdo->tdo_nentries = 0;
   tdo->tdo_entry    = NULL;
 
+  tmpfs_init_times((FAR struct tmpfs_object_s *)tdo);
+
 #ifdef CONFIG_FS_PERMISSION
   tmpfs_init_object((FAR struct tmpfs_object_s *)tdo, mode);
 #else
@@ -1923,6 +1965,11 @@ static ssize_t tmpfs_read(FAR struct file *filep, FAR 
char *buffer,
     {
       memcpy(buffer, &tfo->tfo_data[startpos], nread);
       filep->f_pos += nread;
+
+      if ((filep->f_oflags & O_NOATIME) == 0)
+        {
+          tmpfs_update_atime((FAR struct tmpfs_object_s *)tfo);
+        }
     }
   else
     {
@@ -2116,6 +2163,34 @@ static int tmpfs_unmap(FAR struct task_group_s *group,
   return ret;
 }
 
+/****************************************************************************
+ * Name: tmpfs_msync
+ ****************************************************************************/
+
+static int tmpfs_msync(FAR struct mm_map_entry_s *entry, FAR void *start,
+                       size_t length, int flags)
+{
+  FAR struct tmpfs_file_s *tfo = entry->priv.p;
+  int ret;
+
+  DEBUGASSERT(tfo != NULL);
+
+  /* The mapping is the file data itself, so there is nothing to write
+   * back.  Only mark the times as POSIX requires for a shared mapping.
+   */
+
+  ret = tmpfs_lock_file(tfo);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  tmpfs_update_mtime((FAR struct tmpfs_object_s *)tfo);
+  tfo->tfo_ctime = tfo->tfo_mtime;
+  tmpfs_unlock_file(tfo);
+  return OK;
+}
+
 static int tmpfs_mmap(FAR struct file *filep, FAR struct mm_map_entry_s *map)
 {
   FAR struct tmpfs_file_s *tfo;
@@ -2135,12 +2210,19 @@ static int tmpfs_mmap(FAR struct file *filep, FAR 
struct mm_map_entry_s *map)
       map->vaddr = tfo->tfo_data + map->offset;
       map->priv.p = tfo;
       map->munmap = tmpfs_unmap;
+      map->msync = tmpfs_msync;
       ret = mm_map_add(get_current_mm(), map);
 
       if (ret >= 0)
         {
           tmpfs_lock_file(tfo);
           tfo->tfo_refs++;
+
+          /* POSIX marks st_atime for update on the first access to the
+           * mapping.  tmpfs cannot trap that access, so mark it here.
+           */
+
+          tmpfs_update_atime((FAR struct tmpfs_object_s *)tfo);
           tmpfs_unlock_file(tfo);
         }
     }
@@ -3208,6 +3290,9 @@ static void tmpfs_stat_common(FAR struct tmpfs_object_s 
*to,
   buf->st_blksize = CONFIG_FS_TMPFS_BLOCKSIZE;
   buf->st_blocks  = (objsize + CONFIG_FS_TMPFS_BLOCKSIZE - 1) /
                     CONFIG_FS_TMPFS_BLOCKSIZE;
+  buf->st_atim    = to->to_atime;
+  buf->st_mtim    = to->to_mtime;
+  buf->st_ctim    = to->to_ctime;
 }
 
 /****************************************************************************
diff --git a/fs/tmpfs/fs_tmpfs.h b/fs/tmpfs/fs_tmpfs.h
index 4b6bc67a78f..7b1192f7d29 100644
--- a/fs/tmpfs/fs_tmpfs.h
+++ b/fs/tmpfs/fs_tmpfs.h
@@ -85,6 +85,12 @@ struct tmpfs_object_s
   uint8_t  to_refs;      /* Reference count */
   FAR struct tmpfs_directory_s *to_parent;
   ino_t    to_ino;       /* File serial number (st_ino, d_ino) */
+
+  /* Times of last access, modification and status change */
+
+  struct timespec to_atime;
+  struct timespec to_mtime;
+  struct timespec to_ctime;
 #ifdef CONFIG_FS_PERMISSION
   mode_t   to_mode;      /* File mode (permission bits) */
   uid_t    to_uid;       /* Owner user ID */
@@ -105,6 +111,12 @@ struct tmpfs_directory_s
   uint8_t  tdo_refs;     /* Reference count */
   FAR struct tmpfs_directory_s *tdo_parent;
   ino_t    tdo_ino;      /* File serial number (st_ino, d_ino) */
+
+  /* Times of last access, modification and status change */
+
+  struct timespec tdo_atime;
+  struct timespec tdo_mtime;
+  struct timespec tdo_ctime;
 #ifdef CONFIG_FS_PERMISSION
   mode_t   tdo_mode;     /* Directory mode (permission bits) */
   uid_t    tdo_uid;      /* Owner user ID */
@@ -138,6 +150,12 @@ struct tmpfs_file_s
   uint8_t  tfo_refs;     /* Reference count */
   FAR struct tmpfs_directory_s *tfo_parent;
   ino_t    tfo_ino;      /* File serial number (st_ino, d_ino) */
+
+  /* Times of last access, modification and status change */
+
+  struct timespec tfo_atime;
+  struct timespec tfo_mtime;
+  struct timespec tfo_ctime;
 #ifdef CONFIG_FS_PERMISSION
   mode_t   tfo_mode;     /* File mode (permission bits) */
   uid_t    tfo_uid;      /* Owner user ID */

Reply via email to