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

Commit 36e04aa

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 36e04aa

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

source/fatdir.c

Lines changed: 15 additions & 5 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* lastSlash;
341342
uint32_t parentCluster, dirCluster;
342343
uint8_t newEntryData[DIR_ENTRY_DATA_SIZE];
343344

@@ -376,11 +377,16 @@ 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+
pathEnd = NULL;
381+
lastSlash = strchr (path, DIR_SEPARATOR);
382+
while (lastSlash && lastSlash[1] != '\0') {
383+
pathEnd = lastSlash;
384+
lastSlash = strchr (pathEnd + 1, DIR_SEPARATOR);
385+
}
380386
if (pathEnd == NULL) {
381387
// No path was specified
382388
parentCluster = partition->cwdCluster;
383-
pathEnd = path;
389+
strncpy (dirEntry.filename, path, NAME_MAX - 1);
384390
} else {
385391
// Path was specified -- get the right parentCluster
386392
// Recycling dirEntry, since it needs to be recreated anyway
@@ -391,11 +397,15 @@ int _FAT_mkdir_r (struct _reent *r, const char *path, int mode) {
391397
return -1;
392398
}
393399
parentCluster = _FAT_directory_entryGetCluster (partition, dirEntry.entryData);
394-
// Move the pathEnd past the last DIR_SEPARATOR
395-
pathEnd += 1;
400+
401+
// Set the name -- remove the trailing '/', if present
402+
strncpy (dirEntry.filename, pathEnd + 1, NAME_MAX - 1);
403+
if (lastSlash && lastSlash != pathEnd) {
404+
// replace the last '/' with a null byte
405+
dirEntry.filename[lastSlash - pathEnd - 1] = '\0';
406+
}
396407
}
397408
// Create the entry data
398-
strncpy (dirEntry.filename, pathEnd, NAME_MAX - 1);
399409
memset (dirEntry.entryData, 0, DIR_ENTRY_DATA_SIZE);
400410

401411
// Set the creation time and date

0 commit comments

Comments
 (0)