summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon McVittie <smcv@collabora.com>2022-11-08 11:05:12 +0000
committerSimon McVittie <smcv@collabora.com>2022-11-08 11:17:41 +0000
commitea18312ed03e0077740e327966a8e0e5810d7f5b (patch)
tree25f3c4e0e0dd4244b87ddcff3715e465287f5a79
parent4e44fd9c174e4196a86fb6d954722feaff612c88 (diff)
downloadlibglnx-ea18312ed03e0077740e327966a8e0e5810d7f5b.tar.gz
backports: Add g_steal_fd, from GLib >= 2.70
This is essentially the same as glnx_steal_fd, so make glnx_steal_fd an alias for it. The unit test is taken from GLib, slightly modified to avoid g_close() and also test the old name glnx_steal_fd(). Signed-off-by: Simon McVittie <smcv@collabora.com>
-rw-r--r--glnx-backports.h11
-rw-r--r--glnx-dirfd.c4
-rw-r--r--glnx-fdio.c6
-rw-r--r--glnx-local-alloc.h15
-rw-r--r--glnx-lockfile.c2
-rw-r--r--tests/test-libglnx-backports.c39
6 files changed, 62 insertions, 15 deletions
diff --git a/glnx-backports.h b/glnx-backports.h
index f99f08f..ad70ed9 100644
--- a/glnx-backports.h
+++ b/glnx-backports.h
@@ -118,6 +118,17 @@ _glnx_memdup2 (gconstpointer mem,
(((a) > (b) ? (a) - (b) : (b) - (a)) < (epsilon))
#endif
+#if !GLIB_CHECK_VERSION(2, 70, 0)
+#define g_steal_fd _glnx_steal_fd
+static inline int
+_glnx_steal_fd (int *fdp)
+{
+ int fd = *fdp;
+ *fdp = -1;
+ return fd;
+}
+#endif
+
#if !GLIB_CHECK_VERSION(2, 74, 0)
#define G_APPLICATION_DEFAULT_FLAGS ((GApplicationFlags) 0)
#define G_CONNECT_DEFAULT ((GConnectFlags) 0)
diff --git a/glnx-dirfd.c b/glnx-dirfd.c
index b039c41..b78e2df 100644
--- a/glnx-dirfd.c
+++ b/glnx-dirfd.c
@@ -129,7 +129,7 @@ glnx_dirfd_iterator_init_take_fd (int *dfd,
if (!d)
return glnx_throw_errno_prefix (error, "fdopendir");
- real_dfd_iter->fd = glnx_steal_fd (dfd);
+ real_dfd_iter->fd = g_steal_fd (dfd);
real_dfd_iter->d = d;
real_dfd_iter->initialized = TRUE;
@@ -349,7 +349,7 @@ glnx_mkdtempat (int dfd, const char *tmpl, int mode,
/* Return the initialized directory struct */
out_tmpdir->initialized = TRUE;
out_tmpdir->src_dfd = dfd; /* referenced; see above docs */
- out_tmpdir->fd = glnx_steal_fd (&ret_dfd);
+ out_tmpdir->fd = g_steal_fd (&ret_dfd);
out_tmpdir->path = g_steal_pointer (&path);
return TRUE;
}
diff --git a/glnx-fdio.c b/glnx-fdio.c
index b1af679..9873205 100644
--- a/glnx-fdio.c
+++ b/glnx-fdio.c
@@ -218,7 +218,7 @@ open_tmpfile_core (int dfd, const char *subpath,
return glnx_throw_errno_prefix (error, "fchmod");
out_tmpf->initialized = TRUE;
out_tmpf->src_dfd = dfd; /* Copied; caller must keep open */
- out_tmpf->fd = glnx_steal_fd (&fd);
+ out_tmpf->fd = g_steal_fd (&fd);
out_tmpf->path = NULL;
return TRUE;
}
@@ -245,7 +245,7 @@ open_tmpfile_core (int dfd, const char *subpath,
{
out_tmpf->initialized = TRUE;
out_tmpf->src_dfd = dfd; /* Copied; caller must keep open */
- out_tmpf->fd = glnx_steal_fd (&fd);
+ out_tmpf->fd = g_steal_fd (&fd);
out_tmpf->path = g_steal_pointer (&tmp);
return TRUE;
}
@@ -463,7 +463,7 @@ glnx_tmpfile_reopen_rdonly (GLnxTmpfile *tmpf,
}
glnx_close_fd (&tmpf->fd);
- tmpf->fd = glnx_steal_fd (&rdonly_fd);
+ tmpf->fd = g_steal_fd (&rdonly_fd);
return TRUE;
}
diff --git a/glnx-local-alloc.h b/glnx-local-alloc.h
index 615b954..65ae747 100644
--- a/glnx-local-alloc.h
+++ b/glnx-local-alloc.h
@@ -24,6 +24,8 @@
#include <gio/gio.h>
#include <errno.h>
+#include "glnx-backports.h"
+
G_BEGIN_DECLS
/**
@@ -43,19 +45,14 @@ glnx_local_obj_unref (void *v)
}
#define glnx_unref_object __attribute__ ((cleanup(glnx_local_obj_unref)))
-static inline int
-glnx_steal_fd (int *fdp)
-{
- int fd = *fdp;
- *fdp = -1;
- return fd;
-}
+/* Backwards-compat with older libglnx */
+#define glnx_steal_fd g_steal_fd
/**
* glnx_close_fd:
* @fdp: Pointer to fd
*
- * Effectively `close (glnx_steal_fd (&fd))`. Also
+ * Effectively `close (g_steal_fd (&fd))`. Also
* asserts that `close()` did not raise `EBADF` - encountering
* that error is usually a critical bug in the program.
*/
@@ -66,7 +63,7 @@ glnx_close_fd (int *fdp)
g_assert (fdp);
- int fd = glnx_steal_fd (fdp);
+ int fd = g_steal_fd (fdp);
if (fd >= 0)
{
errsv = errno;
diff --git a/glnx-lockfile.c b/glnx-lockfile.c
index 65a1558..fcda84c 100644
--- a/glnx-lockfile.c
+++ b/glnx-lockfile.c
@@ -129,7 +129,7 @@ glnx_make_lock_file(int dfd, const char *p, int operation, GLnxLockFile *out_loc
out_lock->initialized = TRUE;
out_lock->dfd = dfd;
out_lock->path = g_steal_pointer (&t);
- out_lock->fd = glnx_steal_fd (&fd);
+ out_lock->fd = g_steal_fd (&fd);
out_lock->operation = operation;
return TRUE;
}
diff --git a/tests/test-libglnx-backports.c b/tests/test-libglnx-backports.c
index c475cd4..0fa374e 100644
--- a/tests/test-libglnx-backports.c
+++ b/tests/test-libglnx-backports.c
@@ -2,12 +2,15 @@
*
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
* Copyright 2019 Emmanuel Fleury
+ * Copyright 2021 Collabora Ltd.
* SPDX-License-Identifier: LGPL-2.1-or-later AND LicenseRef-old-glib-tests
*/
#include "libglnx-config.h"
#include "libglnx.h"
+#include <glib/gstdio.h>
+
/* Testing g_memdup2() function with various positive and negative cases */
static void
test_memdup2 (void)
@@ -28,9 +31,45 @@ test_memdup2 (void)
g_free (str_dup);
}
+static void
+test_steal_fd (void)
+{
+ GError *error = NULL;
+ gchar *tmpfile = NULL;
+ int fd = -42;
+ int borrowed;
+ int stolen;
+
+ g_assert_cmpint (g_steal_fd (&fd), ==, -42);
+ g_assert_cmpint (fd, ==, -1);
+ g_assert_cmpint (g_steal_fd (&fd), ==, -1);
+ g_assert_cmpint (fd, ==, -1);
+
+ fd = g_file_open_tmp (NULL, &tmpfile, &error);
+ g_assert_cmpint (fd, >=, 0);
+ g_assert_no_error (error);
+ borrowed = fd;
+ stolen = g_steal_fd (&fd);
+ g_assert_cmpint (fd, ==, -1);
+ g_assert_cmpint (borrowed, ==, stolen);
+
+ g_assert_no_errno (close (g_steal_fd (&stolen)));
+ g_assert_cmpint (stolen, ==, -1);
+
+ g_assert_no_errno (remove (tmpfile));
+ g_free (tmpfile);
+
+ /* Backwards compatibility with older libglnx: glnx_steal_fd is the same
+ * as g_steal_fd */
+ fd = -23;
+ g_assert_cmpint (glnx_steal_fd (&fd), ==, -23);
+ g_assert_cmpint (fd, ==, -1);
+}
+
int main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
+ g_test_add_func ("/mainloop/steal-fd", test_steal_fd);
g_test_add_func ("/strfuncs/memdup2", test_memdup2);
return g_test_run();
}