Merge pull request #3526 from majianjia/master
Export file examples to MSH
This commit is contained in:
commit
50512e8a8c
|
@ -1,22 +1,19 @@
|
||||||
/*
|
/*
|
||||||
* File : listdir.c
|
* Copyright (c) 2006-2020, RT-Thread Development Team
|
||||||
* This file is part of RT-TestCase in RT-Thread RTOS
|
|
||||||
* COPYRIGHT (C) 2010, RT-Thread Development Team
|
|
||||||
*
|
*
|
||||||
* The license and distribution terms for this file may be
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
* found in the file LICENSE in this distribution or at
|
|
||||||
* http://www.rt-thread.org/license/LICENSE
|
|
||||||
*
|
*
|
||||||
* Change Logs:
|
* Change Logs:
|
||||||
* Date Author Notes
|
* Date Author Notes
|
||||||
* 2010-02-10 Bernard first version
|
* 2010-02-10 Bernard first version
|
||||||
|
* 2020-04-12 Jianjia Ma add msh cmd
|
||||||
*/
|
*/
|
||||||
#include <rtthread.h>
|
#include <rtthread.h>
|
||||||
#include <dfs_posix.h>
|
#include <dfs_posix.h>
|
||||||
|
|
||||||
static char fullpath[256];
|
|
||||||
void list_dir(const char* path)
|
void list_dir(const char* path)
|
||||||
{
|
{
|
||||||
|
char * fullpath;
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
|
|
||||||
dir = opendir(path);
|
dir = opendir(path);
|
||||||
|
@ -25,6 +22,13 @@ void list_dir(const char* path)
|
||||||
struct dirent* dirent;
|
struct dirent* dirent;
|
||||||
struct stat s;
|
struct stat s;
|
||||||
|
|
||||||
|
fullpath = rt_malloc(256);
|
||||||
|
if (fullpath == RT_NULL)
|
||||||
|
{
|
||||||
|
rt_kprintf("no memory\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
dirent = readdir(dir);
|
dirent = readdir(dir);
|
||||||
|
@ -35,7 +39,7 @@ void list_dir(const char* path)
|
||||||
rt_sprintf(fullpath, "%s/%s", path, dirent->d_name);
|
rt_sprintf(fullpath, "%s/%s", path, dirent->d_name);
|
||||||
|
|
||||||
stat(fullpath, &s);
|
stat(fullpath, &s);
|
||||||
if ( s.st_mode & DFS_S_IFDIR )
|
if ( s.st_mode & S_IFDIR )
|
||||||
{
|
{
|
||||||
rt_kprintf("%s\t\t<DIR>\n", dirent->d_name);
|
rt_kprintf("%s\t\t<DIR>\n", dirent->d_name);
|
||||||
}
|
}
|
||||||
|
@ -47,10 +51,34 @@ void list_dir(const char* path)
|
||||||
|
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
}
|
}
|
||||||
else rt_kprintf("open %s directory failed\n", path);
|
else
|
||||||
|
{
|
||||||
|
rt_kprintf("open %s directory failed\n", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
rt_free(fullpath);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef RT_USING_FINSH
|
#ifdef RT_USING_FINSH
|
||||||
#include <finsh.h>
|
#include <finsh.h>
|
||||||
FINSH_FUNCTION_EXPORT(list_dir, list directory);
|
FINSH_FUNCTION_EXPORT(list_dir, list directory);
|
||||||
#endif
|
|
||||||
|
#ifdef FINSH_USING_MSH
|
||||||
|
static void cmd_list_dir(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
char* filename;
|
||||||
|
|
||||||
|
if(argc == 2)
|
||||||
|
{
|
||||||
|
filename = argv[1];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rt_kprintf("Usage: list_dir [file_path]\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
list_dir(filename);
|
||||||
|
}
|
||||||
|
FINSH_FUNCTION_EXPORT_ALIAS(cmd_list_dir, __cmd_list_dir, list directory);
|
||||||
|
#endif /* FINSH_USING_MSH */
|
||||||
|
#endif /* RT_USING_FINSH */
|
||||||
|
|
|
@ -1,15 +1,12 @@
|
||||||
/*
|
/*
|
||||||
* File : readspeed.c
|
* Copyright (c) 2006-2020, RT-Thread Development Team
|
||||||
* This file is part of RT-TestCase in RT-Thread RTOS
|
|
||||||
* COPYRIGHT (C) 2010, RT-Thread Development Team
|
|
||||||
*
|
*
|
||||||
* The license and distribution terms for this file may be
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
* found in the file LICENSE in this distribution or at
|
|
||||||
* http://www.rt-thread.org/license/LICENSE
|
|
||||||
*
|
*
|
||||||
* Change Logs:
|
* Change Logs:
|
||||||
* Date Author Notes
|
* Date Author Notes
|
||||||
* 2010-02-10 Bernard first version
|
* 2010-02-10 Bernard first version
|
||||||
|
* 2020-04-12 Jianjia Ma add msh cmd
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <rtthread.h>
|
#include <rtthread.h>
|
||||||
|
@ -34,7 +31,6 @@ void readspeed(const char* filename, int block_size)
|
||||||
{
|
{
|
||||||
rt_kprintf("no memory\n");
|
rt_kprintf("no memory\n");
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,4 +57,31 @@ void readspeed(const char* filename, int block_size)
|
||||||
#ifdef RT_USING_FINSH
|
#ifdef RT_USING_FINSH
|
||||||
#include <finsh.h>
|
#include <finsh.h>
|
||||||
FINSH_FUNCTION_EXPORT(readspeed, perform file read test);
|
FINSH_FUNCTION_EXPORT(readspeed, perform file read test);
|
||||||
#endif
|
|
||||||
|
#ifdef FINSH_USING_MSH
|
||||||
|
static void cmd_readspeed(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
char* filename;
|
||||||
|
int block_size;
|
||||||
|
|
||||||
|
if(argc == 3)
|
||||||
|
{
|
||||||
|
filename = argv[1];
|
||||||
|
block_size = atoi(argv[2]);
|
||||||
|
}
|
||||||
|
else if(argc == 2)
|
||||||
|
{
|
||||||
|
filename = argv[1];
|
||||||
|
block_size = 512;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rt_kprintf("Usage:\nreadspeed [file_path] [block_size]\n");
|
||||||
|
rt_kprintf("readspeed [file_path] with default block size 512\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
readspeed(filename, block_size);
|
||||||
|
}
|
||||||
|
FINSH_FUNCTION_EXPORT_ALIAS(cmd_readspeed, __cmd_readspeed, test file system read speed);
|
||||||
|
#endif /* FINSH_USING_MSH */
|
||||||
|
#endif /* RT_USING_FINSH */
|
||||||
|
|
|
@ -1,123 +1,168 @@
|
||||||
/*
|
/*
|
||||||
* 代码清单:文件读写例子
|
* Copyright (c) 2006-2020, RT-Thread Development Team
|
||||||
*
|
*
|
||||||
* 这个例子演示了如何读写一个文件,特别是写的时候应该如何操作。
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* Change Logs:
|
||||||
|
* Date Author Notes
|
||||||
|
* 2010-02-10 Bernard first version
|
||||||
|
* 2020-04-12 Jianjia Ma add msh cmd
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <rtthread.h>
|
#include <rtthread.h>
|
||||||
#include <dfs_posix.h> /* 当需要使用文件操作时,需要包含这个头文件 */
|
#include <dfs_posix.h>
|
||||||
|
|
||||||
#define TEST_FN "/test.dat"
|
#define TEST_DATA_LEN 120
|
||||||
|
|
||||||
/* 测试用的数据和缓冲 */
|
/* file read write test */
|
||||||
static char test_data[120], buffer[120];
|
|
||||||
|
|
||||||
/* 文件读写测试 */
|
|
||||||
void readwrite(const char* filename)
|
void readwrite(const char* filename)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
int index, length;
|
int index, length;
|
||||||
|
char* test_data;
|
||||||
|
char* buffer;
|
||||||
|
int block_size = TEST_DATA_LEN;
|
||||||
|
|
||||||
/* 只写 & 创建 打开 */
|
/* open with write only & create */
|
||||||
fd = open(TEST_FN, O_WRONLY | O_CREAT | O_TRUNC, 0);
|
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
{
|
{
|
||||||
rt_kprintf("open file for write failed\n");
|
rt_kprintf("open file for write failed\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 准备写入数据 */
|
test_data = rt_malloc(block_size);
|
||||||
for (index = 0; index < sizeof(test_data); index ++)
|
if (test_data == RT_NULL)
|
||||||
|
{
|
||||||
|
rt_kprintf("no memory\n");
|
||||||
|
close(fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = rt_malloc(block_size);
|
||||||
|
if (buffer == RT_NULL)
|
||||||
|
{
|
||||||
|
rt_kprintf("no memory\n");
|
||||||
|
close(fd);
|
||||||
|
rt_free(test_data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* prepare some data */
|
||||||
|
for (index = 0; index < block_size; index ++)
|
||||||
{
|
{
|
||||||
test_data[index] = index + 27;
|
test_data[index] = index + 27;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 写入数据 */
|
/* write to file */
|
||||||
length = write(fd, test_data, sizeof(test_data));
|
length = write(fd, test_data, block_size);
|
||||||
if (length != sizeof(test_data))
|
if (length != block_size)
|
||||||
{
|
{
|
||||||
rt_kprintf("write data failed\n");
|
rt_kprintf("write data failed\n");
|
||||||
close(fd);
|
close(fd);
|
||||||
return;
|
goto __exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 关闭文件 */
|
/* close file */
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
/* 只写并在末尾添加打开 */
|
/* reopen the file with append to the end */
|
||||||
fd = open(TEST_FN, O_WRONLY | O_CREAT | O_APPEND, 0);
|
fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
{
|
{
|
||||||
rt_kprintf("open file for append write failed\n");
|
rt_kprintf("open file for append write failed\n");
|
||||||
return;
|
goto __exit;;
|
||||||
}
|
}
|
||||||
|
|
||||||
length = write(fd, test_data, sizeof(test_data));
|
length = write(fd, test_data, block_size);
|
||||||
if (length != sizeof(test_data))
|
if (length != block_size)
|
||||||
{
|
{
|
||||||
rt_kprintf("append write data failed\n");
|
rt_kprintf("append write data failed\n");
|
||||||
close(fd);
|
close(fd);
|
||||||
return;
|
goto __exit;
|
||||||
}
|
}
|
||||||
/* 关闭文件 */
|
/* close the file */
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
/* 只读打开进行数据校验 */
|
/* open the file for data validation. */
|
||||||
fd = open(TEST_FN, O_RDONLY, 0);
|
fd = open(filename, O_RDONLY, 0);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
{
|
{
|
||||||
rt_kprintf("check: open file for read failed\n");
|
rt_kprintf("check: open file for read failed\n");
|
||||||
return;
|
goto __exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 读取数据(应该为第一次写入的数据) */
|
/* read the data (should be the data written by the first time ) */
|
||||||
length = read(fd, buffer, sizeof(buffer));
|
length = read(fd, buffer, block_size);
|
||||||
if (length != sizeof(buffer))
|
if (length != block_size)
|
||||||
{
|
{
|
||||||
rt_kprintf("check: read file failed\n");
|
rt_kprintf("check: read file failed\n");
|
||||||
close(fd);
|
close(fd);
|
||||||
return;
|
goto __exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 检查数据是否正确 */
|
/* validate */
|
||||||
for (index = 0; index < sizeof(test_data); index ++)
|
for (index = 0; index < block_size; index ++)
|
||||||
{
|
{
|
||||||
if (test_data[index] != buffer[index])
|
if (test_data[index] != buffer[index])
|
||||||
{
|
{
|
||||||
rt_kprintf("check: check data failed at %d\n", index);
|
rt_kprintf("check: check data failed at %d\n", index);
|
||||||
close(fd);
|
close(fd);
|
||||||
return;
|
goto __exit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 读取数据(应该为第二次写入的数据) */
|
/* read the data (should be the second time data) */
|
||||||
length = read(fd, buffer, sizeof(buffer));
|
length = read(fd, buffer, block_size);
|
||||||
if (length != sizeof(buffer))
|
if (length != block_size)
|
||||||
{
|
{
|
||||||
rt_kprintf("check: read file failed\n");
|
rt_kprintf("check: read file failed\n");
|
||||||
close(fd);
|
close(fd);
|
||||||
return;
|
goto __exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 检查数据是否正确 */
|
/* validate */
|
||||||
for (index = 0; index < sizeof(test_data); index ++)
|
for (index = 0; index < block_size; index ++)
|
||||||
{
|
{
|
||||||
if (test_data[index] != buffer[index])
|
if (test_data[index] != buffer[index])
|
||||||
{
|
{
|
||||||
rt_kprintf("check: check data failed at %d\n", index);
|
rt_kprintf("check: check data failed at %d\n", index);
|
||||||
close(fd);
|
close(fd);
|
||||||
return;
|
goto __exit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 检查数据完毕,关闭文件 */
|
/* close the file */
|
||||||
close(fd);
|
close(fd);
|
||||||
/* 打印结果 */
|
/* print result */
|
||||||
rt_kprintf("read/write done.\n");
|
rt_kprintf("read/write test successful!\n");
|
||||||
|
|
||||||
|
__exit:
|
||||||
|
rt_free(test_data);
|
||||||
|
rt_free(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef RT_USING_FINSH
|
#ifdef RT_USING_FINSH
|
||||||
#include <finsh.h>
|
#include <finsh.h>
|
||||||
/* 输出函数到finsh shell命令行中 */
|
/* export to finsh */
|
||||||
FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test);
|
FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test);
|
||||||
#endif
|
|
||||||
|
#ifdef FINSH_USING_MSH
|
||||||
|
static void cmd_readwrite(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
char* filename;
|
||||||
|
|
||||||
|
if(argc == 2)
|
||||||
|
{
|
||||||
|
filename = argv[1];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rt_kprintf("Usage: readwrite [file_path]\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
readwrite(filename);
|
||||||
|
}
|
||||||
|
FINSH_FUNCTION_EXPORT_ALIAS(cmd_readwrite, __cmd_readwrite, perform file read and write test);
|
||||||
|
#endif /* FINSH_USING_MSH */
|
||||||
|
#endif /* RT_USING_FINSH */
|
||||||
|
|
|
@ -1,15 +1,12 @@
|
||||||
/*
|
/*
|
||||||
* File : seekdir.c
|
* Copyright (c) 2006-2020, RT-Thread Development Team
|
||||||
* This file is part of RT-TestCase in RT-Thread RTOS
|
|
||||||
* COPYRIGHT (C) 2011, RT-Thread Development Team
|
|
||||||
*
|
*
|
||||||
* The license and distribution terms for this file may be
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
* found in the file LICENSE in this distribution or at
|
|
||||||
* http://www.rt-thread.org/license/LICENSE
|
|
||||||
*
|
*
|
||||||
* Change Logs:
|
* Change Logs:
|
||||||
* Date Author Notes
|
* Date Author Notes
|
||||||
* 2011-06-02 Bernard first version
|
* 2011-06-02 Bernard first version
|
||||||
|
* 2020-04-12 Jianjia Ma add msh cmd
|
||||||
*/
|
*/
|
||||||
#include <dfs_posix.h>
|
#include <dfs_posix.h>
|
||||||
|
|
||||||
|
@ -17,7 +14,6 @@ void seekdir_test(void)
|
||||||
{
|
{
|
||||||
DIR * dirp;
|
DIR * dirp;
|
||||||
long save3 = 0;
|
long save3 = 0;
|
||||||
long cur;
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
struct dirent *dp;
|
struct dirent *dp;
|
||||||
|
|
||||||
|
@ -27,14 +23,14 @@ void seekdir_test(void)
|
||||||
{
|
{
|
||||||
rt_kprintf("direntry: %s\n", dp->d_name);
|
rt_kprintf("direntry: %s\n", dp->d_name);
|
||||||
|
|
||||||
/* 保存第三个目录项的目录指针 */
|
/* save the pointer of the third directory */
|
||||||
if (i++ == 3)
|
if (i++ == 3)
|
||||||
{
|
{
|
||||||
save3 = telldir(dirp);
|
save3 = telldir(dirp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 回到刚才保存的第三个目录项的目录指针 */
|
/* get back to the third directory */
|
||||||
seekdir (dirp, save3);
|
seekdir (dirp, save3);
|
||||||
rt_kprintf("seek dientry to: %d\n", save3);
|
rt_kprintf("seek dientry to: %d\n", save3);
|
||||||
for (dp = readdir(dirp); dp != RT_NULL; dp = readdir(dirp))
|
for (dp = readdir(dirp); dp != RT_NULL; dp = readdir(dirp))
|
||||||
|
@ -42,9 +38,12 @@ void seekdir_test(void)
|
||||||
rt_kprintf("direntry: %s\n", dp->d_name);
|
rt_kprintf("direntry: %s\n", dp->d_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 关闭目录 */
|
/* close the directory */
|
||||||
closedir (dirp);
|
closedir (dirp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef RT_USING_FINSH
|
||||||
#include <finsh.h>
|
#include <finsh.h>
|
||||||
FINSH_FUNCTION_EXPORT(seekdir_test, perform directory seek test);
|
FINSH_FUNCTION_EXPORT(seekdir_test, perform directory seek test);
|
||||||
|
MSH_CMD_EXPORT(seekdir_test, perform directory seek test);
|
||||||
|
#endif /* RT_USING_FINSH */
|
||||||
|
|
|
@ -1,15 +1,12 @@
|
||||||
/*
|
/*
|
||||||
* File : writespeed.c
|
* Copyright (c) 2006-2020, RT-Thread Development Team
|
||||||
* This file is part of RT-TestCase in RT-Thread RTOS
|
|
||||||
* COPYRIGHT (C) 2010, RT-Thread Development Team
|
|
||||||
*
|
*
|
||||||
* The license and distribution terms for this file may be
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
* found in the file LICENSE in this distribution or at
|
|
||||||
* http://www.rt-thread.org/license/LICENSE
|
|
||||||
*
|
*
|
||||||
* Change Logs:
|
* Change Logs:
|
||||||
* Date Author Notes
|
* Date Author Notes
|
||||||
* 2010-02-10 Bernard first version
|
* 2010-02-10 Bernard first version
|
||||||
|
* 2020-04-12 Jianjia Ma add msh cmd
|
||||||
*/
|
*/
|
||||||
#include <rtthread.h>
|
#include <rtthread.h>
|
||||||
#include <dfs_posix.h>
|
#include <dfs_posix.h>
|
||||||
|
@ -32,7 +29,6 @@ void writespeed(const char* filename, int total_length, int block_size)
|
||||||
{
|
{
|
||||||
rt_kprintf("no memory\n");
|
rt_kprintf("no memory\n");
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,4 +65,34 @@ void writespeed(const char* filename, int total_length, int block_size)
|
||||||
#ifdef RT_USING_FINSH
|
#ifdef RT_USING_FINSH
|
||||||
#include <finsh.h>
|
#include <finsh.h>
|
||||||
FINSH_FUNCTION_EXPORT(writespeed, perform file write test);
|
FINSH_FUNCTION_EXPORT(writespeed, perform file write test);
|
||||||
#endif
|
|
||||||
|
#ifdef FINSH_USING_MSH
|
||||||
|
static void cmd_writespeed(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
char* filename;
|
||||||
|
int length;
|
||||||
|
int block_size;
|
||||||
|
|
||||||
|
if(argc == 4)
|
||||||
|
{
|
||||||
|
filename = argv[1];
|
||||||
|
length = atoi(argv[2]);
|
||||||
|
block_size = atoi(argv[3]);
|
||||||
|
}
|
||||||
|
else if(argc == 2)
|
||||||
|
{
|
||||||
|
filename = argv[1];
|
||||||
|
block_size = 512;
|
||||||
|
length = 1024*1024;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rt_kprintf("Usage:\nwritespeed [file_path] [length] [block_size]\n");
|
||||||
|
rt_kprintf("writespeed [file_path] with default length 1MB and block size 512\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
writespeed(filename, length, block_size);
|
||||||
|
}
|
||||||
|
FINSH_FUNCTION_EXPORT_ALIAS(cmd_writespeed, __cmd_writespeed, test file system write speed);
|
||||||
|
#endif /* FINSH_USING_MSH */
|
||||||
|
#endif /* RT_USING_FINSH */
|
||||||
|
|
Loading…
Reference in New Issue