diff --git a/fsutils/mksmartfs/mksmartfs.c b/fsutils/mksmartfs/mksmartfs.c index 44b68eb1d..d5f8276ed 100644 --- a/fsutils/mksmartfs/mksmartfs.c +++ b/fsutils/mksmartfs/mksmartfs.c @@ -74,6 +74,61 @@ * 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 * diff --git a/include/fsutils/mksmartfs.h b/include/fsutils/mksmartfs.h index 10ea9955d..0df47cf4c 100644 --- a/include/fsutils/mksmartfs.h +++ b/include/fsutils/mksmartfs.h @@ -56,6 +56,28 @@ extern "C" #define EXTERN extern #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 * diff --git a/nshlib/nsh_fscmds.c b/nshlib/nsh_fscmds.c index 0af480ea8..e45408d80 100644 --- a/nshlib/nsh_fscmds.c +++ b/nshlib/nsh_fscmds.c @@ -1449,8 +1449,9 @@ errout_with_fmt: int cmd_mksmartfs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) { char *fullpath = NULL; - int ret = ERROR; + int ret = OK; uint16_t sectorsize = 0; + int force = 0; int opt; #ifdef CONFIG_SMARTFS_MULTI_ROOT_DIRS int nrootdirs = 1; @@ -1459,10 +1460,14 @@ int cmd_mksmartfs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) /* Process any options */ optind = 0; - while ((opt = getopt(argc, argv, "s:")) != -1) + while ((opt = getopt(argc, argv, "fs:")) != -1) { switch (opt) { + case 'f': + force = 1; + break; + case 's': sectorsize = atoi(optarg); if (sectorsize < 256 || sectorsize > 16384) @@ -1504,6 +1509,7 @@ int cmd_mksmartfs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) } else #endif + if (force || issmartfs(fullpath) != OK) { #ifdef CONFIG_SMARTFS_MULTI_ROOT_DIRS ret = mksmartfs(fullpath, sectorsize, nrootdirs);