simple-fatfs 0.1.0-alpha.2

A simple-to-use FAT filesystem library for Rust (mainly targeted at embedded systems)
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
use super::*;

use crate::{error::*, path::*, utils};

use core::{
    cell::{Ref, RefCell},
    cmp, iter, num, ops,
};

#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, string::ToString, vec, vec::Vec};

use ::time;
use embedded_io::*;
use time::PrimitiveDateTime;

/// An enum representing different variants of the FAT filesystem
///
/// The logic is essentially the same in all of them, the only thing that
/// changes is the size in bytes of FAT entries, and thus the maximum volume size
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
// no need for enum variant documentation here
pub enum FATType {
    /// One of the earliest versions, originally used all the way back to 1980.
    /// This probably won't be encountered anywhere outside ancient MS-DOS versions
    /// or pretty low-size volumes, like microcontrollers
    ///
    /// Max volume size: 8 MB
    FAT12,
    /// Used in many low-size volumes
    ///
    /// Min volume size: 8 MB,
    /// Max volume size: 16 GB
    FAT16,
    /// The most commonly-used variant.
    ///
    /// Min volume size: 256 MB,
    /// Max volume size: 16 TB
    FAT32,
    /// An ex-proprietory filesystem that allows for even larger storage sizes
    /// and its use is currently on the rise
    ///
    /// Not currently supported
    ExFAT,
}

impl FATType {
    #[inline]
    /// How many bits this [`FATType`] uses to address clusters in the disk
    fn bits_per_entry(&self) -> u8 {
        match self {
            FATType::FAT12 => 12,
            FATType::FAT16 => 16,
            // the high 4 bits are ignored, but are still part of the entry
            FATType::FAT32 => 32,
            FATType::ExFAT => 32,
        }
    }

    #[inline]
    /// How many bytes this [`FATType`] spans across
    fn entry_size(&self) -> u8 {
        self.bits_per_entry().next_power_of_two() / 8
    }
}

// the first 2 entries are reserved
const RESERVED_FAT_ENTRIES: FATEntryCount = 2;

#[derive(Debug, Clone, PartialEq)]
pub(crate) enum FATEntry {
    /// This cluster is free
    Free,
    /// This cluster is allocated and the next cluster is the contained value
    Allocated(ClusterIndex),
    /// This cluster is reserved
    Reserved,
    /// This is a bad (defective) cluster
    Bad,
    /// This cluster is allocated and is the final cluster of the file
    Eof,
}

impl From<FATEntry> for FATEntryValue {
    fn from(value: FATEntry) -> Self {
        Self::from(&value)
    }
}

impl From<&FATEntry> for FATEntryValue {
    fn from(value: &FATEntry) -> Self {
        match value {
            FATEntry::Free => FATEntryValue::MIN,
            FATEntry::Allocated(cluster) => *cluster,
            FATEntry::Reserved => 0xFFFFFF6,
            FATEntry::Bad => 0xFFFFFF7,
            FATEntry::Eof => FATEntryValue::MAX,
        }
    }
}

/// Properties about the position of a [`FATEntry`] inside the FAT region
struct FATEntryProps {
    /// Each `n`th element of the vector points at the corrensponding sector at the (first) active FAT table
    fat_sector: SectorIndex,
    sector_offset: usize,
}

impl FATEntryProps {
    /// Get the [`FATEntryProps`] of the `n`-th [`FATEntry`] of a [`FileSystem`]
    pub fn new<S>(n: FATEntryIndex, fs: &FileSystem<S>) -> Self
    where
        S: Read + Seek,
    {
        let fat_byte_offset: u64 = u64::from(n) * u64::from(fs.fat_type.bits_per_entry()) / 8;
        let fat_sector = SectorIndex::try_from(
            u64::from(fs.props.first_fat_sector)
                + fat_byte_offset / u64::from(fs.props.sector_size),
        )
        .expect("this should fit into a u32");
        let sector_offset: usize =
            usize::try_from(fat_byte_offset % u64::from(fs.props.sector_size))
                .expect("this should fit into a usize");

        FATEntryProps {
            fat_sector,
            sector_offset,
        }
    }
}

/// the BPB_NumFATs field is 1 byte wide (u8)
pub(crate) type FATOffset = u8;

/// Properties about the position of a sector within the FAT
struct FATSectorProps {
    /// the sector belongs to this FAT copy
    #[allow(unused)]
    fat_offset: FATOffset,
    /// the sector is that many away from the start of the FAT copy
    sector_offset: SectorIndex,
}

impl FATSectorProps {
    /// Returns [`None`] if this sector doesn't belong to a FAT table
    pub fn new<S>(sector: SectorIndex, fs: &FileSystem<S>) -> Option<Self>
    where
        S: Read + Seek,
    {
        if !fs.sector_belongs_to_FAT(sector) {
            return None;
        }

        let sector_offset_from_first_fat = sector - SectorIndex::from(fs.props.first_fat_sector);
        let fat_offset =
            FATOffset::try_from(sector_offset_from_first_fat / fs.props.fat_sector_size)
                .expect("this should fit in a u89");
        let sector_offset = sector_offset_from_first_fat % fs.props.fat_sector_size;

        Some(FATSectorProps {
            fat_offset,
            sector_offset,
        })
    }

    #[allow(non_snake_case)]
    pub fn get_corresponding_FAT_sectors<S>(&self, fs: &FileSystem<S>) -> Box<[SectorIndex]>
    where
        S: Read + Seek,
    {
        let mut vec = Vec::with_capacity(fs.props.fat_table_count.into());

        for i in 0..fs.props.fat_table_count {
            vec.push(
                SectorIndex::from(fs.props.first_fat_sector)
                    + SectorCount::from(i) * fs.props.fat_sector_size
                    + self.sector_offset,
            )
        }

        vec.into_boxed_slice()
    }
}

#[derive(Debug, Clone)]
pub(crate) struct DirInfo {
    pub(crate) path: PathBuf,
    pub(crate) chain_start: EntryLocationUnit,
    /// Indicates the [`EntryLocation`] of the last known allocated or removed [`DirEntry`]
    ///
    /// [`None`] if it is not known
    pub(crate) chain_end: Option<EntryLocation>,
}

impl DirInfo {
    pub(crate) fn at_root_dir(boot_record: &BootRecord) -> Self {
        DirInfo {
            // this is basically the root directory
            path: PathBuf::from(path_consts::SEPARATOR_STR),
            chain_start: match boot_record {
                BootRecord::Fat(boot_record_fat) => match &boot_record_fat.ebr {
                    // it doesn't really matter what value we put in here, since we won't be using it
                    Ebr::FAT12_16(_ebr_fat12_16) => EntryLocationUnit::RootDirSector(0),
                    Ebr::FAT32(ebr_fat32, _) => {
                        EntryLocationUnit::DataCluster(ebr_fat32.root_cluster)
                    }
                },
                BootRecord::ExFAT(_boot_record_exfat) => todo!(),
            },
            chain_end: None,
        }
    }
}

impl<S> iter::FusedIterator for ReadDir<'_, S> where S: Read + Seek {}

pub(crate) trait OffsetConversions {
    fn sector_size(&self) -> u16;
    fn cluster_size(&self) -> u32;
    fn first_data_sector(&self) -> SectorIndex;

    #[inline]
    fn cluster_to_sector(&self, cluster: ClusterIndex) -> SectorIndex {
        cluster * ClusterIndex::from(self.sectors_per_cluster())
    }

    #[inline]
    fn sectors_per_cluster(&self) -> u8 {
        (self.cluster_size() / u32::from(self.sector_size()))
            .try_into()
            .expect("the SecPerClus field is 1 byte long (u8)")
    }

    #[inline]
    fn sector_to_partition_offset(&self, sector: SectorIndex) -> u64 {
        u64::from(sector) * u64::from(self.sector_size())
    }

    #[inline]
    fn data_cluster_to_partition_sector(&self, cluster: ClusterIndex) -> SectorIndex {
        self.cluster_to_sector(cluster - RESERVED_FAT_ENTRIES) + self.first_data_sector()
    }

    #[inline]
    fn partition_sector_to_data_cluster(&self, sector: SectorIndex) -> ClusterIndex {
        (sector - self.first_data_sector()) / ClusterIndex::from(self.sectors_per_cluster())
            + RESERVED_FAT_ENTRIES
    }
}

impl<S> OffsetConversions for FileSystem<S>
where
    S: Read + Seek,
{
    #[inline]
    fn sector_size(&self) -> u16 {
        self.props.sector_size
    }

    #[inline]
    fn cluster_size(&self) -> u32 {
        self.props.cluster_size
    }

    #[inline]
    fn first_data_sector(&self) -> SectorIndex {
        self.props.first_data_sector
    }

    #[inline]
    fn sectors_per_cluster(&self) -> u8 {
        self.props.sec_per_clus
    }
}

