linux fixes

This commit is contained in:
Mars 2025-05-05 01:58:50 -04:00
parent fcb78f74dc
commit ccd20f5461
Signed by: pupbrained
GPG key ID: 0FF5B8826803F895
3 changed files with 27 additions and 46 deletions

View file

@ -79,15 +79,17 @@ namespace {
fsErrCode.clear(); fsErrCode.clear();
if (!fs::is_directory(dirPath, fsErrCode)) { if (!fs::is_directory(dirPath, fsErrCode)) {
if (fsErrCode) if (fsErrCode && fsErrCode != std::errc::no_such_file_or_directory)
return Err(DracError( return Err(DracError(
DracErrorCode::IoError, DracErrorCode::IoError,
std::format("Filesystem error checking if '{}' is a directory: {}", dirPath.string(), fsErrCode.message()) std::format("Filesystem error checking if '{}' is a directory: {}", dirPath.string(), fsErrCode.message())
)); ));
return Err( 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(); fsErrCode.clear();
u64 count = 0; u64 count = 0;
@ -146,7 +148,7 @@ namespace {
const PkgCountCacheData dataToCache = { .count = count, .timestampEpochSeconds = nowEpochSeconds }; const PkgCountCacheData dataToCache = { .count = count, .timestampEpochSeconds = nowEpochSeconds };
if (Result writeResult = WriteCache(pmId, dataToCache); !writeResult) if (Result writeResult = WriteCache(pmId, dataToCache); !writeResult)
error_at(writeResult.error()); debug_at(writeResult.error());
return count; return count;
} }
@ -253,7 +255,7 @@ namespace package {
const PkgCountCacheData dataToCache = { .count = count, .timestampEpochSeconds = nowEpochSeconds }; const PkgCountCacheData dataToCache = { .count = count, .timestampEpochSeconds = nowEpochSeconds };
if (Result writeResult = WriteCache(cacheKey, dataToCache); !writeResult) if (Result writeResult = WriteCache(cacheKey, dataToCache); !writeResult)
error_at(writeResult.error()); debug_at(writeResult.error());
return count; return count;
} }

View file

@ -135,7 +135,7 @@ namespace {
socklen_t len = sizeof(cred); socklen_t len = sizeof(cred);
if (getsockopt(fileDescriptor, SOL_SOCKET, SO_PEERCRED, &cred, &len) == -1) 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<char, 128> exeLinkPathBuf; Array<char, 128> exeLinkPathBuf;
@ -153,7 +153,7 @@ namespace {
const isize bytesRead = readlink(exeLinkPath, exeRealPathBuf.data(), exeRealPathBuf.size() - 1); const isize bytesRead = readlink(exeLinkPath, exeRealPathBuf.data(), exeRealPathBuf.size() - 1);
if (bytesRead == -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'; exeRealPathBuf.at(bytesRead) = '\0';
@ -229,7 +229,7 @@ namespace os {
struct sysinfo info; struct sysinfo info;
if (sysinfo(&info) != 0) if (sysinfo(&info) != 0)
return Err(DracError::withErrno("sysinfo call failed")); return Err(DracError("sysinfo call failed"));
const u64 totalRam = info.totalram; const u64 totalRam = info.totalram;
const u64 memUnit = info.mem_unit; const u64 memUnit = info.mem_unit;
@ -458,7 +458,7 @@ namespace os {
utsname uts; utsname uts;
if (uname(&uts) == -1) if (uname(&uts) == -1)
return Err(DracError::withErrno("uname call failed")); return Err(DracError("uname call failed"));
if (std::strlen(uts.release) == 0) if (std::strlen(uts.release) == 0)
return Err(DracError(DracErrorCode::ParseError, "uname returned null kernel release")); return Err(DracError(DracErrorCode::ParseError, "uname returned null kernel release"));
@ -470,11 +470,11 @@ namespace os {
struct statvfs stat; struct statvfs stat;
if (statvfs("/", &stat) == -1) 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 { return DiskSpace {
.used_bytes = (stat.f_blocks * stat.f_frsize) - (stat.f_bfree * stat.f_frsize), .usedBytes = (stat.f_blocks * stat.f_frsize) - (stat.f_bfree * stat.f_frsize),
.total_bytes = stat.f_blocks * stat.f_frsize, .totalBytes = stat.f_blocks * stat.f_frsize,
}; };
} }
} // namespace os } // namespace os

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <expected> // std::{unexpected, expected} #include <expected> // std::{unexpected, expected}
#include <format> // std::format
#include <source_location> // std::source_location #include <source_location> // std::source_location
#include <system_error> // std::error_code #include <system_error> // std::error_code
@ -94,41 +95,19 @@ namespace util {
); );
} }
#else #else
DracError(const DracErrorCode code_hint, const int errno_val) DracError(const String& context, const std::source_location& loc = std::source_location::current())
: message(std::system_category().message(errno_val)), code(code_hint) { : message(std::format("{}: {}", context, std::system_category().message(errno))), location(loc) {
using namespace matchit;
using enum DracErrorCode; using enum DracErrorCode;
code = match(errno)(
switch (errno_val) { is | EACCES = PermissionDenied,
case EACCES: code = PermissionDenied; break; is | ENOENT = NotFound,
case ENOENT: code = NotFound; break; is | ETIMEDOUT = Timeout,
case ETIMEDOUT: code = Timeout; break; is | ENOTSUP = NotSupported,
case ENOTSUP: code = NotSupported; break; is | EIO = IoError,
default: code = PlatformSpecific; break; is | or_(ECONNREFUSED, ENETDOWN, ENETUNREACH) = NetworkError,
} is | _ = PlatformSpecific
} );
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 };
} }
#endif #endif
}; };