]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/Common/FreeRTOS_Plus_FAT_SL_Demos/CreateExampleFiles/File-system-demo.c
b47c4dec125e917c3f5fe4c0bedbe3e9e75cb06b
[freertos] / FreeRTOS-Plus / Demo / Common / FreeRTOS_Plus_FAT_SL_Demos / CreateExampleFiles / File-system-demo.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*******************************************************************************\r
29  * See the URL in the comments within main.c for the location of the online\r
30  * documentation.\r
31  ******************************************************************************/\r
32 \r
33 /* Standard includes. */\r
34 #include <stdio.h>\r
35 #include <string.h>\r
36 \r
37 /* FreeRTOS includes. */\r
38 #include "FreeRTOS.h"\r
39 \r
40 /* File system includes. */\r
41 #include "fat_sl.h"\r
42 #include "api_mdriver_ram.h"\r
43 \r
44 /* 8.3 format, plus null terminator. */\r
45 #define fsMAX_FILE_NAME_LEN                             13\r
46 \r
47 /* The number of bytes read/written to the example files at a time. */\r
48 #define fsRAM_BUFFER_SIZE                               200\r
49 \r
50 /* The number of bytes written to the file that uses f_putc() and f_getc(). */\r
51 #define fsPUTC_FILE_SIZE                                100\r
52 \r
53 /*-----------------------------------------------------------*/\r
54 \r
55 /*\r
56  * Creates and verifies different files on the volume, demonstrating the use of\r
57  * various different API functions.\r
58  */\r
59 void vCreateAndVerifySampleFiles( void );\r
60 \r
61 /*\r
62  * Create a set of example files in the root directory of the volume using\r
63  * f_write().\r
64  */\r
65 static void prvCreateDemoFilesUsing_f_write( void );\r
66 \r
67 /*\r
68  * Use f_read() to read back and verify the files that were created by\r
69  * prvCreateDemoFilesUsing_f_write().\r
70  */\r
71 static void prvVerifyDemoFileUsing_f_read( void );\r
72 \r
73 /*\r
74  * Create an example file in a sub-directory using f_putc().\r
75  */\r
76 static void prvCreateDemoFileUsing_f_putc( void );\r
77 \r
78 /*\r
79  * Use f_getc() to read back and verify the file that was created by\r
80  * prvCreateDemoFileUsing_f_putc().\r
81  */\r
82 static void prvVerifyDemoFileUsing_f_getc( void );\r
83 \r
84 /*-----------------------------------------------------------*/\r
85 \r
86 /* A buffer used to both create content to write to disk, and read content back\r
87 from a disk.  Note there is no mutual exclusion on this buffer. */\r
88 static char cRAMBuffer[ fsRAM_BUFFER_SIZE ];\r
89 \r
90 /* Names of directories that are created. */\r
91 static const char *pcRoot = "/", *pcDirectory1 = "SUB1", *pcDirectory2 = "SUB2", *pcFullPath = "/SUB1/SUB2";\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 void vCreateAndVerifySampleFiles( void )\r
96 {\r
97 unsigned char ucStatus;\r
98 \r
99         /* First create the volume. */\r
100         ucStatus = f_initvolume( ram_initfunc );\r
101 \r
102         /* It is expected that the volume is not formatted. */\r
103         if( ucStatus == F_ERR_NOTFORMATTED )\r
104         {\r
105                 /* Format the created volume. */\r
106                 ucStatus = f_format( F_FAT12_MEDIA );\r
107         }\r
108 \r
109         if( ucStatus == F_NO_ERROR )\r
110         {\r
111                 /* Create a set of files using f_write(). */\r
112                 prvCreateDemoFilesUsing_f_write();\r
113 \r
114                 /* Read back and verify the files that were created using f_write(). */\r
115                 prvVerifyDemoFileUsing_f_read();\r
116 \r
117                 /* Create sub directories two deep then create a file using putc. */\r
118                 prvCreateDemoFileUsing_f_putc();\r
119 \r
120                 /* Read back and verify the file created by\r
121                 prvCreateDemoFileUsing_f_putc(). */\r
122                 prvVerifyDemoFileUsing_f_getc();\r
123         }\r
124 }\r
125 /*-----------------------------------------------------------*/\r
126 \r
127 static void prvCreateDemoFilesUsing_f_write( void )\r
128 {\r
129 BaseType_t xFileNumber, xWriteNumber;\r
130 char cFileName[ fsMAX_FILE_NAME_LEN ];\r
131 const BaseType_t xMaxFiles = 5;\r
132 long lItemsWritten;\r
133 F_FILE *pxFile;\r
134 \r
135         /* Create xMaxFiles files.  Each created file will be\r
136         ( xFileNumber * fsRAM_BUFFER_SIZE ) bytes in length, and filled\r
137         with a different repeating character. */\r
138         for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )\r
139         {\r
140                 /* Generate a file name. */\r
141                 sprintf( cFileName, "root%03d.txt", ( int ) xFileNumber );\r
142 \r
143                 /* Obtain the current working directory and print out the file name and\r
144                 the     directory into which the file is being written. */\r
145                 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
146 \r
147                 /* Open the file, creating the file if it does not already exist. */\r
148                 pxFile = f_open( cFileName, "w" );\r
149                 configASSERT( pxFile );\r
150 \r
151                 /* Fill the RAM buffer with data that will be written to the file.  This\r
152                 is just a repeating ascii character that indicates the file number. */\r
153                 memset( cRAMBuffer, ( int ) ( '0' + xFileNumber ), fsRAM_BUFFER_SIZE );\r
154 \r
155                 /* Write the RAM buffer to the opened file a number of times.  The\r
156                 number of times the RAM buffer is written to the file depends on the\r
157                 file number, so the length of each created file will be different. */\r
158                 for( xWriteNumber = 0; xWriteNumber < xFileNumber; xWriteNumber++ )\r
159                 {\r
160                         lItemsWritten = f_write( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );\r
161                         configASSERT( lItemsWritten == 1 );\r
162                 }\r
163 \r
164                 /* Close the file so another file can be created. */\r
165                 f_close( pxFile );\r
166         }\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 static void prvVerifyDemoFileUsing_f_read( void )\r
171 {\r
172 BaseType_t xFileNumber, xReadNumber;\r
173 char cFileName[ fsMAX_FILE_NAME_LEN ];\r
174 const BaseType_t xMaxFiles = 5;\r
175 long lItemsRead, lChar;\r
176 F_FILE *pxFile;\r
177 \r
178         /* Read back the files that were created by\r
179         prvCreateDemoFilesUsing_f_write(). */\r
180         for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )\r
181         {\r
182                 /* Generate the file name. */\r
183                 sprintf( cFileName, "root%03d.txt", ( int ) xFileNumber );\r
184 \r
185                 /* Obtain the current working directory and print out the file name and\r
186                 the     directory from which the file is being read. */\r
187                 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
188 \r
189                 /* Open the file for reading. */\r
190                 pxFile = f_open( cFileName, "r" );\r
191                 configASSERT( pxFile );\r
192 \r
193                 /* Read the file into the RAM buffer, checking the file contents are as\r
194                 expected.  The size of the file depends on the file number. */\r
195                 for( xReadNumber = 0; xReadNumber < xFileNumber; xReadNumber++ )\r
196                 {\r
197                         /* Start with the RAM buffer clear. */\r
198                         memset( cRAMBuffer, 0x00, fsRAM_BUFFER_SIZE );\r
199 \r
200                         lItemsRead = f_read( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );\r
201                         configASSERT( lItemsRead == 1 );\r
202 \r
203                         /* Check the RAM buffer is filled with the expected data.  Each\r
204                         file contains a different repeating ascii character that indicates\r
205                         the number of the file. */\r
206                         for( lChar = 0; lChar < fsRAM_BUFFER_SIZE; lChar++ )\r
207                         {\r
208                                 configASSERT( cRAMBuffer[ lChar ] == ( '0' + ( char ) xFileNumber ) );\r
209                         }\r
210                 }\r
211 \r
212                 /* Close the file. */\r
213                 f_close( pxFile );\r
214         }\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 static void prvCreateDemoFileUsing_f_putc( void )\r
219 {\r
220 unsigned char ucReturn;\r
221 int iByte, iReturned;\r
222 F_FILE *pxFile;\r
223 char cFileName[ fsMAX_FILE_NAME_LEN ];\r
224 \r
225         /* Obtain and print out the working directory. */\r
226         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
227 \r
228         /* Create a sub directory. */\r
229         ucReturn = f_mkdir( pcDirectory1 );\r
230         configASSERT( ucReturn == F_NO_ERROR );\r
231 \r
232         /* Move into the created sub-directory. */\r
233         ucReturn = f_chdir( pcDirectory1 );\r
234         configASSERT( ucReturn == F_NO_ERROR );\r
235 \r
236         /* Obtain and print out the working directory. */\r
237         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
238 \r
239         /* Create a subdirectory in the new directory. */\r
240         ucReturn = f_mkdir( pcDirectory2 );\r
241         configASSERT( ucReturn == F_NO_ERROR );\r
242 \r
243         /* Move into the directory just created - now two directories down from\r
244         the root. */\r
245         ucReturn = f_chdir( pcDirectory2 );\r
246         configASSERT( ucReturn == F_NO_ERROR );\r
247 \r
248         /* Obtain and print out the working directory. */\r
249         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
250         configASSERT( strcmp( cRAMBuffer, pcFullPath ) == 0 );\r
251 \r
252         /* Generate the file name. */\r
253         sprintf( cFileName, "%s.txt", pcDirectory2 );\r
254 \r
255         /* Print out the file name and the directory into which the file is being\r
256         written. */\r
257         pxFile = f_open( cFileName, "w" );\r
258 \r
259         /* Create a file 1 byte at a time.  The file is filled with incrementing\r
260         ascii characters starting from '0'. */\r
261         for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )\r
262         {\r
263                 iReturned = f_putc( ( ( int ) '0' + iByte ), pxFile );\r
264                 configASSERT( iReturned ==  ( ( int ) '0' + iByte ) );\r
265         }\r
266 \r
267         /* Finished so close the file. */\r
268         f_close( pxFile );\r
269 \r
270         /* Move back to the root directory. */\r
271         ucReturn = f_chdir( "../.." );\r
272         configASSERT( ucReturn == F_NO_ERROR );\r
273 \r
274         /* Obtain and print out the working directory. */\r
275         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
276         configASSERT( strcmp( cRAMBuffer, pcRoot ) == 0 );\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 static void prvVerifyDemoFileUsing_f_getc( void )\r
281 {\r
282 unsigned char ucReturn;\r
283 int iByte, iReturned;\r
284 F_FILE *pxFile;\r
285 char cFileName[ fsMAX_FILE_NAME_LEN ];\r
286 \r
287         /* Move into the directory in which the file was created. */\r
288         ucReturn = f_chdir( pcFullPath );\r
289         configASSERT( ucReturn == F_NO_ERROR );\r
290 \r
291         /* Obtain and print out the working directory. */\r
292         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
293         configASSERT( strcmp( cRAMBuffer, pcFullPath ) == 0 );\r
294 \r
295         /* Generate the file name. */\r
296         sprintf( cFileName, "%s.txt", pcDirectory2 );\r
297 \r
298         /* This time the file is opened for reading. */\r
299         pxFile = f_open( cFileName, "r" );\r
300 \r
301         /* Read the file 1 byte at a time. */\r
302         for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )\r
303         {\r
304                 iReturned = f_getc( pxFile );\r
305                 configASSERT( iReturned ==  ( ( int ) '0' + iByte ) );\r
306         }\r
307 \r
308         /* Finished so close the file. */\r
309         f_close( pxFile );\r
310 \r
311         /* Move back to the root directory. */\r
312         ucReturn = f_chdir( "../.." );\r
313         configASSERT( ucReturn == F_NO_ERROR );\r
314 \r
315         /* Obtain and print out the working directory. */\r
316         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
317 }\r
318 \r
319 \r
320 \r
321 \r