/// Some generic properties common across all FAT versions, like a sector's size, are cached here
#[derive(Debug)]
pub(crate) struct FSProperties {
    pub(crate) sector_size: u16,
    pub(crate) cluster_size: u32,
    pub(crate) sec_per_clus: u8,
    pub(crate) total_sectors: SectorCount,
    pub(crate) total_clusters: ClusterCount,
    /// sector offset of the FAT
    pub(crate) fat_table_count: u8,
    pub(crate) fat_sector_size: u32,
    pub(crate) first_fat_sector: u16,
    pub(crate) first_root_dir_sector: SectorIndex,
    pub(crate) first_data_sector: SectorIndex,
}

impl From<&BootRecord> for FSProperties {
    fn from(value: &BootRecord) -> Self {
        let sector_size = match value {
            BootRecord::Fat(boot_record_fat) => boot_record_fat.bpb.bytes_per_sector,
            BootRecord::ExFAT(boot_record_exfat) => 1 << boot_record_exfat.sector_shift,
        };
        let cluster_size = match value {
            BootRecord::Fat(boot_record_fat) => {
                u32::from(boot_record_fat.bpb.sectors_per_cluster) * u32::from(sector_size)
            }
            BootRecord::ExFAT(boot_record_exfat) => {
                1 << (boot_record_exfat.sector_shift + boot_record_exfat.cluster_shift)
            }
        };
        let sec_per_clus = match value {
            BootRecord::Fat(boot_record_fat) => boot_record_fat.bpb.sectors_per_cluster,
            BootRecord::ExFAT(_boot_record_exfat) => todo!("ExFAT is not yet implemented"),
        };
        let total_sectors = match value {
            BootRecord::Fat(boot_record_fat) => boot_record_fat.total_sectors(),
            BootRecord::ExFAT(_boot_record_exfat) => todo!("ExFAT is not yet implemented"),
        };
        let total_clusters = match value {
            BootRecord::Fat(boot_record_fat) => boot_record_fat.total_clusters(),
            BootRecord::ExFAT(_boot_record_exfat) => todo!("ExFAT is not yet implemented"),
        };
        let fat_table_count = match value {
            BootRecord::Fat(boot_record_fat) => boot_record_fat.bpb.table_count,
            BootRecord::ExFAT(_boot_record_exfat) => todo!("ExFAT is not yet implemented"),
        };
        let fat_sector_size = match value {
            BootRecord::Fat(boot_record_fat) => boot_record_fat.fat_sector_size(),
            BootRecord::ExFAT(_boot_record_exfat) => todo!("ExFAT not yet implemented"),
        };
        let first_fat_sector = match value {
            BootRecord::Fat(boot_record_fat) => boot_record_fat.first_fat_sector(),
            BootRecord::ExFAT(_boot_record_exfat) => todo!("ExFAT not yet implemented"),
        };
        let first_root_dir_sector = match value {
            BootRecord::Fat(boot_record_fat) => boot_record_fat.first_root_dir_sector(),
            BootRecord::ExFAT(_boot_record_exfat) => todo!("ExFAT is not yet implemented"),
        };
        let first_data_sector = match value {
            BootRecord::Fat(boot_record_fat) => boot_record_fat.first_data_sector(),
            BootRecord::ExFAT(_boot_record_exfat) => todo!("ExFAT is not yet implemented"),
        };

        FSProperties {
            sector_size,
            cluster_size,
            sec_per_clus,
            fat_table_count,
            fat_sector_size,
            first_fat_sector,
            total_sectors,
            total_clusters,
            first_root_dir_sector,
            first_data_sector,
        }
    }
}

/// Filter (or not) things like hidden files/directories
/// for FileSystem operations
#[derive(Debug, Clone)]
pub(crate) struct FileFilter {
    show_hidden: bool,
    show_systen: bool,
}

impl FileFilter {
    pub(crate) fn filter(&self, item: &RawProperties) -> bool {
        let is_hidden = item.attributes.contains(RawAttributes::HIDDEN);
        let is_system = item.attributes.contains(RawAttributes::SYSTEM);
        let should_filter = !self.show_hidden && is_hidden || !self.show_systen && is_system;

        !should_filter
    }
}

#[allow(clippy::derivable_impls)]
impl Default for FileFilter {
    fn default() -> Self {
        // The FAT spec says to filter everything by default
        FileFilter {
            show_hidden: false,
            show_systen: false,
        }
    }
}

type SyncSectorBufferFn<S> = fn(&FileSystem<S>) -> Result<(), <S as ErrorType>::Error>;
type UnmountFn<S> = fn(&FileSystem<S>) -> FSResult<(), <S as ErrorType>::Error>;

/// An API to process a FAT filesystem
#[derive(Debug)]
pub struct FileSystem<S>
where
    S: Read + Seek,
{
    /// Any struct that implements the [`Read`], [`Seek`] and optionally [`Write`] traits
    storage: RefCell<S>,

    /// The length of this will be the sector size of the FS for all FAT types except FAT12, in that case, it will be double that value
    pub(crate) sector_buffer: RefCell<SectorBuffer>,
    fsinfo_modified: RefCell<bool>,

    pub(crate) dir_info: RefCell<DirInfo>,

    sync_f: RefCell<Option<SyncSectorBufferFn<S>>>,
    unmount_f: RefCell<Option<UnmountFn<S>>>,

    pub(crate) options: FSOptions,

    pub(crate) boot_record: RefCell<BootRecord>,
    // since `self.boot_record.fat_type()` calls like 5 nested functions, we keep this cached and expose it with a public getter function
    fat_type: FATType,
    pub(crate) props: FSProperties,
    // this doesn't mean that this is the first free cluster, it just means
    // that if we want to figure that out, we should start from this cluster
    first_free_cluster: RefCell<ClusterIndex>,

    pub(crate) filter: RefCell<FileFilter>,
}

/// Getter functions
impl<S> FileSystem<S>
where
    S: Read + Seek,
{
    /// What is the [`FATType`] of the filesystem
    pub fn fat_type(&self) -> FATType {
        self.fat_type
    }
}

/// Setter functions
impl<S> FileSystem<S>
where
    S: Read + Seek,
{
    /// Whether or not to list hidden files
    ///
    /// Off by default
    #[inline]
    pub fn show_hidden(&self, show: bool) {
        self.filter.borrow_mut().show_hidden = show;
    }

    /// Whether or not to list system files
    ///
    /// Off by default
    #[inline]
    pub fn show_system(&self, show: bool) {
        self.filter.borrow_mut().show_systen = show;
    }
}

/// Constructors for a [`FileSystem`]
impl<S> FileSystem<S>
where
    S: Read + Seek,
{
    /// Create a [`FileSystem`] from a storage object
    ///
    /// Fails if the storage is way too small to support a FAT filesystem.
    /// For most use cases, that shouldn't be an issue, you can just call [`.unwrap()`](Result::unwrap)
    pub fn new(mut storage: S, options: FSOptions) -> FSResult<Self, S::Error> {
        use utils::bincode::BINCODE_CONFIG;

        // Begin by reading the boot record
        // We don't know the sector size yet, so we just go with the biggest possible one for now
        let mut buffer = [0u8; MAX_SECTOR_SIZE];

        let bytes_read = storage.read(&mut buffer)?;
        let mut stored_sector = 0;

        if bytes_read < MIN_SECTOR_SIZE {
            return Err(FSError::InternalFSError(InternalFSError::StorageTooSmall));
        }

        let bpb: BpbFat = bincode::decode_from_slice(&buffer[..BPBFAT_SIZE], BINCODE_CONFIG)
            .map(|(v, _)| v)
            .map_err(utils::bincode::map_err_dec)?;

        let ebr = if bpb.table_size_16 == 0 {
            let ebr_fat32: EBRFAT32 = bincode::decode_from_slice(
                &buffer[BPBFAT_SIZE..BPBFAT_SIZE + EBR_SIZE],
                BINCODE_CONFIG,
            )
            .map(|(v, _)| v)
            .map_err(utils::bincode::map_err_dec)?;

            storage.seek(SeekFrom::Start(
                u64::from(ebr_fat32.fat_info) * u64::from(bpb.bytes_per_sector),
            ))?;
            stored_sector = ebr_fat32.fat_info.into();
            storage.read_exact(&mut buffer[..usize::from(bpb.bytes_per_sector)])?;
            let fsinfo: FSInfoFAT32 = bincode::decode_from_slice(
                &buffer[..usize::from(bpb.bytes_per_sector)],
                BINCODE_CONFIG,
            )
            .map(|(v, _)| v)
            .map_err(utils::bincode::map_err_dec)?;

            if !fsinfo.verify_signature() {
                log::error!("FAT32 FSInfo has invalid signature(s)");
                return Err(FSError::InternalFSError(InternalFSError::InvalidFSInfoSig));
            }

            Ebr::FAT32(ebr_fat32, fsinfo)
        } else {
            Ebr::FAT12_16(
                bincode::decode_from_slice(
                    &buffer[BPBFAT_SIZE..BPBFAT_SIZE + EBR_SIZE],
                    BINCODE_CONFIG,
                )
                .map(|(v, _)| v)
                .map_err(utils::bincode::map_err_dec)?,
            )
        };

        // TODO: see how we will handle this for exfat
        let boot_record = BootRecord::Fat(BootRecordFAT { bpb, ebr });

        // verify boot record signature
        let fat_type = boot_record.fat_type();

        if fat_type == FATType::ExFAT {
            log::error!("Filesystem is ExFAT, which is currently unsupported");
            return Err(FSError::UnsupportedFS);
        }

        log::info!("The FAT type of the filesystem is {fat_type:?}");

        match &boot_record {
            BootRecord::Fat(boot_record_fat) => {
                if boot_record_fat.verify_signature() {
                    log::error!("FAT boot record has invalid signature(s)");
                    return Err(FSError::InternalFSError(InternalFSError::InvalidBPBSig));
                }
            }
            BootRecord::ExFAT(_boot_record_exfat) => todo!("ExFAT not yet implemented"),
        };

        let props = FSProperties::from(&boot_record);

        let fs = Self {
            storage: storage.into(),
            sector_buffer: SectorBuffer::new(
                Box::from(&buffer[..usize::from(props.sector_size)]),
                stored_sector,
            )
            .into(),
            fsinfo_modified: false.into(),
            options,
            dir_info: DirInfo::at_root_dir(&boot_record).into(),
            sync_f: None.into(),
            unmount_f: None.into(),
            boot_record: boot_record.into(),
            fat_type,
            props,
            first_free_cluster: RESERVED_FAT_ENTRIES.into(),
            filter: FileFilter::default().into(),
        };

        if !fs.FAT_tables_are_identical()? {
            return Err(FSError::InternalFSError(
                InternalFSError::MismatchingFATTables,
            ));
        }

        Ok(fs)
    }
}

