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
commit c9d60ae081dc6ff78c3dc066250ae05d3853639a Author: Marco Casaroli <[email protected]> AuthorDate: Wed Nov 11 06:21:50 2026 +0100 fs/vfs: Give each directory entry a file serial number that is not 0. Most mounted file systems (fat, romfs, littlefs, procfs and others) do not set d_ino in readdir(). readdir() then returned the old content of the buffer of the caller, which opendir() allocates with malloc(). Programs take an entry with d_ino 0 as a deleted entry: glob() of a shell found no file in such a directory. Set d_ino to 0 before the file system reads the entry. If the file system leaves it 0, give a hash (FNV-1a) of the path of the entry. inode_hash() in fs/inode/inode.h computes the hash, so that a file system can use the same hash for its own numbers. The pseudo file system, tmpfs, v9fs, hostfs and rpmsgfs set their own numbers and do not change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- fs/inode/inode.h | 24 ++++++++++++++++++++++++ fs/vfs/fs_dir.c | 20 +++++++++++++++++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/fs/inode/inode.h b/fs/inode/inode.h index 9986c4fbc6d..a8944fb2a06 100644 --- a/fs/inode/inode.h +++ b/fs/inode/inode.h @@ -79,6 +79,8 @@ #define INODE_GET_NLINK(i) \ ((atomic_read(&(i)->i_crefs) >> INODE_NLINK_SHIFT) + 1) +#define INODE_HASH_INIT 2166136261u /* Start of inode_hash() */ + /**************************************************************************** * Public Types ****************************************************************************/ @@ -518,6 +520,28 @@ int fs_checkopenperm(uid_t owner, gid_t group, mode_t mode, int oflags); int foreach_inode(foreach_inode_t handler, FAR void *arg); +/**************************************************************************** + * Name: inode_hash + * + * Description: + * Add a string to a 32-bit FNV-1a hash that starts at INODE_HASH_INIT. + * A file system that has no file serial numbers makes them from the hash + * of a path. The result is never 0: programs skip a directory entry + * with d_ino 0. + * + ****************************************************************************/ + +static inline uint32_t inode_hash(uint32_t hash, FAR const char *str) +{ + while (*str != '\0') + { + hash ^= *str++ & 0xff; + hash *= 16777619u; + } + + return hash != 0 ? hash : 1; +} + /**************************************************************************** * Name: dir_allocate * diff --git a/fs/vfs/fs_dir.c b/fs/vfs/fs_dir.c index 8a1e3f6175c..72bd42fd28f 100644 --- a/fs/vfs/fs_dir.c +++ b/fs/vfs/fs_dir.c @@ -488,6 +488,7 @@ static ssize_t dir_read(FAR struct file *filep, FAR char *buffer, size_t buflen) { FAR struct fs_dirent_s *dir = filep->f_priv; + FAR struct dirent *entry = (FAR struct dirent *)buffer; #ifndef CONFIG_DISABLE_MOUNTPOINT FAR struct inode *inode = dir->fd_root; #endif @@ -500,6 +501,10 @@ static ssize_t dir_read(FAR struct file *filep, FAR char *buffer, return -EINVAL; } + /* A file system that has no file serial numbers leaves d_ino alone */ + + entry->d_ino = 0; + /* The way we handle the readdir depends on the type of inode * that we are dealing with. */ @@ -507,15 +512,14 @@ static ssize_t dir_read(FAR struct file *filep, FAR char *buffer, #ifndef CONFIG_DISABLE_MOUNTPOINT if (INODE_IS_MOUNTPT(inode)) { - ret = inode->u.i_mops->readdir(inode, dir, - (FAR struct dirent *)buffer); + ret = inode->u.i_mops->readdir(inode, dir, entry); } else #endif { /* The node is part of the root pseudo file system */ - ret = read_pseudodir(dir, (FAR struct dirent *)buffer); + ret = read_pseudodir(dir, entry); } /* ret < 0 is an error. Special case: ret = -ENOENT is end of file */ @@ -530,6 +534,16 @@ static ssize_t dir_read(FAR struct file *filep, FAR char *buffer, return ret; } + /* Programs skip an entry with d_ino 0 as a deleted one. Give it a hash + * of its path instead. + */ + + if (entry->d_ino == 0) + { + entry->d_ino = inode_hash(inode_hash(INODE_HASH_INIT, dir->fd_path), + entry->d_name); + } + filep->f_pos++; return sizeof(struct dirent); }
