subsys: shell : fs new shell module for fs mount operation

Fixes # 7348, also includes test cases

Signed-off-by: Varun Sharma <varun.sharma@intel.com>
This commit is contained in:
Varun Sharma 2018-06-14 07:40:36 +05:30 committed by Anas Nashif
parent d3390305d1
commit 28f5da6c38
8 changed files with 246 additions and 25 deletions

View file

@ -17,12 +17,45 @@
#include <inttypes.h>
#include <limits.h>
/* FAT */
#ifdef CONFIG_FAT_FILESYSTEM_ELM
#include <ff.h>
#define FATFS_MNTP "/RAM:"
/* FatFs work area */
FATFS fat_fs;
/* mounting info */
static struct fs_mount_t fatfs_mnt = {
.type = FS_FATFS,
.fs_data = &fat_fs,
};
#endif
/* NFFS */
#ifdef CONFIG_FILE_SYSTEM_NFFS
#include <nffs/nffs.h>
#define NFFS_MNTP "/nffs"
/* NFFS work area strcut */
static struct nffs_flash_desc flash_desc;
/* mounting info */
static struct fs_mount_t nffs_mnt = {
.type = FS_NFFS,
.fs_data = &flash_desc,
};
#endif
#define BUF_CNT 64
#define MAX_PATH_LEN 128
#define MAX_FILENAME_LEN 128
#define MAX_INPUT_LEN 20
#define SHELL_FS "fs"
static char cwd[MAX_PATH_LEN] = "/";
static void create_abs_path(const char *name, char *path, size_t len)
@ -378,17 +411,91 @@ static int cmd_trunc(int argc, char *argv[])
return 0;
}
#if defined(CONFIG_FILE_SYSTEM_NFFS) || defined(CONFIG_FAT_FILESYSTEM_ELM)
static int cmd_mount(int argc, char *argv[])
{
int type, res;
char *mntpt;
if (argc != 3) {
return -EINVAL;
}
if (!strcmp(argv[1], "fat")) {
type = FS_FATFS;
} else if (!strcmp(argv[1], "nffs")) {
type = FS_NFFS;
} else {
return -EINVAL;
}
if (argv[2] != NULL) {
if (type == FS_FATFS) {
#ifdef CONFIG_FAT_FILESYSTEM_ELM
mntpt = k_malloc(strlen(argv[2]) + 1);
if (mntpt == NULL) {
return -ENOMEM;
}
((u8_t *)mntpt)[strlen(argv[2])] = '\0';
memcpy(mntpt, argv[2], strlen(argv[2]));
fatfs_mnt.mnt_point = (const char *)mntpt;
res = fs_mount(&fatfs_mnt);
if (res < 0) {
printk("Error mounting fat fs.Error Code [%d]\n", res);
return res;
}
printk("Successfully mounted fat fs:%s\n", fatfs_mnt.mnt_point);
#endif
} else if (type == FS_NFFS) {
#ifdef CONFIG_FILE_SYSTEM_NFFS
mntpt = k_malloc(strlen(argv[2]) + 1);
if (mntpt == NULL) {
return -ENOMEM;
}
((u8_t *)mntpt)[strlen(argv[2])] = '\0';
memcpy(mntpt, argv[2], strlen(argv[2]));
nffs_mnt.mnt_point = (const char *)mntpt;
struct device *flash_dev;
flash_dev = device_get_binding(CONFIG_FS_NFFS_FLASH_DEV_NAME);
if (!flash_dev) {
printk("Error in device_get_binding, while mounting nffs fs\n");
return -ENODEV;
}
nffs_mnt.storage_dev = flash_dev;
res = fs_mount(&nffs_mnt);
if (res < 0) {
printk("Error mounting nffs fs.Error code [%d]\n", res);
return res;
}
printk("Successfully mounted nffs:%s\n", nffs_mnt.mnt_point);
#endif
}
}
return 0;
}
#endif
struct shell_cmd fs_commands[] = {
{ "ls", cmd_ls, "List files in current directory" },
{ "cd", cmd_cd, "Change working directory" },
{ "pwd", cmd_pwd, "Print current working directory" },
{ "mkdir", cmd_mkdir, "Create directory" },
{ "rm", cmd_rm, "Remove file"},
{ "rm", cmd_rm, "Remove file" },
{ "read", cmd_read, "Read from file" },
{ "write", cmd_write, "Write to file" },
{ "trunc", cmd_trunc, "Truncate file" },
#if defined(CONFIG_FILE_SYSTEM_NFFS) || defined(CONFIG_FAT_FILESYSTEM_ELM)
{ "mount", cmd_mount, "<fs e.g: fat/nffs> <mount-point>" },
#endif
{ NULL, NULL }
};
SHELL_REGISTER("fs", fs_commands);
SHELL_REGISTER(SHELL_FS, fs_commands);

