meowwwwwwwwwwwwwwww

This commit is contained in:
Mars 2025-05-11 14:11:33 -04:00
parent 9aa2e44537
commit badb8b232f
Signed by: pupbrained
GPG key ID: 874E22DF2F9DFCB5
4 changed files with 54 additions and 17 deletions

View file

@ -1,21 +1,23 @@
#ifdef __APPLE__ #ifdef __APPLE__
// clang-format off // clang-format off
#include <chrono> // std::chrono::{system_clock, seconds}
#include <flat_map> // std::flat_map #include <flat_map> // std::flat_map
#include <sys/statvfs.h> // statvfs #include <sys/statvfs.h> // statvfs
#include <sys/sysctl.h> // {CTL_KERN, KERN_PROC, KERN_PROC_ALL, kinfo_proc, sysctl, sysctlbyname} #include <sys/sysctl.h> // {CTL_KERN, KERN_PROC, KERN_PROC_ALL, kinfo_proc, sysctl, sysctlbyname}
#include "src/core/package.hpp" #include "OperatingSystem.hpp"
#include "src/util/defs.hpp" #include "Services/PackageCounting.hpp"
#include "src/util/error.hpp" #include "Util/Caching.hpp"
#include "src/util/helpers.hpp" #include "Util/Definitions.hpp"
#include "src/util/types.hpp" #include "Util/Env.hpp"
#include "Util/Error.hpp"
#include "macos/bridge.hpp" #include "Util/Types.hpp"
#include "os.hpp" #include "macOS/Bridge.hpp"
// clang-format on // clang-format on
using namespace util::types; using namespace util::types;
using std::chrono::system_clock, std::chrono::seconds;
using util::error::DracError, util::error::DracErrorCode; using util::error::DracError, util::error::DracErrorCode;
using util::helpers::GetEnv; using util::helpers::GetEnv;
@ -323,22 +325,48 @@ namespace os {
namespace package { namespace package {
fn GetHomebrewCount() -> Result<u64> { fn GetHomebrewCount() -> Result<u64> {
u64 count = 0; using util::cache::ReadCache, util::cache::WriteCache;
Array<fs::path, 2> cellarPaths { Array<fs::path, 2> cellarPaths {
"/opt/homebrew/Cellar", "/opt/homebrew/Cellar",
"/usr/local/Cellar", "/usr/local/Cellar",
}; };
if (Result<PkgCountCacheData> cachedDataResult = ReadCache<PkgCountCacheData>("homebrew_total")) {
const auto& [cachedCount, timestamp] = *cachedDataResult;
bool cacheValid = true;
for (const fs::path& cellarPath : cellarPaths) {
if (std::error_code errc; fs::exists(cellarPath, errc) && !errc) {
const fs::file_time_type dirModTime = fs::last_write_time(cellarPath, errc);
if (!errc) {
const system_clock::time_point cacheTimePoint = system_clock::time_point(seconds(timestamp));
if (cacheTimePoint.time_since_epoch() < dirModTime.time_since_epoch()) {
cacheValid = false;
break;
}
}
}
}
if (cacheValid) {
debug_log("Using valid Homebrew total count cache. Count: {}", cachedCount);
return cachedCount;
}
}
u64 count = 0;
for (const fs::path& cellarPath : cellarPaths) { for (const fs::path& cellarPath : cellarPaths) {
if (std::error_code errc; !fs::exists(cellarPath, errc) || errc) { if (std::error_code errc; !fs::exists(cellarPath, errc) || errc) {
if (errc && errc != std::errc::no_such_file_or_directory) if (errc && errc != std::errc::no_such_file_or_directory)
return Err(DracError(errc)); return Err(DracError(errc));
return Err(DracError(DracErrorCode::NotFound, "Homebrew Cellar directory not found at: " + cellarPath.string())); continue;
} }
Result dirCount = GetCountFromDirectory("homebrew", cellarPath, true); const String cacheKey = "homebrew_" + cellarPath.filename().string();
Result dirCount = GetCountFromDirectory(cacheKey, cellarPath, true);
if (!dirCount) { if (!dirCount) {
if (dirCount.error().code != DracErrorCode::NotFound) if (dirCount.error().code != DracErrorCode::NotFound)
@ -350,6 +378,15 @@ namespace package {
count += *dirCount; count += *dirCount;
} }
if (count == 0)
return Err(DracError(DracErrorCode::NotFound, "No Homebrew packages found in any Cellar directory"));
const i64 timestampEpochSeconds = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
const PkgCountCacheData dataToCache(count, timestampEpochSeconds);
if (Result writeResult = WriteCache("homebrew_total", dataToCache); !writeResult)
debug_at(writeResult.error());
return count; return count;
} }

View file

@ -3,9 +3,9 @@
#ifdef __APPLE__ #ifdef __APPLE__
// clang-format off // clang-format off
#include "src/util/defs.hpp" #include "Util/Definitions.hpp"
#include "src/util/error.hpp" #include "Util/Error.hpp"
#include "src/util/types.hpp" #include "Util/Types.hpp"
// clang-format on // clang-format on
using util::error::DracError; using util::error::DracError;

View file

@ -1,7 +1,7 @@
#ifdef __APPLE__ #ifdef __APPLE__
// clang-format off // clang-format off
#import "bridge.hpp" #import "Bridge.hpp"
#import <dispatch/dispatch.h> #import <dispatch/dispatch.h>
#import <objc/runtime.h> #import <objc/runtime.h>
@ -12,7 +12,7 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include "src/util/error.hpp" #include "Util/Error.hpp"
// clang-format on // clang-format on
using util::error::DracError, util::error::DracErrorCode; using util::error::DracError, util::error::DracErrorCode;

View file

@ -79,7 +79,7 @@ namespace {
#ifdef _WIN32 #ifdef _WIN32
return static_cast<util::types::usize>(_mkgmtime(&time)); return static_cast<util::types::usize>(_mkgmtime(&time));
#else #else
return static_cast<util::types::usize>(timegm(&tm)); return static_cast<util::types::usize>(timegm(&time));
#endif #endif
} }
} // namespace } // namespace