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 57277f5c8481a069b2966d91d7c2be3f7a941f38
Author: Xiang Xiao <[email protected]>
AuthorDate: Thu Nov 12 23:15:38 2026 +0800

    tmpfs: add timestamp maintenance and chstat
    
    Add mtime/ctime updates on file write, truncate, and O_TRUNC open
    to track content modifications. Update parent directory mtime/ctime
    when creating or removing files and directories, and when renaming
    entries across directories.
    
    Extend tmpfs_chstat and tmpfs_fchstat to support utimensat and
    futimens for explicit timestamp modification via CH_STAT_ATIME and
    CH_STAT_MTIME flags, and update ctime on any metadata change.  They
    no longer return -ENOSYS without CONFIG_FS_PERMISSION.
    
    Signed-off-by: Xiang Xiao <[email protected]>
    Signed-off-by: v-liuchenxu3 <[email protected]>
---
 fs/tmpfs/fs_tmpfs.c | 87 +++++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 71 insertions(+), 16 deletions(-)

diff --git a/fs/tmpfs/fs_tmpfs.c b/fs/tmpfs/fs_tmpfs.c
index abbef461ef4..ab1170eb6a2 100644
--- a/fs/tmpfs/fs_tmpfs.c
+++ b/fs/tmpfs/fs_tmpfs.c
@@ -285,6 +285,21 @@ static void tmpfs_init_times(FAR struct tmpfs_object_s *to)
   to->to_ctime = to->to_atime;
 }
 
+/****************************************************************************
+ * Name: tmpfs_update_rename_times
+ ****************************************************************************/
+
+static void tmpfs_update_rename_times(FAR struct tmpfs_object_s *to,
+                                      FAR struct tmpfs_directory_s *oldtdo,
+                                      FAR struct tmpfs_directory_s *newtdo)
+{
+  tmpfs_update_ctime(to);
+  oldtdo->tdo_mtime = to->to_ctime;
+  oldtdo->tdo_ctime = to->to_ctime;
+  newtdo->tdo_mtime = to->to_ctime;
+  newtdo->tdo_ctime = to->to_ctime;
+}
+
 /****************************************************************************
  * Name: tmpfs_realloc_directory
  ****************************************************************************/
@@ -813,6 +828,8 @@ static int tmpfs_check_parentperm(FAR struct tmpfs_s *fs,
   return tmpfs_check_pathperm(fs, relpath, parentlen, amode);
 }
 
+#endif /* CONFIG_FS_PERMISSION */
+
 /****************************************************************************
  * Name: tmpfs_chstat_object
  ****************************************************************************/
@@ -820,6 +837,7 @@ static int tmpfs_check_parentperm(FAR struct tmpfs_s *fs,
 static int tmpfs_chstat_object(FAR struct tmpfs_object_s *to,
                                FAR const struct stat *buf, int flags)
 {
+#ifdef CONFIG_FS_PERMISSION
   FAR struct tcb_s *rtcb;
   uid_t euid;
 
@@ -855,12 +873,31 @@ static int tmpfs_chstat_object(FAR struct tmpfs_object_s 
*to,
     {
       to->to_gid = buf->st_gid;
     }
+#endif
+
+  if ((flags & CH_STAT_ATIME) != 0)
+    {
+      to->to_atime = buf->st_atim;
+    }
+
+  if ((flags & CH_STAT_MTIME) != 0)
+    {
+      to->to_mtime = buf->st_mtim;
+    }
+
+  /* Any metadata change is a status change.  The VFS has already filled
+   * st_ctim with the current time.
+   */
+
+  if ((flags & (CH_STAT_MODE | CH_STAT_UID | CH_STAT_GID |
+                CH_STAT_ATIME | CH_STAT_MTIME)) != 0)
+    {
+      to->to_ctime = buf->st_ctim;
+    }
 
   return OK;
 }
 
-#endif /* CONFIG_FS_PERMISSION */
-
 /****************************************************************************
  * Name: tmpfs_new_ino
  *
@@ -1014,6 +1051,9 @@ static int tmpfs_create_file(FAR struct tmpfs_s *fs,
       goto errout_with_file;
     }
 
+  tmpfs_update_mtime((FAR struct tmpfs_object_s *)parent);
+  tmpfs_update_ctime((FAR struct tmpfs_object_s *)parent);
+
   /* Release the reference and lock on the parent directory */
 
   parent->tdo_refs--;
@@ -1170,6 +1210,9 @@ static int tmpfs_create_directory(FAR struct tmpfs_s *fs,
       goto errout_with_directory;
     }
 
+  tmpfs_update_mtime((FAR struct tmpfs_object_s *)parent);
+  tmpfs_update_ctime((FAR struct tmpfs_object_s *)parent);
+
   /* Free the copy of the relpath, release our reference to the parent
    * directory, and return success
    */
@@ -1810,6 +1853,9 @@ static int tmpfs_open(FAR struct file *filep, FAR const 
char *relpath,
                 {
                   goto errout_with_filelock;
                 }
+
+              tmpfs_update_mtime((FAR struct tmpfs_object_s *)tfo);
+              tmpfs_update_ctime((FAR struct tmpfs_object_s *)tfo);
             }
         }
     }