View file

@ -0,0 +1,21 @@
CONFIG_FILE_SYSTEM=y
CONFIG_CONSOLE_SHELL=y
CONFIG_FILE_SYSTEM_SHELL=y
CONFIG_FAT_FILESYSTEM_ELM=y
CONFIG_DISK_ACCESS_RAM=y
CONFIG_FILE_SYSTEM_NFFS=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_FLASH=y
CONFIG_TEST_FLASH_DRIVERS=y
CONFIG_FS_NFFS_FLASH_DEV_NAME="ram_flash_test_drv"
CONFIG_FS_NFFS_NUM_BLOCKS=1024
CONFIG_FS_NFFS_NUM_CACHE_BLOCKS=1
CONFIG_FS_NFFS_NUM_CACHE_INODES=1
CONFIG_FS_NFFS_NUM_DIRS=4
CONFIG_FS_NFFS_NUM_FILES=4
CONFIG_FS_NFFS_NUM_INODES=1024
CONFIG_HEAP_MEM_POOL_SIZE=1024
CONFIG_MAIN_STACK_SIZE=1024
CONFIG_NFFS_FILESYSTEM_MAX_AREAS=12
CONFIG_ZTEST_STACKSIZE=2048
CONFIG_ZTEST=y

View file

@ -6,6 +6,7 @@
#include "test_fat.h"
#include "test_nffs.h"
#include "test_fs_shell.h"
static struct nffs_area_desc nffs_selftest_area_descs[] = {
{ 0x00000000, 16 * 1024 },
@ -39,28 +40,30 @@ static void test_teardown(void)
void test_main(void)
{
ztest_test_suite(multifs_fs_test,
ztest_unit_test(test_fat_mount),
ztest_unit_test_setup_teardown(test_nffs_mount,
test_setup, test_teardown),
ztest_unit_test(test_fat_mkdir),
ztest_unit_test_setup_teardown(test_nffs_mkdir,
test_setup, test_teardown),
ztest_unit_test(test_fat_readdir),
ztest_unit_test(test_fat_rmdir),
ztest_unit_test_setup_teardown(test_nffs_readdir,
test_setup, test_teardown),
ztest_unit_test(test_fat_open),
ztest_unit_test_setup_teardown(test_nffs_open,
test_setup, test_teardown),
ztest_unit_test(test_fat_write),
ztest_unit_test_setup_teardown(test_nffs_write,
test_setup, test_teardown),
ztest_unit_test(test_fat_read),
ztest_unit_test(test_fat_close),
ztest_unit_test_setup_teardown(test_nffs_read,
test_setup, test_teardown),
ztest_unit_test(test_fat_unlink),
ztest_unit_test_setup_teardown(test_nffs_unlink,
test_setup, test_teardown));
ztest_unit_test(test_fat_mount),
ztest_unit_test_setup_teardown(test_nffs_mount,
test_setup, test_teardown),
ztest_unit_test(test_fat_mkdir),
ztest_unit_test_setup_teardown(test_nffs_mkdir,
test_setup, test_teardown),
ztest_unit_test(test_fat_readdir),
ztest_unit_test(test_fat_rmdir),
ztest_unit_test_setup_teardown(test_nffs_readdir,
test_setup, test_teardown),
ztest_unit_test(test_fat_open),
ztest_unit_test_setup_teardown(test_nffs_open,
test_setup, test_teardown),
ztest_unit_test(test_fat_write),
ztest_unit_test_setup_teardown(test_nffs_write,
test_setup, test_teardown),
ztest_unit_test(test_fat_read),
ztest_unit_test(test_fat_close),
ztest_unit_test_setup_teardown(test_nffs_read,
test_setup, test_teardown),
ztest_unit_test(test_fat_unlink),
ztest_unit_test_setup_teardown(test_nffs_unlink,
test_setup, test_teardown),
ztest_unit_test(test_fs_help),
ztest_unit_test(test_fs_shell_exit));
ztest_run_test_suite(multifs_fs_test);
}

