Fixed filesystem operations on iOS

Full paths are used as-is, relative paths are prepended with a writable path, SDL_GetPrefPath("", ""), since the current directory isn't writable.
This commit is contained in:
Sam Lantinga
2025-09-22 10:05:53 -07:00
parent 7563a3e17d
commit 9f9952d53a
2 changed files with 140 additions and 1 deletions

View File

@@ -957,6 +957,39 @@ SDL_IOStream *SDL_IOFromFile(const char *file, const char *mode)
}
}
#elif defined(SDL_PLATFORM_IOS)
// Try to open the file on the filesystem first
FILE *fp = NULL;
if (*file == '/') {
fp = fopen(file, mode);
} else {
// We can't write to the current directory, so use a writable path
char *base = SDL_GetPrefPath("", "");
if (!base) {
return NULL;
}
char *path = NULL;
SDL_asprintf(&path, "%s%s", base, file);
SDL_free(base);
if (!path) {
return NULL;
}
fp = fopen(path, mode);
SDL_free(path);
}
if (!fp) {
SDL_SetError("Couldn't open %s: %s", file, strerror(errno));
} else if (!IsRegularFileOrPipe(fp)) {
fclose(fp);
SDL_SetError("%s is not a regular file or pipe", file);
} else {
iostr = SDL_IOFromFP(fp, true);
}
#elif defined(SDL_PLATFORM_WINDOWS)
HANDLE handle = windows_file_open(file, mode);
if (handle != INVALID_HANDLE_VALUE) {
@@ -975,7 +1008,6 @@ SDL_IOStream *SDL_IOFromFile(const char *file, const char *mode)
SDL_SetError("Couldn't open %s: %s", file, strerror(errno));
} else if (!IsRegularFileOrPipe(fp)) {
fclose(fp);
fp = NULL;
SDL_SetError("%s is not a regular file or pipe", file);
} else {
iostr = SDL_IOFromFP(fp, true);