Skip to content

Commit

Permalink
Switching error handling utils to PTL ones
Browse files Browse the repository at this point in the history
  • Loading branch information
gershnik committed Jul 20, 2023
1 parent af2cce3 commit d3c899f
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 59 deletions.
2 changes: 1 addition & 1 deletion cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ list(APPEND DECLARED_DEPENDENCIES isptr)

FetchContent_Declare(ptl
GIT_REPOSITORY https://github.com/gershnik/ptl.git
GIT_TAG f793a86b
GIT_TAG 14bf3e52
GIT_SHALLOW TRUE
)
list(APPEND DECLARED_DEPENDENCIES ptl)
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static auto waitForChild() -> std::optional<int> {

int status = 0;
for ( ; ; ) {
ErrorWhitelist ec(EINTR);
ptl::AllowedErrors<EINTR> ec;
auto maybeStatus = g_maybeChildProcess->wait(ec);
if (maybeStatus) {
status = *maybeStatus;
Expand Down
1 change: 1 addition & 0 deletions src/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <ptl/users.h>
#include <ptl/socket.h>
#include <ptl/system.h>
#include <ptl/errors.h>

#include <libxml/parser.h>
#include <libxml/tree.h>
Expand Down
58 changes: 1 addition & 57 deletions src/sys_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,6 @@
#ifndef HEADER_SYS_UTIL_H_INCLUDED
#define HEADER_SYS_UTIL_H_INCLUDED

template<size_t N>
class ErrorWhitelist {
friend ptl::ErrorTraits<ErrorWhitelist<N>>;
public:
ErrorWhitelist(std::array<int, N> allowed):
m_allowed(allowed)
{}

ErrorWhitelist(int (& allowed)[N]):
m_allowed(std::to_array(allowed))
{}

ErrorWhitelist(int (&& allowed)[N]):
m_allowed(std::to_array(allowed))
{}

template <class T, class... U>
ErrorWhitelist(T first, U ...rest)
requires(std::is_same_v<T, int> && (std::is_same_v<U, int> && ...) && sizeof...(U) == N - 1):
m_allowed({first, rest...})
{}

auto code() const noexcept -> const std::error_code &
{ return m_code; }

private:
std::error_code m_code;
std::array<int, N> m_allowed;
};

template <class T, class... U> ErrorWhitelist(T, U...) -> ErrorWhitelist<1 + sizeof...(U)>;

namespace ptl {

template<size_t N> struct ErrorTraits<ErrorWhitelist<N>> {
template<class... T>
[[gnu::always_inline]] static inline void assignError(ErrorWhitelist<N> & err, int code, const char * format, T && ...args) noexcept {
for(auto allowed: err.m_allowed) {
if (code == allowed) {
err.m_code = ptl::makeErrorCode(code);
return;
}
}
ptl::throwErrorCode(code, format, std::forward<T>(args)...);
}

[[gnu::always_inline]] static inline void clearError(ErrorWhitelist<N> & err) noexcept {
err.m_code.clear();
}

[[gnu::always_inline]] static inline auto failed(const ErrorWhitelist<N> & err) noexcept -> bool {
return bool(err.m_code);
}
};
}

class Uuid {
private:
struct IntStorage {
Expand Down Expand Up @@ -280,7 +224,7 @@ inline void createMissingDirs(const std::filesystem::path & path, mode_t mode,
//other than EEXIST like permissions
if (exists(start))
continue;
ErrorWhitelist ec(EEXIST); //but we also need this exception to avoid TOCTOU, sigh
ptl::AllowedErrors<EEXIST> ec; //but we also need this exception to avoid TOCTOU, sigh
ptl::makeDirectory(start, mode, ec);
ptl::changeMode(start, mode);
if (owner)
Expand Down

0 comments on commit d3c899f

Please sign in to comment.