From ccd20f5461146a5e48ecdfcb063a7480a4228fa4 Mon Sep 17 00:00:00 2001 From: pupbrained Date: Mon, 5 May 2025 01:58:50 -0400 Subject: [PATCH] linux fixes --- src/core/package.cpp | 10 +++++---- src/os/linux.cpp | 14 ++++++------- src/util/error.hpp | 49 +++++++++++++------------------------------- 3 files changed, 27 insertions(+), 46 deletions(-) diff --git a/src/core/package.cpp b/src/core/package.cpp index e626084..aca1c2f 100644 --- a/src/core/package.cpp +++ b/src/core/package.cpp @@ -79,15 +79,17 @@ namespace { fsErrCode.clear(); if (!fs::is_directory(dirPath, fsErrCode)) { - if (fsErrCode) + if (fsErrCode && fsErrCode != std::errc::no_such_file_or_directory) return Err(DracError( DracErrorCode::IoError, std::format("Filesystem error checking if '{}' is a directory: {}", dirPath.string(), fsErrCode.message()) )); + return Err( - DracError(DracErrorCode::IoError, std::format("{} path is not a directory: {}", pmId, dirPath.string())) + DracError(DracErrorCode::NotFound, std::format("{} path is not a directory: {}", pmId, dirPath.string())) ); } + fsErrCode.clear(); u64 count = 0; @@ -146,7 +148,7 @@ namespace { const PkgCountCacheData dataToCache = { .count = count, .timestampEpochSeconds = nowEpochSeconds }; if (Result writeResult = WriteCache(pmId, dataToCache); !writeResult) - error_at(writeResult.error()); + debug_at(writeResult.error()); return count; } @@ -253,7 +255,7 @@ namespace package { const PkgCountCacheData dataToCache = { .count = count, .timestampEpochSeconds = nowEpochSeconds }; if (Result writeResult = WriteCache(cacheKey, dataToCache); !writeResult) - error_at(writeResult.error()); + debug_at(writeResult.error()); return count; } diff --git a/src/os/linux.cpp b/src/os/linux.cpp index bd44e89..dac91fa 100644 --- a/src/os/linux.cpp +++ b/src/os/linux.cpp @@ -135,7 +135,7 @@ namespace { socklen_t len = sizeof(cred); if (getsockopt(fileDescriptor, SOL_SOCKET, SO_PEERCRED, &cred, &len) == -1) - return Err(DracError::withErrno("Failed to get socket credentials (SO_PEERCRED)")); + return Err(DracError("Failed to get socket credentials (SO_PEERCRED)")); Array exeLinkPathBuf; @@ -153,7 +153,7 @@ namespace { const isize bytesRead = readlink(exeLinkPath, exeRealPathBuf.data(), exeRealPathBuf.size() - 1); if (bytesRead == -1) - return Err(DracError::withErrno(std::format("Failed to read link '{}'", exeLinkPath))); + return Err(DracError(std::format("Failed to read link '{}'", exeLinkPath))); exeRealPathBuf.at(bytesRead) = '\0'; @@ -229,7 +229,7 @@ namespace os { struct sysinfo info; if (sysinfo(&info) != 0) - return Err(DracError::withErrno("sysinfo call failed")); + return Err(DracError("sysinfo call failed")); const u64 totalRam = info.totalram; const u64 memUnit = info.mem_unit; @@ -458,7 +458,7 @@ namespace os { utsname uts; if (uname(&uts) == -1) - return Err(DracError::withErrno("uname call failed")); + return Err(DracError("uname call failed")); if (std::strlen(uts.release) == 0) return Err(DracError(DracErrorCode::ParseError, "uname returned null kernel release")); @@ -470,11 +470,11 @@ namespace os { struct statvfs stat; if (statvfs("/", &stat) == -1) - return Err(DracError::withErrno(std::format("Failed to get filesystem stats for '/' (statvfs call failed)"))); + return Err(DracError("Failed to get filesystem stats for '/' (statvfs call failed)")); return DiskSpace { - .used_bytes = (stat.f_blocks * stat.f_frsize) - (stat.f_bfree * stat.f_frsize), - .total_bytes = stat.f_blocks * stat.f_frsize, + .usedBytes = (stat.f_blocks * stat.f_frsize) - (stat.f_bfree * stat.f_frsize), + .totalBytes = stat.f_blocks * stat.f_frsize, }; } } // namespace os diff --git a/src/util/error.hpp b/src/util/error.hpp index 88064c8..5b43c44 100644 --- a/src/util/error.hpp +++ b/src/util/error.hpp @@ -1,6 +1,7 @@ #pragma once #include // std::{unexpected, expected} +#include // std::format #include // std::source_location #include // std::error_code @@ -94,41 +95,19 @@ namespace util { ); } #else - DracError(const DracErrorCode code_hint, const int errno_val) - : message(std::system_category().message(errno_val)), code(code_hint) { + DracError(const String& context, const std::source_location& loc = std::source_location::current()) + : message(std::format("{}: {}", context, std::system_category().message(errno))), location(loc) { + using namespace matchit; using enum DracErrorCode; - - switch (errno_val) { - case EACCES: code = PermissionDenied; break; - case ENOENT: code = NotFound; break; - case ETIMEDOUT: code = Timeout; break; - case ENOTSUP: code = NotSupported; break; - default: code = PlatformSpecific; break; - } - } - - static auto withErrno(const String& context, const std::source_location& loc = std::source_location::current()) - -> DracError { - const i32 errNo = errno; - const String msg = std::system_category().message(errNo); - const String fullMsg = std::format("{}: {}", context, msg); - - const DracErrorCode code = [&errNo] { - switch (errNo) { - case EACCES: - case EPERM: return DracErrorCode::PermissionDenied; - case ENOENT: return DracErrorCode::NotFound; - case ETIMEDOUT: return DracErrorCode::Timeout; - case ENOTSUP: return DracErrorCode::NotSupported; - case EIO: return DracErrorCode::IoError; - case ECONNREFUSED: - case ENETDOWN: - case ENETUNREACH: return DracErrorCode::NetworkError; - default: return DracErrorCode::PlatformSpecific; - } - }(); - - return DracError { code, fullMsg, loc }; + code = match(errno)( + is | EACCES = PermissionDenied, + is | ENOENT = NotFound, + is | ETIMEDOUT = Timeout, + is | ENOTSUP = NotSupported, + is | EIO = IoError, + is | or_(ECONNREFUSED, ENETDOWN, ENETUNREACH) = NetworkError, + is | _ = PlatformSpecific + ); } #endif }; @@ -153,4 +132,4 @@ namespace util { template using Err = std::unexpected; } // namespace types -} // namespace util \ No newline at end of file +} // namespace util