Skip to content

Commit

Permalink
Expand support of POSIX temp folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Semphris committed Oct 14, 2024
1 parent 23ab963 commit 9c67348
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 62 deletions.
23 changes: 23 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
41 changes: 41 additions & 0 deletions src/filesystem/dummy/SDL_dummy_tmpfs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
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;
}
90 changes: 90 additions & 0 deletions src/filesystem/posix/SDL_posix_tmpfs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
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 <unistd.h>
#include <string.h>
#include <errno.h>

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;
}
62 changes: 0 additions & 62 deletions src/filesystem/unix/SDL_sysfilesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9c67348

Please sign in to comment.