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 27d356c10f0 fs/driver: unify proxy temp-name generation
27d356c10f0 is described below
commit 27d356c10f0b3cf21a61694be5034137e9629822
Author: jingfei <[email protected]>
AuthorDate: Fri Nov 20 18:58:44 2026 +0800
fs/driver: unify proxy temp-name generation
block_proxy() and mtd_proxy() each carried a near-identical
unique_chardev()/unique_blkdev() helper plus its own devno counter
and lock, differing only in the /dev name prefix. Fold both into a
single unique_devname(prefix, ...) in fs_uniquedevname.c, built when
either proxy is.
No functional change: names stay /dev/tmpc* and /dev/tmpb*.
Signed-off-by: jingfei <[email protected]>
---
fs/driver/CMakeLists.txt | 5 ++
fs/driver/Make.defs | 4 +
fs/driver/driver.h | 16 ++++
fs/driver/fs_blockproxy.c | 74 +----------------
fs/driver/fs_mtdproxy.c | 74 +----------------
fs/driver/{fs_mtdproxy.c => fs_uniquedevname.c} | 102 +++---------------------
6 files changed, 36 insertions(+), 239 deletions(-)
diff --git a/fs/driver/CMakeLists.txt b/fs/driver/CMakeLists.txt
index 1cb92fba9b3..51fec2bec3a 100644
--- a/fs/driver/CMakeLists.txt
+++ b/fs/driver/CMakeLists.txt
@@ -57,6 +57,11 @@ if(NOT CONFIG_DISABLE_MOUNTPOINT)
list(APPEND SRCS fs_blockproxy.c)
endif()
endif()
+
+ if(CONFIG_FS_MTD_PROXY OR (CONFIG_BCH AND NOT
+
CONFIG_DISABLE_PSEUDOFS_OPERATIONS))
+ list(APPEND SRCS fs_uniquedevname.c)
+ endif()
endif()
target_sources(fs PRIVATE ${SRCS})
diff --git a/fs/driver/Make.defs b/fs/driver/Make.defs
index 450ac113fa4..cb5fd551e55 100644
--- a/fs/driver/Make.defs
+++ b/fs/driver/Make.defs
@@ -46,6 +46,10 @@ ifneq ($(CONFIG_DISABLE_PSEUDOFS_OPERATIONS),y)
CSRCS += fs_blockproxy.c
endif
endif # CONFIG_BCH
+
+ifneq ($(filter fs_blockproxy.c fs_mtdproxy.c,$(CSRCS)),)
+CSRCS += fs_uniquedevname.c
+endif
endif # CONFIG_DISABLE_MOUNTPOINT
# Include driver build support
diff --git a/fs/driver/driver.h b/fs/driver/driver.h
index 84bfa24ebee..3671b4b7976 100644
--- a/fs/driver/driver.h
+++ b/fs/driver/driver.h
@@ -75,6 +75,22 @@ int register_partition_with_inode(FAR const char *partition,
off_t firstsector, off_t nsectors);
#endif
+/****************************************************************************
+ * Name: unique_devname
+ *
+ * Description:
+ * Build a temporary device name "/dev/tmp<prefix><6 hex digits>" that
+ * is not in use: prefix 'c' for block_proxy(), 'b' for mtd_proxy().
+ *
+ * Returned Value:
+ * Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_MOUNTPOINT
+int unique_devname(char prefix, FAR char *devbuf, size_t len);
+#endif
+
/****************************************************************************
* Name: block_proxy
*
diff --git a/fs/driver/fs_blockproxy.c b/fs/driver/fs_blockproxy.c
index e72bf4a8b50..c760d5e0763 100644
--- a/fs/driver/fs_blockproxy.c
+++ b/fs/driver/fs_blockproxy.c
@@ -27,7 +27,6 @@
#include <nuttx/config.h>
#include <sys/types.h>
-#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
@@ -41,83 +40,12 @@
#include <nuttx/lib/lib.h>
#include <nuttx/drivers/drivers.h>
#include <nuttx/fs/fs.h>
-#include <nuttx/mutex.h>
#include "driver.h"
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && \
!defined(CONFIG_DISABLE_PSEUDOFS_OPERATIONS)
-/****************************************************************************
- * Private Data
- ****************************************************************************/
-
-static uint32_t g_devno;
-static mutex_t g_devno_lock = NXMUTEX_INITIALIZER;
-
-/****************************************************************************
- * Private Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: unique_chardev
- *
- * Description:
- * Create a unique temporary device name in the /dev/ directory of the
- * pseudo-file system. We cannot use mktemp for this because it will
- * attempt to open() the file.
- *
- * Input Parameters:
- * devbuf - Buffer to store the generated device name
- * len - Length of the buffer
- *
- * Returned Value:
- * Zero (OK) on success; a negated errno value on failure.
- *
- ****************************************************************************/
-
-static int unique_chardev(FAR char *devbuf, size_t len)
-{
- struct stat statbuf;
- uint32_t devno;
- int ret;
-
- /* Loop until we get a unique device name */
-
- for (; ; )
- {
- /* Get the mutex protecting the path number */
-
- ret = nxmutex_lock(&g_devno_lock);
- if (ret < 0)
- {
- ferr("ERROR: nxmutex_lock failed: %d\n", ret);
- return ret;
- }
-
- /* Get the next device number and release the semaphore */
-
- devno = ++g_devno;
- nxmutex_unlock(&g_devno_lock);
-
- /* Construct the full device number */
-
- devno &= 0xffffff;
- snprintf(devbuf, len, "/dev/tmpc%06lx", (unsigned long)devno);
-
- /* Make sure that file name is not in use */
-
- ret = nx_stat(devbuf, &statbuf, 1);
- if (ret < 0)
- {
- DEBUGASSERT(ret == -ENOENT);
- return OK;
- }
-
- /* It is in use, try again */
- }
-}
-
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -151,7 +79,7 @@ int block_proxy(FAR struct file *filep, FAR const char
*blkdev, int oflags)
/* Create a unique temporary file name for the character device */
- ret = unique_chardev(chardev, sizeof(chardev));
+ ret = unique_devname('c', chardev, sizeof(chardev));
if (ret != OK)
{
ferr("ERROR: Failed to create temporary device name\n");
diff --git a/fs/driver/fs_mtdproxy.c b/fs/driver/fs_mtdproxy.c
index a6d55f520d7..6bb763bd0de 100644
--- a/fs/driver/fs_mtdproxy.c
+++ b/fs/driver/fs_mtdproxy.c
@@ -27,7 +27,6 @@
#include <nuttx/config.h>
#include <sys/types.h>
-#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
@@ -38,80 +37,9 @@
#include <nuttx/fs/fs.h>
#include <nuttx/lib/lib.h>
#include <nuttx/mtd/mtd.h>
-#include <nuttx/mutex.h>
#include "driver/driver.h"
-/****************************************************************************
- * Private Data
- ****************************************************************************/
-
-static uint32_t g_devno;
-static mutex_t g_devno_lock = NXMUTEX_INITIALIZER;
-
-/****************************************************************************
- * Private Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: unique_blkdev
- *
- * Description:
- * Create a unique temporary device name in the /dev/ directory of the
- * pseudo-file system. We cannot use mktemp for this because it will
- * attempt to open() the file.
- *
- * Input Parameters:
- * devbuf - Buffer to store the generated device name
- * len - Length of the buffer
- *
- * Returned Value:
- * Zero (OK) on success; a negated errno value on failure.
- *
- ****************************************************************************/
-
-static int unique_blkdev(FAR char *devbuf, size_t len)
-{
- struct stat statbuf;
- uint32_t devno;
- int ret;
-
- /* Loop until we get a unique device name */
-
- for (; ; )
- {
- /* Get the mutex protecting the path number */
-
- ret = nxmutex_lock(&g_devno_lock);
- if (ret < 0)
- {
- ferr("ERROR: nxmutex_lock failed: %d\n", ret);
- return ret;
- }
-
- /* Get the next device number and release the semaphore */
-
- devno = ++g_devno;
- nxmutex_unlock(&g_devno_lock);
-
- /* Construct the full device number */
-
- devno &= 0xffffff;
- snprintf(devbuf, len, "/dev/tmpb%06lx", (unsigned long)devno);
-
- /* Make sure that file name is not in use */
-
- ret = nx_stat(devbuf, &statbuf, 1);
- if (ret < 0)
- {
- DEBUGASSERT(ret == -ENOENT);
- return OK;
- }
-
- /* It is in use, try again */
- }
-}
-
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -146,7 +74,7 @@ int mtd_proxy(FAR const char *mtddev, int mountflags,
/* Create a unique temporary file name for the block device */
- ret = unique_blkdev(blkdev, sizeof(blkdev));
+ ret = unique_devname('b', blkdev, sizeof(blkdev));
if (ret != OK)
{
ferr("ERROR: Failed to create temporary device name\n");
diff --git a/fs/driver/fs_mtdproxy.c b/fs/driver/fs_uniquedevname.c
similarity index 53%
copy from fs/driver/fs_mtdproxy.c
copy to fs/driver/fs_uniquedevname.c
index a6d55f520d7..acbf3fe6ca2 100644
--- a/fs/driver/fs_mtdproxy.c
+++ b/fs/driver/fs_uniquedevname.c
@@ -1,5 +1,5 @@
/****************************************************************************
- * fs/driver/fs_mtdproxy.c
+ * fs/driver/fs_uniquedevname.c
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -26,18 +26,15 @@
#include <nuttx/config.h>
-#include <sys/types.h>
#include <sys/stat.h>
+#include <stdint.h>
#include <stdio.h>
-#include <string.h>
#include <errno.h>
#include <assert.h>
#include <nuttx/debug.h>
#include <nuttx/fs/fs.h>
-#include <nuttx/lib/lib.h>
-#include <nuttx/mtd/mtd.h>
#include <nuttx/mutex.h>
#include "driver/driver.h"
@@ -50,11 +47,11 @@ static uint32_t g_devno;
static mutex_t g_devno_lock = NXMUTEX_INITIALIZER;
/****************************************************************************
- * Private Functions
+ * Public Functions
****************************************************************************/
/****************************************************************************
- * Name: unique_blkdev
+ * Name: unique_devname
*
* Description:
* Create a unique temporary device name in the /dev/ directory of the
@@ -62,6 +59,7 @@ static mutex_t g_devno_lock = NXMUTEX_INITIALIZER;
* attempt to open() the file.
*
* Input Parameters:
+ * prefix - 'c' for a character device, 'b' for a block device
* devbuf - Buffer to store the generated device name
* len - Length of the buffer
*
@@ -70,7 +68,7 @@ static mutex_t g_devno_lock = NXMUTEX_INITIALIZER;
*
****************************************************************************/
-static int unique_blkdev(FAR char *devbuf, size_t len)
+int unique_devname(char prefix, FAR char *devbuf, size_t len)
{
struct stat statbuf;
uint32_t devno;
@@ -89,7 +87,7 @@ static int unique_blkdev(FAR char *devbuf, size_t len)
return ret;
}
- /* Get the next device number and release the semaphore */
+ /* Get the next device number and release the mutex */
devno = ++g_devno;
nxmutex_unlock(&g_devno_lock);
@@ -97,7 +95,8 @@ static int unique_blkdev(FAR char *devbuf, size_t len)
/* Construct the full device number */
devno &= 0xffffff;
- snprintf(devbuf, len, "/dev/tmpb%06lx", (unsigned long)devno);
+ snprintf(devbuf, len, "/dev/tmp%c%06lx", prefix,
+ (unsigned long)devno);
/* Make sure that file name is not in use */
@@ -111,86 +110,3 @@ static int unique_blkdev(FAR char *devbuf, size_t len)
/* It is in use, try again */
}
}
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: mtd_proxy
- *
- * Description:
- * Create a temporary block driver using drivers/mtd/ftl to mediate block
- * oriented accessed to the mtd driver.
- *
- * Input Parameters:
- * mtddev - The path to the mtd driver
- * mountflags - if MS_RDONLY is not set, then driver must support write
- * operations (see include/sys/mount.h)
- * ppinode - address of the location to return the inode reference
- *
- * Returned Value:
- * If zero, non-zero inode pointer is returned on success. This
- * is the inode pointer of the nameless block driver that mediates
- * accesses to the mtd driver. A negated errno value is returned on
- * any failure.
- *
- ****************************************************************************/
-
-int mtd_proxy(FAR const char *mtddev, int mountflags,
- FAR struct inode **ppinode)
-{
- char blkdev[16];
- FAR struct inode *mtd;
- int ret;
-
- /* Create a unique temporary file name for the block device */
-
- ret = unique_blkdev(blkdev, sizeof(blkdev));
- if (ret != OK)
- {
- ferr("ERROR: Failed to create temporary device name\n");
- return ret;
- }
-
- /* Wrap the mtd driver with an instance of the ftl driver */
-
- ret = find_mtddriver(mtddev, &mtd);
- if (ret < 0)
- {
- ferr("ERROR: Failed to find %s mtd driver\n", mtddev);
- return ret;
- }
-
- ret = ftl_initialize_by_path(blkdev, mtd->u.i_mtd, mountflags);
- inode_release(mtd);
- if (ret < 0)
- {
- ferr("ERROR: ftl_initialize_by_path(%s, %s) failed: %d\n",
- mtddev, blkdev, ret);
- return ret;
- }
-
- /* Open the newly created block driver */
-
- ret = open_blockdriver(blkdev, mountflags, ppinode);
- if (ret < 0)
- {
- ferr("ERROR: Failed to open %s: %d\n", blkdev, ret);
- nx_unlink(blkdev);
- return ret;
- }
-
- /* Unlink and free the block device name. The driver instance will
- * persist, provided that CONFIG_DISABLE_PSEUDOFS_OPERATIONS=y (otherwise,
- * we have a problem here!)
- */
-
- ret = nx_unlink(blkdev);
- if (ret < 0)
- {
- ferr("ERROR: Failed to unlink %s: %d\n", blkdev, ret);
- }
-
- return ret;
-}