readwrite.c 3.6 KB
Newer Older
B
Bernard Xiong 已提交
1
/*
mysterywolf's avatar
mysterywolf 已提交
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
B
Bernard Xiong 已提交
3
 *
M
majianjia 已提交
4 5 6 7 8 9
 * 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
B
Bernard Xiong 已提交
10 11 12
 */

#include <rtthread.h>
M
majianjia 已提交
13
#include <dfs_posix.h>
B
Bernard Xiong 已提交
14

M
majianjia 已提交
15
#define TEST_DATA_LEN     120
B
Bernard Xiong 已提交
16

M
majianjia 已提交
17
/* file read write test */
B
Bernard Xiong 已提交
18 19
void readwrite(const char* filename)
{
mysterywolf's avatar
mysterywolf 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    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);
M
majianjia 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    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;
    }

mysterywolf's avatar
mysterywolf 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    /* 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");
M
majianjia 已提交
139 140 141 142

__exit:
    rt_free(test_data);
    rt_free(buffer);
B
Bernard Xiong 已提交
143 144 145 146
}

#ifdef RT_USING_FINSH
#include <finsh.h>
M
majianjia 已提交
147
/* export to finsh */
B
Bernard Xiong 已提交
148
FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test);
M
majianjia 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

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);
}
165
MSH_CMD_EXPORT_ALIAS(cmd_readwrite, readwrite, perform file read and write test);
M
majianjia 已提交
166
#endif /* RT_USING_FINSH */