@@ -80,6 +80,7 @@ struct littlefs_mountpt_s
8080 struct mtd_geometry_s geo ;
8181 struct lfs_config cfg ;
8282 struct lfs lfs ;
83+ bool readonly ;
8384};
8485
8586/* NuttX specific file attributes.
@@ -375,6 +376,15 @@ static int littlefs_open(FAR struct file *filep, FAR const char *relpath,
375376
376377 relpath = littlefs_convert_path (relpath );
377378 oflags = littlefs_convert_oflags (oflags );
379+ if (fs -> readonly )
380+ {
381+ if (oflags != LFS_O_RDONLY )
382+ {
383+ ret = - EROFS ;
384+ goto errout ;
385+ }
386+ }
387+
378388 ret = littlefs_convert_result (lfs_file_open (& fs -> lfs , & priv -> file ,
379389 relpath , oflags ));
380390 if (ret < 0 )
@@ -1184,6 +1194,11 @@ static int littlefs_write_block(FAR const struct lfs_config *c,
11841194 FAR struct inode * drv = fs -> drv ;
11851195 int ret ;
11861196
1197+ if (fs -> readonly )
1198+ {
1199+ return - EROFS ;
1200+ }
1201+
11871202 block = (block * c -> block_size + off ) / geo -> blocksize ;
11881203 size = size / geo -> blocksize ;
11891204
@@ -1210,6 +1225,11 @@ static int littlefs_erase_block(FAR const struct lfs_config *c,
12101225 FAR struct inode * drv = fs -> drv ;
12111226 int ret = OK ;
12121227
1228+ if (fs -> readonly )
1229+ {
1230+ return - EROFS ;
1231+ }
1232+
12131233 if (INODE_IS_MTD (drv ))
12141234 {
12151235 FAR struct mtd_geometry_s * geo = & fs -> geo ;
@@ -1232,6 +1252,11 @@ static int littlefs_sync_block(FAR const struct lfs_config *c)
12321252 FAR struct inode * drv = fs -> drv ;
12331253 int ret ;
12341254
1255+ if (fs -> readonly )
1256+ {
1257+ return - EROFS ;
1258+ }
1259+
12351260 if (INODE_IS_MTD (drv ))
12361261 {
12371262 ret = MTD_IOCTL (drv -> u .i_mtd , BIOC_FLUSH , 0 );
@@ -1385,6 +1410,11 @@ static int littlefs_bind(FAR struct inode *driver, FAR const void *data,
13851410 }
13861411 }
13871412
1413+ if (data && !strcmp (data , "ro" ))
1414+ {
1415+ fs -> readonly = true;
1416+ }
1417+
13881418 ret = littlefs_convert_result (lfs_mount (& fs -> lfs , & fs -> cfg ));
13891419 if (ret < 0 )
13901420 {
0 commit comments