11/*
2- * Copyright (c) 2006-2020 , RT-Thread Development Team
2+ * Copyright (c) 2006-2021 , RT-Thread Development Team
33 *
44 * SPDX-License-Identifier: Apache-2.0
55 *
1717/* file read write test */
1818void readwrite (const char * filename )
1919{
20- int fd ;
21- int index , length ;
22- char * test_data ;
23- char * buffer ;
24- int block_size = TEST_DATA_LEN ;
25-
26- /* open with write only & create */
27- fd = open (filename , O_WRONLY | O_CREAT | O_TRUNC , 0 );
28- if (fd < 0 )
29- {
30- rt_kprintf ("open file for write failed\n" );
31- return ;
32- }
33-
34- test_data = rt_malloc (block_size );
20+ int fd ;
21+ int index , length ;
22+ char * test_data ;
23+ char * buffer ;
24+ int block_size = TEST_DATA_LEN ;
25+
26+ /* open with write only & create */
27+ fd = open (filename , O_WRONLY | O_CREAT | O_TRUNC , 0 );
28+ if (fd < 0 )
29+ {
30+ rt_kprintf ("open file for write failed\n" );
31+ return ;
32+ }
33+
34+ test_data = rt_malloc (block_size );
3535 if (test_data == RT_NULL )
3636 {
3737 rt_kprintf ("no memory\n" );
@@ -48,94 +48,94 @@ void readwrite(const char* filename)
4848 return ;
4949 }
5050
51- /* prepare some data */
52- for (index = 0 ; index < block_size ; index ++ )
53- {
54- test_data [index ] = index + 27 ;
55- }
56-
57- /* write to file */
58- length = write (fd , test_data , block_size );
59- if (length != block_size )
60- {
61- rt_kprintf ("write data failed\n" );
62- close (fd );
63- goto __exit ;
64- }
65-
66- /* close file */
67- close (fd );
68-
69- /* reopen the file with append to the end */
70- fd = open (filename , O_WRONLY | O_CREAT | O_APPEND , 0 );
71- if (fd < 0 )
72- {
73- rt_kprintf ("open file for append write failed\n" );
74- goto __exit ;;
75- }
76-
77- length = write (fd , test_data , block_size );
78- if (length != block_size )
79- {
80- rt_kprintf ("append write data failed\n" );
81- close (fd );
82- goto __exit ;
83- }
84- /* close the file */
85- close (fd );
86-
87- /* open the file for data validation. */
88- fd = open (filename , O_RDONLY , 0 );
89- if (fd < 0 )
90- {
91- rt_kprintf ("check: open file for read failed\n" );
92- goto __exit ;
93- }
94-
95- /* read the data (should be the data written by the first time ) */
96- length = read (fd , buffer , block_size );
97- if (length != block_size )
98- {
99- rt_kprintf ("check: read file failed\n" );
100- close (fd );
101- goto __exit ;
102- }
103-
104- /* validate */
105- for (index = 0 ; index < block_size ; index ++ )
106- {
107- if (test_data [index ] != buffer [index ])
108- {
109- rt_kprintf ("check: check data failed at %d\n" , index );
110- close (fd );
111- goto __exit ;
112- }
113- }
114-
115- /* read the data (should be the second time data) */
116- length = read (fd , buffer , block_size );
117- if (length != block_size )
118- {
119- rt_kprintf ("check: read file failed\n" );
120- close (fd );
121- goto __exit ;
122- }
123-
124- /* validate */
125- for (index = 0 ; index < block_size ; index ++ )
126- {
127- if (test_data [index ] != buffer [index ])
128- {
129- rt_kprintf ("check: check data failed at %d\n" , index );
130- close (fd );
131- goto __exit ;
132- }
133- }
134-
135- /* close the file */
136- close (fd );
137- /* print result */
138- rt_kprintf ("read/write test successful!\n" );
51+ /* prepare some data */
52+ for (index = 0 ; index < block_size ; index ++ )
53+ {
54+ test_data [index ] = index + 27 ;
55+ }
56+
57+ /* write to file */
58+ length = write (fd , test_data , block_size );
59+ if (length != block_size )
60+ {
61+ rt_kprintf ("write data failed\n" );
62+ close (fd );
63+ goto __exit ;
64+ }
65+
66+ /* close file */
67+ close (fd );
68+
69+ /* reopen the file with append to the end */
70+ fd = open (filename , O_WRONLY | O_CREAT | O_APPEND , 0 );
71+ if (fd < 0 )
72+ {
73+ rt_kprintf ("open file for append write failed\n" );
74+ goto __exit ;;
75+ }
76+
77+ length = write (fd , test_data , block_size );
78+ if (length != block_size )
79+ {
80+ rt_kprintf ("append write data failed\n" );
81+ close (fd );
82+ goto __exit ;
83+ }
84+ /* close the file */
85+ close (fd );
86+
87+ /* open the file for data validation. */
88+ fd = open (filename , O_RDONLY , 0 );
89+ if (fd < 0 )
90+ {
91+ rt_kprintf ("check: open file for read failed\n" );
92+ goto __exit ;
93+ }
94+
95+ /* read the data (should be the data written by the first time ) */
96+ length = read (fd , buffer , block_size );
97+ if (length != block_size )
98+ {
99+ rt_kprintf ("check: read file failed\n" );
100+ close (fd );
101+ goto __exit ;
102+ }
103+
104+ /* validate */
105+ for (index = 0 ; index < block_size ; index ++ )
106+ {
107+ if (test_data [index ] != buffer [index ])
108+ {
109+ rt_kprintf ("check: check data failed at %d\n" , index );
110+ close (fd );
111+ goto __exit ;
112+ }
113+ }
114+
115+ /* read the data (should be the second time data) */
116+ length = read (fd , buffer , block_size );
117+ if (length != block_size )
118+ {
119+ rt_kprintf ("check: read file failed\n" );
120+ close (fd );
121+ goto __exit ;
122+ }
123+
124+ /* validate */
125+ for (index = 0 ; index < block_size ; index ++ )
126+ {
127+ if (test_data [index ] != buffer [index ])
128+ {
129+ rt_kprintf ("check: check data failed at %d\n" , index );
130+ close (fd );
131+ goto __exit ;
132+ }
133+ }
134+
135+ /* close the file */
136+ close (fd );
137+ /* print result */
138+ rt_kprintf ("read/write test successful!\n" );
139139
140140__exit :
141141 rt_free (test_data );
0 commit comments