From 828060e6317989eca62cc812a45e602ec1f0ed7a Mon Sep 17 00:00:00 2001 From: sin-ack Date: Mon, 11 Jul 2022 20:49:07 +0000 Subject: [PATCH] LibCore: Add convenience templates for System::{unveil,pledge} These convenience templates allow the following to be written as before: TRY(Core::System::pledge("promises...")); TRY(Core::System::pledge("promises...", "execpromises...")); TRY(Core::System::unveil("path", "permissions")); TRY(Core::System::unveil(nullptr, nullptr)); Other uses must now append sv to any literal string passed to pledge and unveil. --- Userland/Libraries/LibCore/System.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h index d9ca3f6402..a3b4072899 100644 --- a/Userland/Libraries/LibCore/System.h +++ b/Userland/Libraries/LibCore/System.h @@ -55,6 +55,29 @@ inline ErrorOr unveil(StringView, StringView) inline ErrorOr pledge(StringView, StringView = {}) { return {}; } #endif +template +ALWAYS_INLINE ErrorOr pledge(char const (&promises)[N]) +{ + return pledge(StringView { promises, N - 1 }); +} + +template +ALWAYS_INLINE ErrorOr pledge(char const (&promises)[NPromises], char const (&execpromises)[NExecPromises]) +{ + return pledge(StringView { promises, NPromises - 1 }, StringView { execpromises, NExecPromises - 1 }); +} + +template +ALWAYS_INLINE ErrorOr unveil(char const (&path)[NPath], char const (&permissions)[NPermissions]) +{ + return unveil(StringView { path, NPath - 1 }, StringView { permissions, NPermissions - 1 }); +} + +ALWAYS_INLINE ErrorOr unveil(std::nullptr_t, std::nullptr_t) +{ + return unveil(StringView {}, StringView {}); +} + #ifndef AK_OS_BSD_GENERIC ErrorOr> getspent(); ErrorOr> getspnam(StringView name);