From ec8b9a0ce225721a5e78baf56e2205d20e839d23 Mon Sep 17 00:00:00 2001 From: Leptos Date: Thu, 30 May 2024 16:40:35 -0700 Subject: [PATCH] Update to latest libroot (commit 102348f) (#95) https://github.com/opa334/libroot/commit/102348f5e9360ad1f509ae4958bd3ca686347ee8 --- libroot/libroot.h | 51 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/libroot/libroot.h b/libroot/libroot.h index 293a7f1..b30de03 100644 --- a/libroot/libroot.h +++ b/libroot/libroot.h @@ -2,32 +2,69 @@ __BEGIN_DECLS +_Pragma("GCC visibility push(hidden)") + const char *_Nonnull libroot_dyn_get_root_prefix(void); const char *_Nonnull libroot_dyn_get_jbroot_prefix(void); const char *_Nonnull libroot_dyn_get_boot_uuid(void); char *_Nullable libroot_dyn_rootfspath(const char *_Nullable path, char *_Nullable resolvedPath); char *_Nullable libroot_dyn_jbrootpath(const char *_Nullable path, char *_Nullable resolvedPath); +_Pragma("GCC visibility pop") + __END_DECLS +#define __CONVERT_PATH_CSTRING(converter, path) ({ \ + static char outPath[PATH_MAX]; \ + converter(path, outPath); \ +}) + +#define JBROOT_PATH_CSTRING(path) __CONVERT_PATH_CSTRING(libroot_dyn_jbrootpath, path) +#define ROOTFS_PATH_CSTRING(path) __CONVERT_PATH_CSTRING(libroot_dyn_rootfspath, path) + +#if __has_attribute(overloadable) +__attribute__((__overloadable__)) +static inline const char *_Nullable __libroot_convert_path(char *_Nullable (*_Nonnull converter)(const char *_Nonnull, char *_Nullable), const char *_Nullable path, char *_Nonnull buf) { + return converter(path, buf); +} +#endif /* __has_attribute(overloadable) */ + #ifdef __OBJC__ +#import + #define __CONVERT_PATH_NSSTRING(converter, path) ({ \ char tmpBuf[PATH_MAX]; \ - [NSString stringWithUTF8String:converter(path.fileSystemRepresentation, tmpBuf)]; \ + const char *converted = converter(path.fileSystemRepresentation, tmpBuf); \ + converted ? [NSString stringWithUTF8String:converted] : nil; \ }) #define JBROOT_PATH_NSSTRING(path) __CONVERT_PATH_NSSTRING(libroot_dyn_jbrootpath, path) #define ROOTFS_PATH_NSSTRING(path) __CONVERT_PATH_NSSTRING(libroot_dyn_rootfspath, path) +#if __has_attribute(overloadable) +__attribute__((__overloadable__)) +static inline NSString *_Nullable __libroot_convert_path(char *_Nullable (*_Nonnull converter)(const char *_Nonnull, char *_Nullable), NSString *_Nullable path, void *_Nullable const __unused buf) { + return __CONVERT_PATH_NSSTRING(converter, path); +} +#endif /* __has_attribute(overloadable) */ + #endif /* __OBJC__ */ -#define __CONVERT_PATH_CSTRING(converter, path) ({ \ - static char outPath[PATH_MAX]; \ - converter(path, outPath); \ -}) +#if __has_attribute(overloadable) -#define JBROOT_PATH_CSTRING(path) __CONVERT_PATH_CSTRING(libroot_dyn_jbrootpath, path) -#define ROOTFS_PATH_CSTRING(path) __CONVERT_PATH_CSTRING(libroot_dyn_rootfspath, path) +#define __BUFFER_FOR_CHAR_P(x) \ + __builtin_choose_expr( \ + __builtin_types_compatible_p(__typeof__(*(x)), char), \ + ({ static char buf[PATH_MAX]; buf; }), \ + NULL \ + ) + +# define JBROOT_PATH(path) __libroot_convert_path(libroot_dyn_jbrootpath, (path), __BUFFER_FOR_CHAR_P(path)) +# define ROOTFS_PATH(path) __libroot_convert_path(libroot_dyn_rootfspath, (path), __BUFFER_FOR_CHAR_P(path)) +#else +# define JBROOT_PATH(path) _Pragma("GCC error \"JBROOT_PATH is not supported with this compiler, use JBROOT_PATH_CSTRING or JBROOT_PATH_NSSTRING\"") path +# define ROOTFS_PATH(path) _Pragma("GCC error \"ROOTFS_PATH is not supported with this compiler, use ROOTFS_PATH_CSTRING or ROOTFS_PATH_NSSTRING\"") path +#endif /* __has_attribute(overloadable) */ #define JBRAND libroot_dyn_get_boot_uuid()