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 61a1196611d fs/fat: add compile-time read-only configuration
61a1196611d is described below

commit 61a1196611dd471dc8c3fcd3f7f94471d0034eff
Author: Arnav Sharma <[email protected]>
AuthorDate: Wed Nov 11 14:11:56 2026 +0530

    fs/fat: add compile-time read-only configuration
    
    Add CONFIG_FS_FAT_READONLY to eliminate write-side FAT code in read-only
    builds.
    
    When enabled, g_fat_operations uses small -EROFS stubs for unlink, mkdir,
    rmdir and rename, keeps sync as a no-op, and leaves write/truncate NULL so
    the corresponding functions and their write-only helper call graph are
    eligible for linker garbage collection. This follows the existing ROMFS
    read-only filesystem pattern.
    
    fat_open() rejects write-capable and mutating open flags with -EROFS.
    fat_mount() requests read-only media access in read-only mode.
    fat_attrib() rejects attribute changes with -EROFS when 
CONFIG_FS_FAT_READONLY
    is set.
    Read, seek, stat, fstat, directory enumeration and mount/unmount paths
    remain available.
    
    Assisted-by: Claude:Opus 5
    Signed-off-by: Arnav Sharma <[email protected]>
---
 fs/fat/Kconfig          | 11 ++++++
 fs/fat/fs_fat32.c       | 90 +++++++++++++++++++++++++++++++++++++++++++++++++
 fs/fat/fs_fat32attrib.c |  9 +++++
 3 files changed, 110 insertions(+)

diff --git a/fs/fat/Kconfig b/fs/fat/Kconfig
index f9b73a4d3e9..27b77da635d 100644
--- a/fs/fat/Kconfig
+++ b/fs/fat/Kconfig
@@ -225,4 +225,15 @@ config FAT_DIRECT_RETRY
                        *  CONFIG_DIRECT_RETRY cannot be selected with 
CONFIG_FORCE_INDIRECT
                        ** CONFIG_DIRECT_RETRY is automatically selected with 
CONFIG_DMA_MEMORY
 
+config FS_FAT_READONLY
+       bool "FAT read-only filesystem"
+       default n
+       depends on FS_FAT
+       ---help---
+               Compile the FAT filesystem in read-only mode.  Write, truncate,
+               unlink, mkdir, rmdir and rename operations are disabled at build
+               time and the corresponding implementation code is eliminated by
+               linker garbage collection.  Read, seek, stat and directory
+               enumeration remain fully functional.
+
 endif # FAT
