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


##########
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;
+static struct list_node g_cwd_list = LIST_INITIAL_VALUE(g_cwd_list);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: cwd_root
+ *
+ * Description:
+ *   Return the chroot() root of the calling task group, or NULL.
+ *
+ ****************************************************************************/
+
+static FAR const char *cwd_root(void)
+{
+#ifdef CONFIG_FS_CHROOT
+  FAR struct tcb_s *tcb = nxsched_self();
+
+  if (tcb != NULL && tcb->group != NULL)
+    {
+      return tcb->group->tg_root;
+    }
+#endif
+
+  return NULL;
+}
+
+/****************************************************************************
+ * Name: cwd_userpath
+ *
+ * Description:
+ *   Make a host path relative to the root of the caller, in place: the
+ *   path that the caller uses.  -ENOENT if the path is outside the root.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_FS_CHROOT
+static int cwd_userpath(FAR char *path)
+{
+  FAR const char *root = cwd_root();
+  size_t len;
+
+  if (root == NULL)
+    {
+      return OK;
+    }
+
+  len = strlen(root);
+  if (strncmp(path, root, len) != 0 ||
+      (path[len] != '/' && path[len] != '\0'))
+    {
+      return -ENOENT;
+    }
+
+  if (path[len] == '\0')
+    {
+      path[0] = '/';
+      path[1] = '\0';
+    }
+  else
+    {
+      memmove(path, path + len, strlen(path + len) + 1);
+    }
+
+  return OK;
+}
+#endif
+
+/****************************************************************************
+ * Name: cwd_under
+ *
+ * Description:
+ *   Return true if 'path' is 'dir' or a path below 'dir'.
+ *
+ ****************************************************************************/
+
+static bool cwd_under(FAR const char *path, FAR const char *dir)
+{
+  size_t len = strlen(dir);
+
+  if (len == 1 && dir[0] == '/')
+    {
+      return true;
+    }
+
+  return strncmp(path, dir, len) == 0 &&
+         (path[len] == '\0' || path[len] == '/');
+}
+
+/****************************************************************************
+ * Name: cwd_put
+ *
+ * Description:
+ *   Drop one reference to a current directory object.  The caller holds
+ *   g_cwd_lock.  Return the object if it must be freed after the lock is
+ *   released, else NULL.
+ *
+ ****************************************************************************/
+
+static FAR struct fs_cwd_s *cwd_put(FAR struct fs_cwd_s *cwd)
+{
+  if (cwd != NULL && --cwd->crefs == 0)
+    {
+      list_delete(&cwd->node);
+      return cwd;
+    }
+
+  return NULL;
+}
+
+/****************************************************************************
+ * Name: cwd_free
+ ****************************************************************************/
+
+static void cwd_free(FAR struct fs_cwd_s *cwd)
+{
+  if (cwd != NULL)
+    {
+      fs_heap_free(cwd->path);
+      fs_heap_free(cwd);
+    }
+}
+
+/****************************************************************************
+ * Name: cwd_searchable
+ *
+ * Description:
+ *   Return true if the effective IDs of the caller have search permission
+ *   on the directory.  Root needs any execute bit, as in access() and
+ *   exec().
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_FS_PERMISSION
+static bool cwd_searchable(FAR const struct stat *buf)
+{
+  mode_t xbit = S_IXOTH;
+  uid_t uid = geteuid();
+
+  if (uid == 0)
+    {
+      return (buf->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0;
+    }
+
+  if (buf->st_uid == uid)
+    {
+      xbit = S_IXUSR;
+    }
+  else if (buf->st_gid == getegid())
+    {
+      xbit = S_IXGRP;
+    }
+#if NGROUPS_MAX > 0
+  else
+    {
+      gid_t groups[NGROUPS_MAX];
+      int ngroups = getgroups(NGROUPS_MAX, groups);
+      int i;
+
+      for (i = 0; i < ngroups; i++)
+        {
+          if (groups[i] == buf->st_gid)
+            {
+              xbit = S_IXGRP;
+              break;
+            }
+        }
+    }
+#endif
+
+  return (buf->st_mode & xbit) != 0;
+}
+#endif
+
+/****************************************************************************
+ * Name: cwd_tohost
+ *
+ * Description:
+ *   Put the root of the caller in front of an absolute path in 'buf', in
+ *   place.  'buf' holds PATH_MAX bytes.
+ *
+ ****************************************************************************/
+
+static int cwd_tohost(FAR char *buf)
+{
+  FAR const char *root = cwd_root();
+  size_t rootlen;
+  size_t len;
+
+  if (root == NULL)
+    {
+      return OK;
+    }
+
+  rootlen = strlen(root);
+  len     = strlen(buf);
+  if (len == 1)
+    {
+      /* The root itself: no "/" after it */
+
+      buf[0] = '\0';
+      len    = 0;
+    }
+
+  if (rootlen + len >= PATH_MAX)
+    {
+      return -ENAMETOOLONG;
+    }
+
+  memmove(buf + rootlen, buf, len + 1);
+  memcpy(buf, root, rootlen);
+  return OK;
+}
+
+/****************************************************************************
+ * Name: cwd_set
+ *
+ * Description:
+ *   Make 'path' the current directory of the task group of the caller.
+ *   'path' is an absolute path inside the root of the caller.  A buffer
+ *   'buf' of PATH_MAX bytes is given for the work.
+ *
+ ****************************************************************************/
+
+static int cwd_set(FAR const char *path, FAR char *buf)
+{
+  FAR struct task_group_s *group = nxsched_self()->group;
+  FAR struct fs_cwd_s *cwd;
+  FAR struct fs_cwd_s *old;
+  struct stat sb;
+  int ret;
+
+  /* The canonical path: absolute, no ".", ".." or symbolic links.  The
+   * search permission of each component is checked on the way.
+   */
+
+  if (lib_realpath(path, buf, false) == NULL)
+    {
+      return -get_errno();

Review Comment:
   lib_realpath is an internal api, so it's more flex to change the behaviour 
to follow the convention that the internal api return -errno instead.



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