unfinished but im tired
This commit is contained in:
parent
71f9c1ce63
commit
d693c8cfb1
13 changed files with 457 additions and 531 deletions
335
src/core/package.cpp
Normal file
335
src/core/package.cpp
Normal file
|
@ -0,0 +1,335 @@
|
|||
#include "package.hpp"
|
||||
|
||||
#include <SQLiteCpp/Database.h> // SQLite::{Database, OPEN_READONLY}
|
||||
#include <SQLiteCpp/Exception.h> // SQLite::Exception
|
||||
#include <SQLiteCpp/Statement.h> // SQLite::Statement
|
||||
#include <chrono> // std::chrono
|
||||
#include <filesystem> // std::filesystem
|
||||
#include <format> // std::format
|
||||
#include <future> // std::{async, future, launch}
|
||||
#include <system_error> // std::error_code
|
||||
|
||||
#include "src/util/cache.hpp"
|
||||
#include "src/util/error.hpp"
|
||||
#include "src/util/helpers.hpp"
|
||||
#include "src/util/logging.hpp"
|
||||
#include "src/util/types.hpp"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
using namespace std::chrono;
|
||||
using util::cache::ReadCache, util::cache::WriteCache;
|
||||
using util::error::DracError, util::error::DracErrorCode;
|
||||
using util::types::Err, util::types::Exception, util::types::Future, util::types::Result, util::types::String,
|
||||
util::types::Vec, util::types::i64, util::types::u64;
|
||||
|
||||
namespace {
|
||||
fn GetCountFromDirectoryImpl(
|
||||
const String& pmId,
|
||||
const fs::path& dirPath,
|
||||
const String& fileExtensionFilter,
|
||||
const bool subtractOne
|
||||
) -> Result<u64, DracError> {
|
||||
debug_log("Counting packages for '{}' in directory: {}", pmId, dirPath.string());
|
||||
|
||||
std::error_code errc;
|
||||
|
||||
if (!fs::exists(dirPath, errc)) {
|
||||
if (errc)
|
||||
warn_log("Filesystem error checking {} directory '{}': {}", pmId, dirPath.string(), errc.message());
|
||||
|
||||
return Err(DracError(DracErrorCode::NotFound, std::format("{} directory not found: {}", pmId, dirPath.string())));
|
||||
}
|
||||
|
||||
errc.clear();
|
||||
|
||||
if (!fs::is_directory(dirPath, errc)) {
|
||||
if (errc)
|
||||
return Err(DracError(
|
||||
DracErrorCode::IoError,
|
||||
std::format("Filesystem error checking if '{}' is a directory: {}", dirPath.string(), errc.message())
|
||||
));
|
||||
|
||||
return Err(
|
||||
DracError(DracErrorCode::IoError, std::format("{} path is not a directory: {}", pmId, dirPath.string()))
|
||||
);
|
||||
}
|
||||
|
||||
errc.clear();
|
||||
|
||||
u64 count = 0;
|
||||
bool filterActive = !fileExtensionFilter.empty();
|
||||
|
||||
try {
|
||||
const fs::directory_iterator dirIter(dirPath, fs::directory_options::skip_permission_denied, errc);
|
||||
|
||||
if (errc) {
|
||||
return Err(DracError(
|
||||
DracErrorCode::IoError,
|
||||
std::format("Failed to create iterator for {} directory '{}': {}", pmId, dirPath.string(), errc.message())
|
||||
));
|
||||
}
|
||||
|
||||
errc.clear();
|
||||
|
||||
for (const fs::directory_entry& entry : dirIter) {
|
||||
if (entry.path().empty())
|
||||
continue;
|
||||
|
||||
std::error_code entryStatErr;
|
||||
bool isFile = false;
|
||||
|
||||
if (filterActive) {
|
||||
isFile = entry.is_regular_file(entryStatErr);
|
||||
if (entryStatErr) {
|
||||
warn_log(
|
||||
"Error stating entry '{}' in {} directory: {}", entry.path().string(), pmId, entryStatErr.message()
|
||||
);
|
||||
entryStatErr.clear();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (filterActive) {
|
||||
if (isFile && entry.path().extension().string() == fileExtensionFilter)
|
||||
count++;
|
||||
} else
|
||||
count++;
|
||||
}
|
||||
|
||||
} catch (const fs::filesystem_error& e) {
|
||||
return Err(DracError(DracErrorCode::IoError, std::format("Filesystem error during {} directory iteration", pmId))
|
||||
);
|
||||
} catch (const Exception& e) { return Err(DracError(DracErrorCode::InternalError, e.what())); } catch (...) {
|
||||
return Err(DracError(DracErrorCode::Other, std::format("Unknown error iterating {} directory", pmId)));
|
||||
}
|
||||
|
||||
if (subtractOne && count > 0)
|
||||
count--;
|
||||
|
||||
debug_log("Successfully counted {} packages for '{}': {}", std::to_string(count), pmId, dirPath.string());
|
||||
return count;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace package {
|
||||
fn GetCountFromDirectory(
|
||||
const String& pmId,
|
||||
const fs::path& dirPath,
|
||||
const String& fileExtensionFilter,
|
||||
const bool subtractOne
|
||||
) -> Result<u64, DracError> {
|
||||
return GetCountFromDirectoryImpl(pmId, dirPath, fileExtensionFilter, subtractOne);
|
||||
}
|
||||
|
||||
fn GetCountFromDirectory(const String& pmId, const fs::path& dirPath, const String& fileExtensionFilter)
|
||||
-> Result<u64, DracError> {
|
||||
return GetCountFromDirectoryImpl(pmId, dirPath, fileExtensionFilter, false);
|
||||
}
|
||||
|
||||
fn GetCountFromDirectory(const String& pmId, const fs::path& dirPath, const bool subtractOne)
|
||||
-> Result<u64, DracError> {
|
||||
const String noFilter;
|
||||
return GetCountFromDirectoryImpl(pmId, dirPath, noFilter, subtractOne);
|
||||
}
|
||||
|
||||
fn GetCountFromDirectory(const String& pmId, const fs::path& dirPath) -> Result<u64, DracError> {
|
||||
const String noFilter;
|
||||
return GetCountFromDirectoryImpl(pmId, dirPath, noFilter, false);
|
||||
}
|
||||
|
||||
fn GetCountFromDb(const PackageManagerInfo& pmInfo) -> Result<u64, DracError> {
|
||||
const auto& [pmId, dbPath, countQuery] = pmInfo;
|
||||
const String cacheKey = "pkg_count_" + pmId; // More specific cache key
|
||||
|
||||
if (Result<PkgCountCacheData, DracError> cachedDataResult = ReadCache<PkgCountCacheData>(cacheKey)) {
|
||||
const auto& [count, timestamp] = *cachedDataResult;
|
||||
std::error_code errc;
|
||||
const std::filesystem::file_time_type dbModTime = fs::last_write_time(dbPath, errc);
|
||||
|
||||
if (errc) {
|
||||
warn_log(
|
||||
"Could not get modification time for '{}': {}. Invalidating {} cache.", dbPath.string(), errc.message(), pmId
|
||||
);
|
||||
} else {
|
||||
if (const system_clock::time_point cacheTimePoint = system_clock::time_point(seconds(timestamp));
|
||||
cacheTimePoint.time_since_epoch() >= dbModTime.time_since_epoch()) {
|
||||
debug_log(
|
||||
"Using valid {} package count cache (DB file unchanged since {}). Count: {}",
|
||||
pmId,
|
||||
std::format("{:%F %T %Z}", floor<seconds>(cacheTimePoint)),
|
||||
count
|
||||
);
|
||||
return count;
|
||||
}
|
||||
debug_log("{} package count cache stale (DB file modified).", pmId);
|
||||
}
|
||||
} else {
|
||||
if (cachedDataResult.error().code != DracErrorCode::NotFound)
|
||||
debug_at(cachedDataResult.error());
|
||||
debug_log("{} package count cache not found or unreadable.", pmId);
|
||||
}
|
||||
|
||||
debug_log("Fetching fresh {} package count from database: {}", pmId, dbPath.string());
|
||||
u64 count = 0;
|
||||
|
||||
try {
|
||||
// Ensure database file exists before trying to open
|
||||
std::error_code existsErr;
|
||||
if (!fs::exists(dbPath, existsErr) || existsErr) {
|
||||
if (existsErr) {
|
||||
warn_log("Error checking existence of {} DB '{}': {}", pmId, dbPath.string(), existsErr.message());
|
||||
}
|
||||
return Err(
|
||||
DracError(DracErrorCode::NotFound, std::format("{} database not found at '{}'", pmId, dbPath.string()))
|
||||
);
|
||||
}
|
||||
|
||||
const SQLite::Database database(dbPath.string(), SQLite::OPEN_READONLY);
|
||||
SQLite::Statement queryStmt(database, countQuery); // Use query directly
|
||||
|
||||
if (queryStmt.executeStep()) {
|
||||
const i64 countInt64 = queryStmt.getColumn(0).getInt64();
|
||||
if (countInt64 < 0)
|
||||
return Err(
|
||||
DracError(DracErrorCode::ParseError, std::format("Negative count returned by {} DB COUNT query.", pmId))
|
||||
);
|
||||
count = static_cast<u64>(countInt64);
|
||||
} else {
|
||||
// It's possible a query legitimately returns 0 rows (e.g., no packages)
|
||||
debug_log("No rows returned by {} DB COUNT query for '{}', assuming count is 0.", pmId, dbPath.string());
|
||||
count = 0;
|
||||
// return Err(DracError(DracErrorCode::ParseError, std::format("No rows returned by {} DB COUNT query.",
|
||||
// pmId)));
|
||||
}
|
||||
} catch (const SQLite::Exception& e) {
|
||||
// Log specific SQLite errors but return a more general error type
|
||||
error_log("SQLite error occurred accessing {} DB '{}': {}", pmId, dbPath.string(), e.what());
|
||||
return Err(DracError(
|
||||
DracErrorCode::ApiUnavailable, // Or IoError?
|
||||
std::format("Failed to query {} database: {}", pmId, dbPath.string())
|
||||
));
|
||||
} catch (const Exception& e) {
|
||||
error_log("Standard exception accessing {} DB '{}': {}", pmId, dbPath.string(), e.what());
|
||||
return Err(DracError(DracErrorCode::InternalError, e.what()));
|
||||
} catch (...) {
|
||||
error_log("Unknown error occurred accessing {} DB '{}'", pmId, dbPath.string());
|
||||
return Err(DracError(DracErrorCode::Other, std::format("Unknown error occurred accessing {} DB", pmId)));
|
||||
}
|
||||
|
||||
debug_log("Successfully fetched {} package count: {}.", pmId, count);
|
||||
|
||||
const i64 nowEpochSeconds = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
|
||||
const PkgCountCacheData dataToCache = { .count = count, .timestampEpochSeconds = nowEpochSeconds };
|
||||
|
||||
if (Result<void, DracError> writeResult = WriteCache(cacheKey, dataToCache); !writeResult)
|
||||
error_at(writeResult.error()); // Log cache write error but return the count anyway
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
fn GetNixCount() -> Result<u64, DracError> {
|
||||
const PackageManagerInfo nixInfo = {
|
||||
.id = "nix",
|
||||
.dbPath = "/nix/var/nix/db/db.sqlite",
|
||||
.countQuery = "SELECT COUNT(path) FROM ValidPaths WHERE sigs IS NOT NULL",
|
||||
};
|
||||
|
||||
if (std::error_code errc; !fs::exists(nixInfo.dbPath, errc)) {
|
||||
if (errc) {
|
||||
warn_log("Filesystem error checking for Nix DB at '{}': {}", nixInfo.dbPath.string(), errc.message());
|
||||
return Err(DracError(DracErrorCode::IoError, "Filesystem error checking Nix DB: " + errc.message()));
|
||||
}
|
||||
|
||||
return Err(DracError(DracErrorCode::ApiUnavailable, "Nix db not found: " + nixInfo.dbPath.string()));
|
||||
}
|
||||
|
||||
return GetCountFromDb(nixInfo);
|
||||
}
|
||||
#endif
|
||||
|
||||
fn GetCargoCount() -> Result<u64, DracError> {
|
||||
using util::helpers::GetEnv;
|
||||
|
||||
fs::path cargoPath {};
|
||||
|
||||
if (const Result<String, DracError> cargoHome = GetEnv("CARGO_HOME"))
|
||||
cargoPath = fs::path(*cargoHome) / "bin";
|
||||
else if (const Result<String, DracError> homeDir = GetEnv("HOME"))
|
||||
cargoPath = fs::path(*homeDir) / ".cargo" / "bin";
|
||||
|
||||
if (cargoPath.empty() || !fs::exists(cargoPath))
|
||||
return Err(DracError(DracErrorCode::NotFound, "Could not find cargo directory"));
|
||||
|
||||
u64 count = 0;
|
||||
|
||||
for (const fs::directory_entry& entry : fs::directory_iterator(cargoPath))
|
||||
if (entry.is_regular_file())
|
||||
++count;
|
||||
|
||||
debug_log("Found {} packages in cargo directory: {}", count, cargoPath.string());
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
fn GetTotalCount() -> Result<u64, DracError> {
|
||||
Vec<Future<Result<u64, DracError>>> futures;
|
||||
|
||||
#ifdef __linux__
|
||||
futures.push_back(std::async(std::launch::async, GetDpkgCount));
|
||||
futures.push_back(std::async(std::launch::async, GetPacmanCount));
|
||||
// futures.push_back(std::async(std::launch::async, GetRpmCount));
|
||||
// futures.push_back(std::async(std::launch::async, GetPortageCount));
|
||||
// futures.push_back(std::async(std::launch::async, GetZypperCount));
|
||||
// futures.push_back(std::async(std::launch::async, GetApkCount));
|
||||
futures.push_back(std::async(std::launch::async, GetMossCount));
|
||||
#elifdef __APPLE__
|
||||
futures.push_back(std::async(std::launch::async, GetHomebrewCount));
|
||||
futures.push_back(std::async(std::launch::async, GetMacPortsCount));
|
||||
#elifdef _WIN32
|
||||
futures.push_back(std::async(std::launch::async, GetWinRTCount));
|
||||
futures.push_back(std::async(std::launch::async, GetChocolateyCount));
|
||||
futures.push_back(std::async(std::launch::async, GetScoopCount));
|
||||
#elif defined(__FreeBSD__) || defined(__DragonFly__)
|
||||
futures.push_back(std::async(std::launch::async, GetPkgNgCount));
|
||||
#elifdef __NetBSD__
|
||||
futures.push_back(std::async(std::launch::async, GetPkgSrcCount));
|
||||
#elifdef __HAIKU__
|
||||
futures.push_back(std::async(std::launch::async, GetHaikuCount));
|
||||
#elifdef __serenity__
|
||||
futures.push_back(std::async(std::launch::async, GetSerenityCount));
|
||||
#endif
|
||||
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
futures.push_back(std::async(std::launch::async, GetNixCount));
|
||||
#endif
|
||||
futures.push_back(std::async(std::launch::async, GetCargoCount));
|
||||
|
||||
u64 totalCount = 0;
|
||||
bool oneSucceeded = false;
|
||||
|
||||
for (Future<Result<u64, DracError>>& fut : futures) {
|
||||
try {
|
||||
if (Result<u64, DracError> result = fut.get()) {
|
||||
totalCount += *result;
|
||||
oneSucceeded = true;
|
||||
debug_log("Added {} packages. Current total: {}", *result, totalCount);
|
||||
} else {
|
||||
if (result.error().code != DracErrorCode::NotFound && result.error().code != DracErrorCode::ApiUnavailable &&
|
||||
result.error().code != DracErrorCode::NotSupported) {
|
||||
error_at(result.error());
|
||||
} else
|
||||
debug_at(result.error());
|
||||
}
|
||||
} catch (const Exception& e) {
|
||||
error_log("Caught exception while getting package count future: {}", e.what());
|
||||
} catch (...) { error_log("Caught unknown exception while getting package count future."); }
|
||||
}
|
||||
|
||||
if (!oneSucceeded && totalCount == 0)
|
||||
return Err(DracError(DracErrorCode::NotFound, "No package managers found or none reported counts."));
|
||||
|
||||
debug_log("Final total package count: {}", totalCount);
|
||||
return totalCount;
|
||||
}
|
||||
} // namespace package
|
Loading…
Add table
Add a link
Reference in a new issue