/// Internal [`Read`]-related low-level functions
impl<S> FileSystem<S>
where
    S: Read + Seek,
{
    pub(crate) fn process_current_dir<'a>(&'a self) -> ReadDirInt<'a, S> {
        ReadDirInt::new(self, &self.dir_info.borrow().chain_start)
    }

    /// Goes to the parent directory.
    ///
    /// If this is the root directory, it does nothing
    fn _go_to_parent_dir(&self) -> FSResult<(), S::Error> {
        if let Some(parent_path) = self.dir_info.borrow().path.parent() {
            let parent_pathbuf = parent_path.to_path_buf();

            let mut entries = self.process_current_dir();

            // the PARENT DIR entry is always second on a directory
            // other than the root directory
            let parent_entry = entries
                .nth(NONROOT_MIN_DIRENTRIES - 1)
                .transpose()?
                .filter(|entry| entry.is_dir && entry.name == path_consts::PARENT_DIR_STR)
                .ok_or(FSError::InternalFSError(
                    InternalFSError::MalformedEntryChain,
                ))?;

            self.dir_info.borrow_mut().path = parent_pathbuf;
            self.dir_info.borrow_mut().chain_start =
                EntryLocationUnit::DataCluster(parent_entry.data_cluster);
            self.dir_info.borrow_mut().chain_end = None;
        } else {
            self._go_to_root_directory();
        }

        Ok(())
    }

    /// Goes to the given child directory
    ///
    /// If it doesn't exist, the encapsulated [`Option`] will be `None`
    fn _go_to_child_dir(&self, name: &str) -> FSResult<(), S::Error> {
        let mut entries = self.process_current_dir();

        let child_entry = loop {
            let entry = entries.next().ok_or(FSError::NotFound)??;

            if entry.name == name {
                break entry;
            }
        };

        if !child_entry.is_dir {
            return Err(FSError::NotADirectory);
        }

        self.dir_info.borrow_mut().path.push(&child_entry.name);
        self.dir_info.borrow_mut().chain_start =
            EntryLocationUnit::DataCluster(child_entry.data_cluster);
        self.dir_info.borrow_mut().chain_end = None;

        Ok(())
    }

    fn _go_to_root_directory(&self) {
        *self.dir_info.borrow_mut() = DirInfo::at_root_dir(&self.boot_record.borrow());
    }

    // This is a helper function for `go_to_dir`
    fn _go_up_till_target<P>(&self, target: P) -> FSResult<(), S::Error>
    where
        P: AsRef<Path>,
    {
        let target = target.as_ref();

        while self.dir_info.borrow().path != target {
            self._go_to_parent_dir()?;
        }

        Ok(())
    }

    // This is a helper function for `go_to_dir`
    fn _go_down_till_target<P>(&self, target: P) -> FSResult<(), S::Error>
    where
        P: AsRef<Path>,
    {
        let target = target.as_ref();

        let common_path_prefix = find_common_path_prefix(&self.dir_info.borrow().path, target);
        let common_components = common_path_prefix
            .normalize()
            .components()
            .filter(keep_path_normals)
            .count();

        for dir_name in target
            .components()
            .filter(keep_path_normals)
            .skip(common_components)
        {
            self._go_to_child_dir(dir_name.as_str())?;
        }

        Ok(())
    }

    /// Make sure that the sector stored in the sector buffer is the same as
    /// the first sector of cached directory chain
    fn _go_to_cached_dir(&self) -> FSResult<(), S::Error> {
        let dir_chain = self.dir_info.borrow().chain_start;
        let target_sector = dir_chain.get_entry_sector(self);

        if target_sector != self.sector_buffer.borrow().stored_sector {
            self.load_nth_sector(target_sector)?;
        }

        Ok(())
    }

    // There are many ways this can be achieved. That's how we'll do it:
    // Firstly, we find the common path prefix of the `current_path` and the `target`
    // Then, we check whether it is faster to start from the root directory
    // and get down to the target or whether we should start from where we are
    // now, go up till we find the common prefix path and then go down to the `target`

    /// Navigates to the `target` [`Path`]
    pub(crate) fn go_to_dir<P>(&self, target: P) -> FSResult<(), S::Error>
    where
        P: AsRef<Path>,
    {
        let target = target.as_ref();

        if !target.is_valid() {
            return Err(FSError::MalformedPath);
        }

        if self.dir_info.borrow().path == target {
            // there's a chance that the current loaded sector doesn't belong
            // to the directory we have cached, so we must also navigate to the correct sector
            self._go_to_cached_dir()?;

            return Ok(());
        }

        let common_path_prefix = find_common_path_prefix(&self.dir_info.borrow().path, target);

        // Note: these are the distances to the common prefix, not the target path
        let distance_from_root = common_path_prefix.ancestors().count() - 1;
        let distance_from_current_path =
            (self.dir_info.borrow().path.ancestors().count() - 1) - distance_from_root;

        if distance_from_root <= distance_from_current_path {
            self._go_to_root_directory();

            self._go_down_till_target(target)?;
        } else {
            self._go_up_till_target(common_path_prefix)?;

            self._go_down_till_target(target)?;
        }

        Ok(())
    }

    /// Gets the next free cluster. Returns an IO [`Result`]
    /// If the [`Result`] returns [`Ok`] that contains a [`None`], the drive is full
    pub(crate) fn next_free_cluster(&self) -> Result<Option<ClusterIndex>, S::Error> {
        let start_cluster = match *self.boot_record.borrow() {
            BootRecord::Fat(ref boot_record_fat) => {
                let mut first_free_cluster = *self.first_free_cluster.borrow();

                if let Ebr::FAT32(_, fsinfo) = &boot_record_fat.ebr {
                    // a value of u32::MAX denotes unawareness of the first free cluster
                    // we also do a bit of range checking
                    // TODO: if this is unknown, figure it out and write it to the FSInfo structure
                    if fsinfo.first_free_cluster != ClusterIndex::MAX
                        && fsinfo.first_free_cluster <= self.props.total_sectors
                    {
                        first_free_cluster =
                            cmp::min(first_free_cluster, fsinfo.first_free_cluster);
                    }
                }

                first_free_cluster
            }
            BootRecord::ExFAT(_) => todo!("ExFAT not yet implemented"),
        };

        let mut current_cluster = start_cluster;

        while current_cluster < self.props.total_clusters {
            if self.read_nth_FAT_entry(current_cluster)? == FATEntry::Free {
                *self.first_free_cluster.borrow_mut() = current_cluster;

                match *self.boot_record.borrow_mut() {
                    BootRecord::Fat(ref mut boot_record_fat) => {
                        if let Ebr::FAT32(_, fsinfo) = &mut boot_record_fat.ebr {
                            fsinfo.first_free_cluster = current_cluster;
                            *self.fsinfo_modified.borrow_mut() = true;
                        }
                    }
                    BootRecord::ExFAT(_) => todo!("ExFAT not yet implemented"),
                }

                return Ok(Some(current_cluster));
            }
            current_cluster += 1;
        }

        *self.first_free_cluster.borrow_mut() = self.props.total_clusters - 1;
        Ok(None)
    }

    /// Get the next cluster in a cluster chain, otherwise return [`None`]
    pub(crate) fn get_next_cluster(
        &self,
        cluster: ClusterIndex,
    ) -> Result<Option<ClusterIndex>, S::Error> {
        Ok(match self.read_nth_FAT_entry(cluster)? {
            FATEntry::Allocated(next_cluster) => Some(next_cluster),
            // when a `ROFile` is created, `cluster_chain_is_healthy` is called, if it fails, that ROFile is dropped
            _ => None,
        })
    }

    #[allow(non_snake_case)]
    /// Check whether or not the all the FAT tables of the storage medium are identical to each other
    pub(crate) fn FAT_tables_are_identical(&self) -> Result<bool, S::Error> {
        // we could make it work, but we are only testing regular FAT filesystems (for now)
        assert_ne!(
            self.fat_type,
            FATType::ExFAT,
            "this function doesn't work with ExFAT"
        );

        /// How many bytes to probe at max for each FAT per iteration (must be a multiple of [`MAX_SECTOR_SIZE`])
        const MAX_PROBE_SIZE: u32 = 1 << 20;

        let fat_byte_size = match &*self.boot_record.borrow() {
            BootRecord::Fat(boot_record_fat) => boot_record_fat.fat_sector_size(),
            BootRecord::ExFAT(_) => unreachable!(),
        };

        for nth_iteration in 0..fat_byte_size.div_ceil(MAX_PROBE_SIZE) {
            let mut tables: Vec<Vec<u8>> = Vec::new();

            for i in 0..self.props.fat_table_count {
                let fat_start = u32::try_from(
                    self.sector_to_partition_offset(self.boot_record.borrow().nth_FAT_table_sector(i)),
                )
                .expect("there's no way the FAT is more that 4GBs away from the start of the storage medium");
                let current_offset = fat_start + nth_iteration * MAX_PROBE_SIZE;
                let bytes_left = fat_byte_size - nth_iteration * MAX_PROBE_SIZE;

                self.storage
                    .borrow_mut()
                    .seek(SeekFrom::Start(current_offset.into()))?;
                let mut buf = vec![
                    0_u8;
                    usize::try_from(cmp::min(MAX_PROBE_SIZE, bytes_left))
                        .unwrap_or(usize::MAX)
                ];
                self.storage
                    .borrow_mut()
                    .read_exact(buf.as_mut_slice())
                    .map_err(|e| match e {
                        ReadExactError::UnexpectedEof => {
                            panic!("Unexpected EOF while reading FAT table")
                        }
                        ReadExactError::Other(e) => e,
                    })?;
                tables.push(buf);
            }

            // we check each table with the first one (except the first one ofc)
            if !tables.iter().skip(1).all(|buf| buf == &tables[0]) {
                return Ok(false);
            }
        }

        Ok(true)
    }

    #[allow(non_snake_case)]
    pub(crate) fn sector_belongs_to_FAT(&self, sector: SectorIndex) -> bool {
        match &*self.boot_record.borrow() {
            BootRecord::Fat(boot_record_fat) => (boot_record_fat.first_fat_sector().into()
                ..boot_record_fat.first_root_dir_sector())
                .contains(&sector),
            BootRecord::ExFAT(_boot_record_exfat) => todo!("ExFAT not yet implemented"),
        }
    }

    /// Read the nth sector from the partition's beginning and store it in [`self.sector_buffer`](Self::sector_buffer)
    ///
    /// This function also returns an immutable reference to [`self.sector_buffer`](Self::sector_buffer)
    pub(crate) fn load_nth_sector(&self, n: SectorIndex) -> Result<Ref<'_, [u8]>, S::Error> {
        // FIXME: don't rely just on the filesystem for the device storage size
        if n >= self.props.total_sectors {
            panic!(concat!(
                "seeked past end of device medium. ",
                "This is most likely an internal error, please report it: ",
                "https://github.com/Oakchris1955/simple-fatfs/issues"
            ));
        }

        // nothing to do if the sector we wanna read is already cached
        let stored_sector = self.sector_buffer.borrow().stored_sector;
        if n != stored_sector {
            // let's sync the current sector first
            let sync_sector_option = *self.sync_f.borrow();
            if let Some(sync_sector_buffer) = sync_sector_option {
                log::debug!("Syncing sector {stored_sector}");

                sync_sector_buffer(self)?;

                // Now that we have synced the sector buffer, there's no reason
                // to sync it again if there have been no changes
                *self.sync_f.borrow_mut() = None;
            }
            self.storage
                .borrow_mut()
                .seek(SeekFrom::Start(self.sector_to_partition_offset(n)))?;
            self.storage
                .borrow_mut()
                .read_exact(&mut self.sector_buffer.borrow_mut())
                .map_err(|e| match e {
                    ReadExactError::UnexpectedEof => {
                        panic!("Unexpected EOF while reading sector {n}\n\
                        This is most likely an interal error, please report it: https://github.com/Oakchris1955/simple-fatfs/issues")
                    }
                    ReadExactError::Other(e) => e,
                })?;
            self.storage
                .borrow_mut()
                .seek(SeekFrom::Current(-i64::from(self.props.sector_size)))?;

            self.sector_buffer.borrow_mut().stored_sector = n;
        }

        Ok(Ref::map(self.sector_buffer.borrow(), |s| &**s))
    }

    #[allow(non_snake_case)]
    pub(crate) fn read_nth_FAT_entry(&self, n: FATEntryIndex) -> Result<FATEntry, S::Error> {
        // the size of an entry rounded up to bytes
        let entry_size = self.fat_type.entry_size();
        let entry_props = FATEntryProps::new(n, self);

        self.load_nth_sector(entry_props.fat_sector)?;

        let mut value_bytes = [0_u8; 4];
        let bytes_to_read: usize = cmp::min(
            entry_props.sector_offset + usize::from(entry_size),
            usize::from(self.sector_size()),
        ) - entry_props.sector_offset;
        value_bytes[..bytes_to_read].copy_from_slice(
            &self.sector_buffer.borrow_mut()
                [entry_props.sector_offset..entry_props.sector_offset + bytes_to_read],
        ); // this shouldn't panic

        // in FAT12, FAT entries may be split between two different sectors
        if self.fat_type == FATType::FAT12 && bytes_to_read < usize::from(entry_size) {
            self.load_nth_sector(entry_props.fat_sector + 1)?;

            value_bytes[bytes_to_read..usize::from(entry_size)].copy_from_slice(
                &self.sector_buffer.borrow_mut()[..(usize::from(entry_size) - bytes_to_read)],
            );
        };

        let mut value = FATEntryValue::from_le_bytes(value_bytes);
        match self.fat_type {
            // FAT12 entries are split between different bytes
            FATType::FAT12 => {
                if n & 1 != 0 {
                    value >>= 4
                } else {
                    value &= 0xFFF
                }
            }
            // ignore the high 4 bits if this is FAT32
            FATType::FAT32 => value &= 0x0FFFFFFF,
            _ => (),
        }

        /*
        // pad unused bytes with 1s
        let padding: u32 = u32::MAX.to_be() << self.fat_type.bits_per_entry();
        value |= padding.to_le();
        */

        // TODO: perhaps byte padding can replace some redundant code here?
        Ok(match self.fat_type {
            FATType::FAT12 => match value {
                0x000 => FATEntry::Free,
                0xFF7 => FATEntry::Bad,
                #[allow(clippy::manual_range_patterns)]
                0xFF8..=0xFFE | 0xFFF => FATEntry::Eof,
                _ => {
                    if (0x002..(self.props.total_clusters + 1)).contains(&value) {
                        FATEntry::Allocated(value)
                    } else {
                        FATEntry::Reserved
                    }
                }
            },
            FATType::FAT16 => match value {
                0x0000 => FATEntry::Free,
                0xFFF7 => FATEntry::Bad,
                #[allow(clippy::manual_range_patterns)]
                0xFFF8..=0xFFFE | 0xFFFF => FATEntry::Eof,
                _ => {
                    if (0x0002..(self.props.total_clusters + 1)).contains(&value) {
                        FATEntry::Allocated(value)
                    } else {
                        FATEntry::Reserved
                    }
                }
            },
            FATType::FAT32 => match value {
                0x00000000 => FATEntry::Free,
                0x0FFFFFF7 => FATEntry::Bad,
                #[allow(clippy::manual_range_patterns)]
                0x0FFFFFF8..=0xFFFFFFE | 0x0FFFFFFF => FATEntry::Eof,
                _ => {
                    if (0x00000002..(self.props.total_clusters + 1)).contains(&value) {
                        FATEntry::Allocated(value)
                    } else {
                        FATEntry::Reserved
                    }
                }
            },
            FATType::ExFAT => todo!("ExFAT not yet implemented"),
        })
    }
}

