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();
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;
}

View file

@ -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<char, 128> 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

View file

@ -1,6 +1,7 @@
#pragma once
#include <expected> // std::{unexpected, expected}
#include <format> // std::format
#include <source_location> // std::source_location
#include <system_error> // 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
};