subsys/fs: Make FAT FS write support optional

The commit adds FS_FATFS_READ_ONLY Kconfig option; the option, when
selected, excludes write supporting code within ELM FAT driver.

When write support to FAT FS volumes is not desired, this option may be
selected to slightly reduce code size.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
This commit is contained in:
Dominik Ermel 2020-10-07 14:21:28 +00:00 committed by Anas Nashif
commit e43ec70d4f
3 changed files with 68 additions and 22 deletions

View file

@ -226,7 +226,8 @@ int fs_close(struct fs_file_t *zfp);
* @param path Path to the file or directory to delete
*
* @retval 0 on success;
* @retval <0 a negative errno code on error.
* @retval -ENOTSUP when not implemented by underlying file system driver;
* @retval <0 an other negative errno code on error.
*/
int fs_unlink(const char *path);
@ -248,7 +249,8 @@ int fs_unlink(const char *path);
* @param to The destination path
*
* @retval 0 on success;
* @retval <0 a negative errno code on error.
* @retval -ENOTSUP when not implemented by underlying file system driver;
* @retval <0 an other negative errno code on error.
*/
int fs_rename(const char *from, const char *to);
@ -283,7 +285,8 @@ ssize_t fs_read(struct fs_file_t *zfp, void *ptr, size_t size);
* @param size Number of bytes to be written
*
* @retval >=0 a number of bytes written, on success;
* @retval <0 a negative errno code on error.
* @retval -ENOTSUP when not implemented by underlying file system driver;
* @retval <0 an other negative errno code on error.
*/
ssize_t fs_write(struct fs_file_t *zfp, const void *ptr, size_t size);
@ -337,7 +340,8 @@ off_t fs_tell(struct fs_file_t *zfp);
* @param length New size of the file in bytes
*
* @retval 0 on success;
* @retval <0 a negative errno code on error.
* @retval -ENOTSUP when not implemented by underlying file system driver;
* @retval <0 an other negative errno code on error.
*/
int fs_truncate(struct fs_file_t *zfp, off_t length);
@ -365,7 +369,8 @@ int fs_sync(struct fs_file_t *zfp);
* @param path Path to the directory to create
*
* @retval 0 on success;
* @retval <0 a negative errno code on error
* @retval -ENOTSUP when not implemented by underlying file system driver;
* @retval <0 an other negative errno code on error
*/
int fs_mkdir(const char *path);
@ -492,7 +497,8 @@ int fs_stat(const char *path, struct fs_dirent *entry);
* statistics
*
* @retval 0 on success;
* @retval <0 negative errno code on error.
* @retval -ENOTSUP when not implemented by underlying file system driver;
* @retval <0 an other negative errno code on error.
*/
int fs_statvfs(const char *path, struct fs_statvfs *stat);

View file

@ -14,6 +14,18 @@ if FAT_FILESYSTEM_ELM
menu "ELM FAT file system settings"
visible if FAT_FILESYSTEM_ELM
config FS_FATFS_READ_ONLY
bool "Read-only support for all volumes"
help
The option excludes write code from ELM FAT file system driver;
when selected, it no longer will be possible to write data on
the FAT FS.
If write support is not needed, enabling this flag will slightly
reduce application size.
This option translates to _FS_READONLY within ELM FAT file system
driver; it enables exclusion, from compilation, of write supporting
code.
config FS_FATFS_MKFS
bool
help

View file

@ -122,17 +122,23 @@ static int fatfs_close(struct fs_file_t *zfp)
static int fatfs_unlink(struct fs_mount_t *mountp, const char *path)
{
FRESULT res;
int res = -ENOTSUP;
#if !defined(CONFIG_FS_FATFS_READ_ONLY)
res = f_unlink(&path[1]);
return translate_error(res);
res = translate_error(res);
#endif
return res;
}
static int fatfs_rename(struct fs_mount_t *mountp, const char *from,
const char *to)
{
FRESULT res;
int res = -ENOTSUP;
#if !defined(CONFIG_FS_FATFS_READ_ONLY)
FILINFO fno;
/* Check if 'to' path exists; remove it if it does */
@ -144,7 +150,10 @@ static int fatfs_rename(struct fs_mount_t *mountp, const char *from,
}
res = f_rename(&from[1], &to[1]);
return translate_error(res);
res = translate_error(res);
#endif
return res;
}
static ssize_t fatfs_read(struct fs_file_t *zfp, void *ptr, size_t size)
@ -162,9 +171,12 @@ static ssize_t fatfs_read(struct fs_file_t *zfp, void *ptr, size_t size)
static ssize_t fatfs_write(struct fs_file_t *zfp, const void *ptr, size_t size)
{
FRESULT res = FR_OK;
int res = -ENOTSUP;
#if !defined(CONFIG_FS_FATFS_READ_ONLY)
unsigned int bw;
off_t pos = f_size((FIL *)zfp->filep);
res = FR_OK;
/* FA_APPEND flag means that file has been opened for append.
* The FAT FS write does not support the POSIX append semantics,
@ -180,10 +192,13 @@ static ssize_t fatfs_write(struct fs_file_t *zfp, const void *ptr, size_t size)
}
if (res != FR_OK) {
return translate_error(res);
res = translate_error(res);
} else {
res = bw;
}
#endif
return bw;
return res;
}
static int fatfs_seek(struct fs_file_t *zfp, off_t offset, int whence)
@ -221,7 +236,9 @@ static off_t fatfs_tell(struct fs_file_t *zfp)
static int fatfs_truncate(struct fs_file_t *zfp, off_t length)
{
FRESULT res = FR_OK;
int res = -ENOTSUP;
#if !defined(CONFIG_FS_FATFS_READ_ONLY)
off_t cur_length = f_size((FIL *)zfp->filep);
/* f_lseek expands file if new position is larger than file size */
@ -262,25 +279,33 @@ static int fatfs_truncate(struct fs_file_t *zfp, off_t length)
}
}
return translate_error(res);
res = translate_error(res);
#endif
return res;
}
static int fatfs_sync(struct fs_file_t *zfp)
{
FRESULT res = FR_OK;
int res = -ENOTSUP;
#if !defined(CONFIG_FS_FATFS_READ_ONLY)
res = f_sync(zfp->filep);
return translate_error(res);
res = translate_error(res);
#endif
return res;
}
static int fatfs_mkdir(struct fs_mount_t *mountp, const char *path)
{
FRESULT res;
int res = -ENOTSUP;
#if !defined(CONFIG_FS_FATFS_READ_ONLY)
res = f_mkdir(&path[1]);
res = translate_error(res);
#endif
return translate_error(res);
return res;
}
static int fatfs_opendir(struct fs_dir_t *zdp, const char *path)
@ -355,8 +380,9 @@ static int fatfs_stat(struct fs_mount_t *mountp,
static int fatfs_statvfs(struct fs_mount_t *mountp,
const char *path, struct fs_statvfs *stat)
{
int res = -ENOTSUP;
#if !defined(CONFIG_FS_FATFS_READ_ONLY)
FATFS *fs;
FRESULT res;
res = f_getfree(&mountp->mnt_point[1], &stat->f_bfree, &fs);
if (res != FR_OK) {
@ -371,7 +397,9 @@ static int fatfs_statvfs(struct fs_mount_t *mountp,
stat->f_frsize = fs->csize * stat->f_bsize;
stat->f_blocks = (fs->n_fatent - 2);
return translate_error(res);
res = translate_error(res);
#endif
return res;
}
static int fatfs_mount(struct fs_mount_t *mountp)