Skip to content

Commit 9c67348

Browse files
author
Semphris
committed
Expand support of POSIX temp folders
1 parent 23ab963 commit 9c67348

File tree

4 files changed

+154
-62
lines changed

4 files changed

+154
-62
lines changed

CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,9 @@ if(ANDROID)
12821282
sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_sysfsops.c")
12831283
set(HAVE_SDL_FSOPS TRUE)
12841284

1285+
sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_posix_tmpfs.c")
1286+
set(HAVE_SDL_TMPFS TRUE)
1287+
12851288
if(SDL_HAPTIC)
12861289
set(SDL_HAPTIC_ANDROID 1)
12871290
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/haptic/android/*.c")
@@ -1452,6 +1455,9 @@ elseif(EMSCRIPTEN)
14521455
sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_sysfsops.c")
14531456
set(HAVE_SDL_FSOPS TRUE)
14541457

1458+
sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_posix_tmpfs.c")
1459+
set(HAVE_SDL_TMPFS TRUE)
1460+
14551461
if(SDL_CAMERA)
14561462
set(SDL_CAMERA_DRIVER_EMSCRIPTEN 1)
14571463
set(HAVE_CAMERA TRUE)
@@ -1786,6 +1792,9 @@ elseif(UNIX AND NOT APPLE AND NOT RISCOS AND NOT HAIKU)
17861792
sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_sysfsops.c")
17871793
set(HAVE_SDL_FSOPS TRUE)
17881794

1795+
sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_posix_tmpfs.c")
1796+
set(HAVE_SDL_TMPFS TRUE)
1797+
17891798
set(SDL_TIME_UNIX 1)
17901799
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/time/unix/*.c")
17911800
set(HAVE_SDL_TIME TRUE)
@@ -1982,6 +1991,7 @@ elseif(WINDOWS)
19821991
set(SDL_FILESYSTEM_WINDOWS 1)
19831992
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/filesystem/windows/*.c")
19841993
set(HAVE_SDL_FILESYSTEM TRUE)
1994+
set(HAVE_SDL_TMPFS TRUE) # SDL_tmpfs is included in the windows sources
19851995

19861996
set(SDL_FSOPS_WINDOWS 1)
19871997
set(HAVE_SDL_FSOPS TRUE)
@@ -2234,6 +2244,9 @@ elseif(APPLE)
22342244
sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_sysfsops.c")
22352245
set(HAVE_SDL_FSOPS TRUE)
22362246

2247+
sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_posix_tmpfs.c")
2248+
set(HAVE_SDL_TMPFS TRUE)
2249+
22372250
if(SDL_SENSOR)
22382251
if(IOS OR VISIONOS OR WATCHOS)
22392252
set(SDL_SENSOR_COREMOTION 1)
@@ -2437,6 +2450,9 @@ elseif(HAIKU)
24372450
sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_sysfsops.c")
24382451
set(HAVE_SDL_FSOPS TRUE)
24392452

2453+
sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_posix_tmpfs.c")
2454+
set(HAVE_SDL_TMPFS TRUE)
2455+
24402456
set(SDL_TIME_UNIX 1)
24412457
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/time/unix/*.c")
24422458
set(HAVE_SDL_TIME TRUE)
@@ -2477,6 +2493,9 @@ elseif(RISCOS)
24772493
sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_sysfsops.c")
24782494
set(HAVE_SDL_FSOPS TRUE)
24792495

2496+
sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/posix/SDL_posix_tmpfs.c")
2497+
set(HAVE_SDL_TMPFS TRUE)
2498+
24802499
set(SDL_TIME_UNIX 1)
24812500
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/time/unix/*.c")
24822501
set(HAVE_SDL_TIME TRUE)
@@ -2986,6 +3005,10 @@ if(NOT HAVE_SDL_FSOPS)
29863005
set(SDL_FSOPS_DUMMY 1)
29873006
sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/dummy/SDL_sysfsops.c")
29883007
endif()
3008+
if (NOT HAVE_SDL_TMPFS)
3009+
set(SDL_TMPFS_DUMMY 1)
3010+
sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/dummy/SDL_dummy_tmpfs.c")
3011+
endif()
29893012
if(NOT HAVE_SDL_LOCALE)
29903013
set(SDL_LOCALE_DUMMY 1)
29913014
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/locale/dummy/*.c")
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Simple DirectMedia Layer
3+
Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
4+
5+
This software is provided 'as-is', without any express or implied
6+
warranty. In no event will the authors be held liable for any damages
7+
arising from the use of this software.
8+
9+
Permission is granted to anyone to use this software for any purpose,
10+
including commercial applications, and to alter it and redistribute it
11+
freely, subject to the following restrictions:
12+
13+
1. The origin of this software must not be misrepresented; you must not
14+
claim that you wrote the original software. If you use this software
15+
in a product, an acknowledgment in the product documentation would be
16+
appreciated but is not required.
17+
2. Altered source versions must be plainly marked as such, and must not be
18+
misrepresented as being the original software.
19+
3. This notice may not be removed or altered from any source distribution.
20+
*/
21+
22+
#include "SDL_internal.h"
23+
#include "../SDL_sysfilesystem.h"
24+
25+
SDL_IOStream *SDL_SYS_CreateSafeTempFile(void)
26+
{
27+
SDL_Unsupported();
28+
return NULL;
29+
}
30+
31+
char *SDL_SYS_CreateUnsafeTempFile(void)
32+
{
33+
SDL_Unsupported();
34+
return NULL;
35+
}
36+
37+
char *SDL_SYS_CreateTempFolder(void)
38+
{
39+
SDL_Unsupported();
40+
return NULL;
41+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Simple DirectMedia Layer
3+
Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
4+
5+
This software is provided 'as-is', without any express or implied
6+
warranty. In no event will the authors be held liable for any damages
7+
arising from the use of this software.
8+
9+
Permission is granted to anyone to use this software for any purpose,
10+
including commercial applications, and to alter it and redistribute it
11+
freely, subject to the following restrictions:
12+
13+
1. The origin of this software must not be misrepresented; you must not
14+
claim that you wrote the original software. If you use this software
15+
in a product, an acknowledgment in the product documentation would be
16+
appreciated but is not required.
17+
2. Altered source versions must be plainly marked as such, and must not be
18+
misrepresented as being the original software.
19+
3. This notice may not be removed or altered from any source distribution.
20+
*/
21+
22+
#include "SDL_internal.h"
23+
#include "../SDL_sysfilesystem.h"
24+
#include "../../file/SDL_iostream_c.h"
25+
26+
#include <unistd.h>
27+
#include <string.h>
28+
#include <errno.h>
29+
30+
SDL_IOStream *SDL_SYS_CreateSafeTempFile(void)
31+
{
32+
FILE *file = tmpfile();
33+
34+
if (!file) {
35+
SDL_SetError("Could not tmpfile(): %s", strerror(errno));
36+
return NULL;
37+
}
38+
39+
return SDL_IOFromFP(file, true);
40+
}
41+
42+
char *SDL_SYS_CreateUnsafeTempFile(void)
43+
{
44+
/* TODO: Check for possible alternatives to /tmp, like $TMP */
45+
char template[] = "/tmp/tmp.XXXXXX";
46+
47+
char *file = SDL_strdup(template);
48+
49+
if (!file) {
50+
return NULL;
51+
}
52+
53+
int fd = mkstemp(file);
54+
55+
if (fd < 0) {
56+
SDL_free(file);
57+
SDL_SetError("Could not mkstemp(): %s", strerror(errno));
58+
return NULL;
59+
}
60+
61+
/* Normal usage of mkstemp() would use the file descriptor rather than the
62+
path, to avoid issues. In this function, security is assumed to be
63+
unimportant, so no need to worry about it. */
64+
/* See https://stackoverflow.com/questions/27680807/mkstemp-is-it-safe-to-close-descriptor-and-reopen-it-again */
65+
close(fd);
66+
67+
return file;
68+
}
69+
70+
char *SDL_SYS_CreateTempFolder(void)
71+
{
72+
/* TODO: Check for possible alternatives to /tmp, like $TMP */
73+
char template[] = "/tmp/tmp.XXXXXX";
74+
75+
char *folder = SDL_strdup(template);
76+
77+
if (!folder) {
78+
return NULL;
79+
}
80+
81+
char *res = mkdtemp(folder);
82+
83+
if (!res) {
84+
SDL_free(folder);
85+
SDL_SetError("Could not mkdtemp(): %s", strerror(errno));
86+
return NULL;
87+
}
88+
89+
return folder;
90+
}

src/filesystem/unix/SDL_sysfilesystem.c

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -617,66 +617,4 @@ char *SDL_SYS_GetUserFolder(SDL_Folder folder)
617617
return result;
618618
}
619619

620-
SDL_IOStream *SDL_SYS_CreateSafeTempFile(void)
621-
{
622-
FILE *file = tmpfile();
623-
624-
if (!file) {
625-
SDL_SetError("Could not tmpfile(): %s", strerror(errno));
626-
return NULL;
627-
}
628-
629-
return SDL_IOFromFP(file, true);
630-
}
631-
632-
char *SDL_SYS_CreateUnsafeTempFile(void)
633-
{
634-
/* TODO: Check for possible alternatives to /tmp, like $TMP */
635-
char template[] = "/tmp/tmp.XXXXXX";
636-
637-
char *file = SDL_strdup(template);
638-
639-
if (!file) {
640-
return NULL;
641-
}
642-
643-
int fd = mkstemp(file);
644-
645-
if (fd < 0) {
646-
SDL_free(file);
647-
SDL_SetError("Could not mkstemp(): %s", strerror(errno));
648-
return NULL;
649-
}
650-
651-
/* Normal usage of mkstemp() would use the file descriptor rather than the
652-
path, to avoid issues. In this function, security is assumed to be
653-
unimportant, so no need to worry about it. */
654-
/* See https://stackoverflow.com/questions/27680807/mkstemp-is-it-safe-to-close-descriptor-and-reopen-it-again */
655-
close(fd);
656-
657-
return file;
658-
}
659-
660-
char *SDL_SYS_CreateTempFolder(void)
661-
{
662-
/* TODO: Check for possible alternatives to /tmp, like $TMP */
663-
char template[] = "/tmp/tmp.XXXXXX";
664-
665-
char *folder = SDL_strdup(template);
666-
667-
if (!folder) {
668-
return NULL;
669-
}
670-
671-
char *res = mkdtemp(folder);
672-
673-
if (!res) {
674-
SDL_free(folder);
675-
SDL_SetError("Could not mkdtemp(): %s", strerror(errno));
676-
return NULL;
677-
}
678-
679-
return folder;
680-
}
681-
682620
#endif // SDL_FILESYSTEM_UNIX

0 commit comments

Comments
 (0)