apps/fsutils/mksmartfs: Add a check to see if the SmartFS is already formatted. apps/nshlib: Add a force flag (-f) to mksmartfs. SmartFS will be formatted only if (1) the FLASH does not already hold a SmartFS, or (2) the force flag is set

This commit is contained in:
Xiang Xiao 2018-08-23 07:14:30 -06:00 committed by Gregory Nutt
parent feb1229dc3
commit 8116d10da3
3 changed files with 85 additions and 2 deletions

View File

@ -74,6 +74,61 @@
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
/****************************************************************************
* Name: issmartfs
*
* Description:
* Check a SMART (Sector Mapped Allocation for Really Tiny) Flash file
* system image on the specified block device (must be a SMART device).
*
* Inputs:
* pathname - the full path to a registered block driver
*
* Return:
* Zero (OK) on success; -1 (ERROR) on failure with errno set appropriately:
*
* EINVAL - NULL block driver string
* ENOENT - 'pathname' does not refer to anything in the filesystem.
* ENOTBLK - 'pathname' does not refer to a block driver
* EFTYPE - the block driver hasn't been formated yet
*
****************************************************************************/
int issmartfs(FAR const char *pathname)
{
struct smart_format_s fmt;
int ret, fd;
/* Find the inode of the block driver identified by 'source' */
fd = open(pathname, O_RDONLY);
if (fd < 0)
{
return ERROR;
}
/* Get the format information so we know the block have been formatted */
ret = ioctl(fd, BIOC_GETFORMAT, (unsigned long)&fmt);
if (ret < 0)
{
goto out;
}
if (!(fmt.flags & SMART_FMT_ISFORMATTED))
{
set_errno(EFTYPE);
ret = ERROR;
goto out;
}
out:
/* Close the driver */
close(fd);
return ret;
}
/**************************************************************************** /****************************************************************************
* Name: mksmartfs * Name: mksmartfs
* *

View File

@ -56,6 +56,28 @@ extern "C"
#define EXTERN extern #define EXTERN extern
#endif #endif
/****************************************************************************
* Name: issmartfs
*
* Description:
* Check a SMART (Sector Mapped Allocation for Really Tiny) Flash file
* system image on the specified block device (must be a SMART device).
*
* Inputs:
* pathname - the full path to a registered block driver
*
* Return:
* Zero (OK) on success; -1 (ERROR) on failure with errno set appropriately:
*
* EINVAL - NULL block driver string
* ENOENT - 'pathname' does not refer to anything in the filesystem.
* ENOTBLK - 'pathname' does not refer to a block driver
* EFTYPE - the block driver hasn't been formated yet
*
****************************************************************************/
int issmartfs(FAR const char *pathname);
/**************************************************************************** /****************************************************************************
* Name: mksmartfs * Name: mksmartfs
* *

View File

@ -1449,8 +1449,9 @@ errout_with_fmt:
int cmd_mksmartfs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) int cmd_mksmartfs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{ {
char *fullpath = NULL; char *fullpath = NULL;
int ret = ERROR; int ret = OK;
uint16_t sectorsize = 0; uint16_t sectorsize = 0;
int force = 0;
int opt; int opt;
#ifdef CONFIG_SMARTFS_MULTI_ROOT_DIRS #ifdef CONFIG_SMARTFS_MULTI_ROOT_DIRS
int nrootdirs = 1; int nrootdirs = 1;
@ -1459,10 +1460,14 @@ int cmd_mksmartfs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
/* Process any options */ /* Process any options */
optind = 0; optind = 0;
while ((opt = getopt(argc, argv, "s:")) != -1) while ((opt = getopt(argc, argv, "fs:")) != -1)
{ {
switch (opt) switch (opt)
{ {
case 'f':
force = 1;
break;
case 's': case 's':
sectorsize = atoi(optarg); sectorsize = atoi(optarg);
if (sectorsize < 256 || sectorsize > 16384) if (sectorsize < 256 || sectorsize > 16384)
@ -1504,6 +1509,7 @@ int cmd_mksmartfs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
} }
else else
#endif #endif
if (force || issmartfs(fullpath) != OK)
{ {
#ifdef CONFIG_SMARTFS_MULTI_ROOT_DIRS #ifdef CONFIG_SMARTFS_MULTI_ROOT_DIRS
ret = mksmartfs(fullpath, sectorsize, nrootdirs); ret = mksmartfs(fullpath, sectorsize, nrootdirs);