/// Internal [`Write`]-related low-level functions
impl<S> FileSystem<S>
where
    S: Read + Write + Seek,
{
    #[allow(non_snake_case)]
    pub(crate) fn write_nth_FAT_entry(
        &self,
        n: FATEntryIndex,
        entry: FATEntry,
    ) -> Result<(), S::Error> {
        // the size of an entry rounded up to bytes
        let entry_size = self.fat_type.entry_size();
        let entry_props = FATEntryProps::new(n, self);

        let mask = utils::bits::setbits_u32_lo(self.fat_type.bits_per_entry());
        let mut value: FATEntryValue = FATEntryValue::from(entry.clone()) & mask;

        if self.fat_type == FATType::FAT32 {
            // in FAT32, the high 4 bits are unused
            value &= 0x0FFFFFFF;
        }

        match self.fat_type {
            FATType::FAT12 => {
                let should_shift = n & 1 != 0;
                if should_shift {
                    // FAT12 entries are split between different bytes
                    value <<= 4;
                }

                self.load_nth_sector(entry_props.fat_sector)?;

                let value_bytes = value.to_le_bytes();

                let mut first_byte = value_bytes[0];

                if should_shift {
                    let mut old_byte = self.sector_buffer.borrow()[entry_props.sector_offset];
                    // ignore the high 4 bytes of the old entry
                    old_byte &= 0x0F;
                    // OR it with the new value
                    first_byte |= old_byte;
                }

                self.sector_buffer.borrow_mut()[entry_props.sector_offset] = first_byte; // this shouldn't panic
                self.set_modified();

                let bytes_left_on_sector: usize = cmp::min(
                    usize::from(entry_size),
                    usize::from(self.sector_size()) - entry_props.sector_offset,
                );

                if bytes_left_on_sector < entry_size.into() {
                    // looks like this FAT12 entry spans multiple sectors, we must also update the other one
                    self.load_nth_sector(entry_props.fat_sector + 1)?;
                }

                let mut second_byte = value_bytes[1];
                let second_byte_index =
                    (entry_props.sector_offset + 1) % usize::from(self.sector_size());
                if !should_shift {
                    let mut old_byte = self.sector_buffer.borrow()[second_byte_index];
                    // ignore the low 4 bytes of the old entry
                    old_byte &= 0xF0;
                    // OR it with the new value
                    second_byte |= old_byte;
                }

                self.sector_buffer.borrow_mut()[second_byte_index] = second_byte; // this shouldn't panic
                self.set_modified();
            }
            FATType::FAT16 | FATType::FAT32 => {
                self.load_nth_sector(entry_props.fat_sector)?;

                let value_bytes = value.to_le_bytes();

                self.sector_buffer.borrow_mut()[entry_props.sector_offset
                    ..entry_props.sector_offset + usize::from(entry_size)]
                    .copy_from_slice(&value_bytes[..usize::from(entry_size)]); // this shouldn't panic
                self.set_modified();
            }
            FATType::ExFAT => todo!("ExFAT not yet implemented"),
        };

        if entry == FATEntry::Free && n < *self.first_free_cluster.borrow() {
            *self.first_free_cluster.borrow_mut() = n;
        }

        // lastly, update the FSInfoFAT32 structure is it is available
        if let BootRecord::Fat(boot_record_fat) = &mut *self.boot_record.borrow_mut() {
            if let Ebr::FAT32(_, fsinfo) = &mut boot_record_fat.ebr {
                match entry {
                    FATEntry::Free => {
                        fsinfo.free_cluster_count += 1;
                        if n < fsinfo.first_free_cluster {
                            fsinfo.first_free_cluster = n;
                        }
                    }
                    _ => fsinfo.free_cluster_count -= 1,
                };
                *self.fsinfo_modified.borrow_mut() = true;
            }
        }

        Ok(())
    }

    /// Allocate room for at least `n` contiguous [`FATDirEntries`](FATDirEntry)
    /// in the current directory entry chain
    ///
    /// This may or may not allocate new clusters.
    pub(crate) fn allocate_nth_entries(
        &self,
        n: num::NonZero<EntryCount>,
    ) -> FSResult<EntryLocation, S::Error> {
        // navigate to the cached directory, if we aren't already there
        self._go_to_cached_dir()?;

        // we may not even need to allocate new entries.
        // let's check if there is a chain of unused entries big enough to be used
        let mut first_entry = self.dir_info.borrow().chain_end.unwrap_or_else(|| {
            let stored_sector = self.sector_buffer.borrow().stored_sector;
            EntryLocation::from_partition_sector(stored_sector, self)
        });

        let mut last_entry = first_entry;

        let mut chain_len = 0;
        let mut entry_count: EntryCount = 0;

        loop {
            let entry_status = last_entry.entry_status(self)?;
            entry_count += 1;
            match entry_status {
                EntryStatus::Unused | EntryStatus::LastUnused => chain_len += 1,
                EntryStatus::Used => chain_len = 0,
            }

            if chain_len >= n.get() {
                return Ok(first_entry);
            }

            if entry_status == EntryStatus::LastUnused {
                break;
            }

            // we also break if we have reached the end of the cluster chain
            // or else the MalformedEntryChain below will kick in
            if last_entry.unit.get_next_unit(self)?.is_none()
                && last_entry.index + 1 >= last_entry.unit.get_max_offset(self)
            {
                break;
            }

            // what if for whatever reason the data types changes?
            #[allow(clippy::absurd_extreme_comparisons)]
            if entry_count + n.get() >= DIRENTRY_LIMIT {
                // defragment the cluster chain just in case
                // this frees up any space for entries
                let new_entry_count = self.defragment_entry_chain()?;

                if new_entry_count + n.get() >= DIRENTRY_LIMIT {
                    return Err(FSError::DirEntryLimitReached);
                }

                // we have enough entries now, this call should make us
                // enter an infinite recursion
                return self.allocate_nth_entries(n);
            }

            last_entry = last_entry
                .next_entry(self)?
                .ok_or(FSError::InternalFSError(
                    InternalFSError::MalformedEntryChain,
                ))?;

            if entry_status == EntryStatus::Used {
                first_entry = last_entry;
            }
        }

        // let's set the last-known dir entry
        self.dir_info.borrow_mut().chain_end = Some(last_entry);

        // we have broken out of the loop, that means we reached the end of the chain
        // of the already-allocated entries
        match last_entry.unit {
            EntryLocationUnit::RootDirSector(_) => {
                let remaining_sectors: SectorCount = match &*self.boot_record.borrow() {
                    BootRecord::Fat(boot_record_fat) => {
                        boot_record_fat.first_root_dir_sector()
                            + SectorCount::from(boot_record_fat.root_dir_sectors())
                            - last_entry.unit.get_entry_sector(self)
                    }
                    BootRecord::ExFAT(_boot_record_exfat) => todo!("ExFAT not yet implemented"),
                };

                let entries_per_sector = self.props.sector_size
                    / u16::try_from(DIRENTRY_SIZE).expect("32 can fit in a u16");
                let remaining_entries = EntryCount::try_from(
                    remaining_sectors * SectorCount::from(entries_per_sector)
                        + (SectorCount::from(entries_per_sector)
                            - SectorCount::from(last_entry.index)
                            + 1),
                )
                .unwrap();

                if remaining_entries < n.get() - chain_len {
                    Err(FSError::RootDirectoryFull)
                } else {
                    Ok(first_entry)
                }
            }
            EntryLocationUnit::DataCluster(cluster) => {
                // first, we check how many clusters need to be allocated (if any)
                let entries_per_cluster = u16::try_from(
                    self.props.cluster_size
                        / u32::try_from(DIRENTRY_SIZE).expect("32 can fit into u32"),
                )
                .expect("a cluster can have a max of ~16k entries");

                let entries_left = n.get() - chain_len;
                let free_entries_on_current_cluster = entries_per_cluster - (last_entry.index + 1);

                // if we do in fact have to allocate some clusters, we allocate them
                if free_entries_on_current_cluster < entries_left {
                    let clusters_to_allocate = (entries_left - free_entries_on_current_cluster)
                        .div_ceil(entries_per_cluster);

                    let first_cluster = self.allocate_clusters(
                        num::NonZero::new(clusters_to_allocate.into())
                            .expect("this should be at least 1"),
                        Some(cluster),
                    )?;

                    // the entry chain begins in the newly allocated clusters
                    if chain_len == 0 {
                        first_entry.unit = EntryLocationUnit::DataCluster(first_cluster);
                        first_entry.index = 0;
                    }

                    // before we return, we should zero those sectors according to the FAT spec
                    for cluster in
                        first_cluster..(first_cluster + ClusterCount::from(clusters_to_allocate))
                    {
                        let first_sector = self.data_cluster_to_partition_sector(cluster);

                        for sector in first_sector
                            ..(first_sector + SectorCount::from(self.sectors_per_cluster()))
                        {
                            self.load_nth_sector(sector)?;
                            self.sector_buffer.borrow_mut().fill(0);
                            self.set_modified();
                        }
                    }
                }

                Ok(first_entry)
            }
        }
    }

    /// Creates a new cluster chain with the `.` and `..` entries present,
    // The datetime parameter is there so that we fully comply with the FAT32 spec:
    // ". All date and time fields must be set to the same value as that for
    // the containing directory"
    pub(crate) fn create_entry_chain(
        &self,
        parent: EntryLocationUnit,
        datetime: PrimitiveDateTime,
    ) -> FSResult<u32, S::Error> {
        // we need to allocate a cluster
        let dir_cluster = self.allocate_clusters(num::NonZero::new(1).unwrap(), None)?;

        let entries = Box::from([
            MinProperties {
                name: Box::from(typed_path::constants::windows::CURRENT_DIR_STR),
                sfn: CURRENT_DIR_SFN,
                // this needs to be set when creating a file
                attributes: RawAttributes::empty() | RawAttributes::DIRECTORY,
                created: Some(datetime),
                modified: datetime,
                accessed: Some(datetime.date()),
                file_size: 0,
                data_cluster: dir_cluster,
            },
            MinProperties {
                name: Box::from(typed_path::constants::windows::PARENT_DIR_STR),
                sfn: PARENT_DIR_SFN,
                // this needs to be set when creating a file
                attributes: RawAttributes::empty() | RawAttributes::DIRECTORY,
                created: Some(datetime),
                modified: datetime,
                accessed: Some(datetime.date()),
                file_size: 0,
                data_cluster: match parent {
                    EntryLocationUnit::DataCluster(cluster) => cluster,
                    EntryLocationUnit::RootDirSector(_) => 0,
                },
            },
        ]);

        // this composer will ALWAYS generate 2 entries
        let entries_iter = EntryComposer::new(entries, &self.options.codepage);

        self.load_nth_sector(self.data_cluster_to_partition_sector(dir_cluster))?;

        // we zero the current sector
        self.sector_buffer.borrow_mut().fill(0);

        let mut entry_location = EntryLocation {
            unit: EntryLocationUnit::DataCluster(dir_cluster),
            index: 0,
        };

        for (i, bytes) in entries_iter.enumerate() {
            entry_location.set_bytes(self, bytes)?;

            if i < NONROOT_MIN_DIRENTRIES {
                entry_location = entry_location
                    .next_entry(self)?
                    .expect("this will only be called once");
            }
        }

        self.set_modified();

        // we also zero everything else in the cluster
        let stored_sector = self.sector_buffer.borrow().stored_sector;
        for sector in
            (stored_sector + 1)..(stored_sector + SectorCount::from(self.sectors_per_cluster()))
        {
            self.load_nth_sector(sector)?;
            self.sector_buffer.borrow_mut().fill(0);
            self.set_modified();
        }

        Ok(dir_cluster)
    }

    /// Insert the provided `entries` to the cluster chain of the current cached directory
    ///
    /// Returns the corresponding [`DirEntryChain`]
    ///
    /// Panics if the `entries` array is empty
    pub(crate) fn insert_to_entry_chain(
        &self,
        entries: Box<[MinProperties]>,
    ) -> FSResult<DirEntryChain, S::Error> {
        let mut entries_needed = 0;

        self._go_to_cached_dir()?;

        for entry in &entries {
            entries_needed += calc_entries_needed(&*entry.name, &self.options.codepage).get();
        }

        let first_entry = self.allocate_nth_entries(
            num::NonZero::new(entries_needed).expect("The entries array shouldn't be empty"),
        )?;

        let mut entries_iter = EntryComposer::new(entries, &self.options.codepage);

        let mut current_entry = first_entry;
        let mut entry_bytes = entries_iter
            .next()
            .expect("this iterator is guaranteed to return at least once");

        loop {
            current_entry.set_bytes(self, entry_bytes)?;

            match entries_iter.next() {
                Some(bytes) => entry_bytes = bytes,
                None => break,
            };

            current_entry = current_entry
                .next_entry(self)?
                .expect("This entry chain should be valid, we just generated it");
        }

        Ok(DirEntryChain {
            len: entries_needed,
            location: first_entry,
        })
    }

    /// Defragment the entry chain of the current directory
    ///
    /// Returns a [`FSResult`] containing the new number of entries
    pub(crate) fn defragment_entry_chain(&self) -> FSResult<EntryCount, S::Error> {
        let mut current_entry_loc = EntryLocation {
            unit: self.dir_info.borrow().chain_start,
            index: 0,
        };
        let mut new_chain_end = current_entry_loc;
        let mut entry_count: EntryCount = 0;

        loop {
            match current_entry_loc.entry_status(self)? {
                EntryStatus::Used => {
                    // no reason to copy the bytes if both locations are the same
                    if current_entry_loc != new_chain_end {
                        // copy the bytes where they belong
                        let bytes = current_entry_loc.get_bytes(self)?;

                        new_chain_end.set_bytes(self, bytes)?;

                        // what if for whatever reason the data types changes?
                        #[allow(clippy::absurd_extreme_comparisons)]
                        if entry_count >= DIRENTRY_LIMIT {
                            break;
                        }

                        // don't forget to free the entry
                        current_entry_loc.free_entry(self, false)?;
                    }

                    entry_count += 1;

                    // advance the new entry chain
                    new_chain_end = new_chain_end
                        .next_entry(self)?
                        .expect("we just pushed an entry to this chain")
                }
                EntryStatus::LastUnused => break,
                _ => (),
            }

            current_entry_loc = match current_entry_loc.next_entry(self)? {
                Some(entry) => entry,
                None => break,
            }
        }

        // we should also probably mark the entry after the last used one as last and unused
        new_chain_end.free_entry(self, true)?;

        self.dir_info.borrow_mut().chain_end = Some(new_chain_end);

        Ok(entry_count)
    }

    /// Mark the individual entries of a contiguous FAT entry chain as unused
    ///
    /// Note: No validation is done to check whether or not the chain is valid
    pub(crate) fn remove_entry_chain(&self, chain: &DirEntryChain) -> Result<(), S::Error> {
        let mut entries_freed = 0;
        let mut current_entry = chain.location;

        loop {
            current_entry.free_entry(self, false)?;

            entries_freed += 1;

            if entries_freed >= chain.len {
                break;
            }

            current_entry = match current_entry.next_entry(self)? {
                Some(current_entry) => current_entry,
                None => unreachable!(
                    concat!("It is guaranteed that at least as many entries ",
                    "as there are in chain exist, since we counted them when initializing the struct")
                ),
            };
        }

        Ok(())
    }

    /// Frees all the cluster in a cluster chain starting with `first_cluster`
    pub(crate) fn free_cluster_chain(&self, first_cluster: u32) -> Result<(), S::Error> {
        let mut current_cluster = first_cluster;

        loop {
            let next_cluster_option = self.get_next_cluster(current_cluster)?;

            // free the current cluster
            self.write_nth_FAT_entry(current_cluster, FATEntry::Free)?;

            // proceed to the next one, otherwise break
            match next_cluster_option {
                Some(next_cluster) => current_cluster = next_cluster,
                None => break,
            }
        }

        Ok(())
    }

    /// Allocate `n` clusters and return the index of the first one allocated
    ///
    /// Also has a second [`Option`] argument that if not [`None`] indicates
    /// that this cluster should point to the newly allocated cluster chain
    pub(crate) fn allocate_clusters(
        &self,
        n: num::NonZero<ClusterCount>,
        first_cluster: Option<ClusterIndex>,
    ) -> FSResult<ClusterIndex, S::Error> {
        let mut last_cluster_in_chain = first_cluster;
        let mut first_allocated_cluster = None;

        for i in 0..n.into() {
            match self.next_free_cluster()? {
                Some(next_free_cluster) => {
                    // FIXME: in FAT12 filesystems, this can cause a sector
                    // to be updated up to 4 times for seeminly no reason
                    // Similar behavour is observed in FAT16/32, with 2 sync operations
                    // THis number should be halved for both cases

                    if i == 0 {
                        first_allocated_cluster = Some(next_free_cluster);
                    }

                    // we set the last allocated cluster to point to the next free one
                    if let Some(last_cluster_in_chain) = last_cluster_in_chain {
                        self.write_nth_FAT_entry(
                            last_cluster_in_chain,
                            FATEntry::Allocated(next_free_cluster),
                        )?;
                    }
                    // we also set the next free cluster to be EOF
                    self.write_nth_FAT_entry(next_free_cluster, FATEntry::Eof)?;
                    if let Some(last_cluster_in_chain) = last_cluster_in_chain {
                        log::trace!(
                            "cluster {last_cluster_in_chain} now points to {next_free_cluster}"
                        );
                    }
                    // now the next free cluster i the last allocated one
                    last_cluster_in_chain = Some(next_free_cluster);
                }
                None => {
                    log::error!("storage medium full while attempting to allocate more clusters");
                    return Err(FSError::StorageFull);
                }
            }
        }

        Ok(first_allocated_cluster.expect("This should have Some value by now"))
    }

    /// Syncs `self.sector_buffer` back to the storage
    fn _sync_current_sector(&self) -> Result<(), S::Error> {
        self.storage
            .borrow_mut()
            .write_all(&self.sector_buffer.borrow())?;
        self.storage
            .borrow_mut()
            .seek(SeekFrom::Current(-i64::from(self.props.sector_size)))?;

        Ok(())
    }

    /// Syncs a FAT sector to ALL OTHER FAT COPIES on the device medium
    #[allow(non_snake_case)]
    fn _sync_FAT_sector(&self, fat_sector_props: &FATSectorProps) -> Result<(), S::Error> {
        let current_offset = self.storage.borrow_mut().stream_position()?;

        for sector in fat_sector_props.get_corresponding_FAT_sectors(self) {
            self.storage.borrow_mut().seek(SeekFrom::Start(u64::from(
                sector * u32::from(self.props.sector_size),
            )))?;
            self.storage
                .borrow_mut()
                .write_all(&self.sector_buffer.borrow())?;
        }

        self.storage
            .borrow_mut()
            .seek(SeekFrom::Start(current_offset))?;

        Ok(())
    }

    /// Marks that a modification has been made to the storage medium, setting the `sync_f` and `unmount_f` fields
    pub(crate) fn set_modified(&self) {
        *self.sync_f.borrow_mut() = Some(Self::sync_sector_buffer);
        *self.unmount_f.borrow_mut() = Some(Self::unmount);
    }

    pub(crate) fn sync_sector_buffer(&self) -> Result<(), S::Error> {
        // If this is called, we assume the sector buffer has been modified
        let stored_sector = self.sector_buffer.borrow().stored_sector;
        if let Some(fat_sector_props) = FATSectorProps::new(stored_sector, self) {
            log::trace!("syncing FAT sector {}", fat_sector_props.sector_offset,);
            match &*self.boot_record.borrow() {
                BootRecord::Fat(boot_record_fat) => match &boot_record_fat.ebr {
                    Ebr::FAT12_16(_) => {
                        self._sync_FAT_sector(&fat_sector_props)?;
                    }
                    Ebr::FAT32(ebr_fat32, _) => {
                        if ebr_fat32.extended_flags.mirroring_disabled() {
                            self._sync_current_sector()?;
                        } else {
                            self._sync_FAT_sector(&fat_sector_props)?;
                        }
                    }
                },
                BootRecord::ExFAT(_boot_record_exfat) => todo!("ExFAT not yet implemented"),
            }
        } else {
            log::trace!(
                "syncing sector {}",
                self.sector_buffer.borrow().stored_sector
            );
            self._sync_current_sector()?;
        }

        // we don't want to call this again for no reason
        *self.sync_f.borrow_mut() = None;

        Ok(())
    }

    /// Sync the [`FSInfoFAT32`] back to the storage medium
    /// if this is FAT32
    pub(crate) fn sync_fsinfo(&self) -> FSResult<(), S::Error> {
        use utils::bincode::BINCODE_CONFIG;

        if *self.fsinfo_modified.borrow() {
            if let BootRecord::Fat(boot_record_fat) = &*self.boot_record.borrow() {
                if let Ebr::FAT32(ebr_fat32, fsinfo) = &boot_record_fat.ebr {
                    self.load_nth_sector(ebr_fat32.fat_info.into())?;

                    bincode::encode_into_slice(
                        fsinfo,
                        &mut self.sector_buffer.borrow_mut()[..FSINFO_SIZE],
                        BINCODE_CONFIG,
                    )
                    .map_err(utils::bincode::map_err_enc)?;
                }
            }

            *self.fsinfo_modified.borrow_mut() = false;
        }

        Ok(())
    }

    /// Like [`Self::get_rw_file`], but will ignore the read-only flag (if it is present)
    ///
    /// This is a private function for obvious reasons
    fn get_rw_file_unchecked<P: AsRef<Path>>(&self, path: P) -> FSResult<RWFile<'_, S>, S::Error> {
        let ro_file = self.get_ro_file(path)?;

        Ok(ro_file.into())
    }
}

