-
Notifications
You must be signed in to change notification settings - Fork 2
/
fat.c
3655 lines (3015 loc) · 92.1 KB
/
fat.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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2013 Neil McGill
*
* See the LICENSE file for license.
*/
#include "config.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <errno.h>
#include <fcntl.h>
#include "main.h"
#include <unistd.h>
#include <ctype.h>
#include "disk.h"
#include "fat.h"
/*
* FAT constants
*/
static const uint32_t FAT_DIRENT_SIZE = 32;
static const uint32_t FAT_DOS_MAX_FILENAME_LEN = 11;
static const uint32_t FAT_VFAT_FILENAME_FRAG_LEN = 13;
static const uint32_t FAT_FILE_DELETE_CHAR = 0xE5;
/*
* FAT file attr flags.
*/
//static const uint32_t FAT_ATTR_IS_READ_ONLY = 0x01;
//static const uint32_t FAT_ATTR_IS_HIDDEN = 0x02;
//static const uint32_t FAT_ATTR_IS_SYSTEM = 0x04;
//static const uint32_t FAT_ATTR_IS_LABEL = 0x08;
static const uint32_t FAT_ATTR_IS_DIR = 0x10;
static const uint32_t FAT_ATTR_IS_ARCHIVE = 0x20;
/*
* Prototypes.
*/
static boolean dirent_has_extension(fat_dirent_t *dirent);
static uint32_t vfat_fragments(const char *vfat_filename);
static char *dos_last_dot(char *in);
static char *dirent_read_name(disk_t *disk, fat_dirent_t *dirent,
char *vfat_filename);
static boolean dos_file_match(const char *a, const char *b, boolean is_dir);
/*
* fat_type
*
* Work out the type of FAT on this disk based on number of clusters.
*/
uint32_t fat_type (disk_t *disk)
{
uint32_t fat_type;
if (disk->partition_set && disk->parts[disk->partition]) {
switch (disk->parts[disk->partition]->os_id) {
case DISK_FAT12:
return (12);
case DISK_FAT16:
case DISK_FAT16_LBA:
return (16);
case DISK_FAT32:
case DISK_FAT32_LBA:
/*
* Just in case this is a misconfigured disk with FAT32 in the
* partition table, yet FAT16 is inferred from the FS, then
* use FAT12/16 as inferred to avoid corrupting ths disk.
*/
if (disk->mbr->fat_size_sectors) {
/*
* We can only get here for FAT12/16. FAT32 will have the
* fat size sectors as 0.
*/
if (total_clusters(disk) < 4085) {
fat_type = 12;
} else {
fat_type = 16;
}
return (fat_type);
}
return (32);
}
}
if (disk->mbr->fat_size_sectors) {
if (total_clusters(disk) < 4085) {
fat_type = 12;
} else {
fat_type = 16;
}
return (fat_type);
}
if (total_clusters(disk) < 4085) {
fat_type = 12;
} else if (total_clusters(disk) < 65525) {
fat_type = 16;
} else {
fat_type = 32;
}
return (fat_type);
}
/*
* sector_root_dir
*
* The first data sector (that is, the first sector in which directories and
* files may be stored):
*/
uint32_t sector_root_dir (disk_t *disk)
{
return (disk->mbr->reserved_sector_count +
(disk->mbr->number_of_fats * fat_size_sectors(disk)));
}
/*
* sector_first_data_sector
*
* The first data sector (that is, the first sector in which directories and
* files may be stored):
*/
uint32_t sector_first_data_sector (disk_t *disk)
{
uint32_t total;
total = disk->mbr->reserved_sector_count +
(disk->mbr->number_of_fats * fat_size_sectors(disk));
/*
* No root dir for FAT32.
*/
if (fat_type(disk) == 32) {
return (total);
}
return (total + root_dir_size_sectors(disk));
}
/*
* sector_count_data
*
* The total number of data sectors:
*/
uint64_t sector_count_data (disk_t *disk)
{
return (sector_count_total(disk) -
((uint64_t)disk->mbr->reserved_sector_count +
((uint64_t)disk->mbr->number_of_fats * fat_size_sectors(disk)) +
(uint64_t) root_dir_size_sectors(disk)));
}
/*
* fat_size_bytes
*
* How large is the FAT table in bytes?
*/
uint64_t fat_size_bytes (disk_t *disk)
{
return (fat_size_sectors(disk) * disk->mbr->sector_size);
}
/*
* fat_size_sectors
*
* The number of blocks occupied by one copy of the FAT.
*/
uint64_t fat_size_sectors (disk_t *disk)
{
if (disk->mbr->fat_size_sectors) {
return (disk->mbr->fat_size_sectors);
} else {
return (disk->mbr->fat.fat32.fat_size_sectors);
}
}
/*
* root_dir_size_bytes
*
* The size of the root directory (unless you have FAT32, in which case the
* size will be 0):
*/
uint32_t root_dir_size_bytes (disk_t *disk)
{
if (!sector_size(disk)) {
ERR("Boot record, sector size is 0 when calculating root dir size");
return (0);
}
return (disk->mbr->number_of_dirents * FAT_DIRENT_SIZE);
}
/*
* cluster_next
*
* Given a cluster, return the next cluster.
*/
static uint32_t cluster_next (disk_t *disk, uint32_t cluster)
{
uint32_t fat_byte_offset;
uint32_t cluster_next;
uint8_t *fat;
/*
* Find the array index of the current cluster.
*/
if (fat_type(disk) == 12) {
fat_byte_offset = cluster + (cluster / 2); // multiply by 1.5
} else if (fat_type(disk) == 16) {
fat_byte_offset = cluster * sizeof(uint16_t);
} else if (fat_type(disk) == 32) {
fat_byte_offset = cluster * sizeof(uint32_t);
} else {
DIE("bug");
}
/*
* Only look at the first FAT. I think this is ok. The others are backups.
*/
fat = disk->fat;
fat_byte_offset = fat_byte_offset % fat_size_bytes(disk);
/*
* Find the cluster in this next array index.
*/
if (fat_type(disk) == 12) {
cluster_next = *(uint16_t*) (fat + fat_byte_offset);
if (cluster & 0x0001) {
cluster_next = cluster_next >> 4;
} else {
cluster_next = cluster_next & 0x0FFF;
}
} else if (fat_type(disk) == 16) {
cluster_next = *(uint16_t*) (fat + fat_byte_offset);
} else if (fat_type(disk) == 32) {
cluster_next = (*((uint32_t*) (fat + fat_byte_offset))) & 0x0FFFFFFF;
} else {
DIE("bug");
}
return (cluster_next);
}
/*
* cluster_next_set
*
* Given a cluster, set what it points to.
*/
static uint32_t cluster_next_set (disk_t *disk,
uint32_t cluster,
uint32_t cluster_next,
boolean update_fat)
{
uint32_t fat_byte_offset;
uint8_t *fat;
uint16_t old;
/*
* Find the array index of the current cluster.
*/
if (fat_type(disk) == 12) {
fat_byte_offset = cluster + (cluster / 2); // multiply by 1.5
} else if (fat_type(disk) == 16) {
fat_byte_offset = cluster * sizeof(uint16_t);
} else if (fat_type(disk) == 32) {
fat_byte_offset = cluster * sizeof(uint32_t);
} else {
DIE("bug");
}
/*
* Only look at the first FAT. I think this is ok. The others are backups.
*/
fat = disk->fat;
if (fat_byte_offset > fat_size_bytes(disk)) {
/*
* Propbably not critical, but not ideal either. Means we've not
* sized the FAT enough.
*/
DIE("Trying to access cluster %" PRIu32 " which "
"is bigger than the FAT. "
"Total clusters on disk %" PRIu32 ". "
"FAT size in clusters %" PRIu64 ". "
"FAT size in bytes %" PRIu64 ". "
"FAT offset being accessed %" PRIu32 ".",
cluster,
total_clusters(disk),
fat_size_sectors(disk),
fat_size_bytes(disk),
fat_byte_offset);
return (0);
}
fat_byte_offset = fat_byte_offset % fat_size_bytes(disk);
/*
* Find the cluster in this next array index.
*/
if (fat_type(disk) == 12) {
/*
* Need to mask out the old cluster but keep the surrounding bits
* for the cluster that is sharing these 16 bits. What a horrible
* system.
*/
memcpy(&old, fat + fat_byte_offset, 2);
old = *(uint16_t*) (fat + fat_byte_offset);
if (cluster & 0x0001) {
cluster_next = cluster_next << 4;
cluster_next |= old & 0x000F;
} else {
cluster_next = cluster_next & 0x0FFF;
cluster_next |= old & 0xF000;
}
memcpy(fat + fat_byte_offset, &cluster_next, 2);
} else if (fat_type(disk) == 16) {
uint16_t next = cluster_next;
memcpy(fat + fat_byte_offset, &next, 2);
} else if (fat_type(disk) == 32) {
memcpy(fat + fat_byte_offset, &cluster_next, 4);
} else {
DIE("bug");
}
if (!update_fat) {
return (cluster_next);
}
/*
* Update just the touched sectors on the FAT.
*/
uint32_t sector_start = fat_byte_offset / sector_size(disk);
uint32_t sector_end = (fat_byte_offset + 4) / sector_size(disk);
uint32_t sector_max;
uint8_t *data = ((uint8_t*) disk->fat) +
(sector_start * sector_size(disk));
sector_start += sector_reserved_count(disk);
sector_end += sector_reserved_count(disk);
sector_max = sector_reserved_count(disk) + fat_size_sectors(disk);
if (sector_end > sector_max) {
DIE("Failed to update FAT sector %" PRIu32 " .. %" PRIu32
" max %" PRIu32 "",
sector_start, sector_end, sector_max);
sector_end = sector_max;
}
if (!sector_write(disk, sector_start,
data, sector_end - sector_start + 1)) {
DIE("Failed to update FAT sector %" PRIu32 " .. %" PRIu32
" max %" PRIu32 "",
sector_start, sector_end, sector_max);
}
return (cluster_next);
}
/*
* cluster_alloc
*
* Find a free cluster.
*/
static uint32_t cluster_alloc (disk_t *disk)
{
/*
* Start from the first valid cluster.
*/
static uint32_t last_cluster = 2;
uint32_t fat_byte_offset;
uint32_t cluster_next;
uint32_t cluster;
uint8_t *fat;
redo:
/*
* Try from the last cluster found to speed things up and give us
* a chance for things to be sequential.
*/
for (cluster = last_cluster; cluster < total_clusters(disk); cluster++) {
/*
* Find the array index of the current cluster.
*/
if (fat_type(disk) == 12) {
/*
* Ignore root cluster if FAT32.
*/
if (cluster == disk->mbr->fat.fat32.root_cluster) {
continue;
}
fat_byte_offset = cluster + (cluster / 2); // multiply by 1.5
} else if (fat_type(disk) == 16) {
fat_byte_offset = cluster * sizeof(uint16_t);
} else if (fat_type(disk) == 32) {
fat_byte_offset = cluster * sizeof(uint32_t);
} else {
DIE("bug");
}
/*
* Only look at the first FAT. I think this is ok. The others are
* backups.
*/
fat = disk->fat;
fat_byte_offset = fat_byte_offset % fat_size_bytes(disk);
/*
* Find the cluster in this next array index.
*/
if (fat_type(disk) == 12) {
cluster_next = *(uint16_t*) (fat + fat_byte_offset);
if (cluster & 0x0001) {
cluster_next = cluster_next >> 4;
} else {
cluster_next = cluster_next & 0x0FFF;
}
} else if (fat_type(disk) == 16) {
cluster_next = *(uint16_t*) (fat + fat_byte_offset);
} else if (fat_type(disk) == 32) {
cluster_next = (*((uint32_t*)
(fat + fat_byte_offset))) & 0x0FFFFFFF;
} else {
DIE("bug");
}
if (!cluster_next) {
#ifdef FAT_WRITE_EMPTY_CLUSTERS_ON_ALLOC
/*
* This is too slow for file importing. It is needed when making
* writing unless paranoid about removing old info.
*/
uint8_t *tmp;
tmp = myzalloc(cluster_size(disk), __FUNCTION__);
cluster_write(disk, cluster - 2, tmp, 1);
myfree(tmp);
#endif
DBG2("Allocated cluster %" PRIu32, cluster);
/*
* Next time, search from this cluster onwards, for speed.
*/
last_cluster = cluster;
return (cluster);
}
}
/*
* Out of clusters? Retry once.
*/
if (last_cluster > 2) {
last_cluster = 2;
goto redo;
}
ERR("Out of clusters, total clusters on disk, %u, "
"data sectors %" PRIu64 ", "
"sectors per cluster %u",
total_clusters(disk),
sector_count_data(disk),
disk->mbr->sectors_per_cluster);
return (0);
}
/*
* cluster_how_many_free
*
* How many free clusters are there on disk?
*/
uint64_t cluster_how_many_free (disk_t *disk)
{
uint32_t fat_byte_offset;
uint32_t cluster_next;
uint32_t cluster;
uint8_t *fat;
uint64_t free = 0;
/*
* Try from the last cluster found to speed things up and give us
* a chance for things to be sequential.
*/
for (cluster = 2; cluster < total_clusters(disk); cluster++) {
/*
* Find the array index of the current cluster.
*/
if (fat_type(disk) == 12) {
/*
* Ignore root cluster if FAT32.
*/
if (cluster == disk->mbr->fat.fat32.root_cluster) {
continue;
}
fat_byte_offset = cluster + (cluster / 2); // multiply by 1.5
} else if (fat_type(disk) == 16) {
fat_byte_offset = cluster * sizeof(uint16_t);
} else if (fat_type(disk) == 32) {
fat_byte_offset = cluster * sizeof(uint32_t);
} else {
DIE("bug");
}
/*
* Only look at the first FAT. I think this is ok. The others are
* backups.
*/
fat = disk->fat;
fat_byte_offset = fat_byte_offset % fat_size_bytes(disk);
/*
* Find the cluster in this next array index.
*/
if (fat_type(disk) == 12) {
cluster_next = *(uint16_t*) (fat + fat_byte_offset);
if (cluster & 0x0001) {
cluster_next = cluster_next >> 4;
} else {
cluster_next = cluster_next & 0x0FFF;
}
} else if (fat_type(disk) == 16) {
cluster_next = *(uint16_t*) (fat + fat_byte_offset);
} else if (fat_type(disk) == 32) {
cluster_next = (*((uint32_t*)
(fat + fat_byte_offset))) & 0x0FFFFFFF;
} else {
DIE("bug");
}
if (!cluster_next) {
free++;
}
}
return (free);
}
/*
* Read and cache the FAT
*/
void fat_read (disk_t *disk)
{
if (disk->fat) {
return;
}
if (disk->partition_set && disk->parts[disk->partition]) {
switch (disk->parts[disk->partition]->os_id) {
case DISK_FAT12:
case DISK_FAT16:
case DISK_FAT16_LBA:
case DISK_FAT32:
case DISK_FAT32_LBA:
break;
default:
ERR("Cannot read fat at partition %u sector %" PRIu32 "",
disk->partition,
sector_reserved_count(disk));
disk->fat = 0;
return;
}
}
DBG2("Read FAT, %" PRIu64 " sectors...",
sector_reserved_count(disk) * fat_size_sectors(disk));
disk->fat = sector_read(disk,
sector_reserved_count(disk),
fat_size_sectors(disk));
if (!disk->fat) {
ERR("Cannot read fat at sector %" PRIu32 "",
sector_reserved_count(disk));
return;
}
}
/*
* fat_write
*
* Update the FAT on disk with any sectors that are dirtied.
*/
void fat_write (disk_t *disk)
{
uint32_t sector;
uint32_t sectors;
uint8_t *data;
data = (uint8_t*) disk->fat;
if (!data) {
return;
}
sector = sector_reserved_count(disk);
DBG2("FAT write");
sectors = fat_size_sectors(disk);
sector_pre_write_print_dirty_sectors(disk, sector, data, sectors);
if (!sector_write(disk, sector, data, sectors)) {
DIE("cannot write FAT at sector %" PRIu32 "", sector);
}
}
/*
* cluster_max
*
* For the given FS, what is the max cluster?
*/
static uint32_t cluster_max (disk_t *disk)
{
if (fat_type(disk) == 12) {
return (0xFF8);
} else if (fat_type(disk) == 16) {
/*
* FFF0-FFF6: reserved, FFF7: bad cluster, FFF8-FFFF
*/
return (0xFFF8);
} else if (fat_type(disk) == 32) {
/*
* Some operating systems use fff, ffff, xfffffff as end of
* clusterchain markers, but various common utilities may use
* different values.
*
* Linux has always used ff8, fff8, but it now appears that some MP3
* players fail to work unless fff etc. is used.
*
* However, grub chokes if 0x0FFFFFFF is used. Stick with 0x0FFFFFF8
* for now until someone complains.
*/
return (0x0FFFFFF8);
} else {
DIE("cluster max, channot determine disk type");
}
}
/*
* cluster_endchain
*
* Is this cluster the end of a chain?
*/
static uint32_t cluster_endchain (disk_t *disk, uint32_t cluster)
{
if (fat_type(disk) == 32) {
/*
* Root cluster sector cannot be a next sector.
*/
if (cluster <= 2) {
return (true);
}
} else {
if (cluster < 2) {
return (true);
}
}
if (fat_type(disk) == 12) {
/*
* FF0-FF6: reserved, FF7: bad cluster, FF8-FFF
*/
if (cluster >= 0xFF0) {
return (true);
}
} else if (fat_type(disk) == 16) {
/*
* FFF0-FFF6: reserved, FFF7: bad cluster, FFF8-FFFF
*/
if (cluster >= 0xFFF0) {
return (true);
}
} else if (fat_type(disk) == 32) {
if (cluster >= 0x0FF8FFF8) {
return (true);
}
} else {
DIE("cluster end chain, channot determine disk type");
}
return (false);
}
/*
* dirent_first_cluster
*
* Where does the file data begin?
*/
static uint32_t dirent_first_cluster (const fat_dirent_t *dirent)
{
return ((dirent->h_first_cluster << 16) | dirent->l_first_cluster);
}
/*
* dirent_attr_string
*
* Expand file attributes.
*/
static const char *dirent_attr_string (const fat_dirent_t *dirent)
{
/*
* 0 0x01 Read Only. (Since DOS 2.0) If this bit is set, the
* operating system will not allow a file to be opened for modification.
*
* Deliberately setting this bit for files which will not be written to
* (executables, shared libraries and data files) may help avoid problems
* with concurrent file access in multi-tasking, multi-user or network
* environments with applications not specifically designed to work in
* such environments (i.e. non-SHARE-enabled programs).
*
* 1 0x02 Hidden. Hides files or directories from normal directory
* views.
*
* Under DR DOS 3.31 and higher, under PalmDOS, Novell DOS, OpenDOS,
* Concurrent DOS, Multiuser DOS, REAL/32, password protected files and
* directories also have the hidden attribute set.[65] Password-aware
* operating systems should not hide password-protected files from
* directory views, even if this bit may be set. The password protection
* mechanism does not depend on the hidden attribute being set up to
* including DR-DOS 7.03, but if the hidden attribute is set, it should
* not be cleared for any password-protected files.
*
* 2 0x04 System. Indicates that the file belongs to the system and
* must not be physically moved (f.e. during defragmentation), because
* there may be references into the file using absolute addressing
* bypassing the file system (boot loaders, kernel images, swap files,
* extended attributes, etc.).
*
* 3 0x08 Volume Label. (Since DOS 2.0) Indicates an optional
* directory volume label, normally only residing in a volume's root
* directory. Ideally, the volume label should be the first entry in the
* directory (after reserved entries) in order to avoid problems with VFAT
* LFNs. If this volume label is not present, some systems may fall back
* to display the partition volume label instead, if a EBPB is present in
* the boot sector (not present with some non-bootable block device
* drivers, and possibly not writeable with boot sector write protection).
* Even if this volume label is present, partitioning tools like FDISK may
* display the partition volume label instead. The entry occupies a
* directory entry but has no file associated with it. Volume labels have
* a filesize entry of zero.
*
* Pending delete files and directories under DELWATCH have the volume
* attribute set until they are purged or undeleted.[65]
*
* 4 0x10 Subdirectory. (Since DOS 2.0) Indicates that the
* cluster-chain associated with this entry gets interpreted as
* subdirectory instead of as a file. Subdirectories have a filesize entry
* of zero.
*
* 5 0x20 Archive. (Since DOS 2.0) Typically set by the operating
* system as soon as the file is created or modified to mark the file as
* "dirty", and reset by backup software once the file has been backed up
* to indicate "pure" state.
*
* 6 0x40 Device (internally set for character device names found in
* filespecs, never found on disk), must not be changed by disk tools.
*
* 7 0x80 Reserved, must not be changed by disk tools.
*/
static char attributes[] = { 'r', 'h', 's', 'v', 'd', 'a', 'D' };
static char attrs[sizeof(attributes)];
uint32_t attr;
uint32_t i;
attr = dirent->attr;
for (i = 0; i < sizeof(attributes); i++) {
if (attr & 1) {
attrs[i] = attributes[i];
} else {
attrs[i] = '-';
attr >>= 1;
}
}
return (attrs);
}
/*
* dirent_month
*
* Return the month for this file.
*/
static const char *dirent_month (uint32_t month)
{
const char *str_month[12] = {
"Jan", "Feb", "Mar", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"
};
const char *month_str = "???";
if ((month > 0) && (month <= 12)) {
month_str = str_month[month - 1];
}
return (month_str);
}
/*
* dirent_name_trim
*
* Trim spaces from the FAT file name.
*/
static char *dirent_name_trim (char *string)
{
size_t len;
len = strlen(string);
while (len-- > 0) {
if (string[len] != ' ') {
return (string);
}
string[len] = '\0';
}
return (string);
}
/*
* dirent_name_copy
*
* Alloc a filename from the dir entry, FAT or VFAT.
*/
static char *dirent_name_copy (fat_dirent_t *dirent)
{
char *name;
uint32_t i;
name = (typeof(name)) myzalloc(MAX_STR, "FAT filename");
for (i = 0; i < sizeof(dirent->name); i++) {
name[i] = dirent->name[i];
}
name[i] = '\0';
if (dirent_has_extension(dirent)) {
dirent_name_trim(name);
strcat(name, ".");
for (i = 0; i < sizeof(dirent->ext); i++) {
char tmp[2];
tmp[0] = dirent->ext[i];
tmp[1] = 0;
strcat(name, tmp);
}
}
/*
* Here we assume dirent_has_extension will return true only if
* there's a valid (non-blank, non-nil) ext, tx Andy Dalton.
*/
if (dirent_has_extension(dirent)) {
dirent_name_trim(name);
size_t index = strlen(name);
name[index++] = '.';
for (i = 0; i < sizeof(dirent->ext); ++i) {
name[index + i] = dirent->ext[i];
}
name[index + i] = '\0';
}
return (dirent_name_trim(name));
}
/*
* dirent_is_dir
*
* Dirent is a dir?
*/
static boolean dirent_is_dir (fat_dirent_t *dirent)
{
if (dirent->attr & FAT_ATTR_IS_DIR) {
return (true);
}
return (false);
}
/*
* dirent_has_extension
*
* Does the file have a non null three letter extension?
*/
static boolean dirent_has_extension (fat_dirent_t *dirent)
{
if ((dirent->ext[0] == ' ') &&
(dirent->ext[1] == ' ') &&
(dirent->ext[2] == ' ')) {
return (false);
} else {
return (true);
}
}
/*
* file_import
*
* Read in the file and create it (or directory). For directories we make
* the subdir too.
*/
static uint32_t file_import (disk_t *disk,
disk_walk_args_t *args,
fat_dirent_t *dirent,
const char *filename,
uint32_t parent_cluster,
uint32_t depth)
{
char *base = mybasename(filename, __FUNCTION__);
fat_dirent_t *dirent_in = dirent;
fat_dirent_long_t *fat_dirent;
char tmp[MAX_STR] = {0};
uint32_t cluster;
uint8_t *data;
uint32_t count;
int32_t year;
int32_t day;
int32_t month;
count = 0;
strncpy(tmp, base, sizeof(tmp));
/*
* How many VFAT fragments?
*/
const uint32_t fragments = vfat_fragments(filename);
/*
* Make the long VFAT filename out of fragments.
*/
boolean first_fragment = true;
int32_t fragment = fragments;
while (fragment--) {
/*
* Null out the fragment.
*/
fat_dirent = (typeof(fat_dirent)) dirent;
/*
* Sanity check.
*/
if (fat_dirent->order &&
(fat_dirent->order != FAT_FILE_DELETE_CHAR)) {
hex_dump(fat_dirent, 0, sizeof(*dirent));
DIE("overwriting an existing file, order bytes is set "
"while adding %s", filename);
}
memset(fat_dirent, 0, sizeof(*fat_dirent));
uint32_t index = (fragment * FAT_VFAT_FILENAME_FRAG_LEN);
char c;
/*
* This logic is quite horrible in how we pad out names. Blame
* Microsoft.