Skip to content

Commit

Permalink
feat(daemon): use pacman's repo-add desc field order
Browse files Browse the repository at this point in the history
  • Loading branch information
LordTermor committed Aug 22, 2024
1 parent 1b5e83a commit 65cca92
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 75 deletions.
18 changes: 18 additions & 0 deletions daemon/utilities/FixedString.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* === This file is part of bxt ===
*
* SPDX-FileCopyrightText: 2024 Artem Grinev <[email protected]>
* SPDX-License-Identifier: AGPL-3.0-or-later
*
*/
#pragma once

#include <algorithm>

// Needed for string literals to be used as template parameters.
template<unsigned N> struct FixedString {
char buf[N + 1] {};
constexpr FixedString(char const* s) { std::copy(s, s + N, buf); }
constexpr operator char const*() const { return buf; }
};

template<unsigned N> FixedString(char const (&)[N]) -> FixedString<N - 1>;
1 change: 1 addition & 0 deletions daemon/utilities/alpmdb/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <iterator>
#include <memory>
#include <parallel_hashmap/phmap.h>
#include <set>
#include <string>
#include <string_view>
#include <unordered_map>
Expand Down
68 changes: 9 additions & 59 deletions daemon/utilities/alpmdb/Desc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/
#include "Desc.h"

#include "utilities/base64.h"
#include "utilities/hash_from_file.h"
#include "utilities/alpmdb/DescFormatter.h"
#include "utilities/alpmdb/PkgInfo.h"
#include "utilities/libarchive/Error.h"
#include "utilities/libarchive/Reader.h"

Expand All @@ -18,33 +18,10 @@
#include <frozen/set.h>
#include <frozen/string.h>
#include <frozen/unordered_map.h>
#include <openssl/md5.h>
#include <openssl/sha.h>

constexpr static frozen::unordered_map<frozen::string, frozen::string, 20>
m_desc_to_info_mapping {{"NAME", "pkgname"},
{"BASE", "pkgbase"},
{"VERSION", "pkgver"},
{"DESC", "pkgdesc"},
{"ISIZE", "size"},
{"URL", "url"},
{"LICENSE", "license"},
{"ARCH", "arch"},
{"BUILDDATE", "builddate"},
{"PACKAGER", "packager"},
{"REPLACES", "replaces"},
{"CONFLICTS", "conflict"},
{"PROVIDES", "provides"},
{"DEPENDS", "depend"},
{"MAKEDEPENDS", "makedepend"},
{"CHECKDEPENDS", "checkdepend"},
{"OPTDEPENDS", "optdepend"},
{"GROUPS", "groups"},
{"BACKUP", "backup"},
{"INSTALL", "install"}};
#include <optional>