diff --git a/fs/fat/fs_fat32.c b/fs/fat/fs_fat32.c
index 437b910b636..d60ac8f90d5 100644
--- a/fs/fat/fs_fat32.c
+++ b/fs/fat/fs_fat32.c
@@ -69,17 +69,23 @@ static int     fat_open(FAR struct file *filep, FAR const 
char *relpath,
 static int     fat_close(FAR struct file *filep);
 static ssize_t fat_read(FAR struct file *filep, FAR char *buffer,
                  size_t buflen);
+#ifndef CONFIG_FS_FAT_READONLY
 static ssize_t fat_write(FAR struct file *filep, FAR const char *buffer,
                  size_t buflen);
+#endif
 static off_t   fat_seek(FAR struct file *filep, off_t offset, int whence);
 static int     fat_ioctl(FAR struct file *filep, int cmd,
                  unsigned long arg);
 
+#ifndef CONFIG_FS_FAT_READONLY
 static int     fat_sync(FAR struct file *filep);
+#endif
 static int     fat_dup(FAR const struct file *oldp, FAR struct file *newp);
 static int     fat_fstat(FAR const struct file *filep,
                  FAR struct stat *buf);
+#ifndef CONFIG_FS_FAT_READONLY
 static int     fat_truncate(FAR struct file *filep, off_t length);
+#endif
 
 static FAR struct fat_shared_s *
                fat_shared_find(FAR struct fat_mountpt_s *fs,
@@ -90,8 +96,10 @@ static void    fat_shared_reset_cursors(FAR struct 
fat_mountpt_s *fs,
                  FAR struct fat_shared_s *shared);
 static int     fat_shared_release(FAR struct fat_mountpt_s *fs,
                  FAR struct fat_shared_s *shared);
+#ifndef CONFIG_FS_FAT_READONLY
 static int     fat_shrinkshared(FAR struct fat_mountpt_s *fs,
                  FAR struct fat_file_s *ff, off_t length);
+#endif
 
 static int     fat_opendir(FAR struct inode *mountpt,
                  FAR const char *relpath, FAR struct fs_dirent_s **dir);
@@ -110,6 +118,7 @@ static int     fat_unbind(FAR void *handle,
 static int     fat_statfs(FAR struct inode *mountpt,
                  FAR struct statfs *buf);
 
+#ifndef CONFIG_FS_FAT_READONLY
 static int     fat_unlink(FAR struct inode *mountpt,
                  FAR const char *relpath);
 static int     fat_mkdir(FAR struct inode *mountpt, FAR const char *relpath,
@@ -117,6 +126,7 @@ static int     fat_mkdir(FAR struct inode *mountpt, FAR 
const char *relpath,
 static int     fat_rmdir(FAR struct inode *mountpt, FAR const char *relpath);
 static int     fat_rename(FAR struct inode *mountpt,
                  FAR const char *oldrelpath, FAR const char *newrelpath);
+#endif
 static int     fat_stat_common(FAR struct fat_mountpt_s *fs,
                  FAR uint8_t *direntry,
                  FAR const struct fat_shared_s *shared,
@@ -130,6 +140,46 @@ static int     fat_stat_root(FAR struct fat_mountpt_s *fs,
 static int     fat_stat(struct inode *mountpt, const char *relpath,
                  FAR struct stat *buf);
 
+#ifdef CONFIG_FS_FAT_READONLY
+static int fat_sync(FAR struct file *filep)
+{
+  (void)filep;
+  return OK;
+}
+
+static int fat_unlink(FAR struct inode *mountpt, FAR const char *relpath)
+{
+  (void)mountpt;
+  (void)relpath;
+  return -EROFS;
+}
+
+static int fat_mkdir(FAR struct inode *mountpt, FAR const char *relpath,
+                     mode_t mode)
+{
+  (void)mountpt;
+  (void)relpath;
+  (void)mode;
+  return -EROFS;
+}
+
+static int fat_rmdir(FAR struct inode *mountpt, FAR const char *relpath)
+{
+  (void)mountpt;
+  (void)relpath;
+  return -EROFS;
+}
+
+static int fat_rename(FAR struct inode *mountpt, FAR const char *oldrelpath,
+                      FAR const char *newrelpath)
+{
+  (void)mountpt;
+  (void)oldrelpath;
+  (void)newrelpath;
+  return -EROFS;
+}
+#endif
+
 /****************************************************************************
  * Public Data
  ****************************************************************************/
@@ -144,11 +194,19 @@ const struct mountpt_operations g_fat_operations =
   fat_open,          /* open */
   fat_close,         /* close */
   fat_read,          /* read */
+#ifdef CONFIG_FS_FAT_READONLY
+  NULL,              /* write */
+#else
   fat_write,         /* write */
+#endif
   fat_seek,          /* seek */
   fat_ioctl,         /* ioctl */
   NULL,              /* mmap */
+#ifdef CONFIG_FS_FAT_READONLY
+  NULL,              /* truncate */
+#else
   fat_truncate,      /* truncate */
+#endif
   NULL,              /* poll */
   NULL,              /* readv */
   NULL,              /* writev */
@@ -358,6 +416,7 @@ static int fat_shared_release(FAR struct fat_mountpt_s *fs,
  *
  ****************************************************************************/
 
+#ifndef CONFIG_FS_FAT_READONLY
 static int fat_shrinkshared(FAR struct fat_mountpt_s *fs,
                             FAR struct fat_file_s *ff, off_t length)
 {
@@ -434,6 +493,7 @@ static int fat_shrinkshared(FAR struct fat_mountpt_s *fs,
 
   return fat_updatefsinfo(fs);
 }
+#endif /* !CONFIG_FS_FAT_READONLY */
 
 /****************************************************************************
  * Name: fat_open
@@ -454,6 +514,18 @@ static int fat_open(FAR struct file *filep, FAR const char 
*relpath,
 
   DEBUGASSERT(filep->f_priv == NULL);
 
+#ifdef CONFIG_FS_FAT_READONLY
+  /* In read-only mode, reject any attempt to open a file for writing or
+   * to create/truncate a file.
+   */
+
+  if ((oflags & O_ACCMODE) != O_RDONLY ||
+      (oflags & (O_CREAT | O_TRUNC | O_APPEND)) != 0)
+    {
+      return -EROFS;
+    }
+#endif
+
   /* Get the mountpoint inode reference from the file structure and the
    * mountpoint private data from the inode structure
    */
@@ -1375,6 +1447,7 @@ errout_with_lock:
  * Name: fat_write
  ****************************************************************************/
 
+#ifndef CONFIG_FS_FAT_READONLY
 static ssize_t fat_write(FAR struct file *filep, FAR const char *buffer,
                          size_t buflen)
 {
@@ -1634,6 +1707,7 @@ errout_with_lock:
 
   return ret;
 }
+#endif /* !CONFIG_FS_FAT_READONLY */
 
 /****************************************************************************
  * Name: fat_seek
@@ -2226,6 +2300,7 @@ static int fat_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
  *
  ****************************************************************************/
 
+#ifndef CONFIG_FS_FAT_READONLY
 static int fat_sync(FAR struct file *filep)
 {
   FAR struct inode *inode;
@@ -2359,6 +2434,7 @@ errout_with_lock:
   nxmutex_unlock(&fs->fs_lock);
   return ret;
 }
+#endif /* !CONFIG_FS_FAT_READONLY */
 
 /****************************************************************************
  * Name: fat_dup
@@ -2718,6 +2794,7 @@ errout_with_lock:
  *
  ****************************************************************************/
 
+#ifndef CONFIG_FS_FAT_READONLY
 static int fat_truncate(FAR struct file *filep, off_t length)
 {
   FAR struct inode *inode;
@@ -2963,6 +3040,7 @@ errout_with_lock:
   nxmutex_unlock(&fs->fs_lock);
   return ret;
 }
+#endif /* !CONFIG_FS_FAT_READONLY */
 
 /****************************************************************************
  * Name: fat_readdir
@@ -3252,7 +3330,11 @@ static int fat_bind(FAR struct inode *blkdriver, FAR 
const void *data,
    * by this block driver.
    */
 
+#ifdef CONFIG_FS_FAT_READONLY
+  ret = fat_mount(fs, false);
+#else
   ret = fat_mount(fs, true);
+#endif
   if (ret != 0)
     {
       nxmutex_destroy(&fs->fs_lock);
@@ -3436,6 +3518,7 @@ errout_with_lock:
  *
  ****************************************************************************/
 
+#ifndef CONFIG_FS_FAT_READONLY
 static int fat_unlink(FAR struct inode *mountpt, FAR const char *relpath)
 {
   FAR struct fat_mountpt_s *fs;
@@ -3471,6 +3554,7 @@ static int fat_unlink(FAR struct inode *mountpt, FAR 
const char *relpath)
   nxmutex_unlock(&fs->fs_lock);
   return ret;
 }
+#endif /* !CONFIG_FS_FAT_READONLY */
 
 /****************************************************************************
  * Name: fat_mkdir
@@ -3479,6 +3563,7 @@ static int fat_unlink(FAR struct inode *mountpt, FAR 
const char *relpath)
  *
  ****************************************************************************/
 
+#ifndef CONFIG_FS_FAT_READONLY
 static int fat_mkdir(FAR struct inode *mountpt, FAR const char *relpath,
                      mode_t mode)
 {
@@ -3697,6 +3782,7 @@ errout_with_lock:
   nxmutex_unlock(&fs->fs_lock);
   return ret;
 }
+#endif /* !CONFIG_FS_FAT_READONLY */
 
 /****************************************************************************
  * Name: fat_rmdir
@@ -3705,6 +3791,7 @@ errout_with_lock:
  *
  ****************************************************************************/
 
+#ifndef CONFIG_FS_FAT_READONLY
 static int fat_rmdir(FAR struct inode *mountpt, FAR const char *relpath)
 {
   FAR struct fat_mountpt_s *fs;
@@ -3742,6 +3829,7 @@ static int fat_rmdir(FAR struct inode *mountpt, FAR const 
char *relpath)
   nxmutex_unlock(&fs->fs_lock);
   return ret;
 }
+#endif /* !CONFIG_FS_FAT_READONLY */
 
 /****************************************************************************
  * Name: fat_rename
@@ -3750,6 +3838,7 @@ static int fat_rmdir(FAR struct inode *mountpt, FAR const 
char *relpath)
  *
  ****************************************************************************/
 
+#ifndef CONFIG_FS_FAT_READONLY
 static int fat_rename(FAR struct inode *mountpt, FAR const char *oldrelpath,
                       FAR const char *newrelpath)
 {
@@ -3932,6 +4021,7 @@ errout_with_lock:
   nxmutex_unlock(&fs->fs_lock);
   return ret;
 }
+#endif /* !CONFIG_FS_FAT_READONLY */
 
 /****************************************************************************
  * Name: fat_stat_common
diff --git a/fs/fat/fs_fat32attrib.c b/fs/fat/fs_fat32attrib.c
index fb2dbcc0b4d..a096068af06 100644
--- a/fs/fat/fs_fat32attrib.c
+++ b/fs/fat/fs_fat32attrib.c
@@ -57,6 +57,15 @@ static int fat_attrib(const char *path, fat_attrib_t 
*retattrib,
   uint8_t newattributes;
   int ret;
 
+#ifdef CONFIG_FS_FAT_READONLY
+  /* In read-only mode, reject any request that changes attributes. */
+
+  if (setbits != 0 || clearbits != 0)
+    {
+      return -EROFS;
+    }
+#endif
+
   /* Find the inode for this file */
 
   ret = inode_search_setup(&desc, path, false);

Reply via email to