You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ar...@apache.org on 2022/09/20 08:21:14 UTC

[incubator-nuttx] 03/03: libc/ftok: Map token to the root pseduo file system directory

This is an automated email from the ASF dual-hosted git repository.

archer pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 9afb6540dca26cd4c734cff91e72c59a7721cf2a
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Fri Sep 16 14:53:46 2022 +0800

    libc/ftok: Map token to the root pseduo file system directory
    
    to ensure the generated key is unique from each other since only
    root pseduo file system really support st_ino field after:
    commit d35fbf534d51f7ef72382d9666a1f19e07c6f00f
    Author: Xiang Xiao <xi...@xiaomi.com>
    Date:   Fri Sep 16 14:24:55 2022 +0800
    
        fs: Allocate unique serial number for the root pseduo file system node
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 libs/libc/misc/Kconfig    |  6 ++++++
 libs/libc/misc/lib_ftok.c | 14 ++++++++++++--
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/libs/libc/misc/Kconfig b/libs/libc/misc/Kconfig
index 4a7635e533..fc7de51b7d 100644
--- a/libs/libc/misc/Kconfig
+++ b/libs/libc/misc/Kconfig
@@ -62,6 +62,12 @@ config LIBC_ENVPATH
 		Use the contents of the common environment variable to locate executable
 		or library files.  Default: n
 
+config LIBC_FTOK_VFS_PATH
+	string "Relative path to ftok storage"
+	default "/var/ftok"
+	---help---
+		The relative path to where ftok will exist in the root namespace.
+
 config LIBC_MEM_FD_VFS_PATH
 	string "Relative path to memfd storage"
 	default "memfd"
diff --git a/libs/libc/misc/lib_ftok.c b/libs/libc/misc/lib_ftok.c
index 6d38ed62b1..faaddc2c34 100644
--- a/libs/libc/misc/lib_ftok.c
+++ b/libs/libc/misc/lib_ftok.c
@@ -27,6 +27,8 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/ipc.h>
+#include <errno.h>
+#include <string.h>
 
 /****************************************************************************
  * Public Functions
@@ -56,11 +58,19 @@
 
 key_t ftok(FAR const char *pathname, int proj_id)
 {
+  char fullpath[PATH_MAX] = CONFIG_LIBC_FTOK_VFS_PATH "/";
   struct stat st;
 
-  if (stat(pathname, &st) < 0)
+  strlcat(fullpath, pathname, sizeof(fullpath));
+  if (stat(fullpath, &st) < 0 && get_errno() == ENOENT)
     {
-      return (key_t)-1;
+      /* Directory not exist, let's create one for caller */
+
+      mkdir(fullpath, S_IRWXU);
+      if (stat(fullpath, &st) < 0)
+        {
+          return (key_t)-1;
+        }
     }
 
   return ((key_t)proj_id << 24 |