Newer
Older
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
/* Check if we are at the end of the directory */
if (direntry[DIR_NAME] == DIR0_ALLEMPTY)
{
return -ENOENT;
}
/* Is this an LFN entry? Does it have the sequence number we are
* looking for?
*/
if (LDIR_GETATTRIBUTES(direntry) != LDDIR_LFNATTR) ||
LDIR_GETSEQ(direntry) != seqno);
{
/* No, restart the search at the next entry */
seqno = lastseq;
goto nextentry;
}
/* Yes.. If this is not the "last" LFN entry, then the checksum must
* also be the same.
*/
if (seqno == lastseq)
{
/* Just save the checksum for subsequent checks */
cksum = LDIR_GETCHECKSUM(direntry);
}
/* Not the first entry in the sequence. Does the checksum match the
* previous sequences?
*/
else if (cksum != LDIR_GETCHECKSUM(direntry))
{
/* No, restart the search at the next entry */
seqno = lastseq;
goto nextentry;
}
/* Check if the name substring in this LFN matches the corresponding
* substring of the name we are looking for.
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
offset = ((seqno & LDIR0_SEQ_MASK) - 1) * LDIR_MAXLFNCHARS;
if (fat_cmplfname(direntry, &dirinfo.fd_lfname[offset]))
{
/* Yes.. it matches. Check the sequence number. Is this the
* "last" LFN entry (i.e., the one that appears first)?
*/
if (seqno == lastseq)
{
/* Yes.. Save information about this LFN entry position */
dirinfo->fd_seq.ds_lfnsector = fs->fs_currentsector;
dirinfo->fd_seq.ds_lfnoffset = diroffset;
dirinfo->fd_seq.ds_lfncluster = dirinfo->dir.fd_currcluster;
seqno &= LDIR0_SEQ_MASK;
}
/* Is this the first sequence number (i.e., the LFN entry that
* will appear last)?
*/
if (seqno == 1)
{
/* We have found all of the LFN entries. The next directory
* entry should be the one containing the short file name
* alias and all of the meat about the file or directory.
*/
if (fat_nextdirentry(fs, &dirinfo->dir) != OK)
{
return -ENOENT;
}
/* Verify the checksum */
if (fat_lfnchecksum(dirinfo->fd_name) == cksum)
{
/* Success! Save the position of the directory entry and
* return success.
*/
dirinfo->fd_seq.ds_sector = fs->fs_currentsector;
dirinfo->fd_seq.ds_offset = diroffset;
dirinfo->fd_seq.ds_cluster = dirinfo->dir.fd_currcluster;
return OK;
}
/* Bad news.. reset and continue with this entry (which is
* probably not an LFN entry unless the file systen is
* seriously corrupted.
*/
seqno = lastseq;
continue;
}
/* No.. there are more LFN entries to go. Decrement the sequence
* number and check the next directory entry.
*/
seqno--;
}
else
{
/* No.. the names do not match. Restart the search at the next
* entry.
*/
seqno = lastseq;
}
/* Continue at the next directory entry */
next_entry:
if (fat_nextdirentry(fs, &dirinfo->dir) != OK)
{
return -ENOENT;
}
/****************************************************************************
* Name: fat_allocatesfnentry
*
* Desciption: Find a free directory entry for a short file name entry.
*
****************************************************************************/
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
static inline int fat_allocatesfnentry(struct fat_mountpt_s *fs,
struct fat_dirinfo_s *dirinfo)
{
uint16_t diroffset;
uint8_t *direntry;
uint8_t ch;
int ret;
for (;;)
{
/* Read the directory sector into fs_buffer */
ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector);
if (ret < 0)
{
/* Make sure that the return value is NOT -ENOSPC */
return -EIO;
}
/* Get a pointer to the entry at fd_index */
diroffset = (dirinfo->dir.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE;
direntry = &fs->fs_buffer[diroffset];
/* Check if this directory entry is empty */
ch = direntry[DIR_NAME];
if (ch == DIR0_ALLEMPTY || ch == DIR0_EMPTY)
{
/* It is empty -- we have found a directory entry */
dirinfo->fd_seq.ds_sector = fs->fs_currentsector;
dirinfo->fd_seq.ds_offset = diroffset;
#ifdef CONFIG_FAT_LFN
dirinfo->fd_seq.ds_cluster = dirinfo->dir.fd_currcluster;
#endif
/* Set the "last" long file name offset to the same entry */
#ifdef CONFIG_FAT_LFN
dirinfo->fd_seq.ds_lfnsector = dirinfo->fd_seq.ds_sector;
dirinfo->fd_seq.ds_lfnoffset = dirinfo->fd_seq.ds_offset;
dirinfo->fd_seq.ds_lfncluster = dirinfo->fd_seq.ds_cluster;
return OK;
}
/* It is not empty try the next one */
ret = fat_nextdirentry(fs, &dirinfo->dir);
if (ret < 0)
{
/* This will return -ENOSPC if we have examined all of the
* directory entries without finding a free entry.
*/
return ret;
}
}
}
/****************************************************************************
* Desciption: Find a sequence of free directory entries for a several long
* and one short file name entry.
*
****************************************************************************/
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
#ifdef CONFIG_FAT_LFN
static inline int fat_allocatelfnentry(struct fat_mountpt_s *fs,
struct fat_dirinfo_s *dirinfo)
{
uint16_t diroffset;
uint8_t *direntry;
uint8_t nentries;
uint8_t remainder;
uint8_t needed;
int namelen;
int ret;
/* Get the length of the long file name and make sure that it does
* not exceed the maximum.
*/
namelen = strlen(dirinfo->fd_lfname);
if (namelen > LDIR_MAXFNAME)
{
return -EINVAL;
}
/* How many LFN directory entries are we expecting? */
nentries = namelen / LDIR_MAXLFNCHARS;
remainder = namelen - nfullentries * LDIR_MAXLFNCHARS;
if (remainder > 0)
{
nentries++;
}
DEBUGASSERT(nentries > 0 && nentries <= LDIR_MAXLFNS);
/* Plus another for short file name entry that follows the sequence of LFN
* entries.
*/
nentries++;
/* Now, search the directory looking for a sequence for free entries that
* long.
*/
needed = nentries;
for (;;)
{
/* Read the directory sector into fs_buffer */
ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector);
if (ret < 0)
{
/* Make sure that the return value is NOT -ENOSPC */
return -EIO;
}
/* Get a pointer to the entry at fd_index */
diroffset = (dirinfo->dir.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE;
direntry = &fs->fs_buffer[diroffset];
/* Check if this directory entry is empty */
ch = direntry[DIR_NAME];
if (ch == DIR0_ALLEMPTY || ch == DIR0_EMPTY)
{
/* It is empty -- we have found a directory entry. Is this the
* "last" LFN entry (i.e., the one that occurs first)?
*/
if (needed == nentries)
{
/* Yes.. remember the position of this entry */
dirinfo->fd_seq.ds_lfnsector = fs->fs_currentsector;
dirinfo->fd_seq.ds_lfnoffset = diroffset;
dirinfo->fd_seq.ds_lfncluster = dirinfo->dir.fd_currcluster;
}
/* Is this last entry we need (i.e., the entry for the short
* file name entry)?
*/
if (needed <= 1)
{
/* Yes.. remember the position of this entry and return
* success.
*/
dirinfo->fd_seq.ds_sector = fs->fs_currentsector;
dirinfo->fd_seq.ds_offset = diroffset;
dirinfo->fd_seq.ds_cluster = dirinfo->dir.fd_currcluster;
return OK;
}
/* Otherwise, just decrement the number of directory entries
* needed and continue looking.
*/
needed--;
}
/* The directory entry is not available */
else
{
/* Reset the search and continue looking */
needed = nentries;
}
/* Try the next directory entry */
ret = fat_nextdirentry(fs, &dirinfo->dir);
if (ret < 0)
{
/* This will return -ENOSPC if we have examined all of the
* directory entries without finding a free entry.
*/
return ret;
}
}
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: fat_finddirentry
*
* Desciption: Given a path to something that may or may not be in the file
* system, return the directory entry of the item.
*
****************************************************************************/
int fat_finddirentry(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo,
const char *path)
{
char terminator;
int ret;
/* Initialize to traverse the chain. Set it to the cluster of the root
* directory
*/
cluster = fs->fs_rootbase;
if (fs->fs_type == FSTYPE_FAT32)
{
/* For FAT32, the root directory is variable sized and is a cluster
* chain like any other directory. fs_rootbase holds the first
* cluster of the root directory.
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
*/
dirinfo->dir.fd_startcluster = cluster;
dirinfo->dir.fd_currcluster = cluster;
dirinfo->dir.fd_currsector = fat_cluster2sector(fs, cluster);
}
else
{
/* For FAT12/16, the first sector of the root directory is a sector
* relative to the first sector of the fat volume.
*/
dirinfo->dir.fd_startcluster = 0;
dirinfo->dir.fd_currcluster = 0;
dirinfo->dir.fd_currsector = cluster;
}
/* fd_index is the index into the current directory table */
dirinfo->dir.fd_index = 0;
/* If no path was provided, then the root directory must be exactly what
* the caller is looking for.
*/
if (*path == '\0')
{
dirinfo->fd_root = true;
return OK;
}
/* Otherwise, loop until the path is found */
for (;;)
{
/* Convert the next the path segment name into the kind of name that
* we would see in the directory entry.
*/
ret = fat_path2dirname(&path, dirinfo, &terminator);
if (ret < 0)
{
/* ERROR: The filename contains invalid characters or is
* too long.
*/
return ret;
}
/* Is this a path segment a long or a short file. Was a long file
* name parsed?
#ifdef CONFIG_FAT_LFN
if (dirinfo.fd_lfname[0] != '\0')
/* Yes.. Search for the sequence of long file name directory
* entries.
*/
ret = fat_findlfnentry(fs, dirinfo);
}
/* No.. Search for the single short file name directory entry */
else
#endif
{
ret = fat_findsfnentry(fs, dirinfo);
}
/* If the terminator character in the path was the end of the string
* then we have successfully found the directory entry that describes
* the path.
*/
if (!terminator)
{
/* Return success meaning that the description the matching
* directory entry is in dirinfo.
*/
return OK;
}
/* No.. then we have found one of the intermediate directories on
* the way to the final path target. In this case, make sure
* the thing that we found is, indeed, a directory.
*/
direntry = &fs->fs_buffer[dirinfo->fd_seq.ds_offset];
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
if (!(DIR_GETATTRIBUTES(direntry) & FATATTR_DIRECTORY))
{
/* Ooops.. we found something else */
return -ENOTDIR;
}
/* Get the cluster number of this directory */
cluster =
((uint32_t)DIR_GETFSTCLUSTHI(direntry) << 16) |
DIR_GETFSTCLUSTLO(direntry);
/* The restart scanning at the new directory */
dirinfo->dir.fd_currcluster = dirinfo->dir.fd_startcluster = cluster;
dirinfo->dir.fd_currsector = fat_cluster2sector(fs, cluster);
dirinfo->dir.fd_index = 2;
}
}
/****************************************************************************
* Name: fat_allocatedirentry
*
* Desciption: Find a free directory entry
*
****************************************************************************/
int fat_allocatedirentry(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo)
{
int32_t cluster;
off_t sector;
int ret;
int i;
/* Re-initialize directory object */
cluster = dirinfo->dir.fd_startcluster;
/* Loop until we successfully allocate the sequence of directory entries
* or until to fail to extend the directory cluster chain.
*/
for (;;)
{
dirinfo->dir.fd_currcluster = cluster;
dirinfo->dir.fd_currsector = fat_cluster2sector(fs, cluster);
}
else
{
/* Fixed size FAT12/16 root directory is at fixxed offset/size */
dirinfo->dir.fd_currsector = fs->fs_rootbase;
}
dirinfo->dir.fd_index = 0;
/* Is this a path segment a long or a short file. Was a long file
* name parsed?
*/
#ifdef CONFIG_FAT_LFN
if (dirinfo.fd_lfname[0] != '\0')
/* Yes.. Allocate for the sequence of long file name directory
* entries plus a short file name directory entry.
*/
ret = fat_allocatelfnentry(fs, dirinfo);
}
/* No.. Allocate only a short file name directory entry */
else
#endif
{
ret = fat_allocatesfnentry(fs, dirinfo);
/* Did we successfully allocate the directory entries? If the error
* value is -ENOSPC, then we can try to extend the directory cluster
* (we can't handle other return values)
*/
if (ret == OK || ret != -ENOSPC)
{
return ret;
}
/* If we get here, then we have reached the end of the directory table
* in this sector without finding a free directory entry.
*
* It this is a fixed size dirctory entry, then this is an error.
* Otherwise, we can try to extend the directory cluster chain to
* make space for the new directory entry.
*/
if (!cluster)
{
/* The size is fixed */
/* Try to extend the cluster chain for this directory */
cluster = fat_extendchain(fs, dirinfo->dir.fd_currcluster);
if (cluster < 0)
{
return cluster;
}
/* Flush out any cached data in fs_buffer.. we are going to use
* it to initialize the new directory cluster.
*/
ret = fat_fscacheflush(fs);
if (ret < 0)
{
return ret;
}
/* Clear all sectors comprising the new directory cluster */
fs->fs_currentsector = fat_cluster2sector(fs, cluster);
memset(fs->fs_buffer, 0, fs->fs_hwsectorsize);
sector = sector;
for (i = fs->fs_fatsecperclus; i; i--)
ret = fat_hwwrite(fs, fs->fs_buffer, sector, 1);
if ( ret < 0)
{
return ret;
}
sector++;
}
}
}
/****************************************************************************
* Name: fat_freedirentry
*
* Desciption: Free the directory entry.
*
****************************************************************************/
int fat_freedirentry(struct fat_mountpt_s *fs, struct fat_dirseq_s *seq)
#ifdef CONFIG_FAT_LFN
uint16_t diroffset;
uint8_t *direntry;
int ret;
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
/* Set it to the cluster containing the "last" LFN entry (that appears
* first on the media).
*/
dirinfo->dir.fd_startcluster = dirinfo->fd_seq.fd_lfncluster;
dirinfo->dir.fd_currcluster = dirinfo->fd_seq.fd_lfnsector;
dirinfo->dir.fd_index = dirinfo->fd_seq.fd_lfnoffset / DIR_SIZE;
/* Free all of the directory entries used for the sequence of long file name
* and for the single short file name entry.
*/
for (;;)
{
/* Read the directory sector into the sector cache */
ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector);
if (ret < 0)
{
return ret;
}
/* Get a pointer to the directory entry */
diroffset = (dirinfo->dir.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE;
direntry = &fs->fs_buffer[diroffset];
/* Then mark the entry as deleted */
direntry[DIR_NAME] = DIR0_EMPTY;
fs->fs_dirty = true;
/* Did we just free the single short file name entry? */
if (dirinfo->dir.fd_currsector == dirinfo->fd_seq.fd_sector &&
diroffset == dirinfo->fd_seq.fd_sector)
{
/* Yes.. then we are finished. flush anything remaining in the
* cache and return, probably successfully.
*/
return fat_fscacheflush(fs);
}
/* There are moe entries to go.. Try the next directory entry */
ret = fat_nextdirentry(fs, &dirinfo->dir);
if (ret < 0)
{
return ret;
}
}
#else
uint8_t *direntry;
int ret;
/* Free the single short file name entry.
*
* Make sure that the sector containing the directory entry is in the
* cache.
*/
ret = fat_fscacheread(fs, seq->ds_sector);
if (ret == OK)
{
/* Then mark the entry as deleted */
direntry = &fs->fs_buffer[seq->ds_offset];
direntry[DIR_NAME] = DIR0_EMPTY;
fs->fs_dirty = true;
}
return ret;
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
}
/****************************************************************************
* Name: fat_dirname2path
*
* Desciption: Convert a filename in a raw directory entry into a user
* filename. This is essentially the inverse operation of that performed
* by fat_path2dirname. See that function for more details.
*
****************************************************************************/
int fat_dirname2path(char *path, uint8_t *direntry)
{
#ifdef CONFIG_FAT_LCNAMES
uint8_t ntflags;
#endif
int ch;
int ndx;
/* Check if we will be doing upper to lower case conversions */
#ifdef CONFIG_FAT_LCNAMES
ntflags = DIR_GETNTRES(direntry);
#endif
/* Get the 8-byte filename */
for (ndx = 0; ndx < 8; ndx++)
{
/* Get the next filename character from the directory entry */
ch = direntry[ndx];
/* Any space (or ndx==8) terminates the filename */
if (ch == ' ')
{
break;
}
/* In this version, we never write 0xe5 in the directoryfilenames
* (because we do not handle any character sets where 0xe5 is valid
* in a filaname), but we could encounted this in a filesystem
* written by some other system
*/
if (ndx == 0 && ch == DIR0_E5)
{
ch = 0xe5;
}
/* Check if we should perform upper to lower case conversion
* of the (whole) filename.
*/
#ifdef CONFIG_FAT_LCNAMES
if (ntflags & FATNTRES_LCNAME && isupper(ch))
{
ch = tolower(ch);
}
#endif
/* Copy the next character into the filename */
*path++ = ch;
}
/* Check if there is an extension */
if (direntry[8] != ' ')
{
/* Yes, output the dot before the extension */
*path++ = '.';
/* Then output the (up to) 3 character extension */
for (ndx = 8; ndx < 11; ndx++)
{
/* Get the next extensions character from the directory entry */
ch = direntry[DIR_NAME + ndx];
/* Any space (or ndx==11) terminates the extension */
if (ch == ' ')
{
break;
}
/* Check if we should perform upper to lower case conversion
* of the (whole) filename.
*/
#ifdef CONFIG_FAT_LCNAMES
if (ntflags & FATNTRES_LCEXT && isupper(ch))
{
ch = tolower(ch);
}
#endif
/* Copy the next character into the filename */
*path++ = ch;
}
}
/* Put a null terminator at the end of the filename */
*path = '\0';
return OK;
}
/****************************************************************************
* Name: fat_dirnamewrite
*
* Desciption: Write the (possibly long) directory entry name.
*
* Assumption: The directory sector is in the cache.
*
****************************************************************************/
int fat_dirnamewrite(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo)
{
uint8_t *direntry = &fs->fs_buffer[dirinfo->fd_seq.ds_offset];
memcpy(&direntry[DIR_NAME], dirinfo->fd_name, DIR_MAXFNAME);
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
#ifdef CONFIG_FLAT_LCNAMES
DIR_PUTNTRES(direntry, dirinfo->fd_ntflags);
#else
DIR_PUTNTRES(direntry, 0);
#endif
fs->fs_dirty = true;
return OK;
}
/****************************************************************************
* Name: fat_dirwrite
*
* Desciption: Write a directory entry, possibly with a long file name
*
* Assumption: The directory sector is in the cache. The caller will write
* sector information.
*
****************************************************************************/
int fat_dirwrite(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo,
uint8_t attributes, uint32_t fattime)
{
uint8_t *direntry;
/* Initialize the 32-byte directory entry */
direntry = &fs->fs_buffer[dirinfo->fd_seq.ds_offset];
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
memset(direntry, 0, DIR_SIZE);
/* Directory name info */
(void)fat_dirnamewrite(fs, dirinfo);
/* Set the attribute attribute, write time, creation time */
DIR_PUTATTRIBUTES(direntry, attributes);
/* Set the time information */
DIR_PUTWRTTIME(direntry, fattime & 0xffff);
DIR_PUTCRTIME(direntry, fattime & 0xffff);
DIR_PUTWRTDATE(direntry, fattime >> 16);
DIR_PUTCRDATE(direntry, fattime >> 16);
fs->fs_dirty = true;
return OK;
}
/****************************************************************************
* Name: fat_dircreate
*
* Desciption: Create a directory entry for a new file
*
****************************************************************************/
int fat_dircreate(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo)
{
uint32_t fattime;
int ret;
/* Allocate a directory entry entry */
ret = fat_allocatedirentry(fs, dirinfo);
if (ret != OK)
{
/* Failed to set up directory entry */
return ret;
}
/* Write the directory entry with the current time and the ARCHIVE attribute */
fattime = fat_systime2fattime();
return fat_dirwrite(fs, dirinfo, FATATTR_ARCHIVE, fattime);
}
/****************************************************************************
* Name: fat_remove
*
* Desciption: Remove a directory or file from the file system. This
* implements both rmdir() and unlink().
*
****************************************************************************/
int fat_remove(struct fat_mountpt_s *fs, const char *relpath, bool directory)
{
struct fat_dirinfo_s dirinfo;
uint32_t dircluster;
uint8_t *direntry;
int ret;
/* Find the directory entry referring to the entry to be deleted */
ret = fat_finddirentry(fs, &dirinfo, relpath);
if (ret != OK)
{
/* No such path */
return -ENOENT;
}
/* Check if this is a FAT12/16 root directory */
if (dirinfo.fd_root)
{
/* The root directory cannot be removed */
return -EPERM;
}
/* The object has to have write access to be deleted */
direntry = &fs->fs_buffer[dirinfo.fd_seq.ds_offset];
if ((DIR_GETATTRIBUTES(direntry) & FATATTR_READONLY) != 0)
{
/* It is a read-only entry */
return -EACCES;
}
/* Get the directory sector and cluster containing the
* entry to be deleted
*/
dircluster =
((uint32_t)DIR_GETFSTCLUSTHI(direntry) << 16) |
DIR_GETFSTCLUSTLO(direntry);
/* Is this entry a directory? */
if (DIR_GETATTRIBUTES(direntry) & FATATTR_DIRECTORY)
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
{
/* It is a sub-directory. Check if we are be asked to remove
* a directory or a file.
*/
if (!directory)
{
/* We are asked to delete a file */
return -EISDIR;
}
/* We are asked to delete a directory. Check if this
* sub-directory is empty
*/
dirinfo.dir.fd_currcluster = dircluster;
dirinfo.dir.fd_currsector = fat_cluster2sector(fs, dircluster);
dirinfo.dir.fd_index = 2;
/* Loop until either (1) an entry is found in the directory
* (error), (2) the directory is found to be empty, or (3) some
* error occurs.
*/
for (;;)
{
unsigned int subdirindex;
uint8_t *subdirentry;
/* Make sure that the sector containing the of the
* subdirectory sector is in the cache
*/
ret = fat_fscacheread(fs, dirinfo.dir.fd_currsector);
if (ret < 0)
{
return ret;
}
/* Get a reference to the next entry in the directory */
subdirindex = (dirinfo.dir.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE;
subdirentry = &fs->fs_buffer[subdirindex];
/* Is this the last entry in the direcory? */
if (subdirentry[DIR_NAME] == DIR0_ALLEMPTY)
{
/* Yes then the directory is empty. Break out of the
* loop and delete the directory.