From db13d64fa2819221914091348165b917cb0efe19 Mon Sep 17 00:00:00 2001 From: Karel Burda <38255062+karel-burda@users.noreply.github.com> Date: Fri, 29 Nov 2024 08:44:50 +0100 Subject: [PATCH] Delete safe_env.cpp --- safe_env.cpp | 76 ---------------------------------------------------- 1 file changed, 76 deletions(-) delete mode 100644 safe_env.cpp diff --git a/safe_env.cpp b/safe_env.cpp deleted file mode 100644 index 53746a7..0000000 --- a/safe_env.cpp +++ /dev/null @@ -1,76 +0,0 @@ -#include "safe_env/safe_env.hpp" - -#include -#include -#include -#include -#include -#include -#include - -namespace -{ -std::shared_mutex mtx; - -template -std::string burda::env::detail::read(const std::string& name, const F getter) -{ - if (name.empty()) [[unlikely]] - { - throw std::invalid_argument{"Environment variable name is empty"}; - } - - std::shared_lock read_lock{mtx}; - - if (char* result = getter(name.c_str())) - { - read_lock.unlock(); - - return result; - } - - return {}; -} // namespace anonymous - -template -void burda::env::detail::write(const std::string& name, const F setter, Args&&... args) -{ - if (name.empty()) [[unlikely]] - { - throw std::invalid_argument{"Environment variable name is empty"}; - } - - std::unique_lock write_lock{mtx}; - - if (const int ret = setter(std::forward(args)...)) [[unlikely]] - { - // we can unlock, as errno is thread-safe - write_lock.unlock(); - - throw std::system_error{errno, std::generic_category(), "Failed to set environment variable " + name}; - } -} -} // namespace anonymous - -namespace burda::env -{ -std::string burda::env::getenv(const std::string& name) -{ - return burda::env::detail::read(name, ::getenv); -} - -std::string burda::env::secure_getenv(const std::string& name) -{ - return burda::env::detail::read(name, ::secure_getenv); -} - -void burda::env::setenv(const std::string& name, const std::string& value, bool overwrite) -{ - burda::env::detail::write(name, ::setenv, value, static_cast(overwrite)); -} - -void burda::env::unsetenv(const std::string& name) -{ - burda::env::detail::write(name, ::unsetenv); -} -} // namespace burda::env