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
The following commit(s) were added to refs/heads/master by this push:
new 6a66bb1fea6 fs/tmpfs: add link,symlink,readlink,lstat for tmpfs
6a66bb1fea6 is described below
commit 6a66bb1fea6153170cff51bb31f08bcd90769a24
Author: zhengyu16 <[email protected]>
AuthorDate: Sun Nov 29 09:55:18 2026 +0800
fs/tmpfs: add link,symlink,readlink,lstat for tmpfs
Share TMPFS objects between hard links and resolve symbolic links within
one mounted volume. Account for aliases when reporting link counts,
removing names, replacing rename targets and releasing mapped files.
Signed-off-by: zhengyu16 <[email protected]>
Signed-off-by: jingfei <[email protected]>
---
Documentation/os/filesystem/tmpfs.rst | 42 ++
fs/tmpfs/fs_tmpfs.c | 1103 ++++++++++++++++++++++-----------
fs/tmpfs/fs_tmpfs.h | 11 +-
3 files changed, 789 insertions(+), 367 deletions(-)
diff --git a/Documentation/os/filesystem/tmpfs.rst
b/Documentation/os/filesystem/tmpfs.rst
index aff05e6ed5f..b7f670f0987 100644
--- a/Documentation/os/filesystem/tmpfs.rst
+++ b/Documentation/os/filesystem/tmpfs.rst
@@ -40,3 +40,45 @@ unmounted.
object.
- ``utimensat()`` and ``futimens()`` set ``st_atime`` and ``st_mtime``. Any
change of the times, mode or owner updates ``st_ctime``.
+
+Links
+=====
+
+With ``CONFIG_FS_LINKS`` enabled, TMPFS supports ``link()``, ``symlink()``,
+``readlink()`` and ``lstat()``.
+
+Hard links share the same file object, contents and ``st_ino``. The
+``st_nlink`` field reports the number of directory entries referring to that
+object. A regular file or symbolic link may have up to ``LINK_MAX`` names
+within one mounted volume. Directories cannot be hard linked. Removing a
+name does not release the data while another name, open file or mapping still
+refers to it. ``statfs()`` counts a shared file object once, while each name
+still consumes memory. ``FIOC_FILEPATH`` can return any surviving alias;
+it returns ``ENOENT`` after the last name is removed.
+
+A symbolic link stores its target text without changing it. Relative targets
+are interpreted from the directory containing the name used to access the
+link, including ``.`` and ``..`` within the volume. Absolute targets are
+resolved through the VFS and must refer to the same TMPFS mount. Following a
+target on another mount, or a relative ``..`` outside the volume, is not
+supported and returns ``EXDEV``. Target text is limited to ``SYMLINK_MAX``
+bytes, and a lookup follows at most ``SYMLOOP_MAX`` symbolic links before
+returning ``ELOOP``.
+
+Absolute targets use the existing VFS path normalization, including its
+handling of ``..`` before TMPFS resolves symbolic links. This does not
+provide full POSIX resolution for absolute paths combining links and ``..``.
+
+``stat()`` and normal opens follow symbolic links. ``lstat()`` describes the
+link itself, and ``readlink()`` copies its stored target without adding a NUL
+terminator. ``O_NOFOLLOW`` rejects a final symbolic link with ``ELOOP``;
+``O_CREAT | O_EXCL`` treats an existing link, including a dangling one, as
+``EEXIST``. An ordinary ``O_CREAT`` can create a missing target when its
+parent directory exists and permits creation. Unlinking or renaming a link
+acts on the link itself. ``lchmod()``, ``lchown()`` and ``lutimens()``
likewise
+change the link's metadata.
+
+With permissions enabled, resolution checks search access on every traversed
+directory, including those in the target path. Creation checks write and
+search access on the resolved parent directory. Reading the stored target
+or examining the link's metadata does not require access to the target.
diff --git a/fs/tmpfs/fs_tmpfs.c b/fs/tmpfs/fs_tmpfs.c
index 63cb51b0fe5..42b75d04df6 100644
--- a/fs/tmpfs/fs_tmpfs.c
+++ b/fs/tmpfs/fs_tmpfs.c
@@ -31,6 +31,7 @@
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
+#include <limits.h>
#include <errno.h>
#include <assert.h>
#include <nuttx/debug.h>
@@ -87,6 +88,13 @@
* Private Types
****************************************************************************/
+struct tmpfs_lookup_s
+{
+ FAR struct tmpfs_object_s *object;
+ FAR struct tmpfs_directory_s *parent;
+ char name[NAME_MAX + 1];
+};
+
struct tmpfs_dir_s
{
struct fs_dirent_s tf_base; /* Vfs directory structure */
@@ -100,11 +108,17 @@ struct tmpfs_dir_s
/* TMPFS helpers */
+static int tmpfs_lookup(FAR struct tmpfs_s *fs,
+ FAR struct tmpfs_directory_s *start,
+ FAR const char *path, size_t len, bool nofollow,
+ FAR unsigned int *links, FAR struct tmpfs_lookup_s *result);
+
static int tmpfs_realloc_directory(FAR struct tmpfs_directory_s *tdo,
unsigned int nentries);
static int tmpfs_realloc_file(FAR struct tmpfs_file_s *tfo,
size_t newsize);
-static void tmpfs_orphan_file(FAR struct tmpfs_file_s *tfo);
+static void tmpfs_drop_link(FAR struct tmpfs_s *fs,
+ FAR struct tmpfs_file_s *tfo);
static void tmpfs_release_lockedobject(FAR struct tmpfs_object_s *to);
static void tmpfs_release_lockedfile(FAR struct tmpfs_file_s *tfo);
static int tmpfs_release_file(FAR struct tmpfs_file_s *tfo);
@@ -114,13 +128,14 @@ static int tmpfs_remove_dirent(FAR struct
tmpfs_directory_s *tdo,
FAR const char *name);
static int tmpfs_add_dirent(FAR struct tmpfs_directory_s *tdo,
FAR struct tmpfs_object_s *to, FAR const char *name);
-static int tmpfs_replace_dirent(FAR struct tmpfs_directory_s *tdo,
+static int tmpfs_replace_dirent(FAR struct tmpfs_s *fs,
+ FAR struct tmpfs_directory_s *tdo,
int index, FAR struct tmpfs_object_s *to);
static FAR struct tmpfs_file_s *
tmpfs_alloc_file(FAR struct tmpfs_directory_s *parent, mode_t mode);
static int tmpfs_create_file(FAR struct tmpfs_s *fs,
FAR const char *relpath, mode_t mode,
- FAR struct tmpfs_file_s **tfo);
+ FAR struct tmpfs_file_s **tfo, bool nofollow);
static FAR struct tmpfs_directory_s *
tmpfs_alloc_directory(FAR struct tmpfs_directory_s *parent, mode_t mode);
static int tmpfs_create_directory(FAR struct tmpfs_s *fs,
@@ -129,15 +144,15 @@ static int tmpfs_create_directory(FAR struct tmpfs_s *fs,
static int tmpfs_find_object(FAR struct tmpfs_s *fs,
FAR const char *relpath, size_t len,
FAR struct tmpfs_object_s **object,
- FAR struct tmpfs_directory_s **parent);
+ FAR struct tmpfs_directory_s **parent, bool nofollow);
static int tmpfs_find_file(FAR struct tmpfs_s *fs,
FAR const char *relpath,
FAR struct tmpfs_file_s **tfo,
- FAR struct tmpfs_directory_s **parent);
+ FAR struct tmpfs_directory_s **parent, bool nofollow);
static int tmpfs_find_directory(FAR struct tmpfs_s *fs,
FAR const char *relpath, size_t len,
FAR struct tmpfs_directory_s **tdo,
- FAR struct tmpfs_directory_s **parent);
+ FAR struct tmpfs_directory_s **parent, bool nofollow);
static int tmpfs_statfs_callout(FAR struct tmpfs_directory_s *tdo,
unsigned int index, FAR void *arg);
static int tmpfs_free_callout(FAR struct tmpfs_directory_s *tdo,
@@ -190,6 +205,16 @@ static int tmpfs_stat(FAR struct inode *mountpt, FAR
const char *relpath,
FAR struct stat *buf);
static int tmpfs_chstat(FAR struct inode *mountpt, FAR const char *relpath,
FAR const struct stat *buf, int flags);
+#ifdef CONFIG_FS_LINKS
+static int tmpfs_link(FAR struct inode *mountpt, FAR const char *oldrelpath,
+ FAR const char *newrelpath);
+static int tmpfs_symlink(FAR struct inode *mountpt, FAR const char *target,
+ FAR const char *relpath);
+static ssize_t tmpfs_readlink(FAR struct inode *mountpt,
+ FAR const char *relpath, FAR char *buf, size_t buflen);
+static int tmpfs_lstat(FAR struct inode *mountpt, FAR const char *relpath,
+ FAR struct stat *buf);
+#endif
#ifdef CONFIG_FS_PERMISSION
static int tmpfs_permission(FAR struct inode *mountpt,
FAR const char *relpath, int amode);
@@ -244,9 +269,15 @@ const struct mountpt_operations g_tmpfs_operations =
tmpfs_syncfs, /* syncfs */
NULL, /* ioctldir */
#ifdef CONFIG_FS_PERMISSION
- tmpfs_permission /* permission */
+ tmpfs_permission, /* permission */
#else
- NULL /* permission */
+ NULL, /* permission */
+#endif
+#ifdef CONFIG_FS_LINKS
+ tmpfs_link, /* link */
+ tmpfs_symlink, /* symlink */
+ tmpfs_readlink, /* readlink */
+ tmpfs_lstat, /* lstat */
#endif
};
@@ -254,6 +285,47 @@ const struct mountpt_operations g_tmpfs_operations =
* Private Functions
****************************************************************************/
+/****************************************************************************
+ * Name: tmpfs_lock_mount
+ ****************************************************************************/
+
+static int tmpfs_lock_mount(FAR struct inode *mountpt,
+ FAR struct tmpfs_s **fs)
+{
+ int ret;
+
+ /* Unbind holds the inode tree write lock while freeing private data.
+ * Keep this order when resolving absolute symbolic link targets too.
+ */
+
+ inode_rlock();
+ if (!INODE_IS_MOUNTPT(mountpt) ||
+ mountpt->u.i_mops != &g_tmpfs_operations || mountpt->i_private == NULL)
+ {
+ inode_runlock();
+ return -ENOENT;
+ }
+
+ *fs = mountpt->i_private;
+ ret = tmpfs_lock((*fs));
+ if (ret < 0)
+ {
+ inode_runlock();
+ }
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: tmpfs_unlock_mount
+ ****************************************************************************/
+
+static void tmpfs_unlock_mount(FAR struct tmpfs_s *fs)
+{
+ tmpfs_unlock(fs);
+ inode_runlock();
+}
+
/****************************************************************************
* Name: tmpfs_update_atime
****************************************************************************/
@@ -451,20 +523,61 @@ static int tmpfs_realloc_file(FAR struct tmpfs_file_s
*tfo,
}
/****************************************************************************
- * Name: tmpfs_orphan_file
- *
- * Description:
- * The name of an open file goes: the file stays until its last close,
- * but it is in no directory. Its directory can go before it, so the file
- * must not keep a pointer to that directory. The caller holds the lock
- * of the file system.
+ * Name: tmpfs_find_alias
*
****************************************************************************/
-static void tmpfs_orphan_file(FAR struct tmpfs_file_s *tfo)
+static FAR struct tmpfs_directory_s *
+tmpfs_find_alias(FAR struct tmpfs_directory_s *tdo,
+ FAR struct tmpfs_file_s *tfo)
{
- tfo->tfo_flags |= TFO_FLAG_UNLINKED;
- tfo->tfo_parent = NULL;
+ FAR struct tmpfs_directory_s *parent;
+ FAR struct tmpfs_object_s *to;
+ unsigned int i;
+
+ for (i = 0; i < tdo->tdo_nentries; i++)
+ {
+ to = tdo->tdo_entry[i].tde_object;
+ if (to == (FAR struct tmpfs_object_s *)tfo)
+ {
+ return tdo;
+ }
+
+ if (to->to_type == TMPFS_DIRECTORY)
+ {
+ parent = tmpfs_find_alias((FAR struct tmpfs_directory_s *)to, tfo);
+ if (parent != NULL)
+ {
+ return parent;
+ }
+ }
+ }
+
+ return NULL;
+}
+
+/****************************************************************************
+ * Name: tmpfs_drop_link
+ ****************************************************************************/
+
+static void tmpfs_drop_link(FAR struct tmpfs_s *fs,
+ FAR struct tmpfs_file_s *tfo)
+{
+ DEBUGASSERT(tfo->tfo_nlink > 0);
+ tfo->tfo_nlink--;
+ tfo->tfo_parent = NULL;
+ tmpfs_update_ctime((FAR struct tmpfs_object_s *)tfo);
+
+ /* FIOC_FILEPATH must not retain the directory of a removed alias.
+ * Unbind passes NULL because it removes every remaining name.
+ */
+
+ if (fs != NULL && tfo->tfo_nlink > 0)
+ {
+ tfo->tfo_parent = tmpfs_find_alias(
+ (FAR struct tmpfs_directory_s *)fs->tfs_root.tde_object, tfo);
+ DEBUGASSERT(tfo->tfo_parent != NULL);
+ }
}
/****************************************************************************
@@ -477,7 +590,7 @@ static void tmpfs_release_lockedobject(FAR struct
tmpfs_object_s *to)
/* Is this a file object? */
- if (to->to_type == TMPFS_REGULAR)
+ if (to->to_type != TMPFS_DIRECTORY)
{
tmpfs_release_lockedfile((FAR struct tmpfs_file_s *)to);
}
@@ -500,7 +613,7 @@ static void tmpfs_release_lockedfile(FAR struct
tmpfs_file_s *tfo)
* unlinked from its parent directory, then free the file object now.
*/
- if (tfo->tfo_refs == 1 && (tfo->tfo_flags & TFO_FLAG_UNLINKED) != 0)
+ if (tfo->tfo_refs == 1 && tfo->tfo_nlink == 0)
{
tmpfs_unlock_file(tfo);
nxrmutex_destroy(&tfo->tfo_lock);
@@ -647,6 +760,11 @@ static int tmpfs_add_dirent(FAR struct tmpfs_directory_s
*tdo,
}
}
+ if (namelen > NAME_MAX)
+ {
+ return -ENAMETOOLONG;
+ }
+
newname = fs_heap_strndup(name, namelen);
if (newname == NULL)
{
@@ -686,7 +804,8 @@ static int tmpfs_add_dirent(FAR struct tmpfs_directory_s
*tdo,
*
****************************************************************************/
-static int tmpfs_replace_dirent(FAR struct tmpfs_directory_s *tdo,
+static int tmpfs_replace_dirent(FAR struct tmpfs_s *fs,
+ FAR struct tmpfs_directory_s *tdo,
int index, FAR struct tmpfs_object_s *to)
{
FAR struct tmpfs_object_s *old = tdo->tdo_entry[index].tde_object;
@@ -749,18 +868,9 @@ static int tmpfs_replace_dirent(FAR struct
tmpfs_directory_s *tdo,
{
FAR struct tmpfs_file_s *oldfile = (FAR struct tmpfs_file_s *)old;
- if (oldfile->tfo_refs > 0)
- {
- tmpfs_orphan_file(oldfile);
- tmpfs_unlock_file(oldfile);
- }
- else
- {
- tmpfs_unlock_file(oldfile);
- nxrmutex_destroy(&oldfile->tfo_lock);
- fs_heap_free(oldfile->tfo_data);
- fs_heap_free(oldfile);
- }
+ oldfile->tfo_refs++;
+ tmpfs_drop_link(fs, oldfile);
+ tmpfs_release_lockedfile(oldfile);
}
return OK;
@@ -824,7 +934,7 @@ static int tmpfs_check_pathperm(FAR struct tmpfs_s *fs,
continue;
}
- ret = tmpfs_find_directory(fs, relpath, end, &tdo, NULL);
+ ret = tmpfs_find_directory(fs, relpath, end, &tdo, NULL, false);
if (ret < 0)
{
return ret;
@@ -973,7 +1083,7 @@ tmpfs_alloc_file(FAR struct tmpfs_directory_s *parent,
mode_t mode)
tfo->tfo_refs = 1;
tfo->tfo_parent = parent;
tfo->tfo_ino = tmpfs_new_ino();
- tfo->tfo_flags = 0;
+ tfo->tfo_nlink = 1;
tfo->tfo_size = 0;
tfo->tfo_data = NULL;
@@ -997,116 +1107,60 @@ tmpfs_alloc_file(FAR struct tmpfs_directory_s *parent,
mode_t mode)
static int tmpfs_create_file(FAR struct tmpfs_s *fs,
FAR const char *relpath, mode_t mode,
- FAR struct tmpfs_file_s **tfo)
+ FAR struct tmpfs_file_s **tfo, bool nofollow)
{
+ struct tmpfs_lookup_s result;
FAR struct tmpfs_directory_s *parent;
FAR struct tmpfs_file_s *newtfo;
- FAR const char *name;
+ unsigned int links = 0;
int ret;
- /* Separate the path into the file name and the path to the parent
- * directory.
- */
-
- name = strrchr(relpath, '/');
- if (name == NULL)
+ ret = tmpfs_lookup(fs, (FAR struct tmpfs_directory_s *)
+ fs->tfs_root.tde_object, relpath, strlen(relpath),
+ nofollow, &links, &result);
+ if (ret != -ENOENT || result.parent == NULL)
{
- /* No subdirectories... use the root directory */
-
- name = relpath;
- parent = (FAR struct tmpfs_directory_s *)fs->tfs_root.tde_object;
-
- /* Lock the root directory to emulate the behavior of
- * tmpfs_find_directory()
- */
-
- ret = tmpfs_lock_directory(parent);
- if (ret < 0)
- {
- return ret;
- }
-
- parent->tdo_refs++;
+ return ret == OK ? -EEXIST : ret;
}
- else if (name[1] != '\0')
- {
- /* Locate the parent directory that should contain this name.
- * On success, tmpfs_find_directory() will lock the parent
- * directory and increment the reference count.
- */
- ret = tmpfs_find_directory(fs, relpath, name - relpath, &parent, NULL);
- if (ret < 0)
- {
- return ret;
- }
-
- /* Skip the '/' path separator */
-
- name++;
- }
- else
+ parent = result.parent;
+ ret = tmpfs_lock_directory(parent);
+ if (ret < 0)
{
- return -EISDIR;
+ return ret;
}
- /* Verify that no object of this name already exists in the directory */
-
- ret = tmpfs_find_dirent(parent, name, strlen(name));
- if (ret != -ENOENT)
+#ifdef CONFIG_FS_PERMISSION
+ ret = fs_checkmode(parent->tdo_uid, parent->tdo_gid,
+ parent->tdo_mode | S_IFDIR, W_OK | X_OK);
+ if (ret < 0)
{
- /* Something with this name already exists in the directory.
- * OR perhaps some fatal error occurred.
- */
-
- if (ret >= 0)
- {
- ret = -EEXIST;
- }
-
- goto errout_with_parent;
+ goto out_parent;
}
-
- /* Allocate an empty file. The initial state of the file is locked with
- * one reference count.
- */
+#endif
newtfo = tmpfs_alloc_file(parent, mode);
if (newtfo == NULL)
{
ret = -ENOMEM;
- goto errout_with_parent;
+ goto out_parent;
}
- /* Then add the new, empty file to the directory */
-
- ret = tmpfs_add_dirent(parent, (FAR struct tmpfs_object_s *)newtfo, name);
+ ret = tmpfs_add_dirent(parent, (FAR struct tmpfs_object_s *)newtfo,
+ result.name);
if (ret < 0)
{
- goto errout_with_file;
+ tmpfs_unlock_file(newtfo);
+ nxrmutex_destroy(&newtfo->tfo_lock);
+ fs_heap_free(newtfo);
+ goto out_parent;
}
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--;
- tmpfs_unlock_directory(parent);
-
- /* Return success */
-
*tfo = newtfo;
- return OK;
-
- /* Error exits */
-errout_with_file:
- nxrmutex_destroy(&newtfo->tfo_lock);
- fs_heap_free(newtfo);
-
-errout_with_parent:
- parent->tdo_refs--;
+out_parent:
tmpfs_unlock_directory(parent);
return ret;
}
@@ -1198,7 +1252,8 @@ static int tmpfs_create_directory(FAR struct tmpfs_s *fs,
* directory and increment the reference count.
*/
- ret = tmpfs_find_directory(fs, relpath, name - relpath, &parent, NULL);
+ ret = tmpfs_find_directory(fs, relpath, name - relpath,
+ &parent, NULL, false);
if (ret < 0)
{
return ret;
@@ -1277,145 +1332,235 @@ errout_with_parent:
}
/****************************************************************************
- * Name: tmpfs_find_object
+ * Name: tmpfs_lookup
****************************************************************************/
-static int tmpfs_find_object(FAR struct tmpfs_s *fs,
- FAR const char *relpath, size_t len,
- FAR struct tmpfs_object_s **object,
- FAR struct tmpfs_directory_s **parent)
+static int tmpfs_lookup(FAR struct tmpfs_s *fs,
+ FAR struct tmpfs_directory_s *start,
+ FAR const char *path, size_t len, bool nofollow,
+ FAR unsigned int *links,
+ FAR struct tmpfs_lookup_s *result)
{
- FAR struct tmpfs_object_s *to = NULL;
- FAR struct tmpfs_directory_s *tdo = NULL;
- FAR struct tmpfs_directory_s *next_tdo;
+ FAR struct tmpfs_object_s *to = (FAR struct tmpfs_object_s *)start;
+ FAR struct tmpfs_directory_s *parent = NULL;
FAR const char *segment;
- FAR const char *next_segment;
+ size_t used = 0;
+ size_t count;
+#ifdef CONFIG_FS_LINKS
+ bool trailing;
+#endif
int index;
+#if defined(CONFIG_FS_LINKS) || defined(CONFIG_FS_PERMISSION)
int ret;
+#endif
- /* Traverse the file system for any object with the matching name */
-
- to = fs->tfs_root.tde_object;
- next_tdo = (FAR struct tmpfs_directory_s *)fs->tfs_root.tde_object;
-
- for (segment = relpath; len != 0; segment = next_segment + 1)
+ memset(result, 0, sizeof(*result));
+ while (used < len)
{
- /* Get the next segment after the one we are currently working on.
- * This will be NULL is we are working on the final segment of the
- * relpath.
- */
+ memset(result, 0, sizeof(*result));
- /* Skip any slash. */
-
- while (*segment == '/')
+ while (used < len && path[used] == '/')
{
- segment++;
- len--;
+ used++;
}
- next_segment = memchr(segment, '/', len);
- if (next_segment)
+ if (to->to_type != TMPFS_DIRECTORY)
{
- len -= next_segment + 1 - segment;
+ return -ENOTDIR;
}
- else
+
+ if (used == len)
{
- next_segment = segment + len;
- len = 0;
+ break;
}
- /* Search the next directory. */
-
- tdo = next_tdo;
-
- /* Find the TMPFS object with the next segment name in the current
- * directory.
- */
-
- index = tmpfs_find_dirent(tdo, segment, next_segment - segment);
- if (index < 0)
+ parent = (FAR struct tmpfs_directory_s *)to;
+#ifdef CONFIG_FS_PERMISSION
+ ret = tmpfs_lock_directory(parent);
+ if (ret < 0)
{
- /* No object with this name exists in the directory. */
+ return ret;
+ }
- return index;
+ ret = fs_checkmode(parent->tdo_uid, parent->tdo_gid,
+ parent->tdo_mode | S_IFDIR, X_OK);
+ tmpfs_unlock_directory(parent);
+ if (ret < 0)
+ {
+ return ret;
}
+#endif
- to = tdo->tdo_entry[index].tde_object;
+ segment = path + used;
+ count = 0;
+ while (used < len && path[used] != '/')
+ {
+ used++;
+ count++;
+ }
- /* Is this object another directory? */
+ if (count > NAME_MAX)
+ {
+ return -ENAMETOOLONG;
+ }
- if (to->to_type != TMPFS_DIRECTORY)
+#ifdef CONFIG_FS_LINKS
+ trailing = used < len;
+#endif
+ if (count == 1 && segment[0] == '.')
+ {
+ to = (FAR struct tmpfs_object_s *)parent;
+ parent = to->to_parent;
+ }
+ else if (count == 2 && !memcmp(segment, "..", 2))
{
- /* No. Was this the final segment in the path? */
+ if (parent->tdo_parent == NULL)
+ {
+ return -EXDEV;
+ }
- if (len == 0 && *next_segment != '/')
+ to = (FAR struct tmpfs_object_s *)parent->tdo_parent;
+ parent = to->to_parent;
+ }
+ else
+ {
+ index = tmpfs_find_dirent(parent, segment, count);
+ if (index < 0)
{
- /* Then we can break out of the loop now */
+ if (index == -ENOENT && used == len)
+ {
+ result->parent = parent;
+ memcpy(result->name, segment, count);
+ result->name[count] = '\0';
+ }
- break;
+ return index;
}
- /* No, this was not the final segment of the relpath.
- * We cannot continue the search if any of the intermediate
- * segments do no correspond to directories.
- */
-
- return -ENOTDIR;
+ to = parent->tdo_entry[index].tde_object;
}
- /* Search this directory for the next segment. If we
- * exit the loop, tdo will still refer to the parent
- * directory of to.
- */
+#ifdef CONFIG_FS_LINKS
+ if (to->to_type == TMPFS_SYMLINK && (!nofollow || trailing))
+ {
+ FAR struct tmpfs_file_s *link = (FAR struct tmpfs_file_s *)to;
+ FAR const char *target = (FAR const char *)link->tfo_data;
- next_tdo = (FAR struct tmpfs_directory_s *)to;
- }
+ if ((*links)++ >= SYMLOOP_MAX)
+ {
+ return -ELOOP;
+ }
- /* When we exit this loop (successfully), to will point to the TMPFS
- * object associated with the terminal segment of the relpath.
- * Increment the reference count on the located object.
- */
+ if (target[0] == '/')
+ {
+ struct inode_search_s desc;
+ FAR struct inode *inode;
- /* Return what we found */
+ ret = inode_search_setup(&desc, target, false);
+ if (ret < 0)
+ {
+ return ret;
+ }
- if (parent)
- {
- if (tdo != NULL)
- {
- /* Get exclusive access to the parent and increment the reference
- * count on the object.
- */
+ ret = inode_search(&desc, &inode);
+ if (ret >= 0)
+ {
+ if (!INODE_IS_MOUNTPT(inode) || inode->i_private != fs)
+ {
+ ret = -EXDEV;
+ }
+ else
+ {
+ ret = tmpfs_lookup(fs,
+ (FAR struct tmpfs_directory_s *)
+ fs->tfs_root.tde_object, desc.relpath,
+ strlen(desc.relpath), false, links, result);
+ }
+ }
+
+ inode_search_release(&desc);
+ }
+ else
+ {
+ ret = tmpfs_lookup(fs, parent, target, link->tfo_size,
+ false, links, result);
+ }
- ret = tmpfs_lock_directory(tdo);
if (ret < 0)
{
+ if (trailing)
+ {
+ result->parent = NULL;
+ }
+
return ret;
}
- tdo->tdo_refs++;
+ to = result->object;
+ parent = result->parent;
}
+#endif
+ }
+
+ result->object = to;
+ result->parent = parent;
+ return OK;
+}
+
+/****************************************************************************
+ * Name: tmpfs_find_object
+ ****************************************************************************/
- *parent = tdo;
+static int tmpfs_find_object(FAR struct tmpfs_s *fs,
+ FAR const char *relpath, size_t len,
+ FAR struct tmpfs_object_s **object,
+ FAR struct tmpfs_directory_s **parent,
+ bool nofollow)
+{
+ struct tmpfs_lookup_s result;
+ unsigned int links = 0;
+ int ret;
+
+ ret = tmpfs_lookup(fs, (FAR struct tmpfs_directory_s *)
+ fs->tfs_root.tde_object, relpath, len, nofollow,
+ &links, &result);
+ if (ret < 0)
+ {
+ return ret;
}
- if (object)
+ if (parent != NULL && result.parent != NULL)
{
- if (to != NULL)
+ ret = tmpfs_lock_directory(result.parent);
+ if (ret < 0)
{
- /* Get exclusive access to the object and increment the reference
- * count on the object.
- */
+ return ret;
+ }
- ret = tmpfs_lock_object(to);
- if (ret < 0)
+ result.parent->tdo_refs++;
+ }
+
+ if (object != NULL)
+ {
+ ret = tmpfs_lock_object(result.object);
+ if (ret < 0)
+ {
+ if (parent != NULL && result.parent != NULL)
{
- return ret;
+ result.parent->tdo_refs--;
+ tmpfs_unlock_directory(result.parent);
}
- to->to_refs++;
+ return ret;
}
- *object = to;
+ result.object->to_refs++;
+ *object = result.object;
+ }
+
+ if (parent != NULL)
+ {
+ *parent = result.parent;
}
return OK;
@@ -1428,7 +1573,8 @@ static int tmpfs_find_object(FAR struct tmpfs_s *fs,
static int tmpfs_find_file(FAR struct tmpfs_s *fs,
FAR const char *relpath,
FAR struct tmpfs_file_s **tfo,
- FAR struct tmpfs_directory_s **parent)
+ FAR struct tmpfs_directory_s **parent,
+ bool nofollow)
{
FAR struct tmpfs_object_s *to;
size_t len;
@@ -1449,18 +1595,18 @@ static int tmpfs_find_file(FAR struct tmpfs_s *fs,
* reference count on both.
*/
- ret = tmpfs_find_object(fs, relpath, len, &to, parent);
+ ret = tmpfs_find_object(fs, relpath, len, &to, parent, nofollow);
if (ret >= 0)
{
- /* We found it... but is it a regular file? */
+ /* Reject directories before returning a file object. */
- if (to->to_type != TMPFS_REGULAR)
+ if (to->to_type == TMPFS_DIRECTORY)
{
/* No... unlock the object and its parent and return an error */
tmpfs_release_lockedobject(to);
- if (parent)
+ if (parent != NULL && *parent != NULL)
{
FAR struct tmpfs_directory_s *tdo = *parent;
@@ -1486,17 +1632,18 @@ static int tmpfs_find_file(FAR struct tmpfs_s *fs,
static int tmpfs_find_directory(FAR struct tmpfs_s *fs,
FAR const char *relpath, size_t len,
FAR struct tmpfs_directory_s **tdo,
- FAR struct tmpfs_directory_s **parent)
+ FAR struct tmpfs_directory_s **parent,
+ bool nofollow)
{
FAR struct tmpfs_object_s *to;
int ret;
/* Find the object at this path */
- ret = tmpfs_find_object(fs, relpath, len, &to, parent);
+ ret = tmpfs_find_object(fs, relpath, len, &to, parent, nofollow);
if (ret >= 0)
{
- /* We found it... but is it a regular file? */
+ /* The caller requires a directory object. */
if (to->to_type != TMPFS_DIRECTORY)
{
@@ -1504,7 +1651,7 @@ static int tmpfs_find_directory(FAR struct tmpfs_s *fs,
tmpfs_release_lockedobject(to);
- if (parent)
+ if (parent != NULL && *parent != NULL)
{
FAR struct tmpfs_directory_s *tmptdo = *parent;
@@ -1586,6 +1733,7 @@ static int tmpfs_statfs_callout(FAR struct
tmpfs_directory_s *tdo,
{
FAR struct tmpfs_object_s *to;
FAR struct tmpfs_statfs_s *tmpbuf;
+ unsigned int i;
DEBUGASSERT(tdo != NULL && arg != NULL && index < tdo->tdo_nentries);
@@ -1598,12 +1746,31 @@ static int tmpfs_statfs_callout(FAR struct
tmpfs_directory_s *tdo,
* for this object.
*/
- tmpbuf->tsf_alloc += to->to_alloc +
- strlen(tdo->tdo_entry[index].tde_name) + 1;
+ tmpbuf->tsf_alloc += strlen(tdo->tdo_entry[index].tde_name) + 1;
+
+ /* Each alias owns a name; the object and data are shared. */
+
+ if (to->to_type != TMPFS_DIRECTORY)
+ {
+ if (to->to_parent != tdo)
+ {
+ return TMPFS_CONTINUE;
+ }
+
+ for (i = 0; i < index; i++)
+ {
+ if (tdo->tdo_entry[i].tde_object == to)
+ {
+ return TMPFS_CONTINUE;
+ }
+ }
+ }
+
+ tmpbuf->tsf_alloc += to->to_alloc;
/* Is this directory entry a file object? */
- if (to->to_type == TMPFS_REGULAR)
+ if (to->to_type != TMPFS_DIRECTORY)
{
FAR struct tmpfs_file_s *tmptfo;
@@ -1675,21 +1842,12 @@ static int tmpfs_free_callout(FAR struct
tmpfs_directory_s *tdo,
/* Is this directory entry a file object? */
- if (to->to_type == TMPFS_REGULAR)
+ if (to->to_type != TMPFS_DIRECTORY)
{
tfo = (FAR struct tmpfs_file_s *)to;
- /* Are there references to the file? */
-
- if (tfo->tfo_refs > 0)
- {
- /* Yes.. We cannot delete the file now. Just mark it as unlinked. */
-
- tmpfs_orphan_file(tfo);
- return TMPFS_UNLINKED;
- }
-
- fs_heap_free(tfo->tfo_data);
+ tmpfs_drop_link(NULL, tfo);
+ return TMPFS_UNLINKED;
}
else /* if (to->to_type == TMPFS_DIRECTORY) */
{
@@ -1807,9 +1965,6 @@ static int tmpfs_open(FAR struct file *filep, FAR const
char *relpath,
*/
inode = filep->f_inode;
- fs = inode->i_private;
-
- DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
/* Skip over any leading directory separators (shouldn't be any) */
@@ -1828,7 +1983,7 @@ static int tmpfs_open(FAR struct file *filep, FAR const
char *relpath,
/* Get exclusive access to the file system */
- ret = tmpfs_lock(fs);
+ ret = tmpfs_lock_mount(inode, &fs);
if (ret < 0)
{
return ret;
@@ -1837,11 +1992,12 @@ static int tmpfs_open(FAR struct file *filep, FAR const
char *relpath,
/* Find the file object associated with this relative path.
* If successful, this action will lock both the parent directory and
* the file object, adding one to the reference count of both.
- * In the event that -ENOENT, there will still be a reference and
- * lock on the returned directory.
*/
- ret = tmpfs_find_file(fs, relpath, &tfo, NULL);
+ ret = tmpfs_find_file(fs, relpath, &tfo, NULL,
+ (oflags & O_NOFOLLOW) != 0 ||
+ (oflags & (O_CREAT | O_EXCL)) ==
+ (O_CREAT | O_EXCL));
if (ret >= 0)
{
/* The file exists. We hold the lock and one reference count
@@ -1858,6 +2014,14 @@ static int tmpfs_open(FAR struct file *filep, FAR const
char *relpath,
goto errout_with_filelock;
}
+#ifdef CONFIG_FS_LINKS
+ if (tfo->tfo_type == TMPFS_SYMLINK)
+ {
+ ret = -ELOOP;
+ goto errout_with_filelock;
+ }
+#endif
+
/* Check if the caller has sufficient privileges to open the file. */
#ifdef CONFIG_FS_PERMISSION
@@ -1922,15 +2086,7 @@ static int tmpfs_open(FAR struct file *filep, FAR const
char *relpath,
* on the new file object.
*/
-#ifdef CONFIG_FS_PERMISSION
- ret = tmpfs_check_parentperm(fs, relpath, W_OK | X_OK);
- if (ret < 0)
- {
- goto errout_with_fslock;
- }
-#endif
-
- ret = tmpfs_create_file(fs, relpath, mode, &tfo);
+ ret = tmpfs_create_file(fs, relpath, mode, &tfo, false);
if (ret < 0)
{
goto errout_with_fslock;
@@ -1963,7 +2119,7 @@ static int tmpfs_open(FAR struct file *filep, FAR const
char *relpath,
/* Unlock the file file object, but retain the reference count */
tmpfs_unlock_file(tfo);
- tmpfs_unlock(fs);
+ tmpfs_unlock_mount(fs);
return OK;
/* Error exits */
@@ -1972,7 +2128,7 @@ errout_with_filelock:
tmpfs_release_lockedfile(tfo);
errout_with_fslock:
- tmpfs_unlock(fs);
+ tmpfs_unlock_mount(fs);
return ret;
}
@@ -2499,26 +2655,35 @@ static int tmpfs_ioctl(FAR struct file *filep, int cmd,
unsigned long arg)
if (cmd == FIOC_FILEPATH)
{
+ FAR struct tmpfs_s *fs;
FAR char *ptr = (FAR char *)((uintptr_t)arg);
- ret = inode_getpath(filep->f_inode, ptr, PATH_MAX);
+ ret = tmpfs_lock_mount(filep->f_inode, &fs);
if (ret < 0)
{
return ret;
}
- /* An unlinked file has no path */
+ /* Namespace changes can replace the parent selected for an alias. */
- if ((tfo->tfo_flags & TFO_FLAG_UNLINKED) != 0)
+ ret = inode_getpath(filep->f_inode, ptr, PATH_MAX);
+ if (ret < 0)
{
- return -ENOENT;
+ tmpfs_unlock_mount(fs);
+ return ret;
}
- ret = tmpfs_getpath((FAR struct tmpfs_object_s *)tfo, ptr, PATH_MAX);
- if (ret < 0)
+ if (tfo->tfo_nlink == 0)
{
- return ret;
+ ret = -ENOENT;
}
+ else
+ {
+ ret = tmpfs_getpath((FAR struct tmpfs_object_s *)tfo,
+ ptr, PATH_MAX);
+ }
+
+ tmpfs_unlock_mount(fs);
}
else if (cmd == FIOC_XIPBASE)
{
@@ -2720,11 +2885,6 @@ static int tmpfs_opendir(FAR struct inode *mountpt, FAR
const char *relpath,
mountpt, relpath, dir);
DEBUGASSERT(mountpt != NULL && relpath != NULL && dir != NULL);
- /* Get the mountpoint private data from the inode structure */
-
- fs = mountpt->i_private;
- DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
-
tdir = fs_heap_zalloc(sizeof(*tdir));
if (tdir == NULL)
{
@@ -2733,7 +2893,7 @@ static int tmpfs_opendir(FAR struct inode *mountpt, FAR
const char *relpath,
/* Get exclusive access to the file system */
- ret = tmpfs_lock(fs);
+ ret = tmpfs_lock_mount(mountpt, &fs);
if (ret < 0)
{
fs_heap_free(tdir);
@@ -2743,11 +2903,10 @@ static int tmpfs_opendir(FAR struct inode *mountpt, FAR
const char *relpath,
/* Find the directory object associated with this relative path.
* If successful, this action will lock both the parent directory and
* the file object, adding one to the reference count of both.
- * In the event that -ENOENT, there will still be a reference and
- * lock on the returned directory.
*/
- ret = tmpfs_find_directory(fs, relpath, strlen(relpath), &tdo, NULL);
+ ret = tmpfs_find_directory(fs, relpath, strlen(relpath),
+ &tdo, NULL, false);
if (ret >= 0)
{
#ifdef CONFIG_FS_PERMISSION
@@ -2769,7 +2928,7 @@ static int tmpfs_opendir(FAR struct inode *mountpt, FAR
const char *relpath,
/* Release the lock on the file system and return the result */
- tmpfs_unlock(fs);
+ tmpfs_unlock_mount(fs);
if (ret < 0)
{
fs_heap_free(tdir);
@@ -2857,7 +3016,13 @@ static int tmpfs_readdir(FAR struct inode *mountpt,
entry->d_type = DTYPE_DIRECTORY;
}
- else /* to->to_type == TMPFS_REGULAR) */
+#ifdef CONFIG_FS_LINKS
+ else if (to->to_type == TMPFS_SYMLINK)
+ {
+ entry->d_type = DTYPE_LINK;
+ }
+#endif
+ else
{
/* A regular file */
@@ -3006,14 +3171,9 @@ static int tmpfs_statfs(FAR struct inode *mountpt, FAR
struct statfs *buf)
finfo("mountpt: %p buf: %p\n", mountpt, buf);
DEBUGASSERT(mountpt != NULL && buf != NULL);
- /* Get the file system structure from the inode reference. */
-
- fs = mountpt->i_private;
- DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
-
/* Get exclusive access to the file system */
- ret = tmpfs_lock(fs);
+ ret = tmpfs_lock_mount(mountpt, &fs);
if (ret < 0)
{
return ret;
@@ -3037,7 +3197,7 @@ static int tmpfs_statfs(FAR struct inode *mountpt, FAR
struct statfs *buf)
ret = tmpfs_foreach(tdo, tmpfs_statfs_callout, (FAR void *)&tmpbuf);
if (ret < 0)
{
- tmpfs_unlock(fs);
+ tmpfs_unlock_mount(fs);
return -ECANCELED;
}
@@ -3059,7 +3219,7 @@ static int tmpfs_statfs(FAR struct inode *mountpt, FAR
struct statfs *buf)
/* Release the lock on the file system */
- tmpfs_unlock(fs);
+ tmpfs_unlock_mount(fs);
return OK;
}
@@ -3090,14 +3250,9 @@ static int tmpfs_unlink(FAR struct inode *mountpt, FAR
const char *relpath)
finfo("mountpt: %p relpath: %s\n", mountpt, relpath);
DEBUGASSERT(mountpt != NULL && relpath != NULL);
- /* Get the file system structure from the inode reference. */
-
- fs = mountpt->i_private;
- DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
-
/* Get exclusive access to the file system */
- ret = tmpfs_lock(fs);
+ ret = tmpfs_lock_mount(mountpt, &fs);
if (ret < 0)
{
return ret;
@@ -3108,7 +3263,7 @@ static int tmpfs_unlink(FAR struct inode *mountpt, FAR
const char *relpath)
* and the parent directory and take one reference count on each.
*/
- ret = tmpfs_find_file(fs, relpath, &tfo, &tdo);
+ ret = tmpfs_find_file(fs, relpath, &tfo, &tdo, true);
if (ret < 0)
{
goto errout_with_lock;
@@ -3140,30 +3295,8 @@ static int tmpfs_unlink(FAR struct inode *mountpt, FAR
const char *relpath)
goto errout_with_objects;
}
- /* If the reference count is not one, then just mark the file as
- * unlinked
- */
-
- if (tfo->tfo_refs > 1)
- {
- /* Make the file object as unlinked */
-
- tmpfs_orphan_file(tfo);
-
- /* Release the reference count on the file object */
-
- tfo->tfo_refs--;
- tmpfs_unlock_file(tfo);
- }
-
- /* Otherwise we can free the object now */
-
- else
- {
- nxrmutex_destroy(&tfo->tfo_lock);
- fs_heap_free(tfo->tfo_data);
- fs_heap_free(tfo);
- }
+ tmpfs_drop_link(fs, tfo);
+ tmpfs_release_lockedfile(tfo);
tmpfs_update_mtime((FAR struct tmpfs_object_s *)tdo);
tmpfs_update_ctime((FAR struct tmpfs_object_s *)tdo);
@@ -3172,7 +3305,7 @@ static int tmpfs_unlink(FAR struct inode *mountpt, FAR
const char *relpath)
tdo->tdo_refs--;
tmpfs_unlock_directory(tdo);
- tmpfs_unlock(fs);
+ tmpfs_unlock_mount(fs);
return OK;
@@ -3183,7 +3316,7 @@ errout_with_objects:
tmpfs_unlock_directory(tdo);
errout_with_lock:
- tmpfs_unlock(fs);
+ tmpfs_unlock_mount(fs);
return ret;
}
@@ -3200,14 +3333,9 @@ static int tmpfs_mkdir(FAR struct inode *mountpt, FAR
const char *relpath,
finfo("mountpt: %p relpath: %s mode: %04x\n", mountpt, relpath, mode);
DEBUGASSERT(mountpt != NULL && relpath != NULL);
- /* Get the file system structure from the inode reference. */
-
- fs = mountpt->i_private;
- DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
-
/* Get exclusive access to the file system */
- ret = tmpfs_lock(fs);
+ ret = tmpfs_lock_mount(mountpt, &fs);
if (ret < 0)
{
return ret;
@@ -3216,7 +3344,7 @@ static int tmpfs_mkdir(FAR struct inode *mountpt, FAR
const char *relpath,
/* Create the directory. */
ret = tmpfs_create_directory(fs, relpath, mode, NULL);
- tmpfs_unlock(fs);
+ tmpfs_unlock_mount(fs);
return ret;
}
@@ -3230,19 +3358,15 @@ static int tmpfs_rmdir(FAR struct inode *mountpt, FAR
const char *relpath)
FAR struct tmpfs_directory_s *parent;
FAR struct tmpfs_directory_s *tdo;
FAR const char *name;
+ size_t len = strlen(relpath);
int ret;
finfo("mountpt: %p relpath: %s\n", mountpt, relpath);
DEBUGASSERT(mountpt != NULL && relpath != NULL);
- /* Get the file system structure from the inode reference. */
-
- fs = mountpt->i_private;
- DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
-
/* Get exclusive access to the file system */
- ret = tmpfs_lock(fs);
+ ret = tmpfs_lock_mount(mountpt, &fs);
if (ret < 0)
{
return ret;
@@ -3254,12 +3378,25 @@ static int tmpfs_rmdir(FAR struct inode *mountpt, FAR
const char *relpath)
* on each.
*/
- ret = tmpfs_find_directory(fs, relpath, strlen(relpath), &tdo, &parent);
+ while (len > 0 && relpath[len - 1] == '/')
+ {
+ len--;
+ }
+
+ ret = tmpfs_find_directory(fs, relpath, len,
+ &tdo, &parent, true);
if (ret < 0)
{
goto errout_with_lock;
}
+ if (parent == NULL)
+ {
+ tmpfs_release_lockedobject((FAR struct tmpfs_object_s *)tdo);
+ ret = -EBUSY;
+ goto errout_with_lock;
+ }
+
/* Is the directory empty? We cannot remove directories that still
* contain references to file system objects.
*/
@@ -3324,7 +3461,7 @@ static int tmpfs_rmdir(FAR struct inode *mountpt, FAR
const char *relpath)
parent->tdo_refs--;
tmpfs_unlock_directory(parent);
- tmpfs_unlock(fs);
+ tmpfs_unlock_mount(fs);
return OK;
@@ -3336,7 +3473,7 @@ errout_with_objects:
tmpfs_unlock_directory(parent);
errout_with_lock:
- tmpfs_unlock(fs);
+ tmpfs_unlock_mount(fs);
return ret;
}
@@ -3348,12 +3485,14 @@ static int tmpfs_rename(FAR struct inode *mountpt,
FAR const char *oldrelpath,
FAR const char *newrelpath)
{
+ FAR struct tmpfs_directory_s *ancestor;
FAR struct tmpfs_directory_s *oldparent;
FAR struct tmpfs_directory_s *newparent;
FAR struct tmpfs_object_s *to;
FAR struct tmpfs_s *fs;
FAR const char *oldname;
FAR const char *newname;
+ size_t oldlen = strlen(oldrelpath);
int index;
int ret;
@@ -3361,14 +3500,9 @@ static int tmpfs_rename(FAR struct inode *mountpt,
mountpt, oldrelpath, newrelpath);
DEBUGASSERT(mountpt != NULL && oldrelpath != NULL && newrelpath != NULL);
- /* Get the file system structure from the inode reference. */
-
- fs = mountpt->i_private;
- DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
-
/* Get exclusive access to the file system */
- ret = tmpfs_lock(fs);
+ ret = tmpfs_lock_mount(mountpt, &fs);
if (ret < 0)
{
return ret;
@@ -3404,7 +3538,7 @@ static int tmpfs_rename(FAR struct inode *mountpt,
*/
ret = tmpfs_find_directory(fs, newrelpath, newname - newrelpath,
- &newparent, NULL);
+ &newparent, NULL, false);
if (ret < 0)
{
goto errout_with_lock;
@@ -3420,13 +3554,39 @@ static int tmpfs_rename(FAR struct inode *mountpt,
* the reference count on both.
*/
- ret = tmpfs_find_object(fs, oldrelpath, strlen(oldrelpath),
- &to, &oldparent);
+ while (oldlen > 0 && oldrelpath[oldlen - 1] == '/')
+ {
+ oldlen--;
+ }
+
+ ret = tmpfs_find_object(fs, oldrelpath, oldlen,
+ &to, &oldparent, true);
if (ret < 0)
{
goto errout_with_newparent;
}
+ if (oldparent == NULL)
+ {
+ tmpfs_release_lockedobject(to);
+ ret = -EBUSY;
+ goto errout_with_newparent;
+ }
+
+ /* A directory link in the destination path can resolve inside the source.
+ * Reject moving a directory below itself before changing either name.
+ */
+
+ for (ancestor = newparent; ancestor != NULL;
+ ancestor = ancestor->tdo_parent)
+ {
+ if ((FAR struct tmpfs_object_s *)ancestor == to)
+ {
+ ret = -EINVAL;
+ goto errout_with_oldparent;
+ }
+ }
+
/* Get the old file name from the relative path */
oldname = strrchr(oldrelpath, '/');
@@ -3457,7 +3617,7 @@ static int tmpfs_rename(FAR struct inode *mountpt,
{
/* Yes.. replace it in one step, then remove the old entry */
- ret = tmpfs_replace_dirent(newparent, index, to);
+ ret = tmpfs_replace_dirent(fs, newparent, index, to);
if (ret > 0)
{
ret = OK;
@@ -3479,19 +3639,13 @@ static int tmpfs_rename(FAR struct inode *mountpt,
goto errout_with_oldparent;
}
- /* Remove the entry from the parent directory */
-
- ret = tmpfs_remove_dirent(oldparent, oldname);
- if (ret < 0)
- {
- goto errout_with_oldparent;
- }
-
- /* Add an entry to the new parent directory. */
+ /* Preserve the source name if the destination allocation fails. */
ret = tmpfs_add_dirent(newparent, to, newname);
if (ret >= 0)
{
+ ret = tmpfs_remove_dirent(oldparent, oldname);
+ DEBUGASSERT(ret == OK);
tmpfs_update_rename_times(to, oldparent, newparent);
}
@@ -3506,9 +3660,238 @@ errout_with_newparent:
tmpfs_unlock_directory(newparent);
errout_with_lock:
- tmpfs_unlock(fs);
+ tmpfs_unlock_mount(fs);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: tmpfs_link
+ ****************************************************************************/
+
+#ifdef CONFIG_FS_LINKS
+static int tmpfs_link(FAR struct inode *mountpt, FAR const char *oldrelpath,
+ FAR const char *newrelpath)
+{
+ FAR struct tmpfs_s *fs;
+ FAR struct tmpfs_directory_s *parent;
+ FAR struct tmpfs_file_s *target;
+ FAR const char *name;
+ int ret;
+
+ ret = tmpfs_lock_mount(mountpt, &fs);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ if (*oldrelpath == '\0')
+ {
+ ret = -EPERM;
+ goto out_fs;
+ }
+
+#ifdef CONFIG_FS_PERMISSION
+ ret = tmpfs_check_parentperm(fs, oldrelpath, X_OK);
+ if (ret < 0)
+ {
+ goto out_fs;
+ }
+
+ ret = tmpfs_check_parentperm(fs, newrelpath, W_OK | X_OK);
+ if (ret < 0)
+ {
+ goto out_fs;
+ }
+#endif
+
+ ret = tmpfs_find_file(fs, oldrelpath, &target, NULL, true);
+ if (ret < 0)
+ {
+ if (ret == -EISDIR)
+ {
+ ret = -EPERM;
+ }
+
+ goto out_fs;
+ }
+
+ if (target->tfo_nlink >= LINK_MAX)
+ {
+ ret = -EMLINK;
+ goto out_file;
+ }
+
+ name = strrchr(newrelpath, '/');
+ if (name == NULL)
+ {
+ name = newrelpath;
+ ret = tmpfs_find_directory(fs, "", 0, &parent, NULL, false);
+ }
+ else
+ {
+ ret = tmpfs_find_directory(fs, newrelpath, name - newrelpath,
+ &parent, NULL, false);
+ name++;
+ }
+
+ if (ret < 0)
+ {
+ goto out_file;
+ }
+
+ ret = tmpfs_find_dirent(parent, name, strlen(name));
+ if (ret >= 0)
+ {
+ ret = -EEXIST;
+ }
+ else if (ret == -ENOENT)
+ {
+ ret = tmpfs_add_dirent(parent, (FAR struct tmpfs_object_s *)target,
+ name);
+ if (ret >= 0)
+ {
+ target->tfo_nlink++;
+ tmpfs_update_ctime((FAR struct tmpfs_object_s *)target);
+ tmpfs_update_mtime((FAR struct tmpfs_object_s *)parent);
+ tmpfs_update_ctime((FAR struct tmpfs_object_s *)parent);
+ }
+ }
+
+ parent->tdo_refs--;
+ tmpfs_unlock_directory(parent);
+
+out_file:
+ tmpfs_release_lockedfile(target);
+out_fs:
+ tmpfs_unlock_mount(fs);
+ return ret;
+}
+#endif
+
+/****************************************************************************
+ * Name: tmpfs_symlink
+ ****************************************************************************/
+
+#ifdef CONFIG_FS_LINKS
+static int tmpfs_symlink(FAR struct inode *mountpt, FAR const char *target,
+ FAR const char *relpath)
+{
+ FAR struct tmpfs_s *fs;
+ FAR struct tmpfs_file_s *link;
+ FAR char *data;
+ size_t len = strlen(target);
+ int ret;
+
+ if (len == 0)
+ {
+ return -ENOENT;
+ }
+
+ if (len > SYMLINK_MAX)
+ {
+ return -ENAMETOOLONG;
+ }
+
+ data = fs_heap_strdup(target);
+ if (data == NULL)
+ {
+ return -ENOMEM;
+ }
+
+ ret = tmpfs_lock_mount(mountpt, &fs);
+ if (ret < 0)
+ {
+ fs_heap_free(data);
+ return ret;
+ }
+
+ ret = tmpfs_create_file(fs, relpath, 0777, &link, true);
+ if (ret >= 0)
+ {
+ link->tfo_type = TMPFS_SYMLINK;
+ link->tfo_data = (FAR uint8_t *)data;
+ link->tfo_size = len;
+ link->tfo_alloc = len + 1;
+ tmpfs_release_lockedfile(link);
+ }
+ else
+ {
+ fs_heap_free(data);
+ }
+
+ tmpfs_unlock_mount(fs);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: tmpfs_readlink
+ ****************************************************************************/
+
+static ssize_t tmpfs_readlink(FAR struct inode *mountpt,
+ FAR const char *relpath, FAR char *buf,
+ size_t buflen)
+{
+ FAR struct tmpfs_s *fs;
+ FAR struct tmpfs_object_s *to;
+ FAR struct tmpfs_file_s *link;
+ ssize_t ret;
+
+ ret = tmpfs_lock_mount(mountpt, &fs);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = tmpfs_find_object(fs, relpath, strlen(relpath), &to, NULL, true);
+ if (ret >= 0)
+ {
+ if (to->to_type != TMPFS_SYMLINK)
+ {
+ ret = -EINVAL;
+ }
+ else
+ {
+ link = (FAR struct tmpfs_file_s *)to;
+ ret = buflen < link->tfo_size ? buflen : link->tfo_size;
+ memcpy(buf, link->tfo_data, ret);
+ tmpfs_update_atime(to);
+ }
+
+ tmpfs_release_lockedobject(to);
+ }
+
+ tmpfs_unlock_mount(fs);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: tmpfs_lstat
+ ****************************************************************************/
+
+static int tmpfs_lstat(FAR struct inode *mountpt, FAR const char *relpath,
+ FAR struct stat *buf)
+{
+ FAR struct tmpfs_s *fs;
+ FAR struct tmpfs_object_s *to;
+ int ret;
+
+ ret = tmpfs_lock_mount(mountpt, &fs);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = tmpfs_find_object(fs, relpath, strlen(relpath), &to, NULL, true);
+ if (ret >= 0)
+ {
+ tmpfs_stat_common(to, buf);
+ tmpfs_release_lockedobject(to);
+ }
+
+ tmpfs_unlock_mount(fs);
return ret;
}
+#endif
/****************************************************************************
* Name: tmpfs_stat_common
@@ -3524,7 +3907,7 @@ static void tmpfs_stat_common(FAR struct tmpfs_object_s
*to,
memset(buf, 0, sizeof(struct stat));
buf->st_ino = to->to_ino;
- if (to->to_type == TMPFS_REGULAR)
+ if (to->to_type != TMPFS_DIRECTORY)
{
FAR struct tmpfs_file_s *tfo =
(FAR struct tmpfs_file_s *)to;
@@ -3539,9 +3922,17 @@ static void tmpfs_stat_common(FAR struct tmpfs_object_s
*to,
buf->st_mode = S_IRWXO | S_IRWXG | S_IRWXU | S_IFREG;
#endif
+#ifdef CONFIG_FS_LINKS
+ if (to->to_type == TMPFS_SYMLINK)
+ {
+ buf->st_mode = (buf->st_mode & ~S_IFMT) | S_IFLNK;
+ }
+#endif
+
/* Get the size of the object */
objsize = tfo->tfo_size;
+ buf->st_nlink = tfo->tfo_nlink;
}
else /* if (to->to_type == TMPFS_DIRECTORY) */
{
@@ -3588,14 +3979,9 @@ static int tmpfs_stat(FAR struct inode *mountpt, FAR
const char *relpath,
finfo("mountpt=%p relpath=%s buf=%p\n", mountpt, relpath, buf);
DEBUGASSERT(mountpt != NULL && relpath != NULL && buf != NULL);
- /* Get the file system structure from the inode reference. */
-
- fs = mountpt->i_private;
- DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
-
/* Get exclusive access to the file system */
- ret = tmpfs_lock(fs);
+ ret = tmpfs_lock_mount(mountpt, &fs);
if (ret < 0)
{
return ret;
@@ -3606,7 +3992,7 @@ static int tmpfs_stat(FAR struct inode *mountpt, FAR
const char *relpath,
* reference count on the object.
*/
- ret = tmpfs_find_object(fs, relpath, strlen(relpath), &to, NULL);
+ ret = tmpfs_find_object(fs, relpath, strlen(relpath), &to, NULL, false);
if (ret < 0)
{
goto errout_with_fslock;
@@ -3625,7 +4011,7 @@ static int tmpfs_stat(FAR struct inode *mountpt, FAR
const char *relpath,
ret = OK;
errout_with_fslock:
- tmpfs_unlock(fs);
+ tmpfs_unlock_mount(fs);
return ret;
}
@@ -3642,16 +4028,14 @@ static int tmpfs_chstat(FAR struct inode *mountpt, FAR
const char *relpath,
DEBUGASSERT(mountpt != NULL && relpath != NULL && buf != NULL);
- fs = mountpt->i_private;
- DEBUGASSERT(fs != NULL);
-
- ret = tmpfs_lock(fs);
+ ret = tmpfs_lock_mount(mountpt, &fs);
if (ret < 0)
{
return ret;
}
- ret = tmpfs_find_object(fs, relpath, strlen(relpath), &to, NULL);
+ ret = tmpfs_find_object(fs, relpath, strlen(relpath), &to, NULL,
+ (flags & CH_STAT_NOFOLLOW) != 0);
if (ret < 0)
{
goto errout_with_fslock;
@@ -3661,7 +4045,7 @@ static int tmpfs_chstat(FAR struct inode *mountpt, FAR
const char *relpath,
tmpfs_release_lockedobject(to);
errout_with_fslock:
- tmpfs_unlock(fs);
+ tmpfs_unlock_mount(fs);
return ret;
}
@@ -3678,17 +4062,14 @@ static int tmpfs_permission(FAR struct inode *mountpt,
DEBUGASSERT(mountpt != NULL && relpath != NULL);
- fs = mountpt->i_private;
- DEBUGASSERT(fs != NULL);
-
- ret = tmpfs_lock(fs);
+ ret = tmpfs_lock_mount(mountpt, &fs);
if (ret < 0)
{
return ret;
}
ret = tmpfs_check_pathperm(fs, relpath, strlen(relpath), amode);
- tmpfs_unlock(fs);
+ tmpfs_unlock_mount(fs);
return ret;
}
#endif
diff --git a/fs/tmpfs/fs_tmpfs.h b/fs/tmpfs/fs_tmpfs.h
index 69b8f960ea3..96b3283416e 100644
--- a/fs/tmpfs/fs_tmpfs.h
+++ b/fs/tmpfs/fs_tmpfs.h
@@ -40,10 +40,6 @@
* Pre-processor Definitions
****************************************************************************/
-/* Bit definitions for file object flags */
-
-#define TFO_FLAG_UNLINKED (1 << 0) /* Bit 0: File is unlinked */
-
/****************************************************************************
* Public Types
****************************************************************************/
@@ -53,7 +49,10 @@
enum tmpfs_objtype_e
{
TMPFS_DIRECTORY = 0, /* Directory */
- TMPFS_REGULAR /* Regular file */
+ TMPFS_REGULAR, /* Regular file */
+#ifdef CONFIG_FS_LINKS
+ TMPFS_SYMLINK /* Symbolic link */
+#endif
};
/* Values returned by tmpfs_foreach() */
@@ -164,7 +163,7 @@ struct tmpfs_file_s
/* Remaining fields are unique to a directory object */
- uint8_t tfo_flags; /* See TFO_FLAG_* definitions */
+ nlink_t tfo_nlink; /* Directory entries referring to this object */
uint8_t tfo_maps; /* Number of mappings of tfo_data */
size_t tfo_size; /* Valid file size */
FAR uint8_t *tfo_data; /* File data starts here */