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

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


The following commit(s) were added to refs/heads/master by this push:
     new 4fbd4818532 fs/vfs: Give IN_CREATE only for a file that the open made.
4fbd4818532 is described below

commit 4fbd48185326ace0ea33a875099e944dc4297a77
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Nov 27 16:07:49 2026 +0100

    fs/vfs: Give IN_CREATE only for a file that the open made.
    
    An open with O_CREAT of a file that was there gave inotify the events
    IN_CREATE and IN_OPEN.  Linux gives only IN_OPEN then: O_CREAT does not
    tell if the open made the file.
    
    For a file in a mounted file system, file_vopen() now asks the stat method
    of the file system before an open with O_CREAT and without O_EXCL.  When
    the file is there, notify_open() gets no O_CREAT.  A file that the lookup
    of the pseudo file system finds, and a block driver that the open gives a
    proxy for, give no O_CREAT either.  With O_EXCL the file is always new.
    Two opens that race for a new file can both give IN_CREATE.
    
    mq_open(), sem_open(), shm_open() and the event open do not change.  All
    the changes are under CONFIG_FS_NOTIFY.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 fs/vfs/fs_inotify.c |  4 +++-
 fs/vfs/fs_open.c    | 28 ++++++++++++++++++++++++++--
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/fs/vfs/fs_inotify.c b/fs/vfs/fs_inotify.c
index 7d06ed0bfe7..bc4f4a655fe 100644
--- a/fs/vfs/fs_inotify.c
+++ b/fs/vfs/fs_inotify.c
@@ -1713,7 +1713,9 @@ void notify_initialize(void)
  * Name: notify_open
  *
  * Description:
- *   The hook is called when the file is opened.
+ *   The hook is called when the file is opened.  O_CREAT in oflags means
+ *   that the open made the file: the caller clears it for a file that was
+ *   there.
  *
  ****************************************************************************/
 
diff --git a/fs/vfs/fs_open.c b/fs/vfs/fs_open.c
index 6f591ccd019..ee542187bb2 100644
--- a/fs/vfs/fs_open.c
+++ b/fs/vfs/fs_open.c
@@ -119,6 +119,9 @@ static int file_vopen(FAR struct file *filep, FAR const 
char *path,
   FAR struct inode *inode;
 #if !defined(CONFIG_DISABLE_MOUNTPOINT) || defined(CONFIG_PSEUDOFS_FILE)
   mode_t mode = 0666;
+#endif
+#ifdef CONFIG_FS_NOTIFY
+  bool created = false;
 #endif
   int ret;
 
@@ -154,6 +157,9 @@ static int file_vopen(FAR struct file *filep, FAR const 
char *path,
       if ((oflags & O_CREAT) != 0)
         {
           ret = pseudofile_create(&inode, path, mode);
+#ifdef CONFIG_FS_NOTIFY
+          created = ret >= 0;
+#endif
         }
 #endif
 
@@ -225,7 +231,7 @@ static int file_vopen(FAR struct file *filep, FAR const 
char *path,
 #ifdef CONFIG_FS_NOTIFY
       if (ret >= 0)
         {
-          notify_open(path, filep->f_oflags);
+          notify_open(path, filep->f_oflags & ~O_CREAT);
         }
 #endif
 
@@ -310,6 +316,21 @@ static int file_vopen(FAR struct file *filep, FAR const 
char *path,
 #ifndef CONFIG_DISABLE_MOUNTPOINT
   else if (INODE_IS_MOUNTPT(inode))
     {
+#ifdef CONFIG_FS_NOTIFY
+      /* The file system makes the file.  O_CREAT does not tell if the file
+       * is new: ask the stat method before the open.
+       */
+
+      if ((oflags & O_CREAT) != 0)
+        {
+          struct stat buf;
+
+          created = (oflags & O_EXCL) != 0 ||
+                    inode->u.i_mops->stat == NULL ||
+                    inode->u.i_mops->stat(inode, desc.relpath, &buf) < 0;
+        }
+#endif
+
       if (inode->u.i_mops->open != NULL)
         {
           ret = inode->u.i_mops->open(filep, desc.relpath, oflags, mode);
@@ -349,7 +370,10 @@ static int file_vopen(FAR struct file *filep, FAR const 
char *path,
 
   inode_search_release(&desc);
 #ifdef CONFIG_FS_NOTIFY
-  notify_open(path, filep->f_oflags);
+  /* IN_CREATE only for a file that this open made */
+
+  notify_open(path, created ? filep->f_oflags :
+                              filep->f_oflags & ~O_CREAT);
 #endif
 
   filep->f_oflags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);

Reply via email to