Skip to content
This repository was archived by the owner on Nov 11, 2024. It is now read-only.

Commit 73ccaec

Browse files
committed
match newlib
ensure max filename length matches newlib
1 parent abde1cb commit 73ccaec

4 files changed

Lines changed: 24 additions & 24 deletions

File tree

source/directory.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ static int _FAT_directory_lfnLength (const char* name) {
8787
int ucsLength;
8888
const char* tempName = name;
8989

90-
nameLength = strnlen(name, MAX_FILENAME_LENGTH);
90+
nameLength = strnlen(name, NAME_MAX);
9191
// Make sure the name is short enough to be valid
92-
if ( nameLength >= MAX_FILENAME_LENGTH) {
92+
if ( nameLength >= NAME_MAX) {
9393
return -1;
9494
}
9595
// Make sure it doesn't contain any invalid characters
@@ -364,7 +364,7 @@ bool _FAT_directory_getNextEntry (PARTITION* partition, DIR_ENTRY* entry) {
364364
}
365365

366366
if (lfnExists) {
367-
if (_FAT_directory_ucs2tombs (entry->filename, lfn, MAX_FILENAME_LENGTH) == (size_t)-1) {
367+
if (_FAT_directory_ucs2tombs (entry->filename, lfn, NAME_MAX) == (size_t)-1) {
368368
// Failed to convert the file name to UTF-8. Maybe the wrong locale is set?
369369
return false;
370370
}
@@ -405,7 +405,7 @@ bool _FAT_directory_getRootEntry (PARTITION* partition, DIR_ENTRY* entry) {
405405

406406
entry->dataEnd = entry->dataStart;
407407

408-
memset (entry->filename, '\0', MAX_FILENAME_LENGTH);
408+
memset (entry->filename, '\0', NAME_MAX);
409409
entry->filename[0] = '.';
410410

411411
memset (entry->entryData, 0, DIR_ENTRY_DATA_SIZE);
@@ -474,7 +474,7 @@ bool _FAT_directory_entryFromPosition (PARTITION* partition, DIR_ENTRY* entry) {
474474
int lfnPos;
475475
uint8_t entryData[DIR_ENTRY_DATA_SIZE];
476476

477-
memset (entry->filename, '\0', MAX_FILENAME_LENGTH);
477+
memset (entry->filename, '\0', NAME_MAX);
478478

479479
// Create an empty directory entry to overwrite the old ones with
480480
for ( entryStillValid = true, finished = false;
@@ -516,7 +516,7 @@ bool _FAT_directory_entryFromPosition (PARTITION* partition, DIR_ENTRY* entry) {
516516
}
517517
} else {
518518
// Encode the long file name into a multibyte string
519-
if (_FAT_directory_ucs2tombs (entry->filename, lfn, MAX_FILENAME_LENGTH) == (size_t)-1) {
519+
if (_FAT_directory_ucs2tombs (entry->filename, lfn, NAME_MAX) == (size_t)-1) {
520520
return false;
521521
}
522522
}
@@ -571,7 +571,7 @@ bool _FAT_directory_entryFromPath (PARTITION* partition, DIR_ENTRY* entry, const
571571
dirnameLength = strlen(pathPosition);
572572
}
573573

574-
if (dirnameLength > MAX_FILENAME_LENGTH) {
574+
if (dirnameLength > NAME_MAX) {
575575
// The path is too long to bother with
576576
return false;
577577
}
@@ -589,7 +589,7 @@ bool _FAT_directory_entryFromPath (PARTITION* partition, DIR_ENTRY* entry, const
589589

590590
while (foundFile && !found && !notFound) { // It hasn't already found the file
591591
// Check if the filename matches
592-
if ((dirnameLength == strnlen(entry->filename, MAX_FILENAME_LENGTH))
592+
if ((dirnameLength == strnlen(entry->filename, NAME_MAX))
593593
&& (_FAT_directory_mbsncasecmp(pathPosition, entry->filename, dirnameLength) == 0)) {
594594
found = true;
595595
}
@@ -758,9 +758,9 @@ static bool _FAT_directory_entryExists (PARTITION* partition, const char* name,
758758
char alias[MAX_ALIAS_LENGTH];
759759
size_t dirnameLength;
760760

761-
dirnameLength = strnlen(name, MAX_FILENAME_LENGTH);
761+
dirnameLength = strnlen(name, NAME_MAX);
762762

763-
if (dirnameLength >= MAX_FILENAME_LENGTH) {
763+
if (dirnameLength >= NAME_MAX) {
764764
return false;
765765
}
766766

@@ -769,7 +769,7 @@ static bool _FAT_directory_entryExists (PARTITION* partition, const char* name,
769769

770770
while (foundFile) { // It hasn't already found the file
771771
// Check if the filename matches
772-
if ((dirnameLength == strnlen(tempEntry.filename, MAX_FILENAME_LENGTH))
772+
if ((dirnameLength == strnlen(tempEntry.filename, NAME_MAX))
773773
&& (_FAT_directory_mbsncasecmp(name, tempEntry.filename, dirnameLength) == 0)) {
774774
return true;
775775
}
@@ -808,7 +808,7 @@ static int _FAT_directory_createAlias (char* alias, const char* lfn) {
808808

809809
// Primary portion of alias
810810
while (aliasPos < 8 && lfn[lfnPos] != '.' && lfn[lfnPos] != '\0') {
811-
bytesUsed = mbrtowc(&lfnChar, lfn + lfnPos, MAX_FILENAME_LENGTH - lfnPos, &ps);
811+
bytesUsed = mbrtowc(&lfnChar, lfn + lfnPos, NAME_MAX - lfnPos, &ps);
812812
if (bytesUsed < 0) {
813813
return -1;
814814
}
@@ -855,7 +855,7 @@ static int _FAT_directory_createAlias (char* alias, const char* lfn) {
855855
aliasPos++;
856856
memset (&ps, 0, sizeof(ps));
857857
for (aliasExtLen = 0; aliasExtLen < MAX_ALIAS_EXT_LENGTH && *lfnExt != '\0'; aliasExtLen++) {
858-
bytesUsed = mbrtowc(&lfnChar, lfnExt, MAX_FILENAME_LENGTH - lfnPos, &ps);
858+
bytesUsed = mbrtowc(&lfnChar, lfnExt, NAME_MAX - lfnPos, &ps);
859859
if (bytesUsed < 0) {
860860
return -1;
861861
}
@@ -923,7 +923,7 @@ bool _FAT_directory_addEntry (PARTITION* partition, DIR_ENTRY* entry, uint32_t d
923923
#endif
924924

925925
// Make sure the filename is not 0 length
926-
if (strnlen (entry->filename, MAX_FILENAME_LENGTH) < 1) {
926+
if (strnlen (entry->filename, NAME_MAX) < 1) {
927927
return false;
928928
}
929929

@@ -935,7 +935,7 @@ bool _FAT_directory_addEntry (PARTITION* partition, DIR_ENTRY* entry, uint32_t d
935935

936936
// Remove junk in filename
937937
i = strlen (entry->filename);
938-
memset (entry->filename + i, '\0', MAX_FILENAME_LENGTH - i);
938+
memset (entry->filename + i, '\0', NAME_MAX - i);
939939

940940
// Make sure the entry doesn't already exist
941941
if (_FAT_directory_entryExists (partition, entry->filename, dirCluster)) {
@@ -945,11 +945,11 @@ bool _FAT_directory_addEntry (PARTITION* partition, DIR_ENTRY* entry, uint32_t d
945945
// Clear out alias, so we can generate a new one
946946
memset (entry->entryData, ' ', 11);
947947

948-
if ( strncmp(entry->filename, ".", MAX_FILENAME_LENGTH) == 0) {
948+
if ( strncmp(entry->filename, ".", NAME_MAX) == 0) {
949949
// "." entry
950950
entry->entryData[0] = '.';
951951
entrySize = 1;
952-
} else if ( strncmp(entry->filename, "..", MAX_FILENAME_LENGTH) == 0) {
952+
} else if ( strncmp(entry->filename, "..", NAME_MAX) == 0) {
953953
// ".." entry
954954
entry->entryData[0] = '.';
955955
entry->entryData[1] = '.';

source/directory.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
#define _DIRECTORY_H
3232

3333
#include <sys/stat.h>
34+
#include <sys/syslimits.h>
3435

3536
#include "common.h"
3637
#include "partition.h"
3738

3839
#define DIR_ENTRY_DATA_SIZE 0x20
3940
#define MAX_LFN_LENGTH 256
40-
#define MAX_FILENAME_LENGTH 768 // 256 UCS-2 characters encoded into UTF-8 can use up to 768 UTF-8 chars
4141
#define MAX_ALIAS_LENGTH 13
4242
#define LFN_ENTRY_LENGTH 13
4343
#define ALIAS_ENTRY_LENGTH 11
@@ -72,7 +72,7 @@ typedef struct {
7272
uint8_t entryData[DIR_ENTRY_DATA_SIZE];
7373
DIR_ENTRY_POSITION dataStart; // Points to the start of the LFN entries of a file, or the alias for no LFN
7474
DIR_ENTRY_POSITION dataEnd; // Always points to the file/directory's alias entry
75-
char filename[MAX_FILENAME_LENGTH];
75+
char filename[NAME_MAX];
7676
} DIR_ENTRY;
7777

7878
// Directory entry offsets

source/fatdir.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ int _FAT_rename_r (struct _reent *r, const char *oldName, const char *newName) {
292292
memcpy (&newDirEntry, &oldDirEntry, sizeof(DIR_ENTRY));
293293

294294
// Set the new name
295-
strncpy (newDirEntry.filename, pathEnd, MAX_FILENAME_LENGTH - 1);
295+
strncpy (newDirEntry.filename, pathEnd, NAME_MAX - 1);
296296

297297
// Write the new entry
298298
if (!_FAT_directory_addEntry (partition, &newDirEntry, dirCluster)) {
@@ -381,7 +381,7 @@ int _FAT_mkdir_r (struct _reent *r, const char *path, int mode) {
381381
pathEnd += 1;
382382
}
383383
// Create the entry data
384-
strncpy (dirEntry.filename, pathEnd, MAX_FILENAME_LENGTH - 1);
384+
strncpy (dirEntry.filename, pathEnd, NAME_MAX - 1);
385385
memset (dirEntry.entryData, 0, DIR_ENTRY_DATA_SIZE);
386386

387387
// Set the creation time and date
@@ -496,7 +496,7 @@ int _FAT_statvfs_r (struct _reent *r, const char *path, struct statvfs *buf)
496496
buf->f_flag = ST_NOSUID /* No support for ST_ISUID and ST_ISGID file mode bits */
497497
| (partition->readOnly ? ST_RDONLY /* Read only file system */ : 0 ) ;
498498
// Maximum filename length.
499-
buf->f_namemax = MAX_FILENAME_LENGTH;
499+
buf->f_namemax = NAME_MAX;
500500

501501
_FAT_unlock(&partition->lock);
502502
return 0;
@@ -593,7 +593,7 @@ int _FAT_dirnext_r (struct _reent *r, DIR_ITER *dirState, char *filename, struct
593593
}
594594

595595
// Get the filename
596-
strncpy (filename, state->currentEntry.filename, MAX_FILENAME_LENGTH);
596+
strncpy (filename, state->currentEntry.filename, NAME_MAX);
597597
// Get the stats, if requested
598598
if (filestat != NULL) {
599599
_FAT_directory_entryStat (state->partition, &(state->currentEntry), filestat);

source/fatfile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ int _FAT_open_r (struct _reent *r, void *fileStruct, const char *path, int flags
226226
pathEnd += 1;
227227
}
228228
// Create the entry data
229-
strncpy (dirEntry.filename, pathEnd, MAX_FILENAME_LENGTH - 1);
229+
strncpy (dirEntry.filename, pathEnd, NAME_MAX - 1);
230230
memset (dirEntry.entryData, 0, DIR_ENTRY_DATA_SIZE);
231231

232232
// Set the creation time and date

0 commit comments

Comments
 (0)