import RT-Thread@9217865c without bsp, libcpu and components/net
This commit is contained in:
commit
e2376a3709
1414 changed files with 390370 additions and 0 deletions
90
examples/file/listdir.c
Normal file
90
examples/file/listdir.c
Normal file
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2021, 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 <dfs_file.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/statfs.h>
|
||||
|
||||
void list_dir(const char* path)
|
||||
{
|
||||
char * fullpath = RT_NULL;
|
||||
DIR *dir;
|
||||
|
||||
dir = opendir(path);
|
||||
if (dir != RT_NULL)
|
||||
{
|
||||
struct dirent* dirent;
|
||||
struct stat s;
|
||||
|
||||
fullpath = rt_malloc(256);
|
||||
if (fullpath == RT_NULL)
|
||||
{
|
||||
closedir(dir);
|
||||
rt_kprintf("no memory\n");
|
||||
return;
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
dirent = readdir(dir);
|
||||
if (dirent == RT_NULL) break;
|
||||
rt_memset(&s, 0, sizeof(struct stat));
|
||||
|
||||
/* build full path for each file */
|
||||
rt_sprintf(fullpath, "%s/%s", path, dirent->d_name);
|
||||
|
||||
stat(fullpath, &s);
|
||||
if ( s.st_mode & S_IFDIR )
|
||||
{
|
||||
rt_kprintf("%s\t\t<DIR>\n", dirent->d_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("%s\t\t%lu\n", dirent->d_name, s.st_size);
|
||||
}
|
||||
} while (dirent != RT_NULL);
|
||||
|
||||
closedir(dir);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("open %s directory failed\n", path);
|
||||
}
|
||||
|
||||
if (RT_NULL != fullpath)
|
||||
{
|
||||
rt_free(fullpath);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
FINSH_FUNCTION_EXPORT(list_dir, list directory);
|
||||
|
||||
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);
|
||||
}
|
||||
MSH_CMD_EXPORT_ALIAS(cmd_list_dir, list_dir, list directory);
|
||||
#endif /* RT_USING_FINSH */
|
89
examples/file/readspeed.c
Normal file
89
examples/file/readspeed.c
Normal file
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2021, 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 <dfs_file.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/statfs.h>
|
||||
|
||||
void readspeed(const char* filename, int block_size)
|
||||
{
|
||||
int fd;
|
||||
char *buff_ptr;
|
||||
rt_size_t total_length;
|
||||
rt_tick_t tick;
|
||||
|
||||
fd = open(filename, 0, O_RDONLY);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("open file:%s failed\n", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
buff_ptr = rt_malloc(block_size);
|
||||
if (buff_ptr == RT_NULL)
|
||||
{
|
||||
rt_kprintf("no memory\n");
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
tick = rt_tick_get();
|
||||
total_length = 0;
|
||||
while (1)
|
||||
{
|
||||
int length;
|
||||
length = read(fd, buff_ptr, block_size);
|
||||
|
||||
if (length <= 0) break;
|
||||
total_length += length;
|
||||
}
|
||||
tick = rt_tick_get() - tick;
|
||||
|
||||
/* close file and release memory */
|
||||
close(fd);
|
||||
rt_free(buff_ptr);
|
||||
|
||||
/* calculate read speed */
|
||||
rt_kprintf("File read speed: %d byte/s\n", total_length /tick * RT_TICK_PER_SECOND);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
FINSH_FUNCTION_EXPORT(readspeed, perform file read test);
|
||||
|
||||
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);
|
||||
}
|
||||
MSH_CMD_EXPORT_ALIAS(cmd_readspeed, readspeed, test file system read speed);
|
||||
#endif /* RT_USING_FINSH */
|
170
examples/file/readwrite.c
Normal file
170
examples/file/readwrite.c
Normal file
|
@ -0,0 +1,170 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2021, 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 <dfs_file.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/statfs.h>
|
||||
|
||||
#define TEST_DATA_LEN 120
|
||||
|
||||
/* file read write test */
|
||||
void readwrite(const char* filename)
|
||||
{
|
||||
int fd;
|
||||
int index, length;
|
||||
char* test_data;
|
||||
char* buffer;
|
||||
int block_size = TEST_DATA_LEN;
|
||||
|
||||
/* open with write only & create */
|
||||
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("open file for write failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
test_data = rt_malloc(block_size);
|
||||
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;
|
||||
}
|
||||
|
||||
/* write to file */
|
||||
length = write(fd, test_data, block_size);
|
||||
if (length != block_size)
|
||||
{
|
||||
rt_kprintf("write data failed\n");
|
||||
close(fd);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* close file */
|
||||
close(fd);
|
||||
|
||||
/* reopen the file with append to the end */
|
||||
fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("open file for append write failed\n");
|
||||
goto __exit;;
|
||||
}
|
||||
|
||||
length = write(fd, test_data, block_size);
|
||||
if (length != block_size)
|
||||
{
|
||||
rt_kprintf("append write data failed\n");
|
||||
close(fd);
|
||||
goto __exit;
|
||||
}
|
||||
/* close the file */
|
||||
close(fd);
|
||||
|
||||
/* open the file for data validation. */
|
||||
fd = open(filename, O_RDONLY, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("check: open file for read failed\n");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* read the data (should be the data written by the first time ) */
|
||||
length = read(fd, buffer, block_size);
|
||||
if (length != block_size)
|
||||
{
|
||||
rt_kprintf("check: read file failed\n");
|
||||
close(fd);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* validate */
|
||||
for (index = 0; index < block_size; index ++)
|
||||
{
|
||||
if (test_data[index] != buffer[index])
|
||||
{
|
||||
rt_kprintf("check: check data failed at %d\n", index);
|
||||
close(fd);
|
||||
goto __exit;
|
||||
}
|
||||
}
|
||||
|
||||
/* read the data (should be the second time data) */
|
||||
length = read(fd, buffer, block_size);
|
||||
if (length != block_size)
|
||||
{
|
||||
rt_kprintf("check: read file failed\n");
|
||||
close(fd);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* validate */
|
||||
for (index = 0; index < block_size; index ++)
|
||||
{
|
||||
if (test_data[index] != buffer[index])
|
||||
{
|
||||
rt_kprintf("check: check data failed at %d\n", index);
|
||||
close(fd);
|
||||
goto __exit;
|
||||
}
|
||||
}
|
||||
|
||||
/* close the file */
|
||||
close(fd);
|
||||
/* print result */
|
||||
rt_kprintf("read/write test successful!\n");
|
||||
|
||||
__exit:
|
||||
rt_free(test_data);
|
||||
rt_free(buffer);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
/* export to finsh */
|
||||
FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test);
|
||||
|
||||
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);
|
||||
}
|
||||
MSH_CMD_EXPORT_ALIAS(cmd_readwrite, readwrite, perform file read and write test);
|
||||
#endif /* RT_USING_FINSH */
|
53
examples/file/seekdir.c
Normal file
53
examples/file/seekdir.c
Normal file
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-06-02 Bernard first version
|
||||
* 2020-04-12 Jianjia Ma add msh cmd
|
||||
*/
|
||||
#include <dfs_file.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/statfs.h>
|
||||
|
||||
void seekdir_test(void)
|
||||
{
|
||||
DIR * dirp;
|
||||
long save3 = 0;
|
||||
int i = 0;
|
||||
struct dirent *dp;
|
||||
|
||||
dirp = opendir ("/");
|
||||
save3 = telldir(dirp);
|
||||
for (dp = readdir(dirp); dp != RT_NULL; dp = readdir(dirp))
|
||||
{
|
||||
rt_kprintf("direntry: %s\n", dp->d_name);
|
||||
|
||||
/* save the pointer of the third directory */
|
||||
if (i++ == 3)
|
||||
{
|
||||
save3 = telldir(dirp);
|
||||
}
|
||||
}
|
||||
|
||||
/* get back to the third directory */
|
||||
seekdir (dirp, save3);
|
||||
rt_kprintf("seek dientry to: %d\n", save3);
|
||||
for (dp = readdir(dirp); dp != RT_NULL; dp = readdir(dirp))
|
||||
{
|
||||
rt_kprintf("direntry: %s\n", dp->d_name);
|
||||
}
|
||||
|
||||
/* close the directory */
|
||||
closedir (dirp);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
FINSH_FUNCTION_EXPORT(seekdir_test, perform directory seek test);
|
||||
MSH_CMD_EXPORT(seekdir_test, perform directory seek test);
|
||||
#endif /* RT_USING_FINSH */
|
100
examples/file/writespeed.c
Normal file
100
examples/file/writespeed.c
Normal file
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2021, 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 <dfs_file.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/statfs.h>
|
||||
|
||||
void writespeed(const char* filename, int total_length, int block_size)
|
||||
{
|
||||
int fd, index, length;
|
||||
char *buff_ptr;
|
||||
rt_tick_t tick;
|
||||
|
||||
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("open file:%s failed\n", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
buff_ptr = rt_malloc(block_size);
|
||||
if (buff_ptr == RT_NULL)
|
||||
{
|
||||
rt_kprintf("no memory\n");
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
/* prepare write data */
|
||||
for (index = 0; index < block_size; index++)
|
||||
{
|
||||
buff_ptr[index] = index;
|
||||
}
|
||||
index = 0;
|
||||
|
||||
/* get the beginning tick */
|
||||
tick = rt_tick_get();
|
||||
while (index < total_length / block_size)
|
||||
{
|
||||
length = write(fd, buff_ptr, block_size);
|
||||
if (length != block_size)
|
||||
{
|
||||
rt_kprintf("write failed\n");
|
||||
break;
|
||||
}
|
||||
|
||||
index ++;
|
||||
}
|
||||
tick = rt_tick_get() - tick;
|
||||
|
||||
/* close file and release memory */
|
||||
close(fd);
|
||||
rt_free(buff_ptr);
|
||||
|
||||
/* calculate write speed */
|
||||
rt_kprintf("File write speed: %d byte/s\n", total_length / tick * RT_TICK_PER_SECOND);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
FINSH_FUNCTION_EXPORT(writespeed, perform file write test);
|
||||
|
||||
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);
|
||||
}
|
||||
MSH_CMD_EXPORT_ALIAS(cmd_writespeed, writespeed, test file system write speed);
|
||||
#endif /* RT_USING_FINSH */
|
Loading…
Add table
Add a link
Reference in a new issue