Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(daemon): implement sync disk check #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions daemon/infrastructure/alpm/ArchRepoSyncService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <coro/sync_wait.hpp>
#include <coro/thread_pool.hpp>
#include <coro/when_all.hpp>
#include <cstdint>
#include <expected>
#include <httplib.h>
#include <ios>
Expand Down Expand Up @@ -188,10 +189,14 @@ std::optional<ArchRepoSyncService::PackageInfo> parse_descfile(auto& entry) {

if (!hash.has_value()) { return {}; }

const auto package_size = desc.get("ISIZE");
if (!package_size.has_value()) { return {}; }

return ArchRepoSyncService::PackageInfo {.name = *name,
.filename = *filename,
.version = *version,
.hash = *hash};
.hash = *hash,
.package_size = *package_size};
}
coro::task<
ArchRepoSyncService::Result<std::vector<ArchRepoSyncService::PackageInfo>>>
Expand Down Expand Up @@ -242,7 +247,11 @@ coro::task<
co_return bxt::make_error_with_source<DownloadError>(
std::move(open_ok.error()), path, "The archive cannot be opened");
}

uint64_t packages_size = 0;

auto uow = co_await m_uow_factory();

for (auto& [header, entry] : reader) {
std::string pname = archive_entry_pathname(*header);

Expand All @@ -255,13 +264,16 @@ coro::task<
"Unknown", fmt::format("Cannot parse descfile {}", pname));
}

const auto& [name, filename, version, hash] = *parsed_package_info;
const auto& [name, filename, version, hash, package_size] =
*parsed_package_info;

if (is_excluded(section, name)) {
logi("Package {} is excluded. Skipping.", name);
continue;
}

packages_size += std::stoull(package_size);

const auto existing_package =
co_await m_package_repository.find_by_section_async(
SectionDTOMapper::to_entity(section), name, uow);
Expand All @@ -273,6 +285,24 @@ coro::task<
result.emplace_back(std::move(*parsed_package_info));
}
}

std::error_code ec;
const auto filepath =
m_options.sources[section].download_path / bxt::to_string(section);
const auto available_space = std::filesystem::space(filepath, ec).available;
if (ec) {
co_return bxt::make_error<DownloadError>(
"Unknown",
fmt::format("Error checking disk space: {}.", ec.value()));
}
if (available_space <= packages_size) {
co_return bxt::make_error<DownloadError>(
"Unknown",
fmt::format(
"Not enough available disk space. Need: {}, available: {}.",
packages_size, available_space));
}

co_return result;
}

Expand Down
1 change: 1 addition & 0 deletions daemon/infrastructure/alpm/ArchRepoSyncService.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ArchRepoSyncService : public bxt::Core::Application::SyncService {
std::string filename;
Core::Domain::PackageVersion version;
std::string hash;
std::string package_size;
};

ArchRepoSyncService(Utilities::EventBusDispatcher& dispatcher,
Expand Down