@@ -2060,6 +2106,9 @@ static ssize_t tmpfs_write(FAR struct file *filep, FAR 
const char *buffer,
 
   filep->f_pos = endpos;
 
+  tmpfs_update_mtime((FAR struct tmpfs_object_s *)tfo);
+  tmpfs_update_ctime((FAR struct tmpfs_object_s *)tfo);
+
   /* Release the lock on the file */
 
   tmpfs_unlock_file(tfo);
@@ -2370,12 +2419,11 @@ static int tmpfs_fstat(FAR const struct file *filep, 
FAR struct stat *buf)
 static int tmpfs_fchstat(FAR const struct file *filep,
                          FAR const struct stat *buf, int flags)
 {
-  DEBUGASSERT(filep->f_priv != NULL && buf != NULL);
-
-#ifdef CONFIG_FS_PERMISSION
   FAR struct tmpfs_file_s *tfo;
   int ret;
 
+  DEBUGASSERT(filep->f_priv != NULL && buf != NULL);
+
   tfo = filep->f_priv;
 
   ret = tmpfs_lock_file(tfo);
@@ -2387,10 +2435,6 @@ static int tmpfs_fchstat(FAR const struct file *filep,
   ret = tmpfs_chstat_object((FAR struct tmpfs_object_s *)tfo, buf, flags);
   tmpfs_unlock_file(tfo);
   return ret;
-#else
-  UNUSED(flags);
-  return -ENOSYS;
-#endif
 }
 
 /****************************************************************************
@@ -2442,6 +2486,8 @@ static int tmpfs_truncate(FAR struct file *filep, off_t 
length)
           memset(&tfo->tfo_data[oldsize], 0, length - oldsize);
         }
 
+      tmpfs_update_mtime((FAR struct tmpfs_object_s *)tfo);
+      tmpfs_update_ctime((FAR struct tmpfs_object_s *)tfo);
       ret = OK;
     }
 
@@ -2900,6 +2946,9 @@ static int tmpfs_unlink(FAR struct inode *mountpt, FAR 
const char *relpath)
       fs_heap_free(tfo);
     }
 
+  tmpfs_update_mtime((FAR struct tmpfs_object_s *)tdo);
+  tmpfs_update_ctime((FAR struct tmpfs_object_s *)tdo);
+
   /* Release the reference and lock on the parent directory */
 
   tdo->tdo_refs--;
@@ -3049,6 +3098,9 @@ static int tmpfs_rmdir(FAR struct inode *mountpt, FAR 
const char *relpath)
   fs_heap_free(tdo->tdo_entry);
   fs_heap_free(tdo);
 
+  tmpfs_update_mtime((FAR struct tmpfs_object_s *)parent);
+  tmpfs_update_ctime((FAR struct tmpfs_object_s *)parent);
+
   /* Release the reference and lock on the parent directory */
 
   parent->tdo_refs--;
@@ -3194,6 +3246,10 @@ static int tmpfs_rename(FAR struct inode *mountpt,
       else if (ret == OK)
         {
           ret = tmpfs_remove_dirent(oldparent, oldname);
+          if (ret >= 0)
+            {
+              tmpfs_update_rename_times(to, oldparent, newparent);
+            }
         }
 
       goto errout_with_oldparent;
@@ -3215,6 +3271,10 @@ static int tmpfs_rename(FAR struct inode *mountpt,
   /* Add an entry to the new parent directory. */
 
   ret = tmpfs_add_dirent(newparent, to, newname);
+  if (ret >= 0)
+    {
+      tmpfs_update_rename_times(to, oldparent, newparent);
+    }
 
 errout_with_oldparent:
   oldparent->tdo_refs--;
@@ -3357,13 +3417,12 @@ errout_with_fslock:
 static int tmpfs_chstat(FAR struct inode *mountpt, FAR const char *relpath,
                         FAR const struct stat *buf, int flags)
 {
-  DEBUGASSERT(mountpt != NULL && relpath != NULL && buf != NULL);
-
-#ifdef CONFIG_FS_PERMISSION
   FAR struct tmpfs_s *fs;
   FAR struct tmpfs_object_s *to;
   int ret;
 
+  DEBUGASSERT(mountpt != NULL && relpath != NULL && buf != NULL);
+
   fs = mountpt->i_private;
   DEBUGASSERT(fs != NULL);
 
@@ -3385,10 +3444,6 @@ static int tmpfs_chstat(FAR struct inode *mountpt, FAR 
const char *relpath,
 errout_with_fslock:
   tmpfs_unlock(fs);
   return ret;
-#else
-  UNUSED(flags);
-  return -ENOSYS;
-#endif
 }
 
 /****************************************************************************

Reply via email to