@samitouri / QOSamiQemu / commits / 1599015a7a

glib-compat: add fallback for g_clear_fd/g_autofd

Those helpers were added in glib 2.76. Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Reviewed-by: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-ID: <20260623-b4-ui-v4-7-4656aec3398d@redhat.com>

Marc-André Lureau committed Jun 23, 2026 at 11:44 UTC 1599015a7a1a76426981187091034a8ab38f8eb5
1 file changed +29
include/glib-compat.h
+29
@@ -30,6 +30,7 @@
30 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
31
32 #include <glib.h>
33 +#include <glib/gstdio.h>
34 #if defined(G_OS_UNIX)
35 #include <glib-unix.h>
36 #include <sys/types.h>
@@ -129,6 +130,34 @@ qemu_g_test_slow(void)
130 #define g_test_thorough() qemu_g_test_slow()
131 #define g_test_quick() (!qemu_g_test_slow())
132
133 +static inline gboolean g_clear_fd_qemu(int *fd_ptr, GError **error)
134 +{
135 +#if GLIB_CHECK_VERSION(2, 76, 0)
136 + return g_clear_fd(fd_ptr, error);
137 +#else
138 + int fd = *fd_ptr;
139 +
140 + *fd_ptr = -1;
141 +
142 + if (fd < 0) {
143 + return TRUE;
144 + }
145 +
146 + return g_close(fd, error);
147 +#endif
148 +}
149 +#define g_clear_fd(fd, err) g_clear_fd_qemu(fd, err)
150 +
151 +#if !GLIB_CHECK_VERSION(2, 76, 0)
152 +static inline void _g_clear_fd_ignore_error(int *fd_ptr)
153 +{
154 + int errsv = errno;
155 + g_clear_fd(fd_ptr, NULL);
156 + errno = errsv;
157 +}
158 +#define g_autofd __attribute__((cleanup(_g_clear_fd_ignore_error)))
159 +#endif
160 +
161 #pragma GCC diagnostic pop
162
163 #ifndef G_NORETURN