From f5086a4cf9630ccf817df5b21c1f4dd349910d82 Mon Sep 17 00:00:00 2001
From: Christoph Schied <[EMAIL REDACTED]>
Date: Fri, 9 Oct 2026 13:54:50 +0200
Subject: [PATCH] filesystem: fix directory-exists check for iOS/Android
For relative paths, on iOS and Android the original code constructed
`apath' which was then passed to mkdir. When handling the `EEXIST'
return code from mkdir, the code checks whether path refers to a
directory (success). However, this misses the remapping for relative
paths, thus stat() tests a different directory in those cases.
This commit keeps apath alive for the call to stat().
---
src/filesystem/posix/SDL_sysfsops.c | 30 +++++++++++------------------
1 file changed, 11 insertions(+), 19 deletions(-)
diff --git a/src/filesystem/posix/SDL_sysfsops.c b/src/filesystem/posix/SDL_sysfsops.c
index b473dfbfc7f8a..f809ea3162e13 100644
--- a/src/filesystem/posix/SDL_sysfsops.c
+++ b/src/filesystem/posix/SDL_sysfsops.c
@@ -301,50 +301,42 @@ bool SDL_SYS_CopyFile(const char *oldpath, const char *newpath)
bool SDL_SYS_CreateDirectory(const char *path)
{
int rc;
+ const char *mkdir_path = path;
+ char *apath = NULL;
+#if defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_IOS)
+ if (*path != '/') {
#ifdef SDL_PLATFORM_ANDROID
- if (*path == '/') {
- rc = mkdir(path, 0770);
- } else {
- char *apath = NULL;
SDL_asprintf(&apath, "%s/%s", SDL_GetAndroidInternalStoragePath(), path);
- if (!apath) {
- return false;
- }
- rc = mkdir(apath, 0770);
- SDL_free(apath);
- }
#elif defined(SDL_PLATFORM_IOS)
- if (*path == '/') {
- rc = mkdir(path, 0770);
- } else {
char *base = SDL_GetPrefPath("", "");
if (!base) {
return false;
}
- char *apath = NULL;
SDL_asprintf(&apath, "%s%s", base, path);
SDL_free(base);
+#endif
if (!apath) {
return false;
}
- rc = mkdir(apath, 0770);
- SDL_free(apath);
+ mkdir_path = apath;
}
-#else
- rc = mkdir(path, 0770);
#endif
+ rc = mkdir(mkdir_path, 0770);
if (rc < 0) {
const int origerrno = errno;
if (origerrno == EEXIST) {
struct stat statbuf;
- if ((stat(path, &statbuf) == 0) && (S_ISDIR(statbuf.st_mode))) {
+ if ((stat(mkdir_path, &statbuf) == 0) && (S_ISDIR(statbuf.st_mode))) {
+ SDL_free(apath);
return true; // it already exists and it's a directory, consider it success.
}
}
+ SDL_free(apath);
return SDL_SetError("Can't create directory: %s", strerror(origerrno));
}
+ SDL_free(apath);
return true;
}