/// Public [`Read`]-related functions
impl<S> FileSystem<S>
where
    S: Read + Seek,
{
    /// Read all the entries of a directory ([`Path`]) into [`ReadDir`]
    ///
    /// Fails if `path` doesn't represent a directory, or if that directory doesn't exist
    pub fn read_dir<P: AsRef<Path>>(&self, path: P) -> FSResult<ReadDir<'_, S>, S::Error> {
        // normalize the given path
        let path = path.as_ref();

        if !path.is_valid() {
            return Err(FSError::MalformedPath);
        }

        let path = path.normalize();

        self.go_to_dir(&path)?;

        Ok(ReadDir::new(
            self,
            &self.dir_info.borrow().chain_start,
            &self.dir_info.borrow().path,
        ))
    }

    /// Get a corresponding [`ROFile`] object from a [`Path`]
    ///
    /// Fails if `path` doesn't represent a file, or if that file doesn't exist
    pub fn get_ro_file<P: AsRef<Path>>(&self, path: P) -> FSResult<ROFile<'_, S>, S::Error> {
        let path = path.as_ref();

        if !path.is_valid() {
            return Err(FSError::MalformedPath);
        }

        if let Some(file_name) = path.file_name() {
            let parent_dir = self.read_dir(
                path.parent()
                    .expect("we aren't in the root directory, this shouldn't panic"),
            )?;

            // don't ask, I don't know either (https://doc.rust-lang.org/std/boxed/index.html#editions)
            let mut entry = None;

            for dir_entry in parent_dir {
                let dir_entry = dir_entry?;

                if dir_entry
                    .path()
                    .file_name()
                    .is_some_and(|entry_name| entry_name == file_name)
                {
                    entry = Some(dir_entry.entry);

                    break;
                }
            }

            match entry {
                Some(entry) => {
                    let mut file = ROFile::from_props(
                        FileProps {
                            offset: 0,
                            current_cluster: entry.data_cluster,
                            entry,
                        },
                        self,
                    );

                    if file.cluster_chain_is_healthy()? {
                        Ok(file)
                    } else {
                        log::error!("The cluster chain of a file is malformed");
                        Err(FSError::InternalFSError(
                            InternalFSError::MalformedClusterChain,
                        ))
                    }
                }
                None => {
                    log::error!("ROFile {path} not found");

                    Err(FSError::NotFound)
                }
            }
        } else {
            log::error!("Is a directory (not a file)");
            Err(FSError::IsADirectory)
        }
    }
}

