Skip to content

Commit a3b0403

Browse files
committed
filesystem: Fix SDL_GlobDirectory on iOS and Android.
It SDL_SYS_EnumerateDirectory was changing the path string and passing it to the callback, causing chaos in the glob handler, which expected the original string to pass through. Fixes libsdl-org#15057.
1 parent c9591c5 commit a3b0403

1 file changed

Lines changed: 20 additions & 22 deletions

File tree

src/filesystem/posix/SDL_sysfsops.c

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,46 +39,44 @@
3939
#include "../../core/android/SDL_android.h"
4040
#endif
4141

42+
4243
bool SDL_SYS_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback cb, void *userdata)
4344
{
44-
#ifdef SDL_PLATFORM_ANDROID
45-
if (*path != '/') {
46-
char *apath = NULL;
47-
SDL_asprintf(&apath, "%s/%s", SDL_GetAndroidInternalStoragePath(), path);
48-
if (!apath) {
49-
return false;
50-
}
51-
const bool retval = SDL_SYS_EnumerateDirectory(apath, cb, userdata);
52-
SDL_free(apath);
53-
if (retval) {
54-
return true;
55-
}
56-
}
57-
#endif
45+
char *apath = NULL; // absolute path (for Android, iOS, etc). Overrides `path`.
5846

59-
#ifdef SDL_PLATFORM_IOS
47+
#if defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_IOS)
6048
if (*path != '/') {
49+
#ifdef SDL_PLATFORM_ANDROID
50+
SDL_asprintf(&apath, "%s/%s", SDL_GetAndroidInternalStoragePath(), path);
51+
#elif defined(SDL_PLATFORM_IOS)
6152
char *base = SDL_GetPrefPath("", "");
6253
if (!base) {
6354
return false;
6455
}
6556

66-
char *apath = NULL;
6757
SDL_asprintf(&apath, "%s%s", base, path);
6858
SDL_free(base);
59+
#endif
60+
6961
if (!apath) {
7062
return false;
7163
}
72-
const bool retval = SDL_SYS_EnumerateDirectory(apath, cb, userdata);
73-
SDL_free(apath);
74-
if (retval) {
75-
return true;
64+
}
65+
#elif 0 // this is just for testing that `apath` works when you aren't on iOS or Android.
66+
if (*path != '/') {
67+
char *c = SDL_SYS_GetCurrentDirectory();
68+
SDL_asprintf(&apath, "%s%s", c, path);
69+
SDL_free(c);
70+
if (!apath) {
71+
return false;
7672
}
7773
}
7874
#endif
7975

8076
char *pathwithsep = NULL;
81-
int pathwithseplen = SDL_asprintf(&pathwithsep, "%s/", path);
77+
int pathwithseplen = SDL_asprintf(&pathwithsep, "%s/", apath ? apath : path);
78+
const size_t extralen = apath ? (SDL_strlen(apath) - SDL_strlen(path)) : 0;
79+
SDL_free(apath);
8280
if ((pathwithseplen == -1) || (!pathwithsep)) {
8381
return false;
8482
}
@@ -112,7 +110,7 @@ bool SDL_SYS_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback
112110
if ((SDL_strcmp(name, ".") == 0) || (SDL_strcmp(name, "..") == 0)) {
113111
continue;
114112
}
115-
result = cb(userdata, pathwithsep, name);
113+
result = cb(userdata, pathwithsep + extralen, name);
116114
}
117115

118116
closedir(dir);

0 commit comments

Comments
 (0)