1 |
|
|
/*************************************************************************** |
2 |
|
|
* Copyright (c) 2024 Microsoft Corporation |
3 |
|
|
* Copyright (c) 2026-present Eclipse ThreadX contributors |
4 |
|
|
* |
5 |
|
|
* This program and the accompanying materials are made available under the |
6 |
|
|
* terms of the MIT License which is available at |
7 |
|
|
* https://opensource.org/licenses/MIT. |
8 |
|
|
* |
9 |
|
|
* SPDX-License-Identifier: MIT |
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 |
|
|
|
33 |
|
|
#ifndef FX_NO_LOCAL_PATH |
34 |
|
|
FX_LOCAL_PATH_SETUP |
35 |
|
|
#endif |
36 |
|
|
|
37 |
|
|
|
38 |
|
|
/**************************************************************************/ |
39 |
|
|
/* */ |
40 |
|
|
/* FUNCTION RELEASE */ |
41 |
|
|
/* */ |
42 |
|
|
/* _fx_directory_search PORTABLE C */ |
43 |
|
|
/* 6.1.10 */ |
44 |
|
|
/* AUTHOR */ |
45 |
|
|
/* */ |
46 |
|
|
/* William E. Lamie, Microsoft Corporation */ |
47 |
|
|
/* */ |
48 |
|
|
/* DESCRIPTION */ |
49 |
|
|
/* */ |
50 |
|
|
/* This function searches the media for the supplied name. The search */ |
51 |
|
|
/* routine will find files as well as directory names. */ |
52 |
|
|
/* */ |
53 |
|
|
/* INPUT */ |
54 |
|
|
/* */ |
55 |
|
|
/* media_ptr Media control block pointer */ |
56 |
|
|
/* name_ptr Name pointer */ |
57 |
|
|
/* entry_ptr Pointer to directory entry */ |
58 |
|
|
/* record */ |
59 |
|
|
/* last_dir_ptr Pointer to destination for */ |
60 |
|
|
/* the last directory entry */ |
61 |
|
|
/* last_name_ptr Pointer to the last name */ |
62 |
|
|
/* token that was searched for */ |
63 |
|
|
/* */ |
64 |
|
|
/* OUTPUT */ |
65 |
|
|
/* */ |
66 |
|
|
/* return status */ |
67 |
|
|
/* */ |
68 |
|
|
/* CALLS */ |
69 |
|
|
/* */ |
70 |
|
|
/* _fx_directory_name_extract Extract directory name from */ |
71 |
|
|
/* input string */ |
72 |
|
|
/* _fx_directory_entry_read Read entries from root dir */ |
73 |
|
|
/* _fx_utility_FAT_entry_read Read FAT entries to calculate */ |
74 |
|
|
/* the sub-directory size */ |
75 |
|
|
/* */ |
76 |
|
|
/* CALLED BY */ |
77 |
|
|
/* */ |
78 |
|
|
/* FileX System Functions */ |
79 |
|
|
/* */ |
80 |
|
|
/**************************************************************************/ |
81 |
|
97598 |
UINT _fx_directory_search(FX_MEDIA *media_ptr, CHAR *name_ptr, FX_DIR_ENTRY *entry_ptr, |
82 |
|
|
FX_DIR_ENTRY *last_dir_ptr, CHAR **last_name_ptr) |
83 |
|
|
{ |
84 |
|
|
|
85 |
|
|
ULONG i, n; |
86 |
|
|
UINT found; |
87 |
|
|
UINT status; |
88 |
|
|
#ifndef FX_MEDIA_DISABLE_SEARCH_CACHE |
89 |
|
|
UINT v, j; |
90 |
|
|
#endif /* FX_MEDIA_DISABLE_SEARCH_CACHE */ |
91 |
|
97598 |
ULONG cluster, next_cluster = 0; |
92 |
|
|
ULONG64 directory_size; |
93 |
|
|
CHAR *dir_name_ptr; |
94 |
|
|
CHAR *work_ptr; |
95 |
|
|
CHAR *source_name_ptr; |
96 |
|
|
CHAR *destination_name_ptr; |
97 |
|
|
FX_DIR_ENTRY search_dir; |
98 |
|
|
FX_DIR_ENTRY *search_dir_ptr; |
99 |
|
|
CHAR *name, alpha, name_alpha; |
100 |
|
|
#ifndef FX_MEDIA_DISABLE_SEARCH_CACHE |
101 |
|
|
UINT index; |
102 |
|
97598 |
CHAR *path_ptr = FX_NULL; |
103 |
|
97598 |
CHAR *original_name = name_ptr; |
104 |
|
|
#endif |
105 |
|
|
|
106 |
|
|
#ifndef FX_MEDIA_STATISTICS_DISABLE |
107 |
|
|
|
108 |
|
|
/* Increment the number of directory search requests. */ |
109 |
|
97598 |
media_ptr -> fx_media_directory_searches++; |
110 |
|
|
#endif |
111 |
|
|
|
112 |
|
|
/* Setup pointer to media name buffer. */ |
113 |
|
97598 |
name = media_ptr -> fx_media_name_buffer; |
114 |
|
|
|
115 |
|
|
/* Setup the last directory, if required. */ |
116 |
✓✓ |
97598 |
if (last_dir_ptr) |
117 |
|
|
{ |
118 |
|
|
|
119 |
|
|
/* Set the first character of the directory entry to NULL to |
120 |
|
|
indicate root or no directory. */ |
121 |
|
77838 |
last_dir_ptr -> fx_dir_entry_name[0] = 0; |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
/* Determine if the file name has a full directory path. */ |
125 |
✓✓✓✓
|
97598 |
if ((*name_ptr == '\\') || (*name_ptr == '/')) |
126 |
|
|
{ |
127 |
|
|
|
128 |
|
|
/* Directory name has full path, set the search pointer to NULL. */ |
129 |
|
14653 |
search_dir_ptr = FX_NULL; |
130 |
|
|
} |
131 |
|
|
else |
132 |
|
|
{ |
133 |
|
|
|
134 |
|
|
/* Set the initial search directory to the current working |
135 |
|
|
directory - if there is one. */ |
136 |
|
|
|
137 |
|
|
/* First check for a local path pointer stored in the thread control block. This |
138 |
|
|
is only available in ThreadX Version 4 and above. */ |
139 |
|
|
#ifndef FX_NO_LOCAL_PATH |
140 |
✓✓ |
82945 |
if (_tx_thread_current_ptr -> tx_thread_filex_ptr) |
141 |
|
|
{ |
142 |
|
|
|
143 |
|
|
/* Determine if the local directory is not the root directory. */ |
144 |
✓✓ |
746 |
if (((FX_PATH *)_tx_thread_current_ptr -> tx_thread_filex_ptr) -> fx_path_directory.fx_dir_entry_name[0]) |
145 |
|
|
{ |
146 |
|
|
|
147 |
|
|
/* Start at the current working directory of the media. */ |
148 |
|
686 |
search_dir = ((FX_PATH *)_tx_thread_current_ptr -> tx_thread_filex_ptr) -> fx_path_directory; |
149 |
|
|
|
150 |
|
|
#ifndef FX_MEDIA_DISABLE_SEARCH_CACHE |
151 |
|
|
|
152 |
|
|
/* Setup pointer to the path. */ |
153 |
|
686 |
path_ptr = ((FX_PATH *)_tx_thread_current_ptr -> tx_thread_filex_ptr) -> fx_path_string; |
154 |
|
|
#endif |
155 |
|
|
|
156 |
|
|
/* Set the internal pointer to the search directory as well. */ |
157 |
|
686 |
search_dir_ptr = &search_dir; |
158 |
|
|
} |
159 |
|
|
else |
160 |
|
|
{ |
161 |
|
|
|
162 |
|
|
/* We are searching in the root directory. */ |
163 |
|
60 |
search_dir_ptr = FX_NULL; |
164 |
|
|
} |
165 |
|
|
} |
166 |
|
|
else |
167 |
|
|
#endif |
168 |
✓✓ |
82199 |
if (media_ptr -> fx_media_default_path.fx_path_directory.fx_dir_entry_name[0]) |
169 |
|
|
{ |
170 |
|
|
|
171 |
|
|
/* Start at the current working directory of the media. */ |
172 |
|
466 |
search_dir = media_ptr -> fx_media_default_path.fx_path_directory; |
173 |
|
|
|
174 |
|
|
#ifndef FX_MEDIA_DISABLE_SEARCH_CACHE |
175 |
|
|
|
176 |
|
|
/* Setup pointer to the path. */ |
177 |
|
466 |
path_ptr = media_ptr -> fx_media_default_path.fx_path_string; |
178 |
|
|
#endif |
179 |
|
|
|
180 |
|
|
/* Set the internal pointer to the search directory as well. */ |
181 |
|
466 |
search_dir_ptr = &search_dir; |
182 |
|
|
} |
183 |
|
|
else |
184 |
|
|
{ |
185 |
|
|
|
186 |
|
|
/* The current default directory is the root so just set the |
187 |
|
|
search directory pointer to NULL. */ |
188 |
|
81733 |
search_dir_ptr = FX_NULL; |
189 |
|
|
} |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
#ifndef FX_MEDIA_DISABLE_SEARCH_CACHE |
193 |
|
|
|
194 |
|
|
/* Determine if there is a previously found directory entry. */ |
195 |
✓✓ |
97598 |
if (media_ptr -> fx_media_last_found_name[0]) |
196 |
|
|
{ |
197 |
|
|
|
198 |
|
|
UINT match; |
199 |
|
|
CHAR *temp_ptr, beta; |
200 |
|
|
|
201 |
|
|
/* Yes, there is a previously found directory in our cache. */ |
202 |
|
|
|
203 |
|
|
/* Initialize the index. */ |
204 |
|
21137 |
v = 0; |
205 |
|
|
|
206 |
|
|
/* Determine if there is a full path. */ |
207 |
✓✓✓✓
|
21137 |
if ((*name_ptr == '\\') || (*name_ptr == '/')) |
208 |
|
|
{ |
209 |
|
|
|
210 |
|
|
/* Yes, the full path is in the name buffer. Simply compare with what is in |
211 |
|
|
the last search buffer. */ |
212 |
✓✓✓✓
|
3847 |
while ((v < (FX_MAX_LAST_NAME_LEN - 1)) && (name_ptr[v])) |
213 |
|
|
{ |
214 |
|
|
|
215 |
|
|
/* Pickup the respective name characters. */ |
216 |
|
3827 |
alpha = name_ptr[v]; |
217 |
|
3827 |
beta = media_ptr -> fx_media_last_found_name[v]; |
218 |
|
|
|
219 |
|
|
/* Ensure directory markers are the same. */ |
220 |
✓✓ |
3827 |
if (alpha == '\\') |
221 |
|
|
{ |
222 |
|
17 |
alpha = '/'; |
223 |
|
|
} |
224 |
✓✓ |
3827 |
if (beta == '\\') |
225 |
|
|
{ |
226 |
|
14 |
beta = '/'; |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
/* Is the name the same? */ |
230 |
✓✓ |
3827 |
if (alpha != beta) |
231 |
|
|
{ |
232 |
|
|
|
233 |
|
|
/* Break out of loop! */ |
234 |
|
23 |
break; |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
/* Move to next character. */ |
238 |
|
3804 |
v++; |
239 |
|
|
} |
240 |
|
|
|
241 |
|
|
/* Determine if we have a match. */ |
242 |
✓✓ |
43 |
if (name_ptr[v] != media_ptr -> fx_media_last_found_name[v]) |
243 |
|
|
{ |
244 |
|
25 |
match = FX_FALSE; |
245 |
|
|
} |
246 |
|
|
else |
247 |
|
|
{ |
248 |
|
18 |
match = FX_TRUE; |
249 |
|
|
} |
250 |
|
|
} |
251 |
|
|
else |
252 |
|
|
{ |
253 |
|
|
|
254 |
|
|
/* Default to found. */ |
255 |
|
21094 |
match = FX_TRUE; |
256 |
|
|
|
257 |
|
|
/* Determine if there is a default path to compare with. */ |
258 |
✓✓ |
21094 |
if (path_ptr) |
259 |
|
|
{ |
260 |
|
|
|
261 |
|
|
/* Yes, compare the current path with what is contained in the last |
262 |
|
|
found buffer. Note that the last found name must have at least one |
263 |
|
|
path separator as well as room for at least one character for a name. */ |
264 |
✓✓✓✓
|
28971 |
while ((v < (FX_MAX_LAST_NAME_LEN - 1)) && (path_ptr[v])) |
265 |
|
|
{ |
266 |
|
|
|
267 |
|
|
/* Pickup the respective name characters. */ |
268 |
|
28285 |
alpha = media_ptr -> fx_media_last_found_name[v]; |
269 |
|
28285 |
beta = path_ptr[v]; |
270 |
|
|
|
271 |
|
|
/* Ensure directory markers are the same. */ |
272 |
✓✓ |
28285 |
if (alpha == '\\') |
273 |
|
|
{ |
274 |
|
1504 |
alpha = '/'; |
275 |
|
|
} |
276 |
✓✓ |
28285 |
if (beta == '\\') |
277 |
|
|
{ |
278 |
|
1653 |
beta = '/'; |
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
/* Is the name the same? */ |
282 |
✓✓ |
28285 |
if (alpha != beta) |
283 |
|
|
{ |
284 |
|
|
|
285 |
|
|
/* Break out of loop! */ |
286 |
|
10 |
break; |
287 |
|
|
} |
288 |
|
|
|
289 |
|
|
/* Move to next character. */ |
290 |
|
28275 |
v++; |
291 |
|
|
} |
292 |
|
|
|
293 |
|
|
/* Determine if we don't have a match... The relative path must be exhausted. */ |
294 |
✓✓ |
696 |
if (path_ptr[v]) |
295 |
|
|
{ |
296 |
|
10 |
match = FX_FALSE; |
297 |
|
|
} |
298 |
|
|
} |
299 |
|
|
|
300 |
|
|
/* Determine if we still have a match. */ |
301 |
✓✓ |
21094 |
if (match) |
302 |
|
|
{ |
303 |
|
|
|
304 |
|
|
/* Now examine the rest of the last name and the newly supplied |
305 |
|
|
input name. */ |
306 |
|
|
|
307 |
|
|
/* Determine if a valid directory separator is present. */ |
308 |
✓✓ |
21084 |
if ((media_ptr -> fx_media_last_found_name[v] != '\\') && |
309 |
✓✓ |
21077 |
(media_ptr -> fx_media_last_found_name[v] != '/')) |
310 |
|
|
{ |
311 |
|
|
|
312 |
|
|
/* Set match to false - invalid directory path separator. */ |
313 |
|
166 |
match = FX_FALSE; |
314 |
|
|
} |
315 |
|
|
else |
316 |
|
|
{ |
317 |
|
|
/* Position past the next directory separator in the |
318 |
|
|
last name string. */ |
319 |
|
20918 |
v++; |
320 |
|
|
} |
321 |
|
|
|
322 |
|
|
/* Yes, the full path is in the name buffer. Simply compare with what is in |
323 |
|
|
the last search buffer. */ |
324 |
|
21084 |
j = 0; |
325 |
✓✓✓✓ ✓✓ |
104709 |
while ((v < (FX_MAX_LAST_NAME_LEN - 1)) && (name_ptr[j]) && (match)) |
326 |
|
|
{ |
327 |
|
|
|
328 |
|
|
/* Pickup the respective name characters. */ |
329 |
|
104000 |
alpha = name_ptr[j]; |
330 |
|
104000 |
beta = media_ptr -> fx_media_last_found_name[v]; |
331 |
|
|
|
332 |
|
|
/* Ensure directory markers are the same. */ |
333 |
✓✓ |
104000 |
if (alpha == '\\') |
334 |
|
|
{ |
335 |
|
6 |
alpha = '/'; |
336 |
|
|
} |
337 |
✓✓ |
104000 |
if (beta == '\\') |
338 |
|
|
{ |
339 |
|
6 |
beta = '/'; |
340 |
|
|
} |
341 |
|
|
|
342 |
|
|
/* Is the name the same? */ |
343 |
✓✓ |
104000 |
if (alpha != beta) |
344 |
|
|
{ |
345 |
|
|
|
346 |
|
|
/* Break out of loop! */ |
347 |
|
20375 |
break; |
348 |
|
|
} |
349 |
|
|
|
350 |
|
|
/* Move to next character. */ |
351 |
|
83625 |
v++; |
352 |
|
83625 |
j++; |
353 |
|
|
} |
354 |
|
|
|
355 |
|
|
/* Avoid accessing fx_media_last_found_name out of bounds. */ |
356 |
✓✓ |
21084 |
if (v >= 256) |
357 |
|
|
{ |
358 |
|
1 |
match = FX_FALSE; |
359 |
|
|
} |
360 |
✓✓✓✓
|
21083 |
else if ((match) && (name_ptr[j] != media_ptr -> fx_media_last_found_name[v])) |
361 |
|
|
{ |
362 |
|
|
|
363 |
|
|
/* We don't have a match. */ |
364 |
|
20402 |
match = FX_FALSE; |
365 |
|
|
} |
366 |
|
|
} |
367 |
|
|
} |
368 |
|
|
|
369 |
|
|
/* Now determine if we actually found a match. */ |
370 |
✓✓ |
21137 |
if (match) |
371 |
|
|
{ |
372 |
|
|
|
373 |
|
|
/* Save the directory entry name pointer. */ |
374 |
|
533 |
temp_ptr = entry_ptr -> fx_dir_entry_name; |
375 |
|
|
|
376 |
|
|
/* Copy the saved directory entry. */ |
377 |
|
533 |
*entry_ptr = media_ptr -> fx_media_last_found_entry; |
378 |
|
|
|
379 |
|
|
/* Restore the directory entry name pointer. */ |
380 |
|
533 |
entry_ptr -> fx_dir_entry_name = temp_ptr; |
381 |
|
|
|
382 |
|
|
/* Copy the directory name into the destination directory name. */ |
383 |
✓✓ |
7576 |
for (index = 0; index < FX_MAX_LONG_NAME_LEN; index++) |
384 |
|
|
{ |
385 |
|
|
|
386 |
|
|
/* Copy character into the destination. */ |
387 |
|
7574 |
temp_ptr[index] = media_ptr -> fx_media_last_found_file_name[index]; |
388 |
|
|
|
389 |
|
|
/* See if we have copied the NULL termination character. */ |
390 |
✓✓ |
7574 |
if (temp_ptr[index] == (CHAR)FX_NULL) |
391 |
|
|
{ |
392 |
|
|
|
393 |
|
|
/* Determine if we should break here or at the top of the loop. */ |
394 |
✓✓ |
533 |
if (index < (FX_MAX_LONG_NAME_LEN - 1)) |
395 |
|
|
{ |
396 |
|
|
|
397 |
|
|
/* Yes, break out of the loop early. */ |
398 |
|
531 |
break; |
399 |
|
|
} |
400 |
|
|
} |
401 |
|
|
} |
402 |
|
|
|
403 |
|
|
/* Determine if there is a search directory to copy. */ |
404 |
✓✓✓✓
|
533 |
if ((last_dir_ptr) && (media_ptr -> fx_media_last_found_directory_valid)) |
405 |
|
|
{ |
406 |
|
|
|
407 |
|
|
/* Yes, there was a search directory... and one is requested in this request as well. |
408 |
|
|
Simply copy it into the destination. */ |
409 |
|
|
|
410 |
|
|
/* First, save the name pointer from the list directory pointer. */ |
411 |
|
49 |
destination_name_ptr = last_dir_ptr -> fx_dir_entry_name; |
412 |
|
|
|
413 |
|
|
/* Copy the entire directory entry structure. */ |
414 |
|
49 |
*last_dir_ptr = media_ptr -> fx_media_last_found_directory; |
415 |
|
|
|
416 |
|
|
/* Restore the original name buffer pointer. */ |
417 |
|
49 |
last_dir_ptr -> fx_dir_entry_name = destination_name_ptr; |
418 |
|
|
|
419 |
|
|
/* Pickup pointer to name to copy. */ |
420 |
|
49 |
source_name_ptr = media_ptr -> fx_media_last_found_directory.fx_dir_entry_name; |
421 |
|
|
|
422 |
|
|
/* Loop to copy the name into the last directory name buffer. */ |
423 |
✓✓ |
947 |
for (n = 0; n < FX_MAX_LONG_NAME_LEN; n++) |
424 |
|
|
{ |
425 |
|
|
|
426 |
|
|
/* Copy a character. */ |
427 |
|
946 |
destination_name_ptr[n] = source_name_ptr[n]; |
428 |
|
|
|
429 |
|
|
/* See if we have copied the NULL termination character. */ |
430 |
✓✓ |
946 |
if (source_name_ptr[n] == (CHAR)FX_NULL) |
431 |
|
|
{ |
432 |
|
|
|
433 |
|
|
/* Determine if we should break here or at the top of the loop. */ |
434 |
✓✓ |
49 |
if (n < (FX_MAX_LONG_NAME_LEN - 1)) |
435 |
|
|
{ |
436 |
|
|
|
437 |
|
|
/* Yes, break out of the loop early. */ |
438 |
|
48 |
break; |
439 |
|
|
} |
440 |
|
|
} |
441 |
|
|
} |
442 |
|
|
} |
443 |
|
|
|
444 |
|
|
/* Return the last name pointer, if required. */ |
445 |
✓✓ |
533 |
if (last_name_ptr) |
446 |
|
|
{ |
447 |
|
|
|
448 |
|
|
/* Just set the last name to initial name string. */ |
449 |
|
51 |
*last_name_ptr = temp_ptr; |
450 |
|
|
} |
451 |
|
|
|
452 |
|
|
#ifndef FX_MEDIA_STATISTICS_DISABLE |
453 |
|
|
|
454 |
|
|
/* Increment the number of directory search cache hits. */ |
455 |
|
533 |
media_ptr -> fx_media_directory_search_cache_hits++; |
456 |
|
|
#endif |
457 |
|
|
|
458 |
|
|
/* Return success. */ |
459 |
|
533 |
return(FX_SUCCESS); |
460 |
|
|
} |
461 |
|
|
} |
462 |
|
|
|
463 |
|
|
/* Not a sequential search, invalidate the saved information. */ |
464 |
|
97065 |
media_ptr -> fx_media_last_found_name[0] = FX_NULL; |
465 |
|
|
|
466 |
|
|
#ifndef FX_MEDIA_STATISTICS_DISABLE |
467 |
|
|
|
468 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
469 |
|
|
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_DIR_CACHE_MISS, media_ptr, media_ptr -> fx_media_directory_searches - media_ptr -> fx_media_directory_search_cache_hits, 0, 0, FX_TRACE_INTERNAL_EVENTS, 0, 0) |
470 |
|
|
#else |
471 |
|
|
|
472 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
473 |
|
|
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_DIR_CACHE_MISS, media_ptr, 0, 0, 0, FX_TRACE_INTERNAL_EVENTS, 0, 0) |
474 |
|
|
#endif |
475 |
|
|
#endif |
476 |
|
|
|
477 |
|
|
/* Loop to traverse the directory paths to find the specified file. */ |
478 |
|
|
do |
479 |
|
|
{ |
480 |
|
|
|
481 |
|
|
/* Remember the last name pointer, if required. */ |
482 |
✓✓ |
112229 |
if (last_name_ptr) |
483 |
|
|
{ |
484 |
|
|
|
485 |
|
|
/* Just set the last name to initial name string. */ |
486 |
|
92433 |
*last_name_ptr = name_ptr; |
487 |
|
|
} |
488 |
|
|
|
489 |
|
|
/* Extract file name. */ |
490 |
|
112229 |
name_ptr = _fx_directory_name_extract(name_ptr, name); |
491 |
|
|
|
492 |
|
|
/* Calculate the directory size. */ |
493 |
✓✓ |
112229 |
if (search_dir_ptr) |
494 |
|
|
{ |
495 |
|
|
|
496 |
|
|
/* Ensure that the search directory's last search cluster is cleared. */ |
497 |
|
15926 |
search_dir_ptr -> fx_dir_entry_last_search_cluster = 0; |
498 |
|
|
|
499 |
|
|
/* Calculate the directory size by counting the allocated |
500 |
|
|
clusters for it. */ |
501 |
|
15926 |
i = 0; |
502 |
|
15926 |
cluster = search_dir_ptr -> fx_dir_entry_cluster; |
503 |
✓✓ |
61286 |
while (cluster < media_ptr -> fx_media_fat_reserved) |
504 |
|
|
{ |
505 |
|
|
|
506 |
|
|
/* Increment the cluster count. */ |
507 |
|
45376 |
i++; |
508 |
|
|
|
509 |
|
|
/* Read the next FAT entry. */ |
510 |
|
45376 |
status = _fx_utility_FAT_entry_read(media_ptr, cluster, &next_cluster); |
511 |
|
|
|
512 |
|
|
/* Check the return status. */ |
513 |
✓✓ |
45376 |
if (status != FX_SUCCESS) |
514 |
|
|
{ |
515 |
|
|
|
516 |
|
|
/* Return the bad status. */ |
517 |
|
1 |
return(status); |
518 |
|
|
} |
519 |
|
|
|
520 |
|
|
/* Check for error situation. */ |
521 |
✓✓✓✓ ✓✓ |
45375 |
if ((cluster < FX_FAT_ENTRY_START) || (cluster == next_cluster) || (i > media_ptr -> fx_media_total_clusters)) |
522 |
|
|
{ |
523 |
|
|
|
524 |
|
|
/* Return the bad status. */ |
525 |
|
15 |
return(FX_FAT_READ_ERROR); |
526 |
|
|
} |
527 |
|
|
|
528 |
|
45360 |
cluster = next_cluster; |
529 |
|
|
} |
530 |
|
|
|
531 |
|
|
/* Now we can calculate the directory size. */ |
532 |
|
15910 |
directory_size = (((ULONG64) media_ptr -> fx_media_bytes_per_sector) * |
533 |
|
15910 |
((ULONG64) media_ptr -> fx_media_sectors_per_cluster) * i) |
534 |
|
|
/ (ULONG64) FX_DIR_ENTRY_SIZE; |
535 |
|
|
|
536 |
|
|
/* Also save this in the directory entry so we don't have to |
537 |
|
|
calculate it later. */ |
538 |
|
15910 |
search_dir_ptr -> fx_dir_entry_file_size = directory_size; |
539 |
|
|
|
540 |
|
|
/* If required, copy the last search directory entry into the |
541 |
|
|
destination. */ |
542 |
✓✓ |
15910 |
if (last_dir_ptr) |
543 |
|
|
{ |
544 |
|
|
|
545 |
|
|
/* Copy the last search directory into the destination. */ |
546 |
|
|
|
547 |
|
|
/* First, save the name pointer from the list directory pointer. */ |
548 |
|
15185 |
destination_name_ptr = last_dir_ptr -> fx_dir_entry_name; |
549 |
|
|
|
550 |
|
|
/* Copy the entire directory entry structure. */ |
551 |
|
15185 |
*last_dir_ptr = *search_dir_ptr; |
552 |
|
|
|
553 |
|
|
/* Restore the original name buffer pointer. */ |
554 |
|
15185 |
last_dir_ptr -> fx_dir_entry_name = destination_name_ptr; |
555 |
|
|
|
556 |
|
|
/* Pickup pointer to name to copy. */ |
557 |
|
15185 |
source_name_ptr = search_dir_ptr -> fx_dir_entry_name; |
558 |
|
|
|
559 |
|
|
/* Loop to copy the name into the last directory name buffer. */ |
560 |
✓✓ |
84857 |
for (n = 0; n < FX_MAX_LONG_NAME_LEN; n++) |
561 |
|
|
{ |
562 |
|
|
|
563 |
|
|
/* Copy a character. */ |
564 |
|
84854 |
destination_name_ptr[n] = source_name_ptr[n]; |
565 |
|
|
|
566 |
|
|
/* See if we have copied the NULL termination character. */ |
567 |
✓✓ |
84854 |
if (source_name_ptr[n] == (CHAR) FX_NULL) |
568 |
|
|
{ |
569 |
|
|
|
570 |
|
|
/* Determine if we should break here or at the top of the loop. */ |
571 |
✓✓ |
15185 |
if (n < (FX_MAX_LONG_NAME_LEN - 1)) |
572 |
|
|
{ |
573 |
|
|
|
574 |
|
|
/* Yes, break out of the loop early. */ |
575 |
|
15182 |
break; |
576 |
|
|
} |
577 |
|
|
} |
578 |
|
|
} |
579 |
|
|
} |
580 |
|
|
} |
581 |
|
|
else |
582 |
|
|
{ |
583 |
|
|
|
584 |
|
|
/* Directory size is the number of entries in the root directory. */ |
585 |
|
96303 |
directory_size = (ULONG)media_ptr -> fx_media_root_directory_entries; |
586 |
|
|
} |
587 |
|
|
|
588 |
|
|
/* Loop through entries in the directory. Yes, this is a |
589 |
|
|
linear search! */ |
590 |
|
112213 |
i = 0; |
591 |
|
112213 |
found = FX_FALSE; |
592 |
|
|
|
593 |
|
|
|
594 |
|
|
do |
595 |
|
|
{ |
596 |
|
|
|
597 |
|
|
/* Read an entry from the directory. */ |
598 |
|
1425685 |
status = _fx_directory_entry_read(media_ptr, search_dir_ptr, &i, entry_ptr); |
599 |
|
|
|
600 |
|
1425685 |
i++; |
601 |
|
|
|
602 |
|
|
/* Check for error status. */ |
603 |
✓✓ |
1425685 |
if (status != FX_SUCCESS) |
604 |
|
|
{ |
605 |
|
12 |
return(status); |
606 |
|
|
} |
607 |
|
|
|
608 |
|
|
/* Determine if this is the last directory entry. */ |
609 |
✓✓ |
1425673 |
if ((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR)FX_DIR_ENTRY_DONE) |
610 |
|
|
{ |
611 |
|
63561 |
break; |
612 |
|
|
} |
613 |
|
|
|
614 |
|
|
/* Determine if the entry is a volume label entry */ |
615 |
✓✓ |
1362112 |
if ((entry_ptr -> fx_dir_entry_attributes & FX_VOLUME)) |
616 |
|
|
{ |
617 |
|
995 |
continue; |
618 |
|
|
} |
619 |
|
|
|
620 |
|
|
/* Determine if this is an empty entry. */ |
621 |
✓✓✓✓
|
1361117 |
if (((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR)FX_DIR_ENTRY_FREE) && (entry_ptr -> fx_dir_entry_short_name[0] == 0)) |
622 |
|
|
{ |
623 |
|
2712 |
continue; |
624 |
|
|
} |
625 |
|
|
|
626 |
|
|
/* Compare the input name and extension with the directory |
627 |
|
|
entry. */ |
628 |
|
1358405 |
work_ptr = &name[0]; |
629 |
|
1358405 |
dir_name_ptr = &(entry_ptr -> fx_dir_entry_name[0]); |
630 |
|
|
|
631 |
|
|
/* Loop to compare names. */ |
632 |
|
|
do |
633 |
|
|
{ |
634 |
|
|
|
635 |
|
|
/* Pickup character of directory name. */ |
636 |
|
4462143 |
alpha = *dir_name_ptr; |
637 |
|
|
|
638 |
|
|
/* Pickup character of name. */ |
639 |
|
4462143 |
name_alpha = *work_ptr; |
640 |
|
|
|
641 |
|
|
/* Determine if its case needs to be changed. */ |
642 |
✓✓✓✓
|
4462143 |
if ((alpha >= 'a') && (alpha <= 'z')) |
643 |
|
|
{ |
644 |
|
|
|
645 |
|
|
/* Yes, make upper case. */ |
646 |
|
69404 |
alpha = (CHAR)((INT)alpha - 0x20); |
647 |
|
|
} |
648 |
|
|
|
649 |
|
|
/* Determine if its case needs to be changed. */ |
650 |
✓✓✓✓
|
4462143 |
if ((name_alpha >= 'a') && (name_alpha <= 'z')) |
651 |
|
|
{ |
652 |
|
|
|
653 |
|
|
/* Yes, make upper case. */ |
654 |
|
99226 |
name_alpha = (CHAR)((INT)name_alpha - 0x20); |
655 |
|
|
} |
656 |
|
|
|
657 |
|
|
/* Compare name with directory name. */ |
658 |
✓✓ |
4462143 |
if (alpha != name_alpha) |
659 |
|
|
{ |
660 |
|
|
|
661 |
|
|
/* The names don't match, get out of the loop. */ |
662 |
|
1321985 |
break; |
663 |
|
|
} |
664 |
|
|
|
665 |
|
|
/* Otherwise, increment the name pointers. */ |
666 |
|
3140158 |
work_ptr++; |
667 |
|
3140158 |
dir_name_ptr++; |
668 |
✓✓ |
3140158 |
} while (*dir_name_ptr); |
669 |
|
|
|
670 |
|
|
/* Determine if the requested name has been found. If so, |
671 |
|
|
return success to the caller. */ |
672 |
✓✓✓✓
|
1358405 |
if ((*dir_name_ptr == 0) && (*work_ptr == *dir_name_ptr)) |
673 |
|
|
{ |
674 |
|
|
|
675 |
|
|
/* Yes, the name was located. All pertinent directory |
676 |
|
|
information is in the directory entry field. */ |
677 |
|
34631 |
found = FX_TRUE; |
678 |
|
|
} |
679 |
|
|
/* Determine if there is a short name to check. */ |
680 |
✓✓ |
1323774 |
else if (entry_ptr -> fx_dir_entry_short_name[0] != 0) |
681 |
|
|
{ |
682 |
|
|
|
683 |
|
|
/* Yes, check for the short part of the name. */ |
684 |
|
|
|
685 |
|
|
/* Compare the input name and extension with the directory entry. */ |
686 |
|
46915 |
work_ptr = &name[0]; |
687 |
|
46915 |
dir_name_ptr = &(entry_ptr -> fx_dir_entry_short_name[0]); |
688 |
|
|
|
689 |
|
|
/* Loop to compare names. */ |
690 |
|
|
do |
691 |
|
|
{ |
692 |
|
|
|
693 |
|
|
/* Pickup character of directory name. */ |
694 |
|
77450 |
alpha = *dir_name_ptr; |
695 |
|
|
|
696 |
|
|
/* Pickup character of name. */ |
697 |
|
77450 |
name_alpha = *work_ptr; |
698 |
|
|
|
699 |
|
|
/* Determine if its case needs to be changed. */ |
700 |
✓✓✓✓
|
77450 |
if ((name_alpha >= 'a') && (name_alpha <= 'z')) |
701 |
|
|
{ |
702 |
|
|
|
703 |
|
|
/* Yes, make upper case. */ |
704 |
|
52801 |
name_alpha = (CHAR)((INT)name_alpha - 0x20); |
705 |
|
|
} |
706 |
|
|
|
707 |
|
|
/* Compare name with directory name. */ |
708 |
✓✓ |
77450 |
if (alpha != name_alpha) |
709 |
|
|
{ |
710 |
|
|
|
711 |
|
|
/* The names don't match, get out of the loop. */ |
712 |
|
43266 |
break; |
713 |
|
|
} |
714 |
|
|
|
715 |
|
|
/* Otherwise, move the name pointers and increment the |
716 |
|
|
count. */ |
717 |
|
34184 |
work_ptr++; |
718 |
|
34184 |
dir_name_ptr++; |
719 |
✓✓ |
34184 |
} while (*dir_name_ptr); |
720 |
|
|
|
721 |
|
|
/* Determine if the names match. */ |
722 |
✓✓✓✓
|
46915 |
if ((*dir_name_ptr == 0) && (*work_ptr == *dir_name_ptr)) |
723 |
|
|
{ |
724 |
|
|
|
725 |
|
|
/* Yes, the name was located. All pertinent directory |
726 |
|
|
information is in the directory entry field. */ |
727 |
|
2742 |
found = FX_TRUE; |
728 |
|
|
} |
729 |
|
|
} |
730 |
✓✓✓✓
|
1362112 |
} while ((i < directory_size) && (!found)); |
731 |
|
|
|
732 |
|
|
/* Now determine if we have a match. */ |
733 |
✓✓ |
112201 |
if (!found) |
734 |
|
|
{ |
735 |
|
|
|
736 |
|
|
/* Return a "not found" status to the caller. */ |
737 |
|
74828 |
return(FX_NOT_FOUND); |
738 |
|
|
} |
739 |
|
|
|
740 |
|
|
/* Determine if the found entry is indeed a sub-directory. */ |
741 |
✓✓ |
37373 |
if (entry_ptr -> fx_dir_entry_attributes & FX_DIRECTORY) |
742 |
|
|
{ |
743 |
|
|
|
744 |
|
|
/* Move the directory search pointer to this entry. */ |
745 |
|
18090 |
search_dir = *entry_ptr; |
746 |
|
18090 |
search_dir_ptr = &search_dir; |
747 |
|
|
|
748 |
|
|
/* Ensure that the search directory's last search cluster is cleared. */ |
749 |
|
18090 |
search_dir_ptr -> fx_dir_entry_last_search_cluster = 0; |
750 |
|
|
|
751 |
|
|
/* Now determine if the new search directory is the root |
752 |
|
|
directory. */ |
753 |
✓✓ |
18090 |
if (!search_dir_ptr -> fx_dir_entry_cluster) |
754 |
|
|
{ |
755 |
|
|
|
756 |
|
|
/* This is a backward link to the root directory. Make |
757 |
|
|
sure this is indicated in the search directory |
758 |
|
|
information. */ |
759 |
|
19 |
search_dir_ptr -> fx_dir_entry_name[0] = 0; |
760 |
|
|
|
761 |
|
|
/* Determine if we need to remember this in the last |
762 |
|
|
directory searched return area. */ |
763 |
✓✓ |
19 |
if (last_dir_ptr) |
764 |
|
|
{ |
765 |
|
|
|
766 |
|
|
/* Yes, return this value to the caller. */ |
767 |
|
|
|
768 |
|
|
/* First, save the name pointer from the list directory pointer. */ |
769 |
|
1 |
destination_name_ptr = last_dir_ptr -> fx_dir_entry_name; |
770 |
|
|
|
771 |
|
|
/* Copy the entire directory entry structure. */ |
772 |
|
1 |
*last_dir_ptr = *search_dir_ptr; |
773 |
|
|
|
774 |
|
|
/* Restore the original name buffer pointer. */ |
775 |
|
1 |
last_dir_ptr -> fx_dir_entry_name = destination_name_ptr; |
776 |
|
|
|
777 |
|
|
/* Pickup pointer to name to copy. */ |
778 |
|
1 |
source_name_ptr = search_dir_ptr -> fx_dir_entry_name; |
779 |
|
|
|
780 |
|
|
/* Loop to copy the name into the last directory name buffer. */ |
781 |
✓✓ |
257 |
for (n = 0; n < FX_MAX_LONG_NAME_LEN; n++) |
782 |
|
|
{ |
783 |
|
|
|
784 |
|
|
/* Copy a character. */ |
785 |
|
256 |
destination_name_ptr[n] = source_name_ptr[n]; |
786 |
|
|
} |
787 |
|
|
} |
788 |
|
|
|
789 |
|
|
/* Set the search directory pointer to NULL to indicate |
790 |
|
|
we are at the root directory. */ |
791 |
|
19 |
search_dir_ptr = FX_NULL; |
792 |
|
|
} |
793 |
|
|
} |
794 |
|
|
else |
795 |
|
|
{ |
796 |
|
|
|
797 |
|
|
/* This is not a directory, we better return not found |
798 |
|
|
since we can't continue the search. */ |
799 |
✓✓ |
19283 |
if (name_ptr) |
800 |
|
|
{ |
801 |
|
|
|
802 |
|
|
/* Return not-found status to caller. */ |
803 |
|
1 |
return(FX_NOT_FOUND); |
804 |
|
|
} |
805 |
|
|
} |
806 |
✓✓ |
37372 |
} while (name_ptr); |
807 |
|
|
|
808 |
|
|
/* If you reach this point, the directory is found absolutely, since !found will return directly in the loop above. */ |
809 |
|
|
#ifndef FX_MEDIA_DISABLE_SEARCH_CACHE |
810 |
|
|
|
811 |
|
|
|
812 |
|
|
/* At this point, cache the found information. If a subsequent search for the same name is done, |
813 |
|
|
it will return immediately. */ |
814 |
|
|
|
815 |
|
|
/* Set the index of the saved name string. */ |
816 |
|
22208 |
v= 0; |
817 |
|
|
|
818 |
|
|
/* First, build the full path and name. */ |
819 |
✓✓✓✓ ✓✓ |
22208 |
if ((*original_name != '\\') && (*original_name != '/') && (path_ptr)) |
820 |
|
|
{ |
821 |
|
|
|
822 |
|
|
/* Copy the path into the destination. */ |
823 |
✓✓✓✓
|
45042 |
while ((v< (FX_MAX_LAST_NAME_LEN - 1)) && (path_ptr[v])) |
824 |
|
|
{ |
825 |
|
|
|
826 |
|
|
/* Copy one character. */ |
827 |
|
44649 |
media_ptr -> fx_media_last_found_name[v] = path_ptr[v]; |
828 |
|
|
|
829 |
|
|
/* Move to next character. */ |
830 |
|
44649 |
v++; |
831 |
|
|
} |
832 |
|
|
} |
833 |
|
|
|
834 |
|
|
/* Now see if there is no directory path symbol in the name itself. */ |
835 |
✓✓✓✓
|
22208 |
if ((*original_name != '\\') && (*original_name != '/')) |
836 |
|
|
{ |
837 |
|
|
|
838 |
|
|
/* If there is room, place a directory separator character. */ |
839 |
✓✓ |
21982 |
if (v < (FX_MAX_LAST_NAME_LEN - 1)) |
840 |
|
|
{ |
841 |
|
21875 |
media_ptr -> fx_media_last_found_name[v++] = '/'; |
842 |
|
|
} |
843 |
|
|
} |
844 |
|
|
|
845 |
|
|
/* Now append the name to the path. */ |
846 |
|
22208 |
j = 0; |
847 |
✓✓✓✓
|
221448 |
while ((v < FX_MAX_LAST_NAME_LEN) && (original_name[j])) |
848 |
|
|
{ |
849 |
|
|
|
850 |
|
|
/* Copy one character. */ |
851 |
|
199240 |
media_ptr -> fx_media_last_found_name[v] = original_name[j]; |
852 |
|
|
|
853 |
|
|
/* Move to next character. */ |
854 |
|
199240 |
v++; |
855 |
|
199240 |
j++; |
856 |
|
|
} |
857 |
|
|
|
858 |
|
|
/* Null terminate the last name string. */ |
859 |
✓✓ |
22208 |
if (v< FX_MAX_LAST_NAME_LEN) |
860 |
|
|
{ |
861 |
|
|
|
862 |
|
|
/* Null terminate. */ |
863 |
|
22058 |
media_ptr -> fx_media_last_found_name[v] = FX_NULL; |
864 |
|
|
} |
865 |
|
|
else |
866 |
|
|
{ |
867 |
|
|
|
868 |
|
|
/* The string is too big, NULL the string so it won't be used in searching. */ |
869 |
|
150 |
media_ptr -> fx_media_last_found_name[0] = FX_NULL; |
870 |
|
|
} |
871 |
|
|
|
872 |
|
|
/* Determine if there is a search pointer. */ |
873 |
✓✓ |
22208 |
if (search_dir_ptr) |
874 |
|
|
{ |
875 |
|
|
|
876 |
|
|
/* Yes, there is a search directory pointer so save it! */ |
877 |
|
3021 |
media_ptr -> fx_media_last_found_directory = *search_dir_ptr; |
878 |
|
|
|
879 |
|
|
/* Indicate the search directory is valid. */ |
880 |
|
3021 |
media_ptr -> fx_media_last_found_directory_valid = FX_TRUE; |
881 |
|
|
} |
882 |
|
|
else |
883 |
|
|
{ |
884 |
|
|
|
885 |
|
|
/* Indicate the search directory is not valid. */ |
886 |
|
19187 |
media_ptr -> fx_media_last_found_directory_valid = FX_FALSE; |
887 |
|
|
} |
888 |
|
|
|
889 |
|
|
/* Copy the directory entry. */ |
890 |
|
22208 |
media_ptr -> fx_media_last_found_entry = *entry_ptr; |
891 |
|
|
|
892 |
|
|
/* Setup the directory entry for the last found internal file name. */ |
893 |
|
22208 |
media_ptr -> fx_media_last_found_entry.fx_dir_entry_name = media_ptr -> fx_media_last_found_file_name; |
894 |
|
|
|
895 |
|
|
/* Copy the actual directory name into the cached directory name. */ |
896 |
✓✓ |
217372 |
for (index = 0; index < FX_MAX_LONG_NAME_LEN; index++) |
897 |
|
|
{ |
898 |
|
|
|
899 |
|
|
/* Copy character into the cached directory name. */ |
900 |
|
217353 |
media_ptr -> fx_media_last_found_file_name[index] = entry_ptr -> fx_dir_entry_name[index]; |
901 |
|
|
|
902 |
|
|
/* See if we have copied the NULL termination character. */ |
903 |
✓✓ |
217353 |
if (entry_ptr -> fx_dir_entry_name[index] == (CHAR)FX_NULL) |
904 |
|
|
{ |
905 |
|
|
|
906 |
|
|
/* Check to see if we use the break to get out of the loop. */ |
907 |
✓✓ |
22208 |
if (index < (FX_MAX_LONG_NAME_LEN - 1)) |
908 |
|
|
{ |
909 |
|
|
|
910 |
|
|
/* Yes, not at the end of the string, break. */ |
911 |
|
22189 |
break; |
912 |
|
|
} |
913 |
|
|
} |
914 |
|
|
} |
915 |
|
|
#endif |
916 |
|
|
|
917 |
|
22208 |
return(FX_SUCCESS); |
918 |
|
|
} |
919 |
|
|
|