View file

@ -6,7 +6,10 @@
#include "test_fat.h"
#include <ff.h>
#include "test_fs_shell.h"
/* for mount using FS api */
#if !defined(CONFIG_FILE_SYSTEM_SHELL)
/* FatFs work area */
static FATFS fat_fs;
@ -29,8 +32,13 @@ static int test_mount(void)
return TC_PASS;
}
#endif
void test_fat_mount(void)
{
#ifdef CONFIG_FILE_SYSTEM_SHELL
test_fs_fat_mount();
#else
zassert_true(test_mount() == TC_PASS, NULL);
#endif
}

View file

@ -0,0 +1,59 @@
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/** @file
* @brief Interactive shell test suite
*
*/
#include <zephyr.h>
#include <ztest.h>
#include "test_fs_shell.h"
#include <shell/shell.h>
static void test_shell_exec(const char *line, int result)
{
char cmd[80];
int ret;
strncpy(cmd, line, sizeof(cmd) - 1);
cmd[79] = '\0';
ret = shell_exec(cmd);
TC_PRINT("shell_exec(%s): %d\n", line, ret);
zassert_true(ret == result, line);
}
void test_fs_help(void)
{
#ifdef CONFIG_FILE_SYSTEM_SHELL
test_shell_exec("help", 0);
test_shell_exec("help fs", 0);
#else
ztest_test_skip();
#endif
}
void test_fs_fat_mount(void)
{
test_shell_exec("fs mount fat /RAM:", 0);
}
void test_fs_nffs_mount(void)
{
test_shell_exec("fs mount nffs /nffs", 0);
}
void test_fs_shell_exit(void)
{
#ifdef CONFIG_FILE_SYSTEM_SHELL
test_shell_exec("exit", 0);
#else
ztest_test_skip();
#endif
}

View file

@ -0,0 +1,12 @@
/*
* Copyright (c) 2018 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <fs.h>
void test_fs_help(void);
void test_fs_fat_mount(void);
void test_fs_nffs_mount(void);
void test_fs_shell_exit(void);

View file

@ -11,7 +11,9 @@
#include <ztest.h>
#include <ztest_assert.h>
#include "nffs_test_utils.h"
#include "test_fs_shell.h"
#if !defined(CONFIG_FILE_SYSTEM_SHELL)
/* NFFS work area strcut */
static struct nffs_flash_desc flash_desc;
@ -43,8 +45,13 @@ static int test_mount(void)
return TC_PASS;
}
#endif
void test_nffs_mount(void)
{
#ifdef CONFIG_FILE_SYSTEM_SHELL
test_fs_nffs_mount();
#else
zassert_true(test_mount() == TC_PASS, NULL);
#endif
}

View file

@ -2,3 +2,7 @@ tests:
filesystem.multifs:
platform_whitelist: qemu_x86
tags: nffs fatfs fs
fs_shell:
extra_args: CONF_FILE="prj_fs_shell.conf"
platform_whitelist: qemu_x86
tags: nffs fatfs fs