namespace bxt::Utilities::AlpmDb {
std::optional<std::string> Desc::get(const std::string &key) const {
std::optional<std::string> Desc::get(const std::string& key) const {
auto prepared_key = fmt::format("%{}%\n", key);
auto value_begin = desc.find(prepared_key);

Expand All @@ -58,8 +35,8 @@ std::optional<std::string> Desc::get(const std::string &key) const {
return desc.substr(value_begin, value_end - value_begin);
}

Desc::Result<Desc> Desc::parse_package(const std::filesystem::path &filepath,
const std::string &signature,
Desc::Result<Desc> Desc::parse_package(const std::filesystem::path& filepath,
const std::string& signature,
bool create_files) {
std::ostringstream desc;
std::ostringstream files;
Expand All @@ -79,7 +56,7 @@ Desc::Result<Desc> Desc::parse_package(const std::filesystem::path &filepath,
PkgInfo package_info;

bool found = false;
for (auto &[header, entry] : file_reader) {
for (auto& [header, entry] : file_reader) {
if (!header) { continue; }
std::string pathname = archive_entry_pathname(*header);

Expand Down Expand Up @@ -119,36 +96,9 @@ Desc::Result<Desc> Desc::parse_package(const std::filesystem::path &filepath,
ParseError(ParseError::ErrorType::NoPackageInfo));
}

constexpr static char format_string[] = "%{}%\n{}\n\n";
DescFormatter formatter {package_info, filepath, signature};

desc << fmt::format(format_string, "FILENAME",
filepath.filename().string());
desc << fmt::format(format_string, "CSIZE",
std::to_string(std::filesystem::file_size(filepath)));

if (!signature.empty()) {
desc << fmt::format(format_string, "PGPSIG",
bxt::Utilities::b64_encode(signature));
}

desc << fmt::format(
format_string, "MD5SUM",
bxt::hash_from_file<MD5, MD5_DIGEST_LENGTH>(filepath.string()));
desc << fmt::format(
format_string, "SHA256SUM",
bxt::hash_from_file<SHA256, SHA256_DIGEST_LENGTH>(filepath.string()));

for (const auto &mapping : m_desc_to_info_mapping) {
auto values = package_info.values(
std::string {mapping.second.data(), mapping.second.size()});

if (values.size() == 0) continue;

desc << fmt::format(
format_string,
std::string {mapping.first.data(), mapping.first.size()},
boost::join(values, "\n"));
}
desc << formatter.format();

return Desc {.desc = desc.str(), .files = files.str()};
}
Expand Down
58 changes: 58 additions & 0 deletions daemon/utilities/alpmdb/DescFormatter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* === This file is part of bxt ===
*
* SPDX-FileCopyrightText: 2024 Artem Grinev <[email protected]>
* SPDX-License-Identifier: AGPL-3.0-or-later
*
*/

#include "DescFormatter.h"

#include "utilities/base64.h"
#include "utilities/hash_from_file.h"

#include <sstream>

namespace bxt::Utilities::AlpmDb {

// This function formats package information into a desc contents string
// It follows the format used by repo-add.sh in pacman for compatibility:
// https://gitlab.archlinux.org/pacman/pacman/-/blob/6ba5c20e7629ae9bdd7ceaf5a45484c434363ec5/scripts/repo-add.sh.in#L296-326
std::string DescFormatter::format() const {
std::ostringstream oss;
oss << format_entry<"FILENAME">(m_filepath.filename().string());
oss << format_pkginfo_entry<"NAME", "pkgname">();
oss << format_pkginfo_entry<"BASE", "pkgbase">();
oss << format_pkginfo_entry<"VERSION", "pkgver">();
oss << format_pkginfo_entry<"DESC", "pkgdesc">();
oss << format_pkginfo_entry<"GROUPS", "groups">();
oss << format_entry<"CSIZE">(
std::to_string(std::filesystem::file_size(m_filepath)));
oss << format_pkginfo_entry<"ISIZE", "size">();

// add checksums
oss << format_entry<"MD5SUM">(
bxt::hash_from_file<MD5, MD5_DIGEST_LENGTH>(m_filepath.string()));

oss << format_entry<"SHA256SUM">(
bxt::hash_from_file<SHA256, SHA256_DIGEST_LENGTH>(m_filepath.string()));

// add PGP sig
if (!m_signature.empty()) {
oss << format_entry<"PGPSIG">(bxt::Utilities::b64_encode(m_signature));
}

oss << format_pkginfo_entry<"URL", "url">();
oss << format_pkginfo_entry<"LICENSE", "license">();
oss << format_pkginfo_entry<"ARCH", "arch">();
oss << format_pkginfo_entry<"BUILDDATE", "builddate">();
oss << format_pkginfo_entry<"PACKAGER", "packager">();
oss << format_pkginfo_entry<"REPLACES", "replaces">();
oss << format_pkginfo_entry<"CONFLICTS", "conflict">();
oss << format_pkginfo_entry<"PROVIDES", "provides">();
oss << format_pkginfo_entry<"DEPENDS", "depend">();
oss << format_pkginfo_entry<"OPTDEPENDS", "optdepend">();
oss << format_pkginfo_entry<"MAKEDEPENDS", "makedepend">();
oss << format_pkginfo_entry<"CHECKDEPENDS", "checkdepend">();
return oss.str();
}
} // namespace bxt::Utilities::AlpmDb
53 changes: 53 additions & 0 deletions daemon/utilities/alpmdb/DescFormatter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* === This file is part of bxt ===
*
* SPDX-FileCopyrightText: 2024 Artem Grinev <[email protected]>
* SPDX-License-Identifier: AGPL-3.0-or-later
*
*/
#pragma once

#include "utilities/FixedString.h"
#include "utilities/alpmdb/PkgInfo.h"

#include <boost/algorithm/string/join.hpp>
#include <filesystem>
#include <fmt/format.h>
#include <openssl/md5.h>
#include <openssl/sha.h>
#include <string>

namespace bxt::Utilities::AlpmDb {
class DescFormatter {
public:
DescFormatter(PkgInfo m_pkg_info,
std::filesystem::path m_filepath,
std::string m_signature)
: m_pkg_info(std::move(m_pkg_info)),
m_filepath(std::move(m_filepath)),
m_signature(std::move(m_signature)) {}

static constexpr char format_string[] = "%{}%\n{}\n\n";

template<FixedString desc_field, FixedString pkginfo_field>
std::string format_pkginfo_entry() const {
auto values = m_pkg_info.values(pkginfo_field.buf);
if (values.empty()) { return ""; }

return fmt::format(format_string, desc_field.buf,
boost::join(values, "\n"));
}
template<FixedString desc_field>
std::string format_entry(const std::string& value) const {
if (value.empty()) return "";
return fmt::format(format_string, desc_field.buf, value);
}

std::string format() const;

private:
PkgInfo m_pkg_info;
std::filesystem::path m_filepath;
std::string m_signature;
};

} // namespace bxt::Utilities::AlpmDb
18 changes: 5 additions & 13 deletions daemon/utilities/alpmdb/PkgInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,14 @@ void PkgInfo::parse(std::string_view contents) {
std::string_view key = line.substr(0, delim_pos);
std::string_view value = line.substr(delim_pos + 3);

m_values.insert({std::string(key), std::string(value)});
m_values[std::string(key)].emplace_back(value);
}
}

std::set<std::string> PkgInfo::values(const std::string &key) {
std::set<std::string> result;
auto iterator_range = m_values.equal_range(key);

static auto select2nd = []<typename TValue>(const TValue &val) {
return val.second;
};

std::transform(iterator_range.first, iterator_range.second,
std::inserter(result, result.end()), select2nd);

return result;
std::vector<std::string> PkgInfo::values(const std::string& key) const {
auto it = m_values.find(key);
if (it != m_values.end()) { return it->second; }
return {};
}

} // namespace bxt::Utilities::AlpmDb
6 changes: 3 additions & 3 deletions daemon/utilities/alpmdb/PkgInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/
#pragma once

#include <set>
#include <string>
#include <unordered_map>
#include <vector>

namespace bxt::Utilities::AlpmDb {

Expand All @@ -17,10 +17,10 @@ class PkgInfo {
PkgInfo() = default;
void parse(std::string_view contents);

std::set<std::string> values(const std::string& key);
std::vector<std::string> values(const std::string& key) const;

private:
std::unordered_multimap<std::string, std::string> m_values;
std::unordered_map<std::string, std::vector<std::string>> m_values;
};

} // namespace bxt::Utilities::AlpmDb
1 change: 1 addition & 0 deletions dbcli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_executable(
dbcli
dbcli.cpp ../daemon/core/domain/enums/PoolLocation.cpp
../daemon/utilities/alpmdb/Desc.cpp ../daemon/utilities/alpmdb/PkgInfo.cpp
../daemon/utilities/alpmdb/DescFormatter.cpp
../daemon/utilities/libarchive/Reader.cpp)

target_link_libraries(dbcli PRIVATE ${CONAN_LIBS})
Expand Down

0 comments on commit 65cca92

Please sign in to comment.