xiaoxiang781216 commented on code in PR #21907:
URL: https://github.com/apache/nuttx/pull/21907#discussion_r4659558112
##########
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:
Does POSIX describe this behaviour? If not, it's better to keep the simple
implementation
--
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]