/// [`Write`]-related functions
impl<S> FileSystem<S>
where
    S: Read + Write + Seek,
{
    /// Create a new [`RWFile`] and return its handle
    #[inline]
    pub fn create_file<P: AsRef<Path>>(&self, path: P) -> FSResult<RWFile<'_, S>, S::Error> {
        let path = path.as_ref();

        if !path.is_valid() {
            return Err(FSError::MalformedPath);
        }

        let target = path.normalize();

        let parent_dir = match target.parent() {
            Some(parent) => parent,
            // technically, the path provided is a directory, the root directory
            None => return Err(FSError::IsADirectory),
        };

        let file_name = target
            .file_name()
            .expect("the path is normalized and it isn't the root directory either");

        self.go_to_dir(parent_dir)?;

        // check if there is already a file or directory with the same name
        for entry in self.process_current_dir() {
            let entry = entry?;

            if entry.name == file_name {
                return Err(FSError::AlreadyExists);
            }
        }

        let file_cluster = self.allocate_clusters(num::NonZero::new(1).expect("1 != 0"), None)?;

        let sfn = utils::string::gen_sfn(file_name, self, parent_dir)?;

        let now = self.options.clock.now();

        // we got everything to create our first (and only) RawProperties struct
        let raw_properties = MinProperties {
            name: file_name.into(),
            sfn,
            // this needs to be set when creating a file
            attributes: RawAttributes::empty() | RawAttributes::ARCHIVE,
            created: Some(now),
            modified: now,
            accessed: Some(now.date()),
            file_size: 0,
            data_cluster: file_cluster,
        };

        let entries = [raw_properties.clone()];
        let chain = self.insert_to_entry_chain(Box::new(entries))?;

        Ok(RWFile::from_props(
            FileProps {
                current_cluster: raw_properties.data_cluster,
                entry: Properties::from_raw(
                    RawProperties::from_chain(raw_properties, chain),
                    path.into(),
                    self.options.codepage,
                ),
                offset: 0,
            },
            self,
        ))
    }

    /// Create a new directory
    #[inline]
    pub fn create_dir<P: AsRef<Path>>(&self, path: P) -> FSResult<(), S::Error> {
        let path = path.as_ref();

        if !path.is_valid() {
            return Err(FSError::MalformedPath);
        }

        let target = path.normalize();

        let parent_dir = match target.parent() {
            Some(parent) => parent,
            // the path provided is the root directory, which already exists
            None => return Err(FSError::AlreadyExists),
        };

        let file_name = target
            .file_name()
            .expect("the path is normalized and it isn't the root directory either");

        // check if there is already a file or directory with the same name
        for entry in self.process_current_dir() {
            let entry = entry?;

            if entry.name == file_name {
                return Err(FSError::AlreadyExists);
            }
        }

        let now = self.options.clock.now();

        let dir_cluster = self.create_entry_chain(self.dir_info.borrow().chain_start, now)?;

        // The cluster chain of the directory has been created,
        // we now need to add it as an entry to the current directory

        let sfn = utils::string::gen_sfn(file_name, self, parent_dir)?;

        // we got everything to create our first (and only) RawProperties struct
        let raw_properties = MinProperties {
            name: file_name.into(),
            sfn,
            attributes: RawAttributes::empty() | RawAttributes::DIRECTORY,
            created: Some(now),
            modified: now,
            accessed: Some(now.date()),
            file_size: 0,
            data_cluster: dir_cluster,
        };

        let entries = [raw_properties];

        self.go_to_dir(parent_dir)?;

        self.insert_to_entry_chain(Box::new(entries))?;

        Ok(())
    }

    /// Rename a file or directory to a new name
    pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(&self, from: P, to: Q) -> FSResult<(), S::Error> {
        let from = from.as_ref();
        let to = to.as_ref();

        if !from.is_valid() || !to.is_valid() {
            return Err(FSError::MalformedPath);
        }

        let from = from.normalize();
        let to = to.normalize();

        let parent_from = match from.parent() {
            Some(parent) => parent,
            // we can't rename the root directory
            None => return Err(FSError::PermissionDenied),
        };
        let parent_to = match to.parent() {
            Some(parent) => parent,
            // we can't rename the root directory
            None => return Err(FSError::PermissionDenied),
        };

        let entry_from = {
            let mut entry_from = None;

            for entry in self.read_dir(parent_from)? {
                let entry = entry?;

                if *entry.path() == from {
                    entry_from = Some(entry);

                    break;
                }
            }

            match entry_from {
                Some(entry) => entry,
                None => return Err(FSError::NotFound),
            }
        };

        for entry in self.read_dir(parent_to)? {
            let entry = entry?;

            if *entry.path() == to {
                return Err(FSError::AlreadyExists);
            }
        }

        // if the entry is a file, everything is way more simple,
        // we just need to remove this entry a create a new one at
        // the target directory. This can be accomplished in 2 ways:
        // 1. we first remove the old entry and then create the new one, or
        // 2. we first create the new entry and then remove the old one
        // the first method is easier to implement, but has a higher risk of data loss
        // the second method is a bit more difficult and in a worst-case scenario
        // the file won't be lost, althought we will be left with 2 hard links
        // pointing to the same file. Here we use the second method
        self.go_to_dir(parent_to)?;

        let now = self.options.clock.now();

        if entry_from.is_dir() {
            // the process with directories is the same, expect we must modify the ".." entry
            // so that it points to the new parent directory
            // the ".." entry is always the second entry, so we will do something a bit hacky here
            let parent_entry = MinProperties {
                name: Box::from(typed_path::constants::windows::PARENT_DIR_STR),
                sfn: PARENT_DIR_SFN,
                // this needs to be set when creating a file
                attributes: RawAttributes::empty() | RawAttributes::DIRECTORY,
                created: Some(now),
                modified: now,
                accessed: Some(now.date()),
                file_size: 0,
                data_cluster: match self.dir_info.borrow().chain_start {
                    EntryLocationUnit::DataCluster(cluster) => cluster,
                    EntryLocationUnit::RootDirSector(_) => 0,
                },
            };

            // we are modifying the 2nd entry
            let entry_location = EntryLocation {
                unit: EntryLocationUnit::DataCluster(entry_from.data_cluster),
                index: 1,
            };

            use utils::bincode::BINCODE_CONFIG;

            self._go_to_cached_dir()?;
            let mut bytes: [u8; DIRENTRY_SIZE] = [0; DIRENTRY_SIZE];

            bincode::encode_into_slice(FATDirEntry::from(parent_entry), &mut bytes, BINCODE_CONFIG)
                .map_err(utils::bincode::map_err_enc)?;

            entry_location.set_bytes(self, bytes)?;
        }

        let old_chain = entry_from.chain;
        let old_props: MinProperties = entry_from.into();
        let to_filename = to.file_name().expect("this path is normalized");
        let sfn = utils::string::gen_sfn(to_filename, self, parent_to)?;

        let props = MinProperties {
            name: Box::from(to_filename),
            sfn,
            attributes: old_props.attributes,
            created: Some(now),
            modified: now,
            accessed: Some(now.date()),
            file_size: old_props.file_size,
            data_cluster: old_props.data_cluster,
        };
        self.insert_to_entry_chain(Box::from([props]))?;

        self.remove_entry_chain(&old_chain)?;

        Ok(())
    }

    /// Remove a [`RWFile`] from the filesystem
    ///
    /// This is an alias to `self.get_rw_file(path)?.remove()?`
    #[inline]
    pub fn remove_file<P: AsRef<Path>>(&self, path: P) -> FSResult<(), S::Error> {
        self.get_rw_file(path)?.remove()?;

        Ok(())
    }

    /// Remove a file from the filesystem, even if it is read-only
    ///
    /// **USE WITH EXTREME CAUTION!**
    #[inline]
    pub fn remove_file_unchecked<P: AsRef<Path>>(&self, path: P) -> FSResult<(), S::Error> {
        self.get_rw_file_unchecked(path)?.remove()?;

        Ok(())
    }

    /// Remove an empty directory from the filesystem
    ///
    /// Errors if the path provided points to the root directory
    pub fn remove_empty_dir<P: AsRef<Path>>(&self, path: P) -> FSResult<(), S::Error> {
        let path = path.as_ref();

        if !path.is_valid() {
            return Err(FSError::MalformedPath);
        }

        if path
            .components()
            .next_back()
            .expect("this iterator will always yield at least the root directory")
            == WindowsComponent::root()
        {
            // we are in the root directory, we can't remove it
            return Err(FSError::InvalidInput);
        }

        if self.read_dir(path)?.next().is_some() {
            return Err(FSError::DirectoryNotEmpty);
        }

        let parent_path = path
            .parent()
            .expect("we aren't in the root directory, this shouldn't panic");

        let parent_dir_entries = self.read_dir(parent_path)?;

        let entry = {
            let mut entry = None;

            for ent in parent_dir_entries {
                let ent = ent?;

                if ent.path() == path {
                    entry = Some(ent);

                    break;
                }
            }

            entry.ok_or(FSError::NotFound)?
        };

        // we first clear the corresponding entry chain in the parent directory
        self.remove_entry_chain(&entry.chain)?;

        // then we remove the allocated cluster chain
        self.free_cluster_chain(entry.data_cluster)?;

        Ok(())
    }

    /// Removes a directory at this path, after removing all its contents.
    ///
    /// Use with caution!
    ///
    /// This will fail if there is at least 1 (one) read-only file
    /// in this directory or in any subdirectory. To avoid this behavior,
    /// use [`remove_dir_all_unchecked()`](FileSystem::remove_dir_all_unchecked)
    pub fn remove_dir_all<P: AsRef<Path>>(&self, path: P) -> FSResult<(), S::Error> {
        // before we actually start removing stuff,
        // let's make sure there are no read-only files

        if self.check_for_readonly_files(&path)? {
            log::error!(concat!(
                "A read-only file has been found ",
                "in a directory pending deletion."
            ));
            return Err(FSError::ReadOnlyFile);
        }

        // we have checked everything, this is safe to use
        self.remove_dir_all_unchecked(&path)?;

        Ok(())
    }

    /// Like [`remove_dir_all()`](FileSystem::remove_dir_all),
    /// but also removes read-only files.
    ///
    /// **USE WITH EXTREME CAUTION!**
    pub fn remove_dir_all_unchecked<P: AsRef<Path>>(&self, path: P) -> FSResult<(), S::Error> {
        let path = path.as_ref();

        if !path.is_valid() {
            return Err(FSError::MalformedPath);
        }

        let mut read_dir = self.read_dir(path)?;
        loop {
            let entry = match read_dir.next() {
                Some(entry) => entry?,
                None => break,
            };

            if entry.is_dir() {
                self.remove_dir_all_unchecked(&entry.path)?;
            } else if entry.is_file() {
                self.remove_file_unchecked(&entry.path)?;
            } else {
                unreachable!()
            }
        }

        self.remove_empty_dir(path)?;

        Ok(())
    }

    /// Check `path` recursively to see if there are any read-only files in it
    ///
    /// If successful, the `bool` returned indicates
    /// whether or not at least 1 (one) read-only file has been found
    pub fn check_for_readonly_files<P: AsRef<Path>>(&self, path: P) -> FSResult<bool, S::Error> {
        let path = path.as_ref();

        if !path.is_valid() {
            return Err(FSError::MalformedPath);
        }

        let mut read_dir = self.read_dir(path)?;

        loop {
            let entry = match read_dir.next() {
                Some(entry) => entry?,
                None => break,
            };

            let read_only_found = if entry.is_dir() {
                self.check_for_readonly_files(&entry.path)?
            } else if entry.is_file() {
                entry.attributes.read_only
            } else {
                unreachable!()
            };

            if read_only_found {
                // we have found at least 1 read-only file,
                // no need to search any further
                return Ok(true);
            }
        }

        Ok(false)
    }

    /// Get a corresponding [`RWFile`] object from a [`Path`]
    ///
    /// Fails if `path` doesn't represent a file, or if that file doesn't exist
    pub fn get_rw_file<P: AsRef<Path>>(&self, path: P) -> FSResult<RWFile<'_, S>, S::Error> {
        let rw_file = self.get_rw_file_unchecked(path)?;

        if rw_file.attributes.read_only {
            return Err(FSError::ReadOnlyFile);
        }

        Ok(rw_file)
    }

    /// Sync any pending changes back to the storage medium and drop
    ///
    /// Use this to catch any IO errors that might be rejected silently
    /// while [`Drop`]ping
    pub fn unmount(&self) -> FSResult<(), S::Error> {
        self.sync_fsinfo()?;
        let should_sync_buffer = self.sync_f.borrow().is_some();
        if should_sync_buffer {
            self.sync_sector_buffer()?;
        }
        self.storage.borrow_mut().flush()?;

        Ok(())
    }
}

impl<S> ops::Drop for FileSystem<S>
where
    S: Read + Seek,
{
    fn drop(&mut self) {
        if let Some(unmount) = *self.unmount_f.borrow() {
            // nothing to do if this errors out while dropping
            let _ = unmount(self);
        }
    }
}