From 9096c63b5deabeb4efb6d60582e61f0ac2672f35 Mon Sep 17 00:00:00 2001 From: Giorge Koulin Date: Fri, 31 Mar 2023 09:08:48 +0100 Subject: [PATCH] Build `nfd_portal.cpp` on older `glibc` and `dbus` APIs. is available as of [glibc 2.25](https://sourceware.org/legacy-ml/libc-alpha/2017-02/msg00079.html). Add fallback implementation using `syscall` for older versions, adapted from [btzy/nativefiledialog-extended@44e63d5](https://github.com/btzy/nativefiledialog-extended/commit/44e63d5e57960b4312d185312686b966a40b65a2). `dbus_message_iter_get_element_count` is available as of [D-Bus 1.9.16](https://launchpad.net/debian/+source/dbus/1.9.16-1). Fallback on counting elements manually. --- nfd/nfd_portal.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/nfd/nfd_portal.cpp b/nfd/nfd_portal.cpp index 8ef9158f..61859e2a 100644 --- a/nfd/nfd_portal.cpp +++ b/nfd/nfd_portal.cpp @@ -14,8 +14,16 @@ #include #include #include -#include // for the random token string -#include // for access() +#include // for access() + +#if !defined(__has_include) || !defined(__linux__) +#include // for getrandom() - the random token string +#elif __has_include() +#include +#else // for GLIBC < 2.25 +#include +#define getrandom(buf, sz, flags) syscall(SYS_getrandom, buf, sz, flags) +#endif #include "nfd.h" @@ -705,7 +713,17 @@ nfdpathsetsize_t ReadResponseUrisUncheckedGetArraySize(DBusMessage* msg) { nfdpathsetsize_t sz = 0; // Initialization will never be used, but we initialize it to prevent // the uninitialized warning otherwise. ReadDict(iter, "uris", [&sz](DBusMessageIter& uris_iter) { + // `dbus_message_iter_get_element_count` is available as of [D-Bus 1.9.16](https://launchpad.net/debian/+source/dbus/1.9.16-1). +#if DBUS_MAJOR_VERSION > 1 || DBUS_MINOR_VERSION > 9 || (DBUS_MINOR_VERSION == 9 && DBUS_MINOR_VERSION >= 16) sz = dbus_message_iter_get_element_count(&uris_iter); +#else // Otherwise fallback on counting elements manually. + sz = 0; + DBusMessageIter uri_iter; + dbus_message_iter_recurse(&uris_iter, &uri_iter); + do { + ++sz; + } while (dbus_message_iter_next(&uri_iter)); +#endif return NFD_OKAY; }); return sz;