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