Newer
Older
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
/****************************************************************************
* fs/fat/fs_fat32dirent.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* NOTE: If CONFIG_FAT_LFN is defined, then there may be some legal, patent
* issues. The following was extracted from the entry "File Allocation Table
* from Wikipedia, the free encyclopedia:
*
* "On December 3, 2003 Microsoft announced it would be offering licenses
* for use of its FAT specification and 'associated intellectual property',
* at the cost of a US$0.25 royalty per unit sold, with a $250,000 maximum
* royalty per license agreement.
*
* o "U.S. Patent 5,745,902 (http://www.google.com/patents?vid=5745902) -
* Method and system for accessing a file using file names having
* different file name formats. ...
* o "U.S. Patent 5,579,517 (http://www.google.com/patents?vid=5579517) -
* Common name space for long and short filenames. ...
* o "U.S. Patent 5,758,352 (http://www.google.com/patents?vid=5758352) -
* Common name space for long and short filenames. ...
* o "U.S. Patent 6,286,013 (http://www.google.com/patents?vid=6286013) -
* Method and system for providing a common name space for long and
* short file names in an operating system. ...
*
* "Many technical commentators have concluded that these patents only cover
* FAT implementations that include support for long filenames, and that
* removable solid state media and consumer devices only using short names
* would be unaffected. ..."
*
* So you have been forewarned: Use the long filename at your own risk!
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/fs.h>
#include <nuttx/fat.h>
#include "fs_internal.h"
#include "fs_fat32.h"
/****************************************************************************
* Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
enum fat_case_e
{
FATCASE_UNKNOWN = 0,
FATCASE_UPPER,
FATCASE_LOWER
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Variables
****************************************************************************/
/****************************************************************************
* Public Variables
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: fat_lfnchecksum
*
* Desciption: Caculate the checksum of .
*
****************************************************************************/
#ifdef CONFIG_FAT_LFN
static uint8_t fat_lfnchecksum(const uint8_t *sfname)
{
uint8_t sum = 0;
int i;
for (i = DIR_MAXFNAME; i; i--)
{
sum = ((sum & 1) << 7) + (sum >> 1) + *sfname++;
}
return sum;
}
#endif
/****************************************************************************
*
* Desciption: Convert a user filename into a properly formatted FAT
* (short 8.3) filename as it would appear in a directory entry. Here are
* the rules for the 8+3 short file name in the directory:
*
* The first byte:
*
* 0xe5 = The directory is free
* 0x00 = This directory and all following directories are free
* 0x05 = Really 0xe5
* 0x20 = May NOT be ' '
*
* Other characters may be any characters except for the following:
*
* 0x00-0x1f = (except for 0x00 and 0x05 in the first byte)
* 0x22 = '"'
* 0x2a-0x2c = '*', '+', ','
* 0x2e-0x2f = '.', '/'
* 0x3a-0x3f = ':', ';', '<', '=', '>', '?'
* 0x5b-0x5d = '[', '\\', ;]'
* 0x7c = '|'
*
* '.' May only occur once within string and only within the first 9
* bytes. The '.' is not save in the directory, but is implicit in
* 8+3 format.
*
* Lower case characters are not allowed in directory names (without some
* poorly documented operations on the NTRes directory byte). Lower case
* codes may represent different characters in other character sets ("DOS
* code pages". The logic below does not, at present, support any other
* character sets.
*
* Returned value:
* OK - The path refers to a valid 8.3 FAT file name and has been properly
* converted and stored in dirinfo.
* <0 - Otherwise an negated error is returned meaning that the string is
* not a valid 8+3 because:
*
* 1. Contains characters not in the printable character set,
* 2. Contains forbidden characters or multiple '.' characters
* 3. File name or extension is too long.
*
* If CONFIG_FAT_LFN is defined and CONFIG_FAT_LCNAMES is NOT
* defined, then:
*
* 4a. File name or extension contains lower case characters.
*
* If CONFIG_FAT_LFN is defined and CONFIG_FAT_LCNAMES is defined,
* then:
*
* 4b. File name or extension is not all the same case.
*
****************************************************************************/
static inline int fat_parsesfname(const char **path,
struct fat_dirinfo_s *dirinfo,
char *terminator)
{
#ifdef CONFIG_FAT_LCNAMES
unsigned int ntlcenable = FATNTRES_LCNAME | FATNTRES_LCEXT;
unsigned int ntlcfound = 0;
#ifdef CONFIG_FAT_LFN
enum fat_case_e namecase = FATCASE_UNKNOWN;
enum fat_case_e extcase = FATCASE_UNKNOWN;
#endif
const char *node = *path;
int endndx;
uint8_t ch;
int ndx = 0;
memset(dirinfo->fd_name, ' ', DIR_MAXFNAME);
/* Loop until the name is successfully parsed or an error occurs */
endndx = 8;
for (;;)
{
/* Get the next byte from the path */
/* Check if this the last byte in this node of the name */
if ((ch == '\0' || ch == '/') && ndx != 0 )
{
/* Return the accumulated NT flags and the terminating character */
#ifdef CONFIG_FAT_LCNAMES
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
dirinfo->fd_ntflags = ntlcfound & ntlcenable;
#endif
*terminator = ch;
*path = node;
return OK;
}
/* Accept only the printable character set. Note the first byte
* of the name could be 0x05 meaning that is it 0xe5, but this is
* not a printable character in this character in either case.
*/
else if (!isgraph(ch))
{
goto errout;
}
/* Check for transition from name to extension. Only one '.' is
* permitted and it must be within first 9 characters
*/
else if (ch == '.' && endndx == 8)
{
/* Starting the extension */
ndx = 8;
endndx = 11;
continue;
}
/* Reject printable characters forbidden by FAT */
else if (ch == '"' || (ch >= '*' && ch <= ',') ||
ch == '.' || ch == '/' ||
(ch >= ':' && ch <= '?') ||
(ch >= '[' && ch <= ']') ||
(ch == '|'))
{
goto errout;
}
/* Check for upper case characters */
#ifdef CONFIG_FAT_LCNAMES
else if (isupper(ch))
{
/* Some or all of the characters in the name or extension
* are upper case. Force all of the characters to be interpreted
* as upper case.
*/
if (endndx == 8)
{
/* Is there mixed case in the name? */
#ifdef CONFIG_FAT_LFN
if (namecase == FATCASE_LOWER)
{
/* Mixed case in the name -- use the long file name */
goto errout;
}
/* So far, only upper case in the name*/
namecase = FATCASE_UPPER;
ntlcenable &= ~FATNTRES_LCNAME;
}
else
{
/* Is there mixed case in the extension? */
#ifdef CONFIG_FAT_LFN
if (extcase == FATCASE_LOWER)
{
/* Mixed case in the extension -- use the long file name */
/* So far, only upper case in the extension*/
/* Clear lower case extension in mask */
ntlcenable &= ~FATNTRES_LCEXT;
else if (islower(ch))
{
#if defined(CONFIG_FAT_LFN) && !defined(CONFIG_FAT_LCNAMES)
/* If lower case characters are present, then a long file
* name will be constructed.
*/
goto errout;
#else
/* Convert the character to upper case */
ch = toupper(ch);
/* Some or all of the characters in the name or extension
* are lower case. They can be interpreted as lower case if
* only if all of the characters in the name or extension are
* lower case.
*/
#ifdef CONFIG_FAT_LCNAMES
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
if (endndx == 8)
{
/* Is there mixed case in the name? */
#ifdef CONFIG_FAT_LFN
if (namecase == FATCASE_UPPER)
{
/* Mixed case in the name -- use the long file name */
goto errout;
}
/* So far, only lower case in the name*/
namecase = FATCASE_LOWER;
#endif
/* Set lower case name bit */
ntlcfound |= FATNTRES_LCNAME;
}
else
{
/* Is there mixed case in the extension? */
#ifdef CONFIG_FAT_LFN
if (extcase == FATCASE_UPPER)
/* Mixed case in the extension -- use the long file name */
goto errout;
/* So far, only lower case in the extension*/
extcase = FATCASE_LOWER;
}
#endif
#endif /* CONFIG_FAT_LFN && !CONFIG_FAT_LCNAMES */
}
/* Check if the file name exceeds the size permitted (without
* long file name support).
*/
if (ndx >= endndx)
{
goto errout;
}
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
/* Save next character in the accumulated name */
dirinfo->fd_name[ndx++] = ch;
}
errout:
return -EINVAL;
}
/****************************************************************************
* Name: fat_parselfname
*
* Desciption: Convert a user filename into a properly formatted FAT
* long filename as it would appear in a directory entry. Here are
* the rules for the long file name in the directory:
*
* Valid characters are the same as for short file names EXCEPT:
*
* 1. '+', ',', ';', '=', '[', and ']' are accepted in the file name
* 2. '.' (dot) can occur more than once in a filename. Extension is
* the substring after the last dot.
*
* Returned value:
* OK - The path refers to a valid long file name and has been properly
* stored in dirinfo.
* <0 - Otherwise an negated error is returned meaning that the string is
* not a valid long file name:
*
* 1. Contains characters not in the printable character set,
* 2. Contains forbidden characters
* 3. File name is too long.
*
****************************************************************************/
#ifdef CONFIG_FAT_LFN
static inline int fat_parselfname(const char **path,
struct fat_dirinfo_s *dirinfo,
char *terminator)
{
const char *node = *path;
uint8_t ch;
int ndx = 0;
/* Loop until the name is successfully parsed or an error occurs */
for (;;)
{
/* Get the next byte from the path */
ch = *node++;
/* Check if this the last byte in this node of the name */
if ((ch == '\0' || ch == '/') && ndx != 0 )
{
/* Null terminate the string */
dirinfo->fd_lfname[ndx] = '\0';
/* Return the remaining sub-string and the terminating character. */
*terminator = ch;
*path = node;
return OK;
}
/* Accept only the printable character set */
else if (!isgraph(ch))
{
goto errout;
}
/* Reject printable characters forbidden by FAT */
else if (ch == '"' || ch == '*' || ch == '/' || ch == ':' ||
ch == '<' || ch == '>' || ch == '?' || ch == '\\' ||
ch == '|')
{
goto errout;
}
/* Check if the file name exceeds the size permitted. */
if (ndx >= LDIR_MAXFNAME)
{
goto errout;
}
/* Save next character in the accumulated name */
dirinfo->fd_lfname[ndx++] = ch;
}
return -EINVAL;
}
#endif
/****************************************************************************
* Name: fat_createalias
*
* Desciption: Given a valid long file name, create a short filename alias.
* Here are the rules for creation of the alias:
*
* 1. All uppercase
* 2. All dots except the last deleted
* 3. First 6 (uppercase) characters used as a base
* 4. Then ~1. The number is increased if the file already exists in the
* directory. If the number exeeds >10, then character stripped off the
* base.
* 5. The extension is the first 3 uppercase chars of extension.
*
* This function is called only from fat_putlfname()
*
* Returned value:
* OK - The alias was created correctly.
* <0 - Otherwise an negated error is returned.
*
****************************************************************************/
#ifdef CONFIG_FAT_LFN
static inline int fat_createalias(struct fat_dirinfo_s *dirinfo)
{
uint8_t ch; /* Current character being processed */
char *ext; /* Pointer to the extension substring */
char *src; /* Pointer to the long file name source */
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
int len; /* Total length of the long file name */
int namechars; /* Number of characters available in long name */
int extchars; /* Number of characters available in long name extension */
int endndx; /* Maximum index into the short name array */
int ndx; /* Index to store next character */
/* First, let's decide what is name and what is extension */
len = strlen(dirinfo.fd_lfname);
ext = strrchr(dirinfo.fd_lfname, '.');
if (ext)
{
ptrdiff_t tmp;
/* ext points to the final '.'. The difference in bytes from the
* beginning of the string is then the name length.
*/
tmp = ext - dirinfo.fd_lfname;
namechars = tmp;
/* And the rest, exluding the '.' is the extension. */
extchars = len - namechars - 1;
ext++;
}
else
{
/* No '.' found. It is all name and no extension. */
namechars = len;
extchars = 0;
}
/* Alias are always all upper case */
#ifdef CONFIG_FAT_LCNAMES
dirinfo->fd_ntflags = 0;
#endif
/* Initialized the short name with all spaces */
memset(dirinfo->fd_name, ' ', DIR_MAXFNAME);
/* Handle a special case where there is no name. Windows seems to use
* the extension plus random stuff then ~1 to pat to 8 bytes. Some
* examples:
*
* a.b -> a.b No long name
* a., -> A26BE~1._ Padded name to make unique, _ replaces ,
* .b -> B1DD2~1 Extension used as name
* .bbbbbbb -> BBBBBB~1 Extension used as name
* a.bbbbbbb -> AAD39~1.BBB Padded name to make unique.
* aaa.bbbbbbb -> AAA~1.BBBB Not padded, already unique?
* ,.bbbbbbb -> _82AF~1.BBB _ replaces ,
* +[],.bbbbbbb -> ____~1.BBB _ replaces +[],
*/
if (namechars < 1)
{
/* Use the extension as the name */
DEBUGASSERT(ext && extchars > 0);
ext = NULL;
namechar = extchars;
extchars = 0;
}
else
{
}
/* Then copy the name and extension, handling upper case conversions and
* excluding forbidden characters.
*/
ndx = 0; /* Position to write the next name character */
endndx = 6; /* Maximum index before we write ~! and switch to the extension */
for (;;)
{
/* Get the next byte from the path. Break out of the loop if we
* encounter the end of null-terminated the long file name string.
*/
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
if (ch == '\0')
{
break;
}
/* Exclude those few characters included in long file names, but
* excluded in short file name: '+', ',', ';', '=', '[', ']', and '.'
*/
if (ch == '+' || ch == ',' || ch == '.' || ch == ';' ||
ch == '=' || ch == '[' || ch == ']' || ch == '|')
{
/* Use the underbar character instead */
ch = '_';
}
/* Handle lower case characters */
ch = toupper(ch);
/* We now have a valid character to add to the name or extension. */
dirinfo->fd_name[ndx++] = ch;
/* Did we just add a character to the name? */
if (endndx == 6)
{
/* Decrement the number of characters available in the name
* portion of the long name.
*/
namechars--;
/* Is it time to add ~1 to the string? Will will do that if
* either (1) we have already added the maximum number of
* characters to the short name, or (2) if there are no further
* characters available in the name portion of the long name.
*/
if (namechars < 1 || ndx == 6)
{
/* Write the ~1 at the end of the name */
dirinfo->fd_name[ndx++] = '~';
dirinfo->fd_name[ndx++] = '1';
/* Then switch to the extension (if there is one) */
if (!ext || extchars < 1)
{
return OK;
}
ndx = 8;
endndx = 11;
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
}
}
/* No.. we just added a character to the extension */
else
{
/* Decrement the number of characters available in the name
* portion of the long name
*/
extchars--;
/* Is the extension complete? */
if (extchars < 1 || ndx == 11)
{
return OK;
}
}
}
}
#endif
/****************************************************************************
* Name: fat_uniquealias
*
* Desciption: Make sure that the short alias for the long file name is
* unique. Modify the alias as necessary to assure uniqueness.
*
* Returned value:
* OK - The alias is unique.
* <0 - Otherwise an negated error is returned.
*
****************************************************************************/
#ifdef CONFIG_FAT_LFN
static inline int fat_uniquealias(const char **path,
struct fat_dirinfo_s *dirinfo)
{
#warning "Missing alias alias uniqueness logic"
return OK;
}
#endif
/****************************************************************************
* Name: fat_path2dirname
*
* Desciption: Convert a user filename into a properly formatted FAT
* (short 8.3) filename as it would appear in a directory entry.
*
****************************************************************************/
static int fat_path2dirname(const char **path, struct fat_dirinfo_s *dirinfo,
char *terminator)
{
#ifdef CONFIG_FAT_LFN
int ret;
/* Assume no long file name */
dirinfo->fd_lfname[0] = '\0';
/* Then parse the (assumed) 8+3 short file name */
ret = fat_parsesfname(path, dirinfo, terminator);
if (ret < 0)
{
/* No, the name is not a valid short 8+3 file name. Try parsing
* the long file name.
*/
ret = fat_parselfname(path, dirinfo, terminator);
}
return ret;
#else
/* Only short, 8+3 filenames supported */
return fat_parsesfname(path, dirinfo, terminator);
#endif
}
/****************************************************************************
* Name: fat_findsfnentry
*
* Desciption: Find a short file name directory entry.
*
****************************************************************************/
static inline int fat_findsfnentry(struct fat_mountpt_s *fs,
struct fat_dirinfo_s *dirinfo)
{
uint16_t diroffset;
uint8_t *direntry;
int ret;
/* Search, beginning with the current sector, for a directory entry with
* the matching short name
*/
for (;;)
{
/* Read the next sector into memory */
ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector);
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
return ret;
}
/* 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;
}
/* Check if we have found the directory entry that we are looking for */
if (direntry[DIR_NAME] != DIR0_EMPTY &&
!(DIR_GETATTRIBUTES(direntry) & FATATTR_VOLUMEID) &&
!memcmp(&direntry[DIR_NAME], dirinfo->fd_name, DIR_MAXFNAME) )
{
/* Yes.. Return success */
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
/* Position the last long file name directory entry at the same
* position.
*/
#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;
#endif
return OK;
}
/* No... get the next directory index and try again */
if (fat_nextdirentry(fs, &dirinfo->dir) != OK)
{
return -ENOENT;
}
}
}
/****************************************************************************
* Name: fat_cmplfnchunk
*
* 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 perform
* the comparison of a single chunk.
*
****************************************************************************/
#ifdef CONFIG_FAT_LFN
static bool fat_cmplfnchunk(wchar_t *chunk, const uint8_t *substr, int nchunk)
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
{
uint8_t *ptr;
int i;
/* Check bytes 1-nchunk */
for (i = 0; i < nchunk; i++)
{
wchar_t wch;
/* If we encounter the NUL terminator in the name string, then it is
* a match -- return TRUE.
*/
if (*substr == 0)
{
return true;
}
/* 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 = fat_getuint16(chunk);
if (wch & 0xff != (wchar_t)*substr)
{
return false;
}
/* Yes.. the characters match. Try the next */
chunk++;
substr++;
}
/* All of the characters in the chunk match.. Return success */
return true;
}
#endif
/****************************************************************************
* Name: fat_cmplfname
*
* Desciption: Given an LFN directory entry, compare a substring of the name
* to a portion in the directory entry.
*
****************************************************************************/
#ifdef CONFIG_FAT_LFN
static bool fat_cmplfname(const uint8_t *direntry, const uint8_t *substr)
{
uint8_t *chunk;
int i;
/* Check bytes 1-5 */
chunk = LDIR_PTRWCHAR1_5(direntry);
if (fat_cmplfnchunk(chunk, substr, 5))
{
/* Check bytes 6-11 */
chunk = LDIR_PTRWCHAR6_11(direntry);
if (fat_cmplfnchunk(chunk, &substr[5], 6))
{
chunk = LDIR_PTRWCHAR12_13(direntry);
return fat_cmplfnchunk(chunk, &substr[11], 2);
}
}
return false;
}
#endif
/****************************************************************************
* Name: fat_findlfnentry
*
* Desciption: Find a sequence of long file name directory entries.
*
* NOTE: As a side effect, this function returns with the sector containing
* the short file name directory entry in the cache.
*
****************************************************************************/
#ifdef CONFIG_FAT_LFN
static inline int fat_findlfnentry(struct fat_mountpt_s *fs,
struct fat_dirinfo_s *dirinfo)
{
uint16_t diroffset;
uint8_t *direntry;
uint8_t lastseq;
uint8_t seqno;
uint8_t nfullentries;
uint8_t nentries;
uint8_t remainder;
/* 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).
*/
namelen = strlen(dirinfo.fd_lfname);
DEBUGASSERT(namelen <= LDIR_MAXFNAME);
/* 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).
*/
lastseq = LDIR_SEQ | nentries;
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)