From bc282425088455198a7a99511c75914477d4ed32 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 23:14:51 +0200 Subject: Merging upstream version 1.9.3. Signed-off-by: Daniel Baumann --- misc.hh | 69 +++++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 19 deletions(-) (limited to 'misc.hh') diff --git a/misc.hh b/misc.hh index 8800cd7..76887ee 100644 --- a/misc.hh +++ b/misc.hh @@ -49,7 +49,6 @@ class DNSName; typedef enum { TSIG_MD5, TSIG_SHA1, TSIG_SHA224, TSIG_SHA256, TSIG_SHA384, TSIG_SHA512, TSIG_GSS } TSIGHashEnum; namespace pdns { -#if defined(HAVE_LIBCRYPTO) /** * \brief Retrieves the errno-based error message in a reentrant way. * @@ -63,6 +62,7 @@ namespace pdns */ auto getMessageFromErrno(int errnum) -> std::string; +#if defined(HAVE_LIBCRYPTO) namespace OpenSSL { /** @@ -131,9 +131,8 @@ stringtok (Container &container, string const &in, template bool rfc1982LessThan(T a, T b) { - static_assert(std::is_unsigned::value, "rfc1982LessThan only works for unsigned types"); - typedef typename std::make_signed::type signed_t; - return static_cast(a - b) < 0; + static_assert(std::is_unsigned_v, "rfc1982LessThan only works for unsigned types"); + return std::make_signed_t(a - b) < 0; } // fills container with ranges, so {posbegin,posend} @@ -414,17 +413,21 @@ struct CIStringCompare struct CIStringComparePOSIX { - bool operator() (const std::string& lhs, const std::string& rhs) + bool operator() (const std::string& lhs, const std::string& rhs) const { - std::string::const_iterator a,b; const std::locale &loc = std::locale("POSIX"); - a=lhs.begin();b=rhs.begin(); - while(a!=lhs.end()) { - if (b==rhs.end() || std::tolower(*b,loc)::value, bool> = true> +typename std::enable_if_t, bool> = true> const char* addS(Integer siz, const char* singular = "", const char *plural = "s") { if (siz == 1) { @@ -626,7 +629,7 @@ const char* addS(Integer siz, const char* singular = "", const char *plural = "s } template ::value, bool> = true> +typename std::enable_if_t, bool> = true> const char* addS(const C& c, const char* singular = "", const char *plural = "s") { return addS(c.size(), singular, plural); @@ -790,10 +793,7 @@ struct FDWrapper ~FDWrapper() { - if (d_fd != -1) { - close(d_fd); - d_fd = -1; - } + reset(); } FDWrapper(FDWrapper&& rhs) noexcept : d_fd(rhs.d_fd) @@ -803,7 +803,7 @@ struct FDWrapper FDWrapper& operator=(FDWrapper&& rhs) noexcept { - if (d_fd != -1) { + if (d_fd >= 0) { close(d_fd); } d_fd = rhs.d_fd; @@ -821,6 +821,37 @@ struct FDWrapper return d_fd; } + void reset() + { + if (d_fd >= 0) { + close(d_fd); + } + d_fd = -1; + } + private: int d_fd{-1}; }; + +namespace pdns +{ +[[nodiscard]] std::optional visit_directory(const std::string& directory, const std::function& visitor); + +struct FilePtrDeleter +{ + /* using a deleter instead of decltype(&fclose) has two big advantages: + - the deleter is included in the type and does not have to be passed + when creating a new object (easier to use, less memory usage, in theory + better inlining) + - we avoid the annoying "ignoring attributes on template argument ‘int (*)(FILE*)’" + warning from the compiler, which is there because fclose is tagged as __nonnull((1)) + */ + void operator()(FILE* filePtr) const noexcept { + fclose(filePtr); + } +}; + +using UniqueFilePtr = std::unique_ptr; + +UniqueFilePtr openFileForWriting(const std::string& filePath, mode_t permissions, bool mustNotExist = true, bool appendIfExists = false); +} -- cgit v1.2.3