1 |
|
|
/**************************************************************************/ |
2 |
|
|
/* */ |
3 |
|
|
/* Copyright (c) Microsoft Corporation. All rights reserved. */ |
4 |
|
|
/* */ |
5 |
|
|
/* This software is licensed under the Microsoft Software License */ |
6 |
|
|
/* Terms for Microsoft Azure RTOS. Full text of the license can be */ |
7 |
|
|
/* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ |
8 |
|
|
/* and in the root directory of this software. */ |
9 |
|
|
/* */ |
10 |
|
|
/**************************************************************************/ |
11 |
|
|
|
12 |
|
|
|
13 |
|
|
/**************************************************************************/ |
14 |
|
|
/**************************************************************************/ |
15 |
|
|
/** */ |
16 |
|
|
/** FileX Component */ |
17 |
|
|
/** */ |
18 |
|
|
/** Directory */ |
19 |
|
|
/** */ |
20 |
|
|
/**************************************************************************/ |
21 |
|
|
/**************************************************************************/ |
22 |
|
|
|
23 |
|
|
#define FX_SOURCE_CODE |
24 |
|
|
|
25 |
|
|
|
26 |
|
|
/* Include necessary system files. */ |
27 |
|
|
|
28 |
|
|
#include "fx_api.h" |
29 |
|
|
#include "fx_system.h" |
30 |
|
|
#include "fx_directory.h" |
31 |
|
|
#include "fx_utility.h" |
32 |
|
|
#ifdef FX_ENABLE_EXFAT |
33 |
|
|
#include "fx_directory_exFAT.h" |
34 |
|
|
#endif /* FX_ENABLE_EXFAT */ |
35 |
|
|
|
36 |
|
|
#ifndef FX_NO_LOCAL_PATH |
37 |
|
|
FX_LOCAL_PATH_SETUP |
38 |
|
|
#endif |
39 |
|
|
|
40 |
|
|
|
41 |
|
|
/**************************************************************************/ |
42 |
|
|
/* */ |
43 |
|
|
/* FUNCTION RELEASE */ |
44 |
|
|
/* */ |
45 |
|
|
/* _fx_directory_next_entry_find PORTABLE C */ |
46 |
|
|
/* 6.1 */ |
47 |
|
|
/* AUTHOR */ |
48 |
|
|
/* */ |
49 |
|
|
/* William E. Lamie, Microsoft Corporation */ |
50 |
|
|
/* */ |
51 |
|
|
/* DESCRIPTION */ |
52 |
|
|
/* */ |
53 |
|
|
/* This function returns the name of the next entry in the current */ |
54 |
|
|
/* working directory. The function that returns the first name in the */ |
55 |
|
|
/* current directory must be called prior to this function. */ |
56 |
|
|
/* */ |
57 |
|
|
/* INPUT */ |
58 |
|
|
/* */ |
59 |
|
|
/* media_ptr Media control block pointer */ |
60 |
|
|
/* directory_name Destination for directory */ |
61 |
|
|
/* name */ |
62 |
|
|
/* */ |
63 |
|
|
/* OUTPUT */ |
64 |
|
|
/* */ |
65 |
|
|
/* return status */ |
66 |
|
|
/* */ |
67 |
|
|
/* CALLS */ |
68 |
|
|
/* */ |
69 |
|
|
/* _fx_directory_entry_read Read entries from root dir */ |
70 |
|
|
/* _fx_utility_FAT_entry_read Read FAT entries to calculate */ |
71 |
|
|
/* the sub-directory size */ |
72 |
|
|
/* */ |
73 |
|
|
/* CALLED BY */ |
74 |
|
|
/* */ |
75 |
|
|
/* Application Code and */ |
76 |
|
|
/* FileX System Functions */ |
77 |
|
|
/* */ |
78 |
|
|
/* RELEASE HISTORY */ |
79 |
|
|
/* */ |
80 |
|
|
/* DATE NAME DESCRIPTION */ |
81 |
|
|
/* */ |
82 |
|
|
/* 05-19-2020 William E. Lamie Initial Version 6.0 */ |
83 |
|
|
/* 09-30-2020 William E. Lamie Modified comment(s), */ |
84 |
|
|
/* resulting in version 6.1 */ |
85 |
|
|
/* */ |
86 |
|
|
/**************************************************************************/ |
87 |
|
519 |
UINT _fx_directory_next_entry_find(FX_MEDIA *media_ptr, CHAR *directory_name) |
88 |
|
|
{ |
89 |
|
|
|
90 |
|
|
ULONG i; |
91 |
|
|
UINT status; |
92 |
|
|
UINT temp_status; |
93 |
|
519 |
ULONG cluster, next_cluster = 0; |
94 |
|
|
ULONG64 directory_size; |
95 |
|
|
FX_DIR_ENTRY entry; |
96 |
|
|
FX_DIR_ENTRY *search_dir_ptr; |
97 |
|
|
FX_PATH *path_ptr; |
98 |
|
|
#ifndef FX_MEDIA_DISABLE_SEARCH_CACHE |
99 |
|
|
UINT index; |
100 |
|
519 |
CHAR *path_string_ptr = FX_NULL; |
101 |
|
|
#endif |
102 |
|
|
|
103 |
|
|
|
104 |
|
|
#ifndef FX_MEDIA_STATISTICS_DISABLE |
105 |
|
|
|
106 |
|
|
/* Increment the number of times this service has been called. */ |
107 |
|
519 |
media_ptr -> fx_media_directory_next_entry_finds++; |
108 |
|
|
#endif |
109 |
|
|
|
110 |
|
|
/* Setup pointer to media name buffer. */ |
111 |
|
519 |
entry.fx_dir_entry_name = media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN; |
112 |
|
|
|
113 |
|
|
/* Clear the short name string. */ |
114 |
|
519 |
entry.fx_dir_entry_short_name[0] = 0; |
115 |
|
|
|
116 |
|
|
/* Check the media to make sure it is open. */ |
117 |
✓✓ |
519 |
if (media_ptr -> fx_media_id != FX_MEDIA_ID) |
118 |
|
|
{ |
119 |
|
|
|
120 |
|
|
/* Return the media not opened error. */ |
121 |
|
1 |
return(FX_MEDIA_NOT_OPEN); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
125 |
|
|
FX_TRACE_IN_LINE_INSERT(FX_TRACE_DIRECTORY_NEXT_ENTRY_FIND, media_ptr, directory_name, 0, 0, FX_TRACE_DIRECTORY_EVENTS, 0, 0) |
126 |
|
|
|
127 |
|
|
/* Protect against other threads accessing the media. */ |
128 |
|
518 |
FX_PROTECT |
129 |
|
|
|
130 |
|
|
/* First check for a local path pointer stored in the thread control block. This |
131 |
|
|
is only available in ThreadX Version 4 and above. */ |
132 |
|
|
|
133 |
|
|
#ifndef FX_NO_LOCAL_PATH |
134 |
|
|
|
135 |
✓✓ |
518 |
if (_tx_thread_current_ptr -> tx_thread_filex_ptr) |
136 |
|
|
{ |
137 |
|
|
|
138 |
|
|
/* Setup the default path pointer. */ |
139 |
|
322 |
path_ptr = (FX_PATH *)_tx_thread_current_ptr -> tx_thread_filex_ptr; |
140 |
|
|
|
141 |
|
|
/* Determine if we are at the root directory. */ |
142 |
✓✓ |
322 |
if (path_ptr -> fx_path_directory.fx_dir_entry_name[0]) |
143 |
|
|
{ |
144 |
|
|
|
145 |
|
|
/* No, we are not at the root directory. */ |
146 |
|
|
|
147 |
|
|
#ifndef FX_MEDIA_DISABLE_SEARCH_CACHE |
148 |
|
|
|
149 |
|
|
/* Setup pointer to the path. */ |
150 |
|
301 |
path_string_ptr = ((FX_PATH *)_tx_thread_current_ptr -> tx_thread_filex_ptr) -> fx_path_string; |
151 |
|
|
#endif |
152 |
|
|
|
153 |
|
|
/* Set the internal pointer to the search directory as well. */ |
154 |
|
301 |
search_dir_ptr = &(path_ptr -> fx_path_directory); |
155 |
|
|
} |
156 |
|
|
else |
157 |
|
|
{ |
158 |
|
|
|
159 |
|
|
/* The current default directory is the root so just set the |
160 |
|
|
search directory pointer to NULL. */ |
161 |
|
21 |
search_dir_ptr = FX_NULL; |
162 |
|
|
} |
163 |
|
|
} |
164 |
|
|
else |
165 |
|
|
#endif |
166 |
|
|
|
167 |
|
|
/* Set the initial search directory to the current working |
168 |
|
|
directory - if there is one. */ |
169 |
✓✓ |
196 |
if (media_ptr -> fx_media_default_path.fx_path_directory.fx_dir_entry_name[0]) |
170 |
|
|
{ |
171 |
|
|
|
172 |
|
|
/* Setup the path pointer to the global media path. */ |
173 |
|
117 |
path_ptr = &media_ptr -> fx_media_default_path; |
174 |
|
|
|
175 |
|
|
#ifndef FX_MEDIA_DISABLE_SEARCH_CACHE |
176 |
|
|
|
177 |
|
|
/* Setup pointer to the path. */ |
178 |
|
117 |
path_string_ptr = media_ptr -> fx_media_default_path.fx_path_string; |
179 |
|
|
#endif |
180 |
|
|
|
181 |
|
|
/* Set the internal pointer to the search directory as well. */ |
182 |
|
117 |
search_dir_ptr = &(path_ptr -> fx_path_directory); |
183 |
|
|
} |
184 |
|
|
else |
185 |
|
|
{ |
186 |
|
|
|
187 |
|
|
/* Setup the path pointer to the global media path. */ |
188 |
|
79 |
path_ptr = &media_ptr -> fx_media_default_path; |
189 |
|
|
|
190 |
|
|
/* The current default directory is the root so just set the |
191 |
|
|
search directory pointer to NULL. */ |
192 |
|
79 |
search_dir_ptr = FX_NULL; |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
/* Calculate the directory size. */ |
196 |
✓✓ |
518 |
if (search_dir_ptr) |
197 |
|
|
{ |
198 |
|
|
#ifdef FX_ENABLE_EXFAT |
199 |
|
|
if (media_ptr -> fx_media_FAT_type == FX_exFAT) |
200 |
|
|
{ |
201 |
|
|
directory_size = search_dir_ptr -> fx_dir_entry_file_size / FX_DIR_ENTRY_SIZE; |
202 |
|
|
} |
203 |
|
|
else |
204 |
|
|
{ |
205 |
|
|
#endif /* FX_ENABLE_EXFAT */ |
206 |
|
|
/* Determine the directory size. */ |
207 |
✓✓ |
418 |
if (path_ptr -> fx_path_current_entry != 0) |
208 |
|
|
{ |
209 |
|
|
|
210 |
|
|
/* Pickup the previously saved directory size. */ |
211 |
|
334 |
directory_size = search_dir_ptr -> fx_dir_entry_file_size; |
212 |
|
|
} |
213 |
|
|
else |
214 |
|
|
{ |
215 |
|
|
|
216 |
|
|
/* This should only be done on the first time into next directory find. */ |
217 |
|
|
|
218 |
|
|
/* Ensure that the search directory's last search cluster is cleared. */ |
219 |
|
84 |
search_dir_ptr -> fx_dir_entry_last_search_cluster = 0; |
220 |
|
|
|
221 |
|
|
/* Calculate the directory size by counting the allocated |
222 |
|
|
clusters for it. */ |
223 |
|
84 |
i = 0; |
224 |
|
84 |
cluster = search_dir_ptr -> fx_dir_entry_cluster; |
225 |
✓✓ |
309 |
while (cluster < media_ptr -> fx_media_fat_reserved) |
226 |
|
|
{ |
227 |
|
|
|
228 |
|
|
/* Increment the cluster count. */ |
229 |
|
229 |
i++; |
230 |
|
|
|
231 |
|
|
/* Read the next FAT entry. */ |
232 |
|
229 |
status = _fx_utility_FAT_entry_read(media_ptr, cluster, &next_cluster); |
233 |
|
|
|
234 |
|
|
/* Check the return status. */ |
235 |
✓✓ |
229 |
if (status != FX_SUCCESS) |
236 |
|
|
{ |
237 |
|
|
|
238 |
|
|
/* Release media protection. */ |
239 |
|
1 |
FX_UNPROTECT |
240 |
|
|
|
241 |
|
|
/* Return the bad status. */ |
242 |
|
1 |
return(status); |
243 |
|
|
} |
244 |
|
|
|
245 |
✓✓✓✓ ✓✓ |
228 |
if ((cluster < FX_FAT_ENTRY_START) || (cluster == next_cluster) || (i > media_ptr -> fx_media_total_clusters)) |
246 |
|
|
{ |
247 |
|
|
|
248 |
|
|
/* Release media protection. */ |
249 |
|
3 |
FX_UNPROTECT |
250 |
|
|
|
251 |
|
|
/* Return the bad status. */ |
252 |
|
3 |
return(FX_FAT_READ_ERROR); |
253 |
|
|
} |
254 |
|
|
|
255 |
|
225 |
cluster = next_cluster; |
256 |
|
|
} |
257 |
|
|
|
258 |
|
|
/* Now we can calculate the directory size. */ |
259 |
|
80 |
directory_size = (((ULONG64)media_ptr -> fx_media_bytes_per_sector) * |
260 |
|
80 |
((ULONG64)media_ptr -> fx_media_sectors_per_cluster) * i) / |
261 |
|
|
(ULONG64)FX_DIR_ENTRY_SIZE; |
262 |
|
|
|
263 |
|
|
/* Save how many entries there are in the directory. */ |
264 |
|
80 |
search_dir_ptr -> fx_dir_entry_file_size = directory_size; |
265 |
|
|
} |
266 |
|
|
#ifdef FX_ENABLE_EXFAT |
267 |
|
|
} |
268 |
|
|
#endif /* FX_ENABLE_EXFAT */ |
269 |
|
|
} |
270 |
|
|
else |
271 |
|
|
{ |
272 |
|
|
|
273 |
|
|
/* Directory size is the number of entries in the root directory. */ |
274 |
|
100 |
directory_size = (ULONG)media_ptr -> fx_media_root_directory_entries; |
275 |
|
|
} |
276 |
|
|
|
277 |
|
|
/* Preset status with an error return. */ |
278 |
|
514 |
status = FX_NO_MORE_ENTRIES; |
279 |
|
|
|
280 |
|
|
/* Determine if the current entry is inside of the directory's range. */ |
281 |
✓✓ |
525 |
while (path_ptr -> fx_path_current_entry < directory_size) |
282 |
|
|
{ |
283 |
|
|
|
284 |
|
|
/* Read an entry from the directory. */ |
285 |
|
524 |
temp_status = _fx_directory_entry_read(media_ptr, search_dir_ptr, |
286 |
|
|
&(path_ptr -> fx_path_current_entry), &entry); |
287 |
|
|
|
288 |
|
|
/* Check for error status. */ |
289 |
✓✓ |
524 |
if (temp_status != FX_SUCCESS) |
290 |
|
|
{ |
291 |
|
|
|
292 |
|
|
/* Release media protection. */ |
293 |
|
10 |
FX_UNPROTECT |
294 |
|
|
|
295 |
|
|
/* Return error status. */ |
296 |
|
10 |
return(temp_status); |
297 |
|
|
} |
298 |
|
|
#ifdef FX_ENABLE_EXFAT |
299 |
|
|
if (entry.fx_dir_entry_type == FX_EXFAT_DIR_ENTRY_TYPE_END_MARKER) |
300 |
|
|
{ |
301 |
|
|
/* Set the error code. */ |
302 |
|
|
status = FX_NO_MORE_ENTRIES; |
303 |
|
|
|
304 |
|
|
/* Get out of the loop. */ |
305 |
|
|
break; |
306 |
|
|
} |
307 |
|
|
#endif /* FX_ENABLE_EXFAT */ |
308 |
|
|
|
309 |
|
|
/* Check to see if the entry has something in it. */ |
310 |
|
|
#ifdef FX_ENABLE_EXFAT |
311 |
|
|
else if (entry.fx_dir_entry_type != FX_EXFAT_DIR_ENTRY_TYPE_FILE_DIRECTORY) |
312 |
|
|
#else |
313 |
✓✓✓✓
|
514 |
if (((UCHAR)entry.fx_dir_entry_name[0] == (UCHAR)FX_DIR_ENTRY_FREE) && (entry.fx_dir_entry_short_name[0] == 0)) |
314 |
|
|
#endif /* FX_ENABLE_EXFAT */ |
315 |
|
|
{ |
316 |
|
|
|
317 |
|
|
/* Current entry is free, skip to next entry and continue the loop. */ |
318 |
|
11 |
path_ptr -> fx_path_current_entry++; |
319 |
|
11 |
continue; |
320 |
|
|
} |
321 |
|
|
#ifdef FX_ENABLE_EXFAT |
322 |
|
|
else /* FX_EXFAT_DIR_ENTRY_TYPE_FILE_DIRECTORY */ |
323 |
|
|
#else |
324 |
✓✓ |
503 |
else if ((UCHAR)entry.fx_dir_entry_name[0] != (UCHAR)FX_DIR_ENTRY_DONE) |
325 |
|
|
#endif /* FX_ENABLE_EXFAT */ |
326 |
|
|
{ |
327 |
|
|
|
328 |
|
|
/* A valid directory entry is present. */ |
329 |
|
|
|
330 |
|
|
/* Copy the name into the destination. */ |
331 |
✓✓ |
5078 |
for (i = 0; entry.fx_dir_entry_name[i]; i++) |
332 |
|
|
{ |
333 |
|
|
|
334 |
|
4648 |
*directory_name = entry.fx_dir_entry_name[i]; |
335 |
|
4648 |
directory_name++; |
336 |
|
|
} |
337 |
|
|
|
338 |
|
|
/* Place a NULL at the end of the directory name. */ |
339 |
|
430 |
*directory_name = (CHAR)0; |
340 |
|
|
|
341 |
|
|
/* Increment the current entry for the media. */ |
342 |
|
430 |
path_ptr -> fx_path_current_entry++; |
343 |
|
|
|
344 |
|
|
#ifndef FX_MEDIA_DISABLE_SEARCH_CACHE |
345 |
|
|
{ |
346 |
|
|
UINT v, j; |
347 |
|
|
|
348 |
|
|
|
349 |
|
|
/* If a subsequent search for the same name is done, it will find it immediately. */ |
350 |
|
|
|
351 |
|
|
/* Set the index of the saved name string. */ |
352 |
|
430 |
v = 0; |
353 |
|
|
|
354 |
|
|
/* First, build the full path and name. */ |
355 |
✓✓ |
430 |
if (path_string_ptr) |
356 |
|
|
{ |
357 |
|
|
|
358 |
|
|
/* Copy the path into the destination. */ |
359 |
✓✓✓✓
|
10276 |
while ((v < (FX_MAX_LAST_NAME_LEN - 1)) && (path_string_ptr[v])) |
360 |
|
|
{ |
361 |
|
|
|
362 |
|
|
/* Copy one character. */ |
363 |
|
9940 |
media_ptr -> fx_media_last_found_name[v] = path_string_ptr[v]; |
364 |
|
|
|
365 |
|
|
/* Move to next character. */ |
366 |
|
9940 |
v++; |
367 |
|
|
} |
368 |
|
|
} |
369 |
|
|
|
370 |
|
|
/* We know there is room at this point, place a directory separator character. */ |
371 |
|
430 |
media_ptr -> fx_media_last_found_name[v++] = '/'; |
372 |
|
|
|
373 |
|
|
/* Now append the name to the path. */ |
374 |
|
430 |
j = 0; |
375 |
✓✓✓✓
|
5049 |
while ((v < FX_MAX_LAST_NAME_LEN) && (entry.fx_dir_entry_name[j])) |
376 |
|
|
{ |
377 |
|
|
|
378 |
|
|
/* Copy one character. */ |
379 |
|
4619 |
media_ptr -> fx_media_last_found_name[v] = entry.fx_dir_entry_name[j]; |
380 |
|
|
|
381 |
|
|
/* Move to next character. */ |
382 |
|
4619 |
v++; |
383 |
|
4619 |
j++; |
384 |
|
|
} |
385 |
|
|
|
386 |
|
|
/* Null terminate the last name string. */ |
387 |
✓✓ |
430 |
if (v < FX_MAX_LAST_NAME_LEN) |
388 |
|
|
{ |
389 |
|
|
|
390 |
|
|
/* Null terminate. */ |
391 |
|
423 |
media_ptr -> fx_media_last_found_name[v] = FX_NULL; |
392 |
|
|
} |
393 |
|
|
else |
394 |
|
|
{ |
395 |
|
|
|
396 |
|
|
/* The string is too big, NULL the string so it won't be used in searching. */ |
397 |
|
7 |
media_ptr -> fx_media_last_found_name[0] = FX_NULL; |
398 |
|
|
} |
399 |
|
|
|
400 |
|
|
/* Determine if there is a search pointer. */ |
401 |
✓✓ |
430 |
if (search_dir_ptr) |
402 |
|
|
{ |
403 |
|
|
|
404 |
|
|
/* Yes, there is a search directory pointer so save it! */ |
405 |
|
336 |
media_ptr -> fx_media_last_found_directory = *search_dir_ptr; |
406 |
|
|
|
407 |
|
|
/* Indicate the search directory is valid. */ |
408 |
|
336 |
media_ptr -> fx_media_last_found_directory_valid = FX_TRUE; |
409 |
|
|
} |
410 |
|
|
else |
411 |
|
|
{ |
412 |
|
|
|
413 |
|
|
/* Indicate the search directory is not valid. */ |
414 |
|
94 |
media_ptr -> fx_media_last_found_directory_valid = FX_FALSE; |
415 |
|
|
} |
416 |
|
|
|
417 |
|
|
/* Copy the directory entry. */ |
418 |
|
430 |
media_ptr -> fx_media_last_found_entry = entry; |
419 |
|
|
|
420 |
|
|
/* Setup the last found directory entry to point at the last found internal file name. */ |
421 |
|
430 |
media_ptr -> fx_media_last_found_entry.fx_dir_entry_name = media_ptr -> fx_media_last_found_file_name; |
422 |
|
|
|
423 |
|
|
/* Copy the actual directory name into the cached directory name. */ |
424 |
✓✓ |
6591 |
for (index = 0; index < FX_MAX_LONG_NAME_LEN; index++) |
425 |
|
|
{ |
426 |
|
|
|
427 |
|
|
/* Copy character into the cached directory name. */ |
428 |
|
6584 |
media_ptr -> fx_media_last_found_file_name[index] = entry.fx_dir_entry_name[index]; |
429 |
|
|
|
430 |
|
|
/* See if we have copied the NULL termination character. */ |
431 |
✓✓ |
6584 |
if (entry.fx_dir_entry_name[index] == (CHAR)FX_NULL) |
432 |
|
|
{ |
433 |
|
|
|
434 |
|
|
/* Check to see if we use the break to get out of the loop. */ |
435 |
✓✓ |
478 |
if (v < (FX_MAX_LONG_NAME_LEN - 1)) |
436 |
|
|
{ |
437 |
|
|
|
438 |
|
|
/* Yes, not at the end of the string, break. */ |
439 |
|
423 |
break; |
440 |
|
|
} |
441 |
|
|
} |
442 |
|
|
} |
443 |
|
|
} |
444 |
|
|
#endif |
445 |
|
|
|
446 |
|
|
/* Set return status to success. */ |
447 |
|
430 |
status = FX_SUCCESS; |
448 |
|
|
|
449 |
|
|
/* Get out of the loop. */ |
450 |
|
430 |
break; |
451 |
|
|
} |
452 |
|
|
#ifndef FX_ENABLE_EXFAT |
453 |
|
|
else |
454 |
|
|
{ |
455 |
|
|
|
456 |
|
|
/* Set the error code. */ |
457 |
|
73 |
status = FX_NO_MORE_ENTRIES; |
458 |
|
|
|
459 |
|
|
/* Get out of the loop. */ |
460 |
|
73 |
break; |
461 |
|
|
} |
462 |
|
|
#endif /* FX_ENABLE_EXFAT */ |
463 |
|
|
} |
464 |
|
|
|
465 |
|
|
/* Release media protection. */ |
466 |
|
504 |
FX_UNPROTECT |
467 |
|
|
|
468 |
|
|
/* Return status to the caller. */ |
469 |
|
504 |
return(status); |
470 |
|
|
} |
471 |
|
|
|