casaroli commented on code in PR #21907:
URL: https://github.com/apache/nuttx/pull/21907#discussion_r4660228255


##########
fs/vfs/fs_cwd.c:
##########
@@ -0,0 +1,718 @@
+/****************************************************************************
+ * fs/vfs/fs_cwd.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/stat.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/lib/lib.h>
+#include <nuttx/list.h>
+#include <nuttx/mutex.h>
+#include <nuttx/sched.h>
+
+#include "inode/inode.h"
+#include "fs_heap.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* The current directory of one or more task groups.  The groups that a
+ * group creates share its object until one of them calls chdir().  All
+ * fields are protected by g_cwd_lock.
+ */
+
+struct fs_cwd_s
+{
+  struct list_node node;      /* In g_cwd_list */
+  unsigned int     crefs;     /* Number of task groups that use it */
+  bool             removed;   /* The directory was removed */
+  FAR char        *path;      /* Absolute host path, no symbolic links */
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static mutex_t g_cwd_lock = NXMUTEX_INITIALIZER;

Review Comment:
   The kernel keeps the current directory as a path, and relative names resolve 
against that path. When the parent renames `/tmp/a` to `/tmp/b`, a child in 
`/tmp/a` keeps the same directory. Without the update its `open("x")` looks up 
`/tmp/a/x` and gets `ENOENT`, or, after a new `mkdir("/tmp/a")`, opens a file 
in a directory that is not its current directory.



##########
fs/vfs/fs_cwd.c:
##########
@@ -413,6 +413,197 @@ static int cwd_get(FAR char *buf)
   return ret;
 }
 
+/****************************************************************************
+ * Name: cwd_hostpath
+ *
+ * Description:
+ *   Write the canonical host path of 'path' to 'buf' (PATH_MAX bytes): the
+ *   form of the paths that chdir() keeps.  The last component is not
+ *   followed: only its directory is resolved, so this works after the
+ *   last component was removed or renamed.
+ *
+ ****************************************************************************/
+
+static int cwd_hostpath(FAR const char *path, FAR char *buf)
+{
+  FAR const char *base;
+  FAR char *dir = NULL;
+  size_t baselen = 0;
+  size_t len;
+  int ret;
+
+  len = strlen(path);
+  while (len > 1 && path[len - 1] == '/')
+    {
+      len--;
+    }
+
+  /* The last component, and its directory */
+
+  base = path + len;
+  while (base > path && *(base - 1) != '/')
+    {
+      base--;
+    }
+
+  baselen = path + len - base;
+  if (baselen == 0 || baselen > NAME_MAX ||
+      (base[0] == '.' && (baselen == 1 || (baselen == 2 && base[1] == '.'))))
+    {
+      base = NULL;     /* "/", "." or "..": resolve the whole path */
+    }
+
+  if (base != NULL)
+    {
+      if (base - path > PATH_MAX)
+        {
+          return -ENAMETOOLONG;
+        }
+
+      dir = lib_get_pathbuffer();
+      if (dir == NULL)
+        {
+          return -ENOMEM;
+        }
+
+      if (base == path)
+        {
+          strlcpy(dir, ".", PATH_MAX);
+        }
+      else if (base == path + 1)
+        {
+          strlcpy(dir, "/", PATH_MAX);
+        }
+      else
+        {
+          strlcpy(dir, path, base - path);
+        }
+
+      path = dir;
+    }
+
+  if (lib_realpath(path, buf, false) == NULL)
+    {
+      ret = -get_errno();
+    }
+  else
+    {
+      ret = OK;
+      if (base != NULL)
+        {
+          len = strlen(buf);
+          if (len > 1)
+            {
+              buf[len++] = '/';
+            }
+
+          if (len + baselen >= PATH_MAX)
+            {
+              ret = -ENAMETOOLONG;
+            }
+          else
+            {
+              memcpy(buf + len, base, baselen);
+              buf[len + baselen] = '\0';
+            }
+        }
+
+      if (ret >= 0)
+        {
+          ret = cwd_tohost(buf);
+        }
+    }
+
+  if (dir != NULL)
+    {
+      lib_put_pathbuffer(dir);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: cwd_rename
+ *
+ * Description:
+ *   Each current directory at or below the host path 'oldpath' gets the
+ *   new host path, and a current directory at 'newpath' (an empty
+ *   directory that the rename replaced) is removed.
+ *
+ ****************************************************************************/
+
+static void cwd_rename(FAR const char *oldpath, FAR const char *newpath)
+{
+  FAR struct fs_cwd_s *cwd;
+  size_t oldlen = strlen(oldpath);
+  size_t newlen = strlen(newpath);
+  FAR char *path;
+
+  if (strcmp(oldpath, newpath) == 0)
+    {
+      return;
+    }
+
+  nxmutex_lock(&g_cwd_lock);
+  list_for_every_entry(&g_cwd_list, cwd, struct fs_cwd_s, node)
+    {
+      if (cwd->removed)
+        {
+          continue;
+        }
+
+      /* An empty directory that the rename replaced */
+
+      if (strcmp(cwd->path, newpath) == 0)
+        {
+          cwd->removed = true;
+          continue;
+        }
+
+      if (!cwd_under(cwd->path, oldpath))
+        {
+          continue;
+        }
+
+      path = fs_heap_malloc(newlen + strlen(cwd->path + oldlen) + 1);
+      if (path == NULL)
+        {
+          continue;   /* Keep the old name */
+        }
+
+      memcpy(path, newpath, newlen);
+      strcpy(path + newlen, cwd->path + oldlen);
+      fs_heap_free(cwd->path);
+      cwd->path = path;
+    }
+
+  nxmutex_unlock(&g_cwd_lock);
+}
+
+/****************************************************************************
+ * Name: cwd_remove
+ *
+ * Description:
+ *   A current directory at the host path 'path' is removed.
+ *
+ ****************************************************************************/
+
+static void cwd_remove(FAR const char *path)
+{
+  FAR struct fs_cwd_s *cwd;
+
+  nxmutex_lock(&g_cwd_lock);
+  list_for_every_entry(&g_cwd_list, cwd, struct fs_cwd_s, node)
+    {
+      if (strcmp(cwd->path, path) == 0)
+        {
+          cwd->removed = true;

Review Comment:
   Yes. `rmdir()`: "If one or more processes have the directory open when the 
last link is removed, ... no new entries may be created in the directory". If 
we give the path to the file system, after `rmdir()` of the current directory 
and `mkdir()` of the same name, `open("x", O_CREAT)` creates `x` in the new 
directory, which is not the current directory of the process.



-- 
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