Newer
Older
* NOTE that the name length is incremented to include the NUL terminating
* character that must also be written to the directory entry.
namelen = strlen((char*)dirinfo->fd_lfname) + 1;
DEBUGASSERT(namelen <= LDIR_MAXFNAME+1);
/* How many LFN directory entries are we expecting? */
nfullentries = namelen / LDIR_MAXLFNCHARS;
remainder = namelen - nfullentries * LDIR_MAXLFNCHARS;
nentries = nfullentries;
if (remainder > 0)
{
nentries++;
}
DEBUGASSERT(nentries > 0 && nentries <= LDIR_MAXLFNS);
/* This is the first sequency number we are looking for, the sequence
* number of the last LFN entry (remember that they appear in reverse
* order.. from last to first).
*/
seqno = lastseq;
/* Search, beginning with the current sector, for a directory entry this
* the match shore name
*/
for (;;)
{
/* Read the next sector into memory */
ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector);
if (ret < 0)
{
/* Get a pointer to the directory entry */
diroffset = DIRSEC_BYTENDX(fs, dirinfo->dir.fd_index);
direntry = &fs->fs_buffer[diroffset];
/* 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;
}
/* 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 */
checksum = LDIR_GETCHECKSUM(direntry);
}
/* Not the first entry in the sequence. Does the checksum match the
* previous sequences?
*/
else if (checksum != LDIR_GETCHECKSUM(direntry))
{
/* No, restart the search at the next entry */
seqno = lastseq;
}
/* Check if the name substring in this LFN matches the corresponding
* substring of the name we are looking for.
offset = ((seqno & LDIR0_SEQ_MASK) - 1) * LDIR_MAXLFNCHARS;
if (fat_cmplfname(direntry, &dirinfo->fd_lfname[offset]))
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
{
/* 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;
}
/* Make sure that the directory entry is in the sector cache */
ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector);
if (ret < 0)
{
return ret;
}
/* Get a pointer to the directory entry */
diroffset = DIRSEC_BYTENDX(fs, dirinfo->dir.fd_index);
direntry = &fs->fs_buffer[diroffset];
/* Verify the checksum */
if (fat_lfnchecksum(&direntry[DIR_NAME]) == checksum)
{
/* 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.
*
****************************************************************************/
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
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.
*
****************************************************************************/
#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;
/* Get the length of the long file name (size of the fd_lfname array is
* LDIR_MAXFNAME+1 we do not have to check the length of the string).
* NOTE that the name length is incremented to include the NUL terminating
* character that must also be written to the directory entry.
namelen = strlen((char *)dirinfo->fd_lfname) + 1;
DEBUGASSERT(namelen <= LDIR_MAXFNAME+1);
/* How many LFN directory entries are we expecting? */
nentries = namelen / LDIR_MAXLFNCHARS;
remainder = namelen - nentries * LDIR_MAXLFNCHARS;
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
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 */
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;
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
/* 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
/****************************************************************************
* Desciption: Get the 8.3 filename from a directory entry. On entry, the
* short file name entry is already in the cache.
*
****************************************************************************/
static inline int fat_getsfname(uint8_t *direntry, char *buffer,
unsigned int buflen)
#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
/* Reserve a byte for the NUL terminator */
/* Get the 8-byte filename */
for (ndx = 0; ndx < 8 && buflen > 0; ndx++)
{
/* Get the next filename character from the directory entry */
/* Any space (or ndx==8) terminates the filename */
if (ch == ' ')
{
break;
}
/* In this version, we never write 0xe5 in the directory filenames
* (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 */
*buffer++ = ch;
buflen--;
}
/* Check if there is an extension */
if (direntry[8] != ' ' && buflen > 0)
{
/* Yes, output the dot before the extension */
*buffer++ = '.';
buflen--;
/* Then output the (up to) 3 character extension */
for (ndx = 8; ndx < 11 && buflen > 0; 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 */
*buffer++ = ch;
buflen--;
}
}
/* Put a null terminator at the end of the filename. We don't have to
* check if there is room because we reserved a byte for the NUL
* terminator at the beginning of this function.
*/
*buffer = '\0';
return OK;
}
/****************************************************************************
* Desciption: There are 13 characters per LFN entry, broken up into three
* chunks for characts 1-5, 6-11, and 12-13. This function will get the
* file name characters from one chunk.
*
****************************************************************************/
static void fat_getlfnchunk(uint8_t *chunk, uint8_t *dest, int nchunk)
for (i = 0; i < nchunk; i++)
/* Get the next unicode character from the chunk. We only handle ASCII.
* For ASCII, the upper byte should be zero and the lower should match
* the ASCII code.
*/
wch = (wchar_t)fat_getuint16(chunk);
*dest++ = (uint8_t)(wch & 0xff);
chunk += sizeof(wchar_t);
}
}
#endif
/****************************************************************************
* Name: fat_getlfname
*
* Desciption: Get the long filename from a sequence of directory entries.
* On entry, the "last" long file name entry is in the cache. Returns with
* the short file name entry in the cache.
*
****************************************************************************/
#ifdef CONFIG_FAT_LFN
static inline int fat_getlfname(struct fat_mountpt_s *fs, struct fs_dirent_s *dir)
{
uint8_t lfname[LDIR_MAXLFNCHARS];
uint16_t diroffset;
uint8_t *direntry;
uint8_t seqno;
uint8_t offset;
uint8_t checksum;
int nsrc;
int i;
/* Get a reference to the current directory entry */
diroffset = (dir->u.fat.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE;
direntry = &fs->fs_buffer[diroffset];
/* Get the starting sequence number */
seqno = LDIR_GETSEQ(direntry);
DEBUGASSERT((seqno & LDIR0_LAST) != 0);
/* Sanity check */
rawseq = (seqno & LDIR0_SEQ_MASK);
if (rawseq < 1 || rawseq > LDIR_MAXLFNS)
{
return -EINVAL;
}
/* Save the checksum value */
checksum = LDIR_GETCHECKSUM(direntry);
/* Loop until the whole file name has been transferred */
for (;;)
{
/* Get the string offset associated with the "last" entry. */
offset = (rawseq - 1) * LDIR_MAXLFNCHARS;
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
/* Will any of this file name fit into the destination buffer? */
if (offset < NAME_MAX)
{
/* Yes.. extract and convert the unicode name */
fat_getlfnchunk(LDIR_PTRWCHAR1_5(direntry), lfname, 5);
fat_getlfnchunk(LDIR_PTRWCHAR6_11(direntry), &lfname[5], 6);
fat_getlfnchunk(LDIR_PTRWCHAR12_13(direntry), &lfname[11], 2);
/* Ignore trailing spaces on the "last" directory entry. The
* number of characters avaiable is LDIR_MAXLFNCHARS or that
* minus the number of trailing spaces on the "last" directory
* entry.
*/
nsrc = LDIR_MAXLFNCHARS;
if ((seqno & LDIR0_LAST) != 0)
{
/* Reduce the number of characters by the number of trailing
* spaces.
*/
for (; nsrc > 0 && lfname[nsrc-1] == ' '; nsrc--);
/* Further reduce the length so that it fits in the destination
* buffer.
*/
if (offset + nsrc > NAME_MAX)
{
nsrc = NAME_MAX - offset;
}
/* Add a null terminator to the destination string (the actual
* length of the destination buffer is NAME_MAX+1, so the NUL
* terminator will fit).
*/
dir->fd_dir.d_name[offset+nsrc] = '\0';
}
/* Then transfer the characters */
for (i = 0; i < nsrc && offset+i < NAME_MAX; i++)
{
dir->fd_dir.d_name[offset+i] = lfname[i];
}
}
/* Read next directory entry */
if (fat_nextdirentry(fs, &dir->u.fat) != OK)
{
return -ENOENT;
}
/* Make sure that the directory sector into the sector cache */
ret = fat_fscacheread(fs, dir->u.fat.fd_currsector);
if (ret < 0)
{
return ret;
}
/* Get a reference to the current directory entry */
diroffset = (dir->u.fat.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE;
direntry = &fs->fs_buffer[diroffset];
/* Get the next expected sequence number. */
if (seqno < 1)
{
/* We just completed processing the "first" long file name entry
* and we just read the short file name entry. Verify that the
* checksum of the short file name matches the checksum that we
* found in the long file name entries.
*/
if (fat_lfnchecksum(direntry) == checksum)
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
{
/* Yes.. return success! */
return OK;
}
/* No, the checksum is bad. */
return -EINVAL;
}
/* Verify the next long file name entry. Is this an LFN entry? Does it
* have the sequence number we are looking for? Does the checksum
* match the previous entries?
*/
if (LDIR_GETATTRIBUTES(direntry) != LDDIR_LFNATTR ||
LDIR_GETSEQ(direntry) != seqno ||
LDIR_GETCHECKSUM(direntry) != checksum)
{
return -EINVAL;
}
}
}
#endif
/****************************************************************************
* Name: fat_putsfname
*
* Desciption: Write the short directory entry name.
*
* Assumption: The directory sector is in the cache.
*
****************************************************************************/
static int fat_putsfname(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);
DIR_PUTNTRES(direntry, dirinfo->fd_ntflags);
#else
DIR_PUTNTRES(direntry, 0);
#endif
fs->fs_dirty = true;
return OK;
}
/****************************************************************************
*
* Desciption: There are 13 characters per LFN entry, broken up into three
* chunks for characts 1-5, 6-11, and 12-13. This function will put the
* 0xffff characters into one chunk.
*
****************************************************************************/
#ifdef CONFIG_FAT_LFN
static void fat_initlfname(uint8_t *chunk, int nchunk)
{
int i;
/* Initialize unicode characters 1-nchunk */
for (i = 0; i < nchunk; i++)
{
/* The write the 16-bit 0xffff character into the directory entry. */
fat_putuint16((uint8_t *)chunk, (uint16_t)0xffff);
chunk += sizeof(wchar_t);
}
}
#endif
/****************************************************************************
* Name: fat_putlfnchunk
*
* Desciption: There are 13 characters per LFN entry, broken up into three
* chunks for characts 1-5, 6-11, and 12-13. This function will put the
* file name characters into one chunk.
*
****************************************************************************/
#ifdef CONFIG_FAT_LFN
static void fat_putlfnchunk(uint8_t *chunk, const uint8_t *src, int nchunk)
{
uint16_t wch;
int i;
/* Write bytes 1-nchunk */
for (i = 0; i < nchunk; i++)
{
/* Get the next ascii character from the name substring and convert it
* to unicode. The upper byte should be zero and the lower should be
* the ASCII code. The write the unicode character into the directory
* entry.
*/
wch = (uint16_t)*src++;
fat_putuint16(chunk, wch);
chunk += sizeof(wchar_t);
}
}
#endif
/****************************************************************************
* Name: fat_putlfname
*
* Desciption: Write the long filename into a sequence of directory entries.
* On entry, the "last" long file name entry is in the cache. Returns with
* the short file name entry in the cache.
*
****************************************************************************/
#ifdef CONFIG_FAT_LFN
static int fat_putlfname(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo)
{
uint16_t diroffset;
uint8_t *direntry;
uint8_t nfullentries;
uint8_t nentries;
uint8_t remainder;
uint8_t offset;
uint8_t seqno;
uint8_t checksum;
int namelen;
int ret;
/* Get the length of the long file name (size of the fd_lfname array is
* LDIR_MAXFNAME+1 we do not have to check the length of the string).
* NOTE that the name length is incremented to include the NUL terminating
* character that must also be written to the directory entry.
namelen = strlen((char*)dirinfo->fd_lfname) + 1;
DEBUGASSERT(namelen <= LDIR_MAXFNAME+1);
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
/* How many LFN directory entries do we need to write? */
nfullentries = namelen / LDIR_MAXLFNCHARS;
remainder = namelen - nfullentries * LDIR_MAXLFNCHARS;
nentries = nfullentries;
if (remainder > 0)
{
nentries++;
}
DEBUGASSERT(nentries > 0 && nentries <= LDIR_MAXLFNS);
/* Create the short file name alias */
ret = fat_createalias(dirinfo);
if (ret < 0)
{
return ret;
}
/* Get the short file name checksum */
checksum = fat_lfnchecksum(dirinfo->fd_name);
/* Setup the starting sequence number */
seqno = LDIR0_LAST | nentries;
/* Set up the initial positional data */
dirinfo->dir.fd_currcluster = dirinfo->fd_seq.ds_lfncluster;
dirinfo->dir.fd_currsector = dirinfo->fd_seq.ds_lfnsector;
dirinfo->dir.fd_index = dirinfo->fd_seq.ds_lfnoffset / DIR_SIZE;
/* Make sure that the sector containing the "last" long file name entry
* is in the sector cache (it probably is not).
*/
ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector);
if (ret < 0)
{
return ret;
}
/* Now loop, writing each long file name entry */
for (;;)
{
/* Get the string offset associated with the directory entry. */
/* Get a reference to the current directory entry */
diroffset = (dirinfo->dir.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE;
direntry = &fs->fs_buffer[diroffset];
/* Is this the "last" LFN directory entry? */
if ((seqno & LDIR0_LAST) != 0 && remainder != 0)
{
int nbytes;
/* Initialize the "last" directory entry name to all 0xffff */
fat_initlfname(LDIR_PTRWCHAR1_5(direntry), 5);
fat_initlfname(LDIR_PTRWCHAR6_11(direntry), 6);
fat_initlfname(LDIR_PTRWCHAR12_13(direntry), 2);
/* Store the tail portion of the long file name in directory entry */
nbytes = MIN(5, remainder);
fat_putlfnchunk(LDIR_PTRWCHAR1_5(direntry),
&dirinfo->fd_lfname[offset], nbytes);
remainder -= nbytes;
if (remainder > 0)
{
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
fat_putlfnchunk(LDIR_PTRWCHAR6_11(direntry),
&dirinfo->fd_lfname[offset+5], nbytes);
remainder -= nbytes;
}
if (remainder > 0)
{
nbytes = MIN(2, remainder);
fat_putlfnchunk(LDIR_PTRWCHAR12_13(direntry),
&dirinfo->fd_lfname[offset+11], nbytes);
remainder -= nbytes;
}
/* The remainder should now be zero */
DEBUGASSERT(remainder == 0);
}
else
{
/* Store a portion long file name in this directory entry */
fat_putlfnchunk(LDIR_PTRWCHAR1_5(direntry),
&dirinfo->fd_lfname[offset], 5);
fat_putlfnchunk(LDIR_PTRWCHAR6_11(direntry),
&dirinfo->fd_lfname[offset+5], 6);
fat_putlfnchunk(LDIR_PTRWCHAR12_13(direntry),
&dirinfo->fd_lfname[offset+11], 2);
}
/* Write the remaining directory entries */
LDIR_PUTSEQ(direntry, seqno);
LDIR_PUTATTRIBUTES(direntry, LDDIR_LFNATTR);
LDIR_PUTNTRES(direntry, 0);
LDIR_PUTCHECKSUM(direntry, checksum);
fs->fs_dirty = true;
/* Read next directory entry */
if (fat_nextdirentry(fs, &dirinfo->dir) != OK)
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
{
return -ENOENT;
}
/* Make sure that the sector containing the directory entry is in the
* sector cache
*/
ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector);
if (ret < 0)
{
return ret;
}
/* Decrement the number of entries and get the next sequence number. */
if (--nentries <= 0)
{
/* We have written all of the long file name entries to the media
* and we have the short file name entry in the cache. We can
* just return success.
*/
return OK;
}
/* The sequence number is just the number of entries left to be
* written.
*/
seqno = nentries;
}
}
#endif
/****************************************************************************
* Name: fat_putsfdirentry
*
* Desciption: Write a short file name directory entry
*
* Assumption: The directory sector is in the cache. The caller will write
* sector information.
*
****************************************************************************/
static int fat_putsfdirentry(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];
memset(direntry, 0, DIR_SIZE);
/* Directory name info */
(void)fat_putsfname(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);