1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2010-02-10     Bernard      first version
9  * 2020-04-12     Jianjia Ma   add msh cmd
10  */
11 
12 #include <rtthread.h>
13 #include <dfs_file.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <sys/stat.h>
17 #include <sys/statfs.h>
18 
19 #define TEST_DATA_LEN     120
20 
21 /* file read write test */
readwrite(const char * filename)22 void readwrite(const char* filename)
23 {
24     int fd;
25     int index, length;
26     char* test_data;
27     char* buffer;
28     int block_size = TEST_DATA_LEN;
29 
30     /* open with write only & create */
31     fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0);
32     if (fd < 0)
33     {
34         rt_kprintf("open file for write failed\n");
35         return;
36     }
37 
38     test_data = rt_malloc(block_size);
39     if (test_data == RT_NULL)
40     {
41         rt_kprintf("no memory\n");
42         close(fd);
43         return;
44     }
45 
46     buffer = rt_malloc(block_size);
47     if (buffer == RT_NULL)
48     {
49         rt_kprintf("no memory\n");
50         close(fd);
51         rt_free(test_data);
52         return;
53     }
54 
55     /* prepare some data */
56     for (index = 0; index < block_size; index ++)
57     {
58         test_data[index] = index + 27;
59     }
60 
61     /* write to file */
62     length = write(fd, test_data, block_size);
63     if (length != block_size)
64     {
65         rt_kprintf("write data failed\n");
66         close(fd);
67         goto __exit;
68     }
69 
70     /* close file */
71     close(fd);
72 
73     /* reopen the file with append to the end */
74     fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0);
75     if (fd < 0)
76     {
77         rt_kprintf("open file for append write failed\n");
78         goto __exit;;
79     }
80 
81     length = write(fd, test_data, block_size);
82     if (length != block_size)
83     {
84         rt_kprintf("append write data failed\n");
85         close(fd);
86         goto __exit;
87     }
88     /* close the file */
89     close(fd);
90 
91     /* open the file for data validation. */
92     fd = open(filename, O_RDONLY, 0);
93     if (fd < 0)
94     {
95         rt_kprintf("check: open file for read failed\n");
96         goto __exit;
97     }
98 
99     /* read the data (should be the data written by the first time ) */
100     length = read(fd, buffer, block_size);
101     if (length != block_size)
102     {
103         rt_kprintf("check: read file failed\n");
104         close(fd);
105         goto __exit;
106     }
107 
108     /* validate */
109     for (index = 0; index < block_size; index ++)
110     {
111         if (test_data[index] != buffer[index])
112         {
113             rt_kprintf("check: check data failed at %d\n", index);
114             close(fd);
115             goto __exit;
116         }
117     }
118 
119     /* read the data (should be the second time data) */
120     length = read(fd, buffer, block_size);
121     if (length != block_size)
122     {
123         rt_kprintf("check: read file failed\n");
124         close(fd);
125         goto __exit;
126     }
127 
128     /* validate */
129     for (index = 0; index < block_size; index ++)
130     {
131         if (test_data[index] != buffer[index])
132         {
133             rt_kprintf("check: check data failed at %d\n", index);
134             close(fd);
135             goto __exit;
136         }
137     }
138 
139     /* close the file */
140     close(fd);
141     /* print result */
142     rt_kprintf("read/write test successful!\n");
143 
144 __exit:
145     rt_free(test_data);
146     rt_free(buffer);
147 }
148 
149 #ifdef RT_USING_FINSH
150 #include <finsh.h>
151 /* export to finsh */
152 FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test);
153 
cmd_readwrite(int argc,char * argv[])154 static void cmd_readwrite(int argc, char *argv[])
155 {
156     char* filename;
157 
158     if(argc == 2)
159     {
160         filename = argv[1];
161     }
162     else
163     {
164         rt_kprintf("Usage: readwrite [file_path]\n");
165         return;
166     }
167     readwrite(filename);
168 }
169 MSH_CMD_EXPORT_ALIAS(cmd_readwrite, readwrite, perform file read and write test);
170 #endif /* RT_USING_FINSH */
171