GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_directory_next_full_entry_find.c Lines: 103 103 100.0 %
Date: 2024-01-10 21:53:23 Branches: 70 70 100.0 %

Line Branch Exec Source
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_full_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 and various information about the name.  The      */
55
/*    function that returns the first name in the current directory must  */
56
/*    be called prior to this function.                                   */
57
/*                                                                        */
58
/*  INPUT                                                                 */
59
/*                                                                        */
60
/*    media_ptr                             Media control block pointer   */
61
/*    directory_name                        Destination for directory     */
62
/*                                            name                        */
63
/*    attributes                            Destination for attributes    */
64
/*    size                                  Destination for size          */
65
/*    year                                  Destination for year          */
66
/*    month                                 Destination for month         */
67
/*    day                                   Destination for day           */
68
/*    hour                                  Destination for hour          */
69
/*    minute                                Destination for minute        */
70
/*    second                                Destination for second        */
71
/*                                                                        */
72
/*  OUTPUT                                                                */
73
/*                                                                        */
74
/*    return status                                                       */
75
/*                                                                        */
76
/*  CALLS                                                                 */
77
/*                                                                        */
78
/*    _fx_directory_entry_read              Read entries from root dir    */
79
/*    _fx_utility_FAT_entry_read            Read FAT entries              */
80
/*    _fx_directory_exFAT_entry_read        Read exFAT entries            */
81
/*                                                                        */
82
/*  CALLED BY                                                             */
83
/*                                                                        */
84
/*    Application Code and                                                */
85
/*    FileX System Functions                                              */
86
/*                                                                        */
87
/*  RELEASE HISTORY                                                       */
88
/*                                                                        */
89
/*    DATE              NAME                      DESCRIPTION             */
90
/*                                                                        */
91
/*  05-19-2020     William E. Lamie         Initial Version 6.0           */
92
/*  09-30-2020     William E. Lamie         Modified comment(s),          */
93
/*                                            resulting in version 6.1    */
94
/*                                                                        */
95
/**************************************************************************/
96
56
UINT  _fx_directory_next_full_entry_find(FX_MEDIA *media_ptr,
97
                                         CHAR *directory_name, UINT *attributes, ULONG *size,
98
                                         UINT *year, UINT *month, UINT *day, UINT *hour, UINT *minute, UINT *second)
99
{
100
101
ULONG         i;
102
UINT          status;
103
UINT          temp_status;
104
56
ULONG         cluster, next_cluster = 0;
105
ULONG64       directory_size;
106
FX_DIR_ENTRY  entry;
107
FX_DIR_ENTRY *search_dir_ptr;
108
FX_PATH      *path_ptr;
109
#ifndef FX_MEDIA_DISABLE_SEARCH_CACHE
110
UINT          index;
111
56
CHAR         *path_string_ptr =  FX_NULL;
112
#endif
113
#ifdef FX_ENABLE_EXFAT
114
/* TODO: maybe will be better to use dynamic memory, just because unicode_name is not needed in case of not exFAT.  */
115
UCHAR         unicode_name[FX_MAX_LONG_NAME_LEN * 2];
116
UINT          unicode_len = 0;
117
#endif /* FX_ENABLE_EXFAT */
118
119
120
#ifndef FX_MEDIA_STATISTICS_DISABLE
121
122
    /* Increment the number of times this service has been called.  */
123
56
    media_ptr -> fx_media_directory_next_full_entry_finds++;
124
#endif
125
126
    /* Setup pointer to media name buffer.  */
127
56
    entry.fx_dir_entry_name =  media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN;
128
129
    /* Clear the short name string.  */
130
56
    entry.fx_dir_entry_short_name[0] =  0;
131
132
#ifdef FX_ENABLE_EXFAT
133
    /* Will be set by exFAT.  */
134
    entry.fx_dir_entry_secondary_count = 0;
135
#endif /* FX_ENABLE_EXFAT */
136
137
    /* Check the media to make sure it is open.  */
138
56
    if (media_ptr -> fx_media_id != FX_MEDIA_ID)
139
    {
140
141
        /* Return the media not opened error.  */
142
1
        return(FX_MEDIA_NOT_OPEN);
143
    }
144
145
    /* If trace is enabled, insert this event into the trace buffer.  */
146
    FX_TRACE_IN_LINE_INSERT(FX_TRACE_DIRECTORY_NEXT_FULL_ENTRY_FIND, media_ptr, directory_name, 0, 0, FX_TRACE_DIRECTORY_EVENTS, 0, 0)
147
148
    /* Protect against other threads accessing the media.  */
149
55
    FX_PROTECT
150
151
    /* First check for a local path pointer stored in the thread control block.  This
152
       is only available in ThreadX Version 4 and above.  */
153
#ifndef FX_NO_LOCAL_PATH
154
55
    if (_tx_thread_current_ptr -> tx_thread_filex_ptr)
155
    {
156
157
        /* Setup the default path pointer.  */
158
2
        path_ptr =  (FX_PATH *)_tx_thread_current_ptr -> tx_thread_filex_ptr;
159
160
        /* Determine if we are at the root directory.  */
161
2
        if (path_ptr -> fx_path_directory.fx_dir_entry_name[0])
162
        {
163
164
            /* No, we are not at the root directory.  */
165
166
#ifndef FX_MEDIA_DISABLE_SEARCH_CACHE
167
168
            /* Setup pointer to the path.  */
169
1
            path_string_ptr =  ((FX_PATH *)_tx_thread_current_ptr -> tx_thread_filex_ptr) -> fx_path_string;
170
#endif
171
172
            /* Set the internal pointer to the search directory as well.  */
173
1
            search_dir_ptr =  &path_ptr -> fx_path_directory;
174
        }
175
        else
176
        {
177
178
            /* The current default directory is the root so just set the
179
               search directory pointer to NULL.  */
180
1
            search_dir_ptr =  FX_NULL;
181
        }
182
    }
183
    else
184
#endif
185
186
    /* Set the initial search directory to the current working
187
       directory - if there is one.  */
188
53
    if (media_ptr -> fx_media_default_path.fx_path_directory.fx_dir_entry_name[0])
189
    {
190
191
        /* Setup the path pointer to the global media path.  */
192
34
        path_ptr =  &media_ptr -> fx_media_default_path;
193
194
#ifndef FX_MEDIA_DISABLE_SEARCH_CACHE
195
196
        /* Setup pointer to the path.  */
197
34
        path_string_ptr =  media_ptr -> fx_media_default_path.fx_path_string;
198
#endif
199
200
        /* Set the internal pointer to the search directory as well.  */
201
34
        search_dir_ptr =  &path_ptr -> fx_path_directory;
202
    }
203
    else
204
    {
205
206
        /* Setup the path pointer to the global media path.  */
207
19
        path_ptr =  &media_ptr -> fx_media_default_path;
208
209
        /* The current default directory is the root so just set the
210
           search directory pointer to NULL.  */
211
19
        search_dir_ptr =  FX_NULL;
212
    }
213
214
    /* Calculate the directory size.  */
215
55
    if (search_dir_ptr)
216
    {
217
218
#ifdef FX_ENABLE_EXFAT
219
220
        if (media_ptr -> fx_media_FAT_type == FX_exFAT)
221
        {
222
            directory_size = search_dir_ptr -> fx_dir_entry_file_size / FX_DIR_ENTRY_SIZE;
223
        }
224
        else
225
        {
226
#endif /* FX_ENABLE_EXFAT */
227
228
            /* Determine the directory size.  */
229
35
            if (path_ptr -> fx_path_current_entry !=  0)
230
            {
231
232
                /* Pickup the previously saved directory size.  */
233
27
                directory_size =  search_dir_ptr -> fx_dir_entry_file_size;
234
            }
235
            else
236
            {
237
238
                /* This should only be done on the first time into next directory find.  */
239
240
                /* Ensure that the search directory's last search cluster is cleared.  */
241
8
                search_dir_ptr -> fx_dir_entry_last_search_cluster =  0;
242
243
                /* Calculate the directory size by counting the allocated
244
                clusters for it.  */
245
8
                i =        0;
246
8
                cluster =  search_dir_ptr -> fx_dir_entry_cluster;
247
34
                while (cluster < media_ptr -> fx_media_fat_reserved)
248
                {
249
250
                    /* Increment the cluster count.  */
251
30
                    i++;
252
253
                    /* Read the next FAT entry.  */
254
30
                    status =  _fx_utility_FAT_entry_read(media_ptr, cluster, &next_cluster);
255
256
257
                    /* Check the return status.  */
258
30
                    if (status != FX_SUCCESS)
259
                    {
260
261
                        /* Release media protection.  */
262
1
                        FX_UNPROTECT
263
264
                        /* Return the bad status.  */
265
1
                        return(status);
266
                    }
267
268

29
                    if ((cluster < FX_FAT_ENTRY_START) || (cluster == next_cluster) || (i > media_ptr -> fx_media_total_clusters))
269
                    {
270
271
                        /* Release media protection.  */
272
3
                        FX_UNPROTECT
273
274
                        /* Return the bad status.  */
275
3
                        return(FX_FAT_READ_ERROR);
276
                    }
277
278
26
                    cluster = next_cluster;
279
                }
280
281
                /* Now we can calculate the directory size.  */
282
4
                directory_size =  (((ULONG64) media_ptr -> fx_media_bytes_per_sector) *
283
4
                                   ((ULONG64) media_ptr -> fx_media_sectors_per_cluster) * i)
284
                                    / (ULONG64) FX_DIR_ENTRY_SIZE;
285
286
                /* Save how many entries there are in the directory.  */
287
4
                search_dir_ptr -> fx_dir_entry_file_size =  directory_size;
288
            }
289
#ifdef FX_ENABLE_EXFAT
290
        }
291
#endif /* FX_ENABLE_EXFAT */
292
    }
293
    else
294
    {
295
296
        /* Directory size is the number of entries in the root directory.  */
297
20
        directory_size =  (ULONG)media_ptr -> fx_media_root_directory_entries;
298
    }
299
300
    /* Preset status with an error return.  */
301
51
    status =  FX_NO_MORE_ENTRIES;
302
303
    /* Determine if the current entry is inside of the directory's range.  */
304
52
    while (path_ptr -> fx_path_current_entry < directory_size)
305
    {
306
307
        /* Read an entry from the directory.  */
308
#ifdef FX_ENABLE_EXFAT
309
        if (media_ptr -> fx_media_FAT_type == FX_exFAT)
310
        {
311
            temp_status = _fx_directory_exFAT_entry_read(media_ptr, search_dir_ptr,
312
                                                         &(path_ptr -> fx_path_current_entry), &entry,
313
                                                         0, FX_FALSE, unicode_name, &unicode_len);
314
        }
315
        else
316
        {
317
#endif /* FX_ENABLE_EXFAT */
318
51
            temp_status = _fx_directory_entry_read(media_ptr, search_dir_ptr,
319
                                                   &(path_ptr -> fx_path_current_entry), &entry);
320
#ifdef FX_ENABLE_EXFAT
321
        }
322
#endif /* FX_ENABLE_EXFAT */
323
324
        /* Check for error status.  */
325
51
        if (temp_status != FX_SUCCESS)
326
        {
327
328
            /* Release media protection.  */
329
1
            FX_UNPROTECT
330
331
            /* Return error status.  */
332
1
            return(temp_status);
333
        }
334
335
#ifdef FX_ENABLE_EXFAT
336
        if (entry.fx_dir_entry_type == FX_EXFAT_DIR_ENTRY_TYPE_END_MARKER)
337
        {
338
            /* Set the error code.  */
339
            status =  FX_NO_MORE_ENTRIES;
340
341
            /* Get out of the loop.  */
342
            break;
343
        }
344
#endif /* FX_ENABLE_EXFAT */
345
346
        /* Check to see if the entry has something in it.  */
347
#ifdef FX_ENABLE_EXFAT
348
        else if (entry.fx_dir_entry_type != FX_EXFAT_DIR_ENTRY_TYPE_FILE_DIRECTORY)
349
#else
350

50
        if (((UCHAR)entry.fx_dir_entry_name[0] == (UCHAR)FX_DIR_ENTRY_FREE) && (entry.fx_dir_entry_short_name[0] == 0))
351
#endif /* FX_ENABLE_EXFAT */
352
        {
353
354
            /* Current entry is free, skip to next entry and continue the loop.  */
355
1
            path_ptr -> fx_path_current_entry++;
356
1
            continue;
357
        }
358
359
        /* Determine if a valid directory entry is present.  */
360
#ifdef FX_ENABLE_EXFAT
361
        else /* FX_EXFAT_DIR_ENTRY_TYPE_FILE_DIRECTORY */
362
#else
363
49
        else if ((UCHAR)entry.fx_dir_entry_name[0] != (UCHAR)FX_DIR_ENTRY_DONE)
364
#endif /* FX_ENABLE_EXFAT */
365
        {
366
367
            /* A valid directory entry is present.  */
368
369
            /* Copy the name into the destination.  */
370
789
            for (i = 0; entry.fx_dir_entry_name[i]; i++)
371
            {
372
373
744
                *directory_name =  entry.fx_dir_entry_name[i];
374
744
                directory_name++;
375
            }
376
377
            /* Place a NULL at the end of the directory name.  */
378
45
            *directory_name =  (CHAR)0;
379
380
            /* Determine if the entry's attributes are required.  */
381
45
            if (attributes)
382
            {
383
384
                /* Pickup the entry's attributes.  */
385
23
                *attributes =  entry.fx_dir_entry_attributes;
386
            }
387
388
            /* Determine if the entry's size is required.  */
389
45
            if (size)
390
            {
391
392
                /* Pickup the entry's size.  */
393
23
                *size =  (ULONG)entry.fx_dir_entry_file_size;
394
            }
395
396
            /* Determine if the entry's year is required.  */
397
45
            if (year)
398
            {
399
400
                /* Pickup the entry's year.  */
401
23
                *year =  ((entry.fx_dir_entry_date >> FX_YEAR_SHIFT) & FX_YEAR_MASK) + FX_BASE_YEAR;
402
            }
403
404
            /* Determine if the entry's month is required.  */
405
45
            if (month)
406
            {
407
408
                /* Pickup the entry's month.  */
409
23
                *month =  (entry.fx_dir_entry_date >> FX_MONTH_SHIFT) & FX_MONTH_MASK;
410
            }
411
412
            /* Determine if the entry's day is required.  */
413
45
            if (day)
414
            {
415
416
                /* Pickup the entry's day.  */
417
23
                *day =  entry.fx_dir_entry_date & FX_DAY_MASK;
418
            }
419
420
            /* Determine if the entry's hour is required.  */
421
45
            if (hour)
422
            {
423
424
                /* Pickup the entry's hour.  */
425
23
                *hour =  (entry.fx_dir_entry_time >> FX_HOUR_SHIFT) & FX_HOUR_MASK;
426
            }
427
428
            /* Determine if the entry's minute is required.  */
429
45
            if (minute)
430
            {
431
432
                /* Pickup the entry's minute.  */
433
24
                *minute =  (entry.fx_dir_entry_time >> FX_MINUTE_SHIFT) & FX_MINUTE_MASK;
434
            }
435
436
            /* Determine if the entry's second is required.  */
437
45
            if (second)
438
            {
439
440
                /* Pickup the entry's second.  */
441
44
                *second =  (entry.fx_dir_entry_time & FX_SECOND_MASK) * 2;
442
            }
443
444
            /* Increment the current entry for the media.  */
445
45
            path_ptr -> fx_path_current_entry++;
446
447
#ifndef FX_MEDIA_DISABLE_SEARCH_CACHE
448
            {
449
            UINT v, j;
450
451
452
                /* If a subsequent search for the same name is done, it will find it immediately.  */
453
454
                /* Set the index of the saved name string.  */
455
45
                v =  0;
456
457
                /* First, build the full path and name.  */
458
45
                if (path_string_ptr)
459
                {
460
461
                    /* Copy the path into the destination.  */
462

1016
                    while ((v < (FX_MAX_LAST_NAME_LEN - 1)) && (path_string_ptr[v]))
463
                    {
464
465
                        /* Copy one character.   */
466
989
                        media_ptr -> fx_media_last_found_name[v] =  path_string_ptr[v];
467
468
                        /* Move to next character.  */
469
989
                        v++;
470
                    }
471
                }
472
473
                /* We know there is room at this point, place a directory separator character.  */
474
45
                media_ptr -> fx_media_last_found_name[v++] =  '/';
475
476
                /* Now append the name to the path.  */
477
45
                j =  0;
478

781
                while ((v < FX_MAX_LAST_NAME_LEN) && (entry.fx_dir_entry_name[j]))
479
                {
480
481
                    /* Copy one character.   */
482
736
                    media_ptr -> fx_media_last_found_name[v] =  entry.fx_dir_entry_name[j];
483
484
                    /* Move to next character.  */
485
736
                    v++;
486
736
                    j++;
487
                }
488
489
                /* Null terminate the last name string.   */
490
45
                if (v < FX_MAX_LAST_NAME_LEN)
491
                {
492
493
                    /* Null terminate.  */
494
42
                    media_ptr -> fx_media_last_found_name[v] =  FX_NULL;
495
                }
496
                else
497
                {
498
499
                    /* The string is too big, NULL the string so it won't be used in searching.  */
500
3
                    media_ptr -> fx_media_last_found_name[0] =  FX_NULL;
501
                }
502
503
                /* Determine if there is a search pointer.  */
504
45
                if (search_dir_ptr)
505
                {
506
507
                    /* Yes, there is a search directory pointer so save it!   */
508
27
                    media_ptr -> fx_media_last_found_directory =  *search_dir_ptr;
509
510
                    /* Indicate the search directory is valid.  */
511
27
                    media_ptr -> fx_media_last_found_directory_valid =  FX_TRUE;
512
                }
513
                else
514
                {
515
516
                    /* Indicate the search directory is not valid.  */
517
18
                    media_ptr -> fx_media_last_found_directory_valid =  FX_FALSE;
518
                }
519
520
                /* Copy the directory entry.  */
521
45
                media_ptr -> fx_media_last_found_entry =  entry;
522
523
                /* Setup the last found directory entry to point at the last found internal file name.  */
524
45
                media_ptr -> fx_media_last_found_entry.fx_dir_entry_name =  media_ptr -> fx_media_last_found_file_name;
525
526
                /* Copy the actual directory name into the cached directory name.  */
527
1299
                for (index = 0; index < FX_MAX_LONG_NAME_LEN; index++)
528
                {
529
530
                    /* Copy character into the cached directory name.  */
531
1296
                    media_ptr -> fx_media_last_found_file_name[index] =  entry.fx_dir_entry_name[index];
532
533
                    /* See if we have copied the NULL termination character.  */
534
1296
                    if (entry.fx_dir_entry_name[index] == (CHAR)FX_NULL)
535
                    {
536
537
                        /* Check to see if we use the break to get out of the loop.  */
538
73
                        if (v < (FX_MAX_LONG_NAME_LEN - 1))
539
                        {
540
541
                            /* Yes, not at the end of the string, break.  */
542
42
                            break;
543
                        }
544
                    }
545
                }
546
            }
547
#endif
548
549
            /* Set return status to success.  */
550
45
            status =  FX_SUCCESS;
551
552
            /* Get out of the loop.  */
553
45
            break;
554
        }
555
#ifndef FX_ENABLE_EXFAT
556
        else
557
        {
558
            /* Set the error code.  */
559
4
            status =  FX_NO_MORE_ENTRIES;
560
561
            /* Get out of the loop.  */
562
4
            break;
563
        }
564
#endif /* FX_ENABLE_EXFAT */
565
    }
566
567
    /* Release media protection.  */
568
50
    FX_UNPROTECT
569
570
    /* Return status to the caller.  */
571
50
    return(status);
572
}
573