Skip to content

Commit

Permalink
feat(daemon): implement sync disk check
Browse files Browse the repository at this point in the history
Resolves #35
  • Loading branch information
eightbyte81 committed Jun 8, 2024
1 parent 8ed9c61 commit 56e2d22
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
30 changes: 28 additions & 2 deletions daemon/infrastructure/alpm/ArchRepoSyncService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <string>
#include <system_error>
#include <vector>
#include <cstdint>

namespace bxt::Infrastructure {

Expand Down Expand Up @@ -179,10 +180,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 @@ -234,6 +239,8 @@ coro::task<
std::move(open_ok.error()), path, "The archive cannot be opened");
}

uint64_t packages_size = 0;

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

Expand All @@ -246,13 +253,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);
Expand All @@ -264,6 +274,22 @@ 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 @@ -42,6 +42,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

0 comments on commit 56e2d22

Please sign in to comment.