gstdio: Add support for the e flag (O_CLOEXEC) to g_fopen()

This adds cross-platform support for it: on glibc, musl and BSD’s libc,
the flag is natively supported. On Windows, convert it to the `N` flag,
which similarly indicates that an open file shouldn’t be inherited by
child processes.

This allows us to unconditionally pass `e` to `g_fopen()` so `O_CLOEXEC`
can easily be set on its FDs.

Also do the same for `g_freopen()`, since it shares the same underlying
mode handling code.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
This commit is contained in:
Philip Withnall
2025-03-20 12:27:21 +00:00
parent c108dc09b9
commit f9a7ac11f5
18 changed files with 57 additions and 33 deletions

View File

@@ -792,7 +792,7 @@ test_internal_enhanced_stdio (void)
g_remove (ps);
f = g_fopen (ps, "wb");
f = g_fopen (ps, "wbe");
g_assert_nonnull (f);
h = (HANDLE) _get_osfhandle (fileno (f));
@@ -875,7 +875,7 @@ test_internal_enhanced_stdio (void)
g_assert_true (SystemTimeToFileTime (&st, &ft));
f = g_fopen (p0, "w");
f = g_fopen (p0, "we");
g_assert_nonnull (f);
h = (HANDLE) _get_osfhandle (fileno (f));
@@ -888,7 +888,7 @@ test_internal_enhanced_stdio (void)
fclose (f);
f = g_fopen (p1, "w");
f = g_fopen (p1, "we");
g_assert_nonnull (f);
fclose (f);

View File

@@ -23,6 +23,7 @@
*/
#include <glib/glib.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <errno.h>
#include <stdlib.h>
@@ -305,7 +306,7 @@ test_create_structure (gconstpointer test_data)
basename = g_path_get_basename (item.filename);
path = g_build_filename (test_data, dir, ".hidden", NULL);
f = fopen (path, "a");
f = g_fopen (path, "ae");
fprintf (f, "%s\n", basename);
fclose (f);