Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define FX_SOURCE_CODE
#include "fx_api.h"
#include "fx_system.h"
#include "fx_directory.h"
#include "fx_utility.h"
#include "fx_directory_exFAT.h"
...
...
_fx_directory_free_search(FX_MEDIA *, FX_DIR_ENTRY *, FX_DIR_ENTRY *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesfilexcommon/src/fx_directory_free_search.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** FileX Component */ /** */ /** Directory */ /** */... /**************************************************************************/ /**************************************************************************/ #define FX_SOURCE_CODE /* Include necessary system files. */ #include "fx_api.h" #include "fx_system.h" #include "fx_directory.h" #include "fx_utility.h" #ifdef FX_ENABLE_EXFAT #include "fx_directory_exFAT.h" #endif /* FX_ENABLE_EXFAT */ ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _fx_directory_free_search PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function searches the media for a free directory entry. */ /* */ /* INPUT */ /* */ /* media_ptr Media control block pointer */ /* directory_ptr Pointer to directory to */ /* search in */ /* entry_ptr Pointer to directory entry */ /* record */ /* */ /* OUTPUT */ /* */ /* return status */ /* */ /* CALLS */ /* */ /* _fx_directory_entry_read Read entries from directory */ /* _fx_directory_entry_write Write entries to directory */ /* _fx_utility_FAT_entry_read Read a FAT entry */ /* _fx_utility_FAT_entry_write Write a FAT entry */ /* _fx_utility_FAT_flush Flush written FAT entries */ /* _fx_utility_logical_sector_flush Flush logical sector cache */ /* _fx_utility_logical_sector_read Read logical sector */ /* _fx_utility_logical_sector_write Write logical sector */ /* */ /* CALLED BY */ /* */ /* FileX System Functions */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 William E. Lamie Initial Version 6.0 */ /* 09-30-2020 William E. Lamie Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _fx_directory_free_search(FX_MEDIA *media_ptr, FX_DIR_ENTRY *directory_ptr, FX_DIR_ENTRY *entry_ptr) { ULONG i, j; UCHAR *work_ptr; UINT status, total_entries; ULONG entry_sector, entry_offset; ULONG FAT_index, FAT_value; ULONG cluster, total_clusters, clusters_needed; ULONG first_new_cluster, last_cluster, clusters; ULONG directory_index; ULONG directory_entries; ULONG logical_sector; FX_DIR_ENTRY *search_dir_ptr; ULONG free_entry_start; UINT sectors; FX_INT_SAVE_AREA #ifdef FX_ENABLE_EXFAT /* Check if media format is exFAT. */ if (media_ptr -> fx_media_FAT_type == FX_exFAT) { /* Call exFAT specific function. */ return(_fx_directory_exFAT_free_search(media_ptr, directory_ptr, entry_ptr)); }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } /* ... */#endif /* FX_ENABLE_EXFAT */ #ifndef FX_MEDIA_STATISTICS_DISABLE /* Increment the number of directory free entry search requests. */ media_ptr -> fx_media_directory_free_searches++;/* ... */ #endif /* Initialize the entry sector values. */ entry_sector = entry_offset = 0; /* Set the long file name flag to false. */ entry_ptr -> fx_dir_entry_long_name_present = 0; /* Are there leading dots? */ if (entry_ptr -> fx_dir_entry_name[0] == '.') { /* Is there more than 1 dot? */ if (entry_ptr -> fx_dir_entry_name[1] == '.') { /* Yes, consider the name invalid. */ return(FX_INVALID_NAME); }if (entry_ptr -> fx_dir_entry_name[1] == '.') { ... } }if (entry_ptr -> fx_dir_entry_name[0] == '.') { ... } /* Determine if a long file name is present. */ for (i = 0, j = 0; entry_ptr -> fx_dir_entry_name[i]; i++) { /* Check for upper-case characters. */ if ((entry_ptr -> fx_dir_entry_name[i] >= 'A') && (entry_ptr -> fx_dir_entry_name[i] <= 'Z')) { continue; }if ((entry_ptr -> fx_dir_entry_name[i] >= 'A') && (entry_ptr -> fx_dir_entry_name[i] <= 'Z')) { ... } /* Check for numeric characters. */ else if ((entry_ptr -> fx_dir_entry_name[i] >= '0') && (entry_ptr -> fx_dir_entry_name[i] <= '9')) { continue; }else if ((entry_ptr -> fx_dir_entry_name[i] >= '0') && (entry_ptr -> fx_dir_entry_name[i] <= '9')) { ... } /* Check for any lower-case characters. */ else if ((entry_ptr -> fx_dir_entry_name[i] >= 'a') && (entry_ptr -> fx_dir_entry_name[i] <= 'z')) { entry_ptr -> fx_dir_entry_long_name_present = 1; }else if ((entry_ptr -> fx_dir_entry_name[i] >= 'a') && (entry_ptr -> fx_dir_entry_name[i] <= 'z')) { ... } /* Check for a space in the middle of the name. */ else if (entry_ptr -> fx_dir_entry_name[i] == ' ') { entry_ptr -> fx_dir_entry_long_name_present = 1; }else if (entry_ptr -> fx_dir_entry_name[i] == ' ') { ... } /* Check for a dot in the name. */ else if (entry_ptr -> fx_dir_entry_name[i] == '.') { /* Determine if this is the first dot detected. */ if (j == 0) { /* First dot, remember where it was. */ j = i; /* Determine if this is a leading dot. */ if (i == 0) { /* Leading dot detected, treat as a long filename. */ entry_ptr -> fx_dir_entry_long_name_present = 1; }if (i == 0) { ... } }if (j == 0) { ... } else { /* Second dot detected, must have a long file name. */ entry_ptr -> fx_dir_entry_long_name_present = 1; }else { ... } }else if (entry_ptr -> fx_dir_entry_name[i] == '.') { ... } /* Check for a special 0xE5 character. */ else if ((UCHAR)entry_ptr -> fx_dir_entry_name[i] == (UCHAR)0xE5) { entry_ptr -> fx_dir_entry_long_name_present = 1; }else if ((UCHAR)entry_ptr -> fx_dir_entry_name[i] == (UCHAR)0xE5) { ... } /* Check for code point value greater than 127. */ else if ((UCHAR)entry_ptr -> fx_dir_entry_name[i] > (UCHAR)127) { continue; }else if ((UCHAR)entry_ptr -> fx_dir_entry_name[i] > (UCHAR)127) { ... } /* Check for any special characters. */ else if ((entry_ptr -> fx_dir_entry_name[i] == '~') || (entry_ptr -> fx_dir_entry_name[i] == '-') || (entry_ptr -> fx_dir_entry_name[i] == '_') || (entry_ptr -> fx_dir_entry_name[i] == '}') || (entry_ptr -> fx_dir_entry_name[i] == '{') || (entry_ptr -> fx_dir_entry_name[i] == '(') || (entry_ptr -> fx_dir_entry_name[i] == ')') || (entry_ptr -> fx_dir_entry_name[i] == '`') || (entry_ptr -> fx_dir_entry_name[i] == '\'') || (entry_ptr -> fx_dir_entry_name[i] == '!') || (entry_ptr -> fx_dir_entry_name[i] == '#') || (entry_ptr -> fx_dir_entry_name[i] == '$') || (entry_ptr -> fx_dir_entry_name[i] == '&') || (entry_ptr -> fx_dir_entry_name[i] == '@') || (entry_ptr -> fx_dir_entry_name[i] == '^') || (entry_ptr -> fx_dir_entry_name[i] == '%')) { continue; }else if ((entry_ptr -> fx_dir_entry_name[i] == '~') || (entry_ptr -> fx_dir_entry_name[i] == '-') || (entry_ptr -> fx_dir_entry_name[i] == '_') || (entry_ptr -> fx_dir_entry_name[i] == '}') || (entry_ptr -> fx_dir_entry_name[i] == '{') || (entry_ptr -> fx_dir_entry_name[i] == '(') || (entry_ptr -> fx_dir_entry_name[i] == ')') || (entry_ptr -> fx_dir_entry_name[i] == '`') || (entry_ptr -> fx_dir_entry_name[i] == '\'') || (entry_ptr -> fx_dir_entry_name[i] == '!') || (entry_ptr -> fx_dir_entry_name[i] == '#') || (entry_ptr -> fx_dir_entry_name[i] == '$') || (entry_ptr -> fx_dir_entry_name[i] == '&') || (entry_ptr -> fx_dir_entry_name[i] == '@') || (entry_ptr -> fx_dir_entry_name[i] == '^') || (entry_ptr -> fx_dir_entry_name[i] == '%')) { ... } /* Check for long filename special characters. */ else if ((entry_ptr -> fx_dir_entry_name[i] == '+') || (entry_ptr -> fx_dir_entry_name[i] == ',') || (entry_ptr -> fx_dir_entry_name[i] == ';') || (entry_ptr -> fx_dir_entry_name[i] == '=') || (entry_ptr -> fx_dir_entry_name[i] == '[') || (entry_ptr -> fx_dir_entry_name[i] == ']')) { entry_ptr -> fx_dir_entry_long_name_present = 1; }else if ((entry_ptr -> fx_dir_entry_name[i] == '+') || (entry_ptr -> fx_dir_entry_name[i] == ',') || (entry_ptr -> fx_dir_entry_name[i] == ';') || (entry_ptr -> fx_dir_entry_name[i] == '=') || (entry_ptr -> fx_dir_entry_name[i] == '[') || (entry_ptr -> fx_dir_entry_name[i] == ']')) { ... } /* Something is wrong with the supplied name. */ else { return(FX_INVALID_NAME); }else { ... } }for (i = 0, j = 0; entry_ptr -> fx_dir_entry_name[i]; i++) { ... } /* Determine if a dot was found. */ if (j != 0) { /* Yes, Determine if the extension exceeds a 3 character extension. */ if ((i - j) > 4) { /* Yes, long file name is present. */ entry_ptr -> fx_dir_entry_long_name_present = 1; }if ((i - j) > 4) { ... } }if (j != 0) { ... } /* Calculate the total entries needed. */ if ((i <= 12) && (entry_ptr -> fx_dir_entry_long_name_present == 0)) { /* Initialize the total entries to 1. */ total_entries = 1; /* Check for special instance of long file name. */ if ((j >= 9) || ((i - j) >= 9)) { /* The dot is after 8 character or there is no dot and the name is greater than 8 character. *//* ... */ entry_ptr -> fx_dir_entry_long_name_present = 1; total_entries = 2; }if ((j >= 9) || ((i - j) >= 9)) { ... } }if ((i <= 12) && (entry_ptr -> fx_dir_entry_long_name_present == 0)) { ... } else { /* Long file name is present, calculate how many entries are needed to represent it. *//* ... */ if (i % 13 == 0) { /* Exact fit, just add one for the 8.3 short name. */ total_entries = i / 13 + 1; }if (i % 13 == 0) { ... } else { /* Non-exact fit, add two for 8.3 short name and overlap. */ total_entries = i / 13 + 2; }else { ... } }else { ... } /* Determine if the search is in the root directory or in a sub-directory. Note: the directory search function clears the first character of the name for the root directory. *//* ... */ if (directory_ptr -> fx_dir_entry_name[0]) { /* Search for a free entry in a sub-directory. */ /* Pickup the number of entries in this directory. This was placed into the unused file size field. *//* ... */ directory_entries = (ULONG)directory_ptr -> fx_dir_entry_file_size; /* Point the search directory pointer to this entry. */ search_dir_ptr = directory_ptr; /* Ensure that the search directory's last search cluster is cleared. */ search_dir_ptr -> fx_dir_entry_last_search_cluster = 0; /* Set the initial index to 2, since the first two directory entries are always allocated. *//* ... */ directory_index = 2; }if (directory_ptr -> fx_dir_entry_name[0]) { ... } else { /* Find a free entry in the root directory. */ /* Setup the number of directory entries. */ directory_entries = (ULONG)media_ptr -> fx_media_root_directory_entries; /* Set the search pointer to NULL since we are working off of the root directory. *//* ... */ search_dir_ptr = FX_NULL; /* Set the initial index to 0, since the first entry of the root directory is valid. */ directory_index = 0; }else { ... } /* Loop through entries in the search directory. Yes, this is a linear search! *//* ... */ free_entry_start = directory_entries; do { /* Read an entry from the directory. */ status = _fx_directory_entry_read(media_ptr, search_dir_ptr, &directory_index, entry_ptr); /* Check for error status. */ if (status != FX_SUCCESS) { return(status); }if (status != FX_SUCCESS) { ... } /* Determine if this is an empty entry. */ if ((((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR)FX_DIR_ENTRY_FREE) && (entry_ptr -> fx_dir_entry_short_name[0] == 0)) || ((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR)FX_DIR_ENTRY_DONE)) { /* Determine how many entries are needed. */ if (total_entries > 1) { /* Multiple entries are needed for long file names. Mark this entry as free. *//* ... */ if (entry_ptr -> fx_dir_entry_name[0] == FX_DIR_ENTRY_DONE) { entry_ptr -> fx_dir_entry_long_name_present = 0; entry_ptr -> fx_dir_entry_name[0] = (CHAR)FX_DIR_ENTRY_FREE; entry_ptr -> fx_dir_entry_name[1] = (CHAR)0; /* Write out the directory entry. */ status = _fx_directory_entry_write(media_ptr, entry_ptr); if(status != FX_SUCCESS) { return(status); }if (status != FX_SUCCESS) { ... } /* Note that for long names we need to avoid holes in the middle, i.e. entries must be logically contiguous. *//* ... */ }if (entry_ptr -> fx_dir_entry_name[0] == FX_DIR_ENTRY_DONE) { ... } }if (total_entries > 1) { ... } /* Determine if we are at the first free entry. */ if (free_entry_start == directory_entries) { /* Remember the start of the free entry. */ free_entry_start = directory_index; entry_sector = (ULONG)entry_ptr -> fx_dir_entry_log_sector; entry_offset = entry_ptr -> fx_dir_entry_byte_offset; }if (free_entry_start == directory_entries) { ... } /* Determine if there are enough free entries to satisfy the request. */ if ((directory_index - free_entry_start + 1) >= total_entries) { /* Found an empty slot. Most pertinent information is already in the entry structure. *//* ... */ /* Setup the the sector and the offset. */ entry_ptr -> fx_dir_entry_log_sector = entry_sector; entry_ptr -> fx_dir_entry_byte_offset = entry_offset; /* Initialize the additional directory entries. */ entry_ptr -> fx_dir_entry_reserved = 0; entry_ptr -> fx_dir_entry_created_time_ms = 0; /* Lockout interrupts for time/date access. */ FX_DISABLE_INTS entry_ptr -> fx_dir_entry_created_time = _fx_system_time; entry_ptr -> fx_dir_entry_created_date = _fx_system_date; entry_ptr -> fx_dir_entry_last_accessed_date = _fx_system_date; /* Restore interrupts. */ FX_RESTORE_INTS /* Determine if a long file name is present. */ if (total_entries == 1) { entry_ptr -> fx_dir_entry_long_name_present = 0; }if (total_entries == 1) { ... } else { entry_ptr -> fx_dir_entry_long_name_present = 1; }else { ... } /* Return a successful completion. */ return(FX_SUCCESS); }if ((directory_index - free_entry_start + 1) >= total_entries) { ... } }if ((((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR)FX_DIR_ENTRY_FREE) && (entry_ptr -> fx_dir_entry_short_name[0] == 0)) || ((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR)FX_DIR_ENTRY_DONE)) { ... } else { /* Reset the free entry start. */ free_entry_start = directory_entries; }else { ... } /* Move to the next entry. */ directory_index++; /* Determine if we have exceeded the number of entries in the current directory. */ if (directory_index >= directory_entries) { /* Calculate how many sectors we need for the new directory entry. */ sectors = ((total_entries * FX_DIR_ENTRY_SIZE) + (media_ptr -> fx_media_bytes_per_sector - 1))/ media_ptr -> fx_media_bytes_per_sector; /* Now calculate how many clusters we need for the new directory entry. */ clusters_needed = (sectors + (media_ptr -> fx_media_sectors_per_cluster - 1)) / media_ptr -> fx_media_sectors_per_cluster; /* Not enough empty entries were found. If the specified directory is a sub-directory, attempt to allocate another cluster to it. *//* ... */ if (((search_dir_ptr) || (media_ptr -> fx_media_32_bit_FAT)) && (media_ptr -> fx_media_available_clusters > clusters_needed)) { /* Search for the additional clusters we need. */ first_new_cluster = 0; total_clusters = media_ptr -> fx_media_total_clusters; last_cluster = 0; FAT_index = media_ptr -> fx_media_cluster_search_start; clusters = clusters_needed; /* Loop to find the needed clusters. */ while (clusters) { /* Decrease the cluster count. */ clusters--; /* Loop to find the first available cluster. */ do { /* Make sure we stop looking after one pass through the FAT table. */ if (!total_clusters) { /* Something is wrong with the media - the desired clusters were not found in the FAT table. *//* ... */ return(FX_NO_MORE_SPACE); }if (!total_clusters) { ... } /* Read FAT entry. */ status = _fx_utility_FAT_entry_read(media_ptr, FAT_index, &FAT_value); /* Check for a bad status. */ if (status != FX_SUCCESS) { /* Return the bad status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Decrement the total cluster count. */ total_clusters--; /* Determine if the FAT entry is free. */ if (FAT_value == FX_FREE_CLUSTER) { /* Move cluster search pointer forward. */ media_ptr -> fx_media_cluster_search_start = FAT_index + 1; /* Determine if this needs to be wrapped. */ if (media_ptr -> fx_media_cluster_search_start >= (media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START)) { /* Wrap the search to the beginning FAT entry. */ media_ptr -> fx_media_cluster_search_start = FX_FAT_ENTRY_START; }if (media_ptr -> fx_media_cluster_search_start >= (media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START)) { ... } /* Break this loop. */ break; }if (FAT_value == FX_FREE_CLUSTER) { ... } else { /* FAT entry is not free... Advance the FAT index. */ FAT_index++; /* Determine if we need to wrap the FAT index around. */ if (FAT_index >= (media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START)) { /* Wrap the search to the beginning FAT entry. */ FAT_index = FX_FAT_ENTRY_START; }if (FAT_index >= (media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START)) { ... } }else { ... } ...} while (FX_TRUE); /* We found an available cluster. We now need to clear all of entries in each of the cluster's sectors. *//* ... */ /* Calculate the logical sector of this cluster. */ logical_sector = ((ULONG) media_ptr -> fx_media_data_sector_start) + ((((ULONG) FAT_index) - FX_FAT_ENTRY_START) * ((ULONG) media_ptr -> fx_media_sectors_per_cluster)); /* Pickup the number of sectors for the next directory cluster. */ sectors = media_ptr -> fx_media_sectors_per_cluster; /* Read the logical sector just for cache reasons. */ status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) logical_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DIRECTORY_SECTOR); /* Check the return value. */ if (status != FX_SUCCESS) { /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Clear the entire first sector of the new sub-directory cluster. */ work_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer; i = 0; while (i < media_ptr -> fx_media_bytes_per_sector) { /* Clear 4 bytes. */ *((ULONG *)work_ptr) = (ULONG)0; /* Increment pointer. */ work_ptr = work_ptr + sizeof(ULONG); /* Increment counter. */ i = i + (ULONG)sizeof(ULONG); }while (i < media_ptr -> fx_media_bytes_per_sector) { ... } /* Write the logical sector to ensure the zeros are written. */ status = _fx_utility_logical_sector_write(media_ptr, (ULONG64) logical_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DIRECTORY_SECTOR); /* Determine if the write was successful. */ if (status != FX_SUCCESS) { /* Return the error code. */ return(status); }if (status != FX_SUCCESS) { ... } /* Determine if there are more sectors to clear in the first cluster of the new sub-directory. *//* ... */ if (sectors > 1) { /* Yes, invalidate all cached sectors that are contained in the newly allocated first cluster of the directory. *//* ... */ /* Flush the internal logical sector cache. */ status = _fx_utility_logical_sector_flush(media_ptr, (ULONG64) (logical_sector + 1), (ULONG64) (sectors - 1), FX_TRUE); /* Determine if the flush was successful. */ if (status != FX_SUCCESS) { /* Return the error code. */ return(status); }if (status != FX_SUCCESS) { ... } /* Clear all additional sectors of new sub-directory. */ sectors--; while (sectors) { #ifndef FX_MEDIA_STATISTICS_DISABLE /* Increment the number of driver write sector(s) requests. */ media_ptr -> fx_media_driver_write_requests++;/* ... */ #endif /* Build Write request to the driver. */ media_ptr -> fx_media_driver_request = FX_DRIVER_WRITE; media_ptr -> fx_media_driver_status = FX_IO_ERROR; media_ptr -> fx_media_driver_buffer = media_ptr -> fx_media_memory_buffer; media_ptr -> fx_media_driver_logical_sector = (ULONG)logical_sector + ((ULONG)sectors); media_ptr -> fx_media_driver_sectors = 1; media_ptr -> fx_media_driver_sector_type = FX_DIRECTORY_SECTOR; /* Set the system write flag since we are writing a directory sector. */ media_ptr -> fx_media_driver_system_write = FX_TRUE; /* If trace is enabled, insert this event into the trace buffer. */ FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_WRITE, media_ptr, ((ULONG)logical_sector) + ((ULONG)sectors), 1, media_ptr -> fx_media_memory_buffer, FX_TRACE_INTERNAL_EVENTS, 0, 0) /* Invoke the driver to write the sector. */ (media_ptr -> fx_media_driver_entry) (media_ptr); /* Clear the system write flag. */ media_ptr -> fx_media_driver_system_write = FX_FALSE; /* Determine if an error occurred. */ if (media_ptr -> fx_media_driver_status != FX_SUCCESS) { /* Return error code. */ return(media_ptr -> fx_media_driver_status); }if (media_ptr -> fx_media_driver_status != FX_SUCCESS) { ... } /* Decrease the number of sectors to clear. */ sectors--; }while (sectors) { ... } }if (sectors > 1) { ... } /* Determine if we have found the first new cluster yet. */ if (first_new_cluster == 0) { /* Remember the first new cluster. */ first_new_cluster = FAT_index; }if (first_new_cluster == 0) { ... } /* Check for a valid last cluster to link. */ if (last_cluster) { /* Normal condition - link the last cluster with the new found cluster. *//* ... */ status = _fx_utility_FAT_entry_write(media_ptr, last_cluster, FAT_index); /* Check for a bad FAT write status. */ if (status != FX_SUCCESS) { /* Return the bad status. */ return(status); }if (status != FX_SUCCESS) { ... } }if (last_cluster) { ... } /* Otherwise, remember the new FAT index as the last. */ last_cluster = FAT_index; /* Move to the next FAT entry. */ FAT_index = media_ptr -> fx_media_cluster_search_start; }while (clusters) { ... } /* Place an end-of-file marker on the last cluster. */ status = _fx_utility_FAT_entry_write(media_ptr, last_cluster, media_ptr -> fx_media_fat_last); /* Check for a bad FAT write status. */ if (status != FX_SUCCESS) { /* Return the bad status. */ return(status); }if (status != FX_SUCCESS) { ... } #ifdef FX_FAULT_TOLERANT /* Ensure the new FAT chain is properly written to the media. */ /* Flush the cached individual FAT entries */ _fx_utility_FAT_flush(media_ptr);/* ... */ #endif /* Now the new cluster needs to be linked to the sub-directory. */ if (search_dir_ptr) { cluster = search_dir_ptr -> fx_dir_entry_cluster; }if (search_dir_ptr) { ... } else { cluster = media_ptr -> fx_media_root_cluster_32; }else { ... } /* Initialize loop variables. */ last_cluster = 0; i = 0; /* Follow the link of FAT entries. */ while (cluster < media_ptr -> fx_media_fat_reserved) { /* Read the current cluster entry from the FAT. */ status = _fx_utility_FAT_entry_read(media_ptr, cluster, &FAT_value); i++; /* Check the return value. */ if (status != FX_SUCCESS) { /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Determine if the FAT read was invalid. */ if ((cluster < FX_FAT_ENTRY_START) || (cluster == FAT_value) || (i > media_ptr -> fx_media_total_clusters)) { /* Return the bad status. */ return(FX_FAT_READ_ERROR); }if ((cluster < FX_FAT_ENTRY_START) || (cluster == FAT_value) || (i > media_ptr -> fx_media_total_clusters)) { ... } /* Save the last valid cluster. */ last_cluster = cluster; /* Setup for the next cluster. */ cluster = FAT_value; }while (cluster < media_ptr -> fx_media_fat_reserved) { ... } /* Decrease the available clusters in the media. */ media_ptr -> fx_media_available_clusters = media_ptr -> fx_media_available_clusters - clusters_needed; /* Increase the number of directory entries. */ directory_entries = directory_entries + ((clusters_needed * media_ptr -> fx_media_sectors_per_cluster) * media_ptr -> fx_media_bytes_per_sector) / FX_DIR_ENTRY_SIZE; /* Determine if we need to reset the free entry start since we changed the number of directory entries. If the last entry was not free, then we should definitely reset the free entry start. *//* ... */ if (!(((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR) FX_DIR_ENTRY_FREE) && (entry_ptr -> fx_dir_entry_short_name[0] == 0))) { /* Reset the free entry start to indicate we haven't found a starting free entry yet. */ free_entry_start = directory_entries; }if (!(((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR) FX_DIR_ENTRY_FREE) && (entry_ptr -> fx_dir_entry_short_name[0] == 0))) { ... } /* Update the directory size field. */ directory_ptr -> fx_dir_entry_file_size = directory_entries; /* Defer the update of the FAT entry and the last cluster of the current directory entry until after the new cluster is initialized and written out. *//* ... */ /* Determine if a FAT32 is present. */ if ((media_ptr -> fx_media_32_bit_FAT) && (search_dir_ptr == FX_NULL)) { /* Change root directory entry count - FAT32 has a variable sized root directory. */ media_ptr -> fx_media_root_directory_entries = directory_entries; }if ((media_ptr -> fx_media_32_bit_FAT) && (search_dir_ptr == FX_NULL)) { ... } /* At this point, link up the last cluster with the new cluster. */ status = _fx_utility_FAT_entry_write(media_ptr, last_cluster, first_new_cluster); /* Check the return value. */ if (status != FX_SUCCESS) { /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } #ifdef FX_FAULT_TOLERANT /* Flush the cached individual FAT entries */ _fx_utility_FAT_flush(media_ptr);/* ... */ #endif }if (((search_dir_ptr) || (media_ptr -> fx_media_32_bit_FAT)) && (media_ptr -> fx_media_available_clusters > clusters_needed)) { ... } }if (directory_index >= directory_entries) { ... } ...} while (directory_index < directory_entries); /* Return FX_NO_MORE_SPACE status to the caller. */ return(FX_NO_MORE_SPACE); }{ ... }