match newlib

ensure max filename length matches newlib
This commit is contained in:
Dave Murphy 2016-12-20 20:07:08 +00:00
parent abde1cb73b
commit 73ccaec815
4 changed files with 24 additions and 24 deletions

View File

@ -87,9 +87,9 @@ static int _FAT_directory_lfnLength (const char* name) {
int ucsLength; int ucsLength;
const char* tempName = name; const char* tempName = name;
nameLength = strnlen(name, MAX_FILENAME_LENGTH); nameLength = strnlen(name, NAME_MAX);
// Make sure the name is short enough to be valid // Make sure the name is short enough to be valid
if ( nameLength >= MAX_FILENAME_LENGTH) { if ( nameLength >= NAME_MAX) {
return -1; return -1;
} }
// Make sure it doesn't contain any invalid characters // Make sure it doesn't contain any invalid characters
@ -364,7 +364,7 @@ bool _FAT_directory_getNextEntry (PARTITION* partition, DIR_ENTRY* entry) {
} }
if (lfnExists) { if (lfnExists) {
if (_FAT_directory_ucs2tombs (entry->filename, lfn, MAX_FILENAME_LENGTH) == (size_t)-1) { if (_FAT_directory_ucs2tombs (entry->filename, lfn, NAME_MAX) == (size_t)-1) {
// Failed to convert the file name to UTF-8. Maybe the wrong locale is set? // Failed to convert the file name to UTF-8. Maybe the wrong locale is set?
return false; return false;
} }
@ -405,7 +405,7 @@ bool _FAT_directory_getRootEntry (PARTITION* partition, DIR_ENTRY* entry) {
entry->dataEnd = entry->dataStart; entry->dataEnd = entry->dataStart;
memset (entry->filename, '\0', MAX_FILENAME_LENGTH); memset (entry->filename, '\0', NAME_MAX);
entry->filename[0] = '.'; entry->filename[0] = '.';
memset (entry->entryData, 0, DIR_ENTRY_DATA_SIZE); memset (entry->entryData, 0, DIR_ENTRY_DATA_SIZE);
@ -474,7 +474,7 @@ bool _FAT_directory_entryFromPosition (PARTITION* partition, DIR_ENTRY* entry) {
int lfnPos; int lfnPos;
uint8_t entryData[DIR_ENTRY_DATA_SIZE]; uint8_t entryData[DIR_ENTRY_DATA_SIZE];
memset (entry->filename, '\0', MAX_FILENAME_LENGTH); memset (entry->filename, '\0', NAME_MAX);
// Create an empty directory entry to overwrite the old ones with // Create an empty directory entry to overwrite the old ones with
for ( entryStillValid = true, finished = false; for ( entryStillValid = true, finished = false;
@ -516,7 +516,7 @@ bool _FAT_directory_entryFromPosition (PARTITION* partition, DIR_ENTRY* entry) {
} }
} else { } else {
// Encode the long file name into a multibyte string // Encode the long file name into a multibyte string
if (_FAT_directory_ucs2tombs (entry->filename, lfn, MAX_FILENAME_LENGTH) == (size_t)-1) { if (_FAT_directory_ucs2tombs (entry->filename, lfn, NAME_MAX) == (size_t)-1) {
return false; return false;
} }
} }
@ -571,7 +571,7 @@ bool _FAT_directory_entryFromPath (PARTITION* partition, DIR_ENTRY* entry, const
dirnameLength = strlen(pathPosition); dirnameLength = strlen(pathPosition);
} }
if (dirnameLength > MAX_FILENAME_LENGTH) { if (dirnameLength > NAME_MAX) {
// The path is too long to bother with // The path is too long to bother with
return false; return false;
} }
@ -589,7 +589,7 @@ bool _FAT_directory_entryFromPath (PARTITION* partition, DIR_ENTRY* entry, const
while (foundFile && !found && !notFound) { // It hasn't already found the file while (foundFile && !found && !notFound) { // It hasn't already found the file
// Check if the filename matches // Check if the filename matches
if ((dirnameLength == strnlen(entry->filename, MAX_FILENAME_LENGTH)) if ((dirnameLength == strnlen(entry->filename, NAME_MAX))
&& (_FAT_directory_mbsncasecmp(pathPosition, entry->filename, dirnameLength) == 0)) { && (_FAT_directory_mbsncasecmp(pathPosition, entry->filename, dirnameLength) == 0)) {
found = true; found = true;
} }
@ -758,9 +758,9 @@ static bool _FAT_directory_entryExists (PARTITION* partition, const char* name,
char alias[MAX_ALIAS_LENGTH]; char alias[MAX_ALIAS_LENGTH];
size_t dirnameLength; size_t dirnameLength;
dirnameLength = strnlen(name, MAX_FILENAME_LENGTH); dirnameLength = strnlen(name, NAME_MAX);
if (dirnameLength >= MAX_FILENAME_LENGTH) { if (dirnameLength >= NAME_MAX) {
return false; return false;
} }
@ -769,7 +769,7 @@ static bool _FAT_directory_entryExists (PARTITION* partition, const char* name,
while (foundFile) { // It hasn't already found the file while (foundFile) { // It hasn't already found the file
// Check if the filename matches // Check if the filename matches
if ((dirnameLength == strnlen(tempEntry.filename, MAX_FILENAME_LENGTH)) if ((dirnameLength == strnlen(tempEntry.filename, NAME_MAX))
&& (_FAT_directory_mbsncasecmp(name, tempEntry.filename, dirnameLength) == 0)) { && (_FAT_directory_mbsncasecmp(name, tempEntry.filename, dirnameLength) == 0)) {
return true; return true;
} }
@ -808,7 +808,7 @@ static int _FAT_directory_createAlias (char* alias, const char* lfn) {
// Primary portion of alias // Primary portion of alias
while (aliasPos < 8 && lfn[lfnPos] != '.' && lfn[lfnPos] != '\0') { while (aliasPos < 8 && lfn[lfnPos] != '.' && lfn[lfnPos] != '\0') {
bytesUsed = mbrtowc(&lfnChar, lfn + lfnPos, MAX_FILENAME_LENGTH - lfnPos, &ps); bytesUsed = mbrtowc(&lfnChar, lfn + lfnPos, NAME_MAX - lfnPos, &ps);
if (bytesUsed < 0) { if (bytesUsed < 0) {
return -1; return -1;
} }
@ -855,7 +855,7 @@ static int _FAT_directory_createAlias (char* alias, const char* lfn) {
aliasPos++; aliasPos++;
memset (&ps, 0, sizeof(ps)); memset (&ps, 0, sizeof(ps));
for (aliasExtLen = 0; aliasExtLen < MAX_ALIAS_EXT_LENGTH && *lfnExt != '\0'; aliasExtLen++) { for (aliasExtLen = 0; aliasExtLen < MAX_ALIAS_EXT_LENGTH && *lfnExt != '\0'; aliasExtLen++) {
bytesUsed = mbrtowc(&lfnChar, lfnExt, MAX_FILENAME_LENGTH - lfnPos, &ps); bytesUsed = mbrtowc(&lfnChar, lfnExt, NAME_MAX - lfnPos, &ps);
if (bytesUsed < 0) { if (bytesUsed < 0) {
return -1; return -1;
} }
@ -923,7 +923,7 @@ bool _FAT_directory_addEntry (PARTITION* partition, DIR_ENTRY* entry, uint32_t d
#endif #endif
// Make sure the filename is not 0 length // Make sure the filename is not 0 length
if (strnlen (entry->filename, MAX_FILENAME_LENGTH) < 1) { if (strnlen (entry->filename, NAME_MAX) < 1) {
return false; return false;
} }
@ -935,7 +935,7 @@ bool _FAT_directory_addEntry (PARTITION* partition, DIR_ENTRY* entry, uint32_t d
// Remove junk in filename // Remove junk in filename
i = strlen (entry->filename); i = strlen (entry->filename);
memset (entry->filename + i, '\0', MAX_FILENAME_LENGTH - i); memset (entry->filename + i, '\0', NAME_MAX - i);
// Make sure the entry doesn't already exist // Make sure the entry doesn't already exist
if (_FAT_directory_entryExists (partition, entry->filename, dirCluster)) { if (_FAT_directory_entryExists (partition, entry->filename, dirCluster)) {
@ -945,11 +945,11 @@ bool _FAT_directory_addEntry (PARTITION* partition, DIR_ENTRY* entry, uint32_t d
// Clear out alias, so we can generate a new one // Clear out alias, so we can generate a new one
memset (entry->entryData, ' ', 11); memset (entry->entryData, ' ', 11);
if ( strncmp(entry->filename, ".", MAX_FILENAME_LENGTH) == 0) { if ( strncmp(entry->filename, ".", NAME_MAX) == 0) {
// "." entry // "." entry
entry->entryData[0] = '.'; entry->entryData[0] = '.';
entrySize = 1; entrySize = 1;
} else if ( strncmp(entry->filename, "..", MAX_FILENAME_LENGTH) == 0) { } else if ( strncmp(entry->filename, "..", NAME_MAX) == 0) {
// ".." entry // ".." entry
entry->entryData[0] = '.'; entry->entryData[0] = '.';
entry->entryData[1] = '.'; entry->entryData[1] = '.';

View File

@ -31,13 +31,13 @@
#define _DIRECTORY_H #define _DIRECTORY_H
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/syslimits.h>
#include "common.h" #include "common.h"
#include "partition.h" #include "partition.h"
#define DIR_ENTRY_DATA_SIZE 0x20 #define DIR_ENTRY_DATA_SIZE 0x20
#define MAX_LFN_LENGTH 256 #define MAX_LFN_LENGTH 256
#define MAX_FILENAME_LENGTH 768 // 256 UCS-2 characters encoded into UTF-8 can use up to 768 UTF-8 chars
#define MAX_ALIAS_LENGTH 13 #define MAX_ALIAS_LENGTH 13
#define LFN_ENTRY_LENGTH 13 #define LFN_ENTRY_LENGTH 13
#define ALIAS_ENTRY_LENGTH 11 #define ALIAS_ENTRY_LENGTH 11
@ -72,7 +72,7 @@ typedef struct {
uint8_t entryData[DIR_ENTRY_DATA_SIZE]; uint8_t entryData[DIR_ENTRY_DATA_SIZE];
DIR_ENTRY_POSITION dataStart; // Points to the start of the LFN entries of a file, or the alias for no LFN DIR_ENTRY_POSITION dataStart; // Points to the start of the LFN entries of a file, or the alias for no LFN
DIR_ENTRY_POSITION dataEnd; // Always points to the file/directory's alias entry DIR_ENTRY_POSITION dataEnd; // Always points to the file/directory's alias entry
char filename[MAX_FILENAME_LENGTH]; char filename[NAME_MAX];
} DIR_ENTRY; } DIR_ENTRY;
// Directory entry offsets // Directory entry offsets

View File

@ -292,7 +292,7 @@ int _FAT_rename_r (struct _reent *r, const char *oldName, const char *newName) {
memcpy (&newDirEntry, &oldDirEntry, sizeof(DIR_ENTRY)); memcpy (&newDirEntry, &oldDirEntry, sizeof(DIR_ENTRY));
// Set the new name // Set the new name
strncpy (newDirEntry.filename, pathEnd, MAX_FILENAME_LENGTH - 1); strncpy (newDirEntry.filename, pathEnd, NAME_MAX - 1);
// Write the new entry // Write the new entry
if (!_FAT_directory_addEntry (partition, &newDirEntry, dirCluster)) { if (!_FAT_directory_addEntry (partition, &newDirEntry, dirCluster)) {
@ -381,7 +381,7 @@ int _FAT_mkdir_r (struct _reent *r, const char *path, int mode) {
pathEnd += 1; pathEnd += 1;
} }
// Create the entry data // Create the entry data
strncpy (dirEntry.filename, pathEnd, MAX_FILENAME_LENGTH - 1); strncpy (dirEntry.filename, pathEnd, NAME_MAX - 1);
memset (dirEntry.entryData, 0, DIR_ENTRY_DATA_SIZE); memset (dirEntry.entryData, 0, DIR_ENTRY_DATA_SIZE);
// Set the creation time and date // Set the creation time and date
@ -496,7 +496,7 @@ int _FAT_statvfs_r (struct _reent *r, const char *path, struct statvfs *buf)
buf->f_flag = ST_NOSUID /* No support for ST_ISUID and ST_ISGID file mode bits */ buf->f_flag = ST_NOSUID /* No support for ST_ISUID and ST_ISGID file mode bits */
| (partition->readOnly ? ST_RDONLY /* Read only file system */ : 0 ) ; | (partition->readOnly ? ST_RDONLY /* Read only file system */ : 0 ) ;
// Maximum filename length. // Maximum filename length.
buf->f_namemax = MAX_FILENAME_LENGTH; buf->f_namemax = NAME_MAX;
_FAT_unlock(&partition->lock); _FAT_unlock(&partition->lock);
return 0; return 0;
@ -593,7 +593,7 @@ int _FAT_dirnext_r (struct _reent *r, DIR_ITER *dirState, char *filename, struct
} }
// Get the filename // Get the filename
strncpy (filename, state->currentEntry.filename, MAX_FILENAME_LENGTH); strncpy (filename, state->currentEntry.filename, NAME_MAX);
// Get the stats, if requested // Get the stats, if requested
if (filestat != NULL) { if (filestat != NULL) {
_FAT_directory_entryStat (state->partition, &(state->currentEntry), filestat); _FAT_directory_entryStat (state->partition, &(state->currentEntry), filestat);

View File

@ -226,7 +226,7 @@ int _FAT_open_r (struct _reent *r, void *fileStruct, const char *path, int flags
pathEnd += 1; pathEnd += 1;
} }
// Create the entry data // Create the entry data
strncpy (dirEntry.filename, pathEnd, MAX_FILENAME_LENGTH - 1); strncpy (dirEntry.filename, pathEnd, NAME_MAX - 1);
memset (dirEntry.entryData, 0, DIR_ENTRY_DATA_SIZE); memset (dirEntry.entryData, 0, DIR_ENTRY_DATA_SIZE);
// Set the creation time and date // Set the creation time and date