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

Commit af85943

Browse files
committed
Allow passing a path with a trailing slash to mkdir
Some applications invoke mkdir() with a path that ends with a slash, and expect that if the last component of the path does not exist, it will be created. This indeed is the behaviour under Linux.
1 parent 69543f0 commit af85943

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

source/fatdir.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ int _FAT_mkdir_r (struct _reent *r, const char *path, int mode) {
338338
bool fileExists;
339339
DIR_ENTRY dirEntry;
340340
const char* pathEnd;
341+
const char* trailingSlash;
341342
uint32_t parentCluster, dirCluster;
342343
uint8_t newEntryData[DIR_ENTRY_DATA_SIZE];
343344

@@ -376,8 +377,12 @@ int _FAT_mkdir_r (struct _reent *r, const char *path, int mode) {
376377
}
377378

378379
// Get the directory it has to go in
379-
pathEnd = strrchr (path, DIR_SEPARATOR);
380-
if (pathEnd == NULL) {
380+
pathEnd = path + strlen (path) - 1;
381+
trailingSlash = *pathEnd == DIR_SEPARATOR ? pathEnd : NULL;
382+
if (trailingSlash) pathEnd--;
383+
while (pathEnd > path && *pathEnd != DIR_SEPARATOR)
384+
--pathEnd;
385+
if (pathEnd == path) {
381386
// No path was specified
382387
parentCluster = partition->cwdCluster;
383388
pathEnd = path;
@@ -396,6 +401,10 @@ int _FAT_mkdir_r (struct _reent *r, const char *path, int mode) {
396401
}
397402
// Create the entry data
398403
strncpy (dirEntry.filename, pathEnd, NAME_MAX - 1);
404+
if (trailingSlash) {
405+
// Remove the trailing slash
406+
dirEntry.filename[trailingSlash - pathEnd] = '\0';
407+
}
399408
memset (dirEntry.entryData, 0, DIR_ENTRY_DATA_SIZE);
400409

401410
// Set the creation time and date

0 commit comments

Comments
 (0)