1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
/*
* Copyright (C) Serenity Cybersecurity, LLC <license@futurecrew.ru>
* Author: Gleb Popov <arrowd@FreeBSD.org>
*
* Licensed under the GNU General Public License Version 2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#include <cstdlib>
#include <functional>
#include <pk-backend.h>
#include <pkg.h>
#include "Deleters.hpp"
class PackageView
{
public:
PackageView(struct pkg* pkg) {
char* buf;
// TODO: don't we pull too much from pkg eagerly?
pkg_asprintf(&buf, "%n", pkg);
_name = free_deleted_unique_ptr<char>(buf);
pkg_asprintf(&buf, "%v", pkg);
_version = free_deleted_unique_ptr<char>(buf);
pkg_asprintf(&buf, "%q", pkg);
_abi = free_deleted_unique_ptr<char>(buf);
pkg_asprintf(&buf, "%N", pkg);
_reponame = free_deleted_unique_ptr<char>(buf);
pkg_asprintf(&buf, "%c", pkg);
_comment = free_deleted_unique_ptr<char>(buf);
pkg_asprintf(&buf, "%e", pkg);
_descr = free_deleted_unique_ptr<char>(buf);
pkg_asprintf(&buf, "%w", pkg);
_url = free_deleted_unique_ptr<char>(buf);
pkg_asprintf(&buf, "%C%{%Cn%||%}", pkg);
if (buf) {
_categories = g_strfreev_deleted_unique_ptr<gchar*> (g_strsplit(buf, "|", 0));
free(buf);
}
pkg_asprintf(&buf, "%L", pkg);
_license = free_deleted_unique_ptr<char>(buf);
pkg_asprintf(&buf, "%s", pkg);
_flatsize = std::strtoul(buf, nullptr, 10);
free(buf);
pkg_asprintf(&buf, "%x", pkg);
_compressedsize = std::strtoull(buf, nullptr, 10);
free(buf);
}
PackageView(gchar* package_id)
: external_pk_id (package_id), pk_namever(nullptr) {
g_assert (pk_package_id_check (package_id));
pk_id_parts = g_strfreev_deleted_unique_ptr<gchar*> (pk_package_id_split (package_id));
}
PackageView(const PackageView&) = delete;
const gchar* name() const {
if (pk_id_parts)
return pk_id_parts.get()[PK_PACKAGE_ID_NAME];
else
return _name.get();
}
const gchar* version() const {
if (pk_id_parts)
return pk_id_parts.get()[PK_PACKAGE_ID_VERSION];
else
return _version.get();
}
gchar* nameversion() {
if (pk_namever)
return pk_namever.get();
pk_namever = g_free_deleted_unique_ptr<gchar> (g_strconcat(name(), "-", version(), NULL));
return pk_namever.get();
}
const gchar* comment() const {
// comment can only be obtained from pkg*
g_assert (pk_id_parts == nullptr);
return _comment.get();
}
const gchar* description() const {
// description can only be obtained from pkg*
g_assert (pk_id_parts == nullptr);
return _descr.get();
}
const gchar* url() const {
// description can only be obtained from pkg*
g_assert (pk_id_parts == nullptr);
return _url.get();
}
const gchar* arch() const {
if (pk_id_parts)
return pk_id_parts.get()[PK_PACKAGE_ID_ARCH];
else {
// abi has the form of "FreeBSD:13:amd64"
// we want only the last part
const gchar* ptr = _abi.get();
while (*ptr != ':') ptr++;
ptr++;
while (*ptr != ':') ptr++;
ptr++;
return ptr;
}
}
gchar** categories() const {
// licenses can only be obtained from pkg*
g_assert (pk_id_parts == nullptr);
return _categories.get();
}
const gchar* license() const {
// licenses can only be obtained from pkg*
g_assert (pk_id_parts == nullptr);
return _license.get();
}
gulong flatsize() const {
// flatsize can only be obtained from pkg*
g_assert (pk_id_parts == nullptr);
return _flatsize;
}
guint64 compressedsize() const {
// flatsize can only be obtained from pkg*
g_assert (pk_id_parts == nullptr);
return _compressedsize;
}
const gchar* repository() const {
if (pk_id_parts)
return pk_id_parts.get()[PK_PACKAGE_ID_DATA];
else
return _reponame.get();
}
gchar* packageKitId() {
if (external_pk_id)
return external_pk_id;
if (!built_pk_id)
built_pk_id = g_free_deleted_unique_ptr<gchar> (
pk_package_id_build(name(), version(), arch(), repository()));
return built_pk_id.get();
}
private:
free_deleted_unique_ptr<char> _name,
_version, _abi, _reponame, _comment,
_descr, _url, _license;
g_strfreev_deleted_unique_ptr<gchar*> _categories;
gulong _flatsize;
guint64 _compressedsize;
gchar* external_pk_id = nullptr;
g_free_deleted_unique_ptr<gchar> built_pk_id;
g_strfreev_deleted_unique_ptr<gchar*> pk_id_parts;
g_free_deleted_unique_ptr<gchar> pk_namever;
};
|