From 73ccaec8154e2a12fd8995514e10be83400f1fd1 Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Tue, 20 Dec 2016 20:07:08 +0000 Subject: [PATCH] match newlib ensure max filename length matches newlib --- source/directory.c | 34 +++++++++++++++++----------------- source/directory.h | 4 ++-- source/fatdir.c | 8 ++++---- source/fatfile.c | 2 +- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/source/directory.c b/source/directory.c index ff849e2..53bc548 100644 --- a/source/directory.c +++ b/source/directory.c @@ -87,9 +87,9 @@ static int _FAT_directory_lfnLength (const char* name) { int ucsLength; 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 - if ( nameLength >= MAX_FILENAME_LENGTH) { + if ( nameLength >= NAME_MAX) { return -1; } // 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 (_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? return false; } @@ -405,7 +405,7 @@ bool _FAT_directory_getRootEntry (PARTITION* partition, DIR_ENTRY* entry) { entry->dataEnd = entry->dataStart; - memset (entry->filename, '\0', MAX_FILENAME_LENGTH); + memset (entry->filename, '\0', NAME_MAX); entry->filename[0] = '.'; memset (entry->entryData, 0, DIR_ENTRY_DATA_SIZE); @@ -474,7 +474,7 @@ bool _FAT_directory_entryFromPosition (PARTITION* partition, DIR_ENTRY* entry) { int lfnPos; 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 for ( entryStillValid = true, finished = false; @@ -516,7 +516,7 @@ bool _FAT_directory_entryFromPosition (PARTITION* partition, DIR_ENTRY* entry) { } } else { // 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; } } @@ -571,7 +571,7 @@ bool _FAT_directory_entryFromPath (PARTITION* partition, DIR_ENTRY* entry, const dirnameLength = strlen(pathPosition); } - if (dirnameLength > MAX_FILENAME_LENGTH) { + if (dirnameLength > NAME_MAX) { // The path is too long to bother with 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 // 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)) { found = true; } @@ -758,9 +758,9 @@ static bool _FAT_directory_entryExists (PARTITION* partition, const char* name, char alias[MAX_ALIAS_LENGTH]; 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; } @@ -769,7 +769,7 @@ static bool _FAT_directory_entryExists (PARTITION* partition, const char* name, while (foundFile) { // It hasn't already found the file // 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)) { return true; } @@ -808,7 +808,7 @@ static int _FAT_directory_createAlias (char* alias, const char* lfn) { // Primary portion of alias 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) { return -1; } @@ -855,7 +855,7 @@ static int _FAT_directory_createAlias (char* alias, const char* lfn) { aliasPos++; memset (&ps, 0, sizeof(ps)); 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) { return -1; } @@ -923,7 +923,7 @@ bool _FAT_directory_addEntry (PARTITION* partition, DIR_ENTRY* entry, uint32_t d #endif // 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; } @@ -935,7 +935,7 @@ bool _FAT_directory_addEntry (PARTITION* partition, DIR_ENTRY* entry, uint32_t d // Remove junk in 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 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 memset (entry->entryData, ' ', 11); - if ( strncmp(entry->filename, ".", MAX_FILENAME_LENGTH) == 0) { + if ( strncmp(entry->filename, ".", NAME_MAX) == 0) { // "." entry entry->entryData[0] = '.'; entrySize = 1; - } else if ( strncmp(entry->filename, "..", MAX_FILENAME_LENGTH) == 0) { + } else if ( strncmp(entry->filename, "..", NAME_MAX) == 0) { // ".." entry entry->entryData[0] = '.'; entry->entryData[1] = '.'; diff --git a/source/directory.h b/source/directory.h index ac66c78..9f7af8e 100644 --- a/source/directory.h +++ b/source/directory.h @@ -31,13 +31,13 @@ #define _DIRECTORY_H #include +#include #include "common.h" #include "partition.h" #define DIR_ENTRY_DATA_SIZE 0x20 #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 LFN_ENTRY_LENGTH 13 #define ALIAS_ENTRY_LENGTH 11 @@ -72,7 +72,7 @@ typedef struct { 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 dataEnd; // Always points to the file/directory's alias entry - char filename[MAX_FILENAME_LENGTH]; + char filename[NAME_MAX]; } DIR_ENTRY; // Directory entry offsets diff --git a/source/fatdir.c b/source/fatdir.c index fe0e781..3a1f58d 100644 --- a/source/fatdir.c +++ b/source/fatdir.c @@ -292,7 +292,7 @@ int _FAT_rename_r (struct _reent *r, const char *oldName, const char *newName) { memcpy (&newDirEntry, &oldDirEntry, sizeof(DIR_ENTRY)); // Set the new name - strncpy (newDirEntry.filename, pathEnd, MAX_FILENAME_LENGTH - 1); + strncpy (newDirEntry.filename, pathEnd, NAME_MAX - 1); // Write the new entry 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; } // 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); // 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 */ | (partition->readOnly ? ST_RDONLY /* Read only file system */ : 0 ) ; // Maximum filename length. - buf->f_namemax = MAX_FILENAME_LENGTH; + buf->f_namemax = NAME_MAX; _FAT_unlock(&partition->lock); return 0; @@ -593,7 +593,7 @@ int _FAT_dirnext_r (struct _reent *r, DIR_ITER *dirState, char *filename, struct } // Get the filename - strncpy (filename, state->currentEntry.filename, MAX_FILENAME_LENGTH); + strncpy (filename, state->currentEntry.filename, NAME_MAX); // Get the stats, if requested if (filestat != NULL) { _FAT_directory_entryStat (state->partition, &(state->currentEntry), filestat); diff --git a/source/fatfile.c b/source/fatfile.c index eeef416..9f2b97a 100644 --- a/source/fatfile.c +++ b/source/fatfile.c @@ -226,7 +226,7 @@ int _FAT_open_r (struct _reent *r, void *fileStruct, const char *path, int flags pathEnd += 1; } // 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); // Set the creation time and date