From 9c6734877b9674d8c47dc0cc38f9fcb9c083a5aa Mon Sep 17 00:00:00 2001 From: Semphris Date: Sat, 12 Oct 2024 21:38:30 -0400 Subject: [PATCH] Expand support of POSIX temp folders --- CMakeLists.txt | 23 +++++++ src/filesystem/dummy/SDL_dummy_tmpfs.c | 41 +++++++++++ src/filesystem/posix/SDL_posix_tmpfs.c | 90 +++++++++++++++++++++++++ src/filesystem/unix/SDL_sysfilesystem.c | 62 ----------------- 4 files changed, 154 insertions(+), 62 deletions(-) create mode 100644 src/filesystem/dummy/SDL_dummy_tmpfs.c create mode 100644 src/filesystem/posix/SDL_posix_tmpfs.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 01cc974662c5e..e26a4f489d8e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1282,6 +1282,9 @@ if(ANDROID) sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_sysfsops.c") set(HAVE_SDL_FSOPS TRUE) + sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_posix_tmpfs.c") + set(HAVE_SDL_TMPFS TRUE) + if(SDL_HAPTIC) set(SDL_HAPTIC_ANDROID 1) sdl_glob_sources("${SDL3_SOURCE_DIR}/src/haptic/android/*.c") @@ -1452,6 +1455,9 @@ elseif(EMSCRIPTEN) sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_sysfsops.c") set(HAVE_SDL_FSOPS TRUE) + sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_posix_tmpfs.c") + set(HAVE_SDL_TMPFS TRUE) + if(SDL_CAMERA) set(SDL_CAMERA_DRIVER_EMSCRIPTEN 1) set(HAVE_CAMERA TRUE) @@ -1786,6 +1792,9 @@ elseif(UNIX AND NOT APPLE AND NOT RISCOS AND NOT HAIKU) sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_sysfsops.c") set(HAVE_SDL_FSOPS TRUE) + sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_posix_tmpfs.c") + set(HAVE_SDL_TMPFS TRUE) + set(SDL_TIME_UNIX 1) sdl_glob_sources("${SDL3_SOURCE_DIR}/src/time/unix/*.c") set(HAVE_SDL_TIME TRUE) @@ -1982,6 +1991,7 @@ elseif(WINDOWS) set(SDL_FILESYSTEM_WINDOWS 1) sdl_glob_sources("${SDL3_SOURCE_DIR}/src/filesystem/windows/*.c") set(HAVE_SDL_FILESYSTEM TRUE) + set(HAVE_SDL_TMPFS TRUE) # SDL_tmpfs is included in the windows sources set(SDL_FSOPS_WINDOWS 1) set(HAVE_SDL_FSOPS TRUE) @@ -2234,6 +2244,9 @@ elseif(APPLE) sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_sysfsops.c") set(HAVE_SDL_FSOPS TRUE) + sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_posix_tmpfs.c") + set(HAVE_SDL_TMPFS TRUE) + if(SDL_SENSOR) if(IOS OR VISIONOS OR WATCHOS) set(SDL_SENSOR_COREMOTION 1) @@ -2437,6 +2450,9 @@ elseif(HAIKU) sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_sysfsops.c") set(HAVE_SDL_FSOPS TRUE) + sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_posix_tmpfs.c") + set(HAVE_SDL_TMPFS TRUE) + set(SDL_TIME_UNIX 1) sdl_glob_sources("${SDL3_SOURCE_DIR}/src/time/unix/*.c") set(HAVE_SDL_TIME TRUE) @@ -2477,6 +2493,9 @@ elseif(RISCOS) sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_sysfsops.c") set(HAVE_SDL_FSOPS TRUE) + sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_posix_tmpfs.c") + set(HAVE_SDL_TMPFS TRUE) + set(SDL_TIME_UNIX 1) sdl_glob_sources("${SDL3_SOURCE_DIR}/src/time/unix/*.c") set(HAVE_SDL_TIME TRUE) @@ -2986,6 +3005,10 @@ if(NOT HAVE_SDL_FSOPS) set(SDL_FSOPS_DUMMY 1) sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/dummy/SDL_sysfsops.c") endif() +if (NOT HAVE_SDL_TMPFS) + set(SDL_TMPFS_DUMMY 1) + sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/dummy/SDL_dummy_tmpfs.c") +endif() if(NOT HAVE_SDL_LOCALE) set(SDL_LOCALE_DUMMY 1) sdl_glob_sources("${SDL3_SOURCE_DIR}/src/locale/dummy/*.c") diff --git a/src/filesystem/dummy/SDL_dummy_tmpfs.c b/src/filesystem/dummy/SDL_dummy_tmpfs.c new file mode 100644 index 0000000000000..ed72a1ecd283a --- /dev/null +++ b/src/filesystem/dummy/SDL_dummy_tmpfs.c @@ -0,0 +1,41 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2024 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "SDL_internal.h" +#include "../SDL_sysfilesystem.h" + +SDL_IOStream *SDL_SYS_CreateSafeTempFile(void) +{ + SDL_Unsupported(); + return NULL; +} + +char *SDL_SYS_CreateUnsafeTempFile(void) +{ + SDL_Unsupported(); + return NULL; +} + +char *SDL_SYS_CreateTempFolder(void) +{ + SDL_Unsupported(); + return NULL; +} diff --git a/src/filesystem/posix/SDL_posix_tmpfs.c b/src/filesystem/posix/SDL_posix_tmpfs.c new file mode 100644 index 0000000000000..56f343c2723d5 --- /dev/null +++ b/src/filesystem/posix/SDL_posix_tmpfs.c @@ -0,0 +1,90 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2024 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "SDL_internal.h" +#include "../SDL_sysfilesystem.h" +#include "../../file/SDL_iostream_c.h" + +#include +#include +#include + +SDL_IOStream *SDL_SYS_CreateSafeTempFile(void) +{ + FILE *file = tmpfile(); + + if (!file) { + SDL_SetError("Could not tmpfile(): %s", strerror(errno)); + return NULL; + } + + return SDL_IOFromFP(file, true); +} + +char *SDL_SYS_CreateUnsafeTempFile(void) +{ + /* TODO: Check for possible alternatives to /tmp, like $TMP */ + char template[] = "/tmp/tmp.XXXXXX"; + + char *file = SDL_strdup(template); + + if (!file) { + return NULL; + } + + int fd = mkstemp(file); + + if (fd < 0) { + SDL_free(file); + SDL_SetError("Could not mkstemp(): %s", strerror(errno)); + return NULL; + } + + /* Normal usage of mkstemp() would use the file descriptor rather than the + path, to avoid issues. In this function, security is assumed to be + unimportant, so no need to worry about it. */ + /* See https://stackoverflow.com/questions/27680807/mkstemp-is-it-safe-to-close-descriptor-and-reopen-it-again */ + close(fd); + + return file; +} + +char *SDL_SYS_CreateTempFolder(void) +{ + /* TODO: Check for possible alternatives to /tmp, like $TMP */ + char template[] = "/tmp/tmp.XXXXXX"; + + char *folder = SDL_strdup(template); + + if (!folder) { + return NULL; + } + + char *res = mkdtemp(folder); + + if (!res) { + SDL_free(folder); + SDL_SetError("Could not mkdtemp(): %s", strerror(errno)); + return NULL; + } + + return folder; +} diff --git a/src/filesystem/unix/SDL_sysfilesystem.c b/src/filesystem/unix/SDL_sysfilesystem.c index 79c579ffc6712..af51dfe45810e 100644 --- a/src/filesystem/unix/SDL_sysfilesystem.c +++ b/src/filesystem/unix/SDL_sysfilesystem.c @@ -617,66 +617,4 @@ char *SDL_SYS_GetUserFolder(SDL_Folder folder) return result; } -SDL_IOStream *SDL_SYS_CreateSafeTempFile(void) -{ - FILE *file = tmpfile(); - - if (!file) { - SDL_SetError("Could not tmpfile(): %s", strerror(errno)); - return NULL; - } - - return SDL_IOFromFP(file, true); -} - -char *SDL_SYS_CreateUnsafeTempFile(void) -{ - /* TODO: Check for possible alternatives to /tmp, like $TMP */ - char template[] = "/tmp/tmp.XXXXXX"; - - char *file = SDL_strdup(template); - - if (!file) { - return NULL; - } - - int fd = mkstemp(file); - - if (fd < 0) { - SDL_free(file); - SDL_SetError("Could not mkstemp(): %s", strerror(errno)); - return NULL; - } - - /* Normal usage of mkstemp() would use the file descriptor rather than the - path, to avoid issues. In this function, security is assumed to be - unimportant, so no need to worry about it. */ - /* See https://stackoverflow.com/questions/27680807/mkstemp-is-it-safe-to-close-descriptor-and-reopen-it-again */ - close(fd); - - return file; -} - -char *SDL_SYS_CreateTempFolder(void) -{ - /* TODO: Check for possible alternatives to /tmp, like $TMP */ - char template[] = "/tmp/tmp.XXXXXX"; - - char *folder = SDL_strdup(template); - - if (!folder) { - return NULL; - } - - char *res = mkdtemp(folder); - - if (!res) { - SDL_free(folder); - SDL_SetError("Could not mkdtemp(): %s", strerror(errno)); - return NULL; - } - - return folder; -} - #endif // SDL_FILESYSTEM_UNIX