File: rpm_package.cpp

package info (click to toggle)
dnf5 5.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,960 kB
  • sloc: cpp: 94,312; python: 3,370; xml: 1,073; ruby: 600; sql: 250; ansic: 232; sh: 104; perl: 62; makefile: 30
file content (250 lines) | stat: -rw-r--r-- 7,501 bytes parent folder | download
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// Libdnf 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libdnf.  If not, see <https://www.gnu.org/licenses/>.


#include "libdnf5/transaction/rpm_package.hpp"

#include "db/rpm.hpp"

#include "libdnf5/rpm/nevra.hpp"
#include "libdnf5/transaction/transaction.hpp"

#include <sstream>


namespace libdnf5::transaction {

class Package::Impl {
private:
    friend Package;
    std::string name;
    std::string epoch;
    std::string version;
    std::string release;
    std::string arch;
};

Package::Package(const Transaction & trans)
    : TransactionItem::TransactionItem(trans),
      p_impl(std::make_unique<Impl>()) {}

Package::Package(const Package & src) : TransactionItem(src), p_impl(new Impl(*src.p_impl)) {}
Package::Package(Package && src) noexcept = default;

Package & Package::operator=(const Package & src) {
    if (this != &src) {
        if (p_impl) {
            *p_impl = *src.p_impl;
        } else {
            p_impl = std::make_unique<Impl>(*src.p_impl);
        }
    }

    return *this;
}
Package & Package::operator=(Package && src) noexcept = default;
Package::~Package() = default;

const std::string & Package::get_name() const noexcept {
    return p_impl->name;
}
const std::string & Package::get_epoch() const noexcept {
    return p_impl->epoch;
}
const std::string & Package::get_release() const noexcept {
    return p_impl->release;
}
const std::string & Package::get_arch() const noexcept {
    return p_impl->arch;
}
const std::string & Package::get_version() const noexcept {
    return p_impl->version;
}

void Package::set_name(const std::string & value) {
    p_impl->name = value;
}
void Package::set_epoch(const std::string & value) {
    p_impl->epoch = value;
}
void Package::set_version(const std::string & value) {
    p_impl->version = value;
}
void Package::set_release(const std::string & value) {
    p_impl->release = value;
}
void Package::set_arch(const std::string & value) {
    p_impl->arch = value;
}

uint32_t Package::get_epoch_int() const {
    if (get_epoch().empty()) {
        return 0;
    }
    return static_cast<uint32_t>(std::stoi(get_epoch()));
}


std::string Package::to_string() const {
    return libdnf5::rpm::to_full_nevra_string(*this);
}


/*
TransactionItemReason Package::resolveTransactionItemReason(
    libdnf5::utils::SQLite3 & conn,
    const std::string & name,
    const std::string & arch,
    [[maybe_unused]] int64_t maxTransactionId) {
    const char * sql = R"**(
        SELECT
            ti.action as action,
            ti.reason as reason
        FROM
            trans_item ti
        JOIN
            trans t ON ti.trans_id = t.id
        JOIN
            rpm i USING (item_id)
        WHERE
            t.state = 1
            AND ti.action not in (3, 5, 7, 10)
            AND i.name = ?
            AND i.arch = ?
        ORDER BY
            ti.trans_id DESC
        LIMIT 1
    )**";

    if (arch != "") {
        libdnf5::utils::SQLite3::Query query(conn, sql);
        query.bindv(name, arch);

        if (query.step() == libdnf5::utils::SQLite3::Statement::StepResult::ROW) {
            auto action = static_cast<TransactionItemAction>(query.get<int64_t>("action"));
            if (action == TransactionItemAction::REMOVE) {
                return TransactionItemReason::UNKNOWN;
            }
            auto reason = static_cast<TransactionItemReason>(query.get<int64_t>("reason"));
            return reason;
        }
    } else {
        const char * arch_sql = R"**(
            SELECT DISTINCT
                arch
            FROM
                rpm
            WHERE
                name = ?
        )**";

        libdnf5::utils::SQLite3::Query arch_query(conn, arch_sql);
        arch_query.bindv(name);

        TransactionItemReason result = TransactionItemReason::UNKNOWN;

        while (arch_query.step() == libdnf5::utils::SQLite3::Statement::StepResult::ROW) {
            auto rpm_arch = arch_query.get<std::string>("arch");

            libdnf5::utils::SQLite3::Query query(conn, sql);
            query.bindv(name, rpm_arch);
            while (query.step() == libdnf5::utils::SQLite3::Statement::StepResult::ROW) {
                auto action = static_cast<TransactionItemAction>(query.get<int64_t>("action"));
                if (action == TransactionItemAction::REMOVE) {
                    continue;
                }
                auto reason = static_cast<TransactionItemReason>(query.get<int64_t>("reason"));
                if (reason > result) {
                    result = reason;
                }
            }
        }
        return result;
    }
    return TransactionItemReason::UNKNOWN;
}
*/

bool Package::operator<(const Package & other) const {
    // TODO(dmach): replace the whole function with a different implementation, preferably an existing code
    // compare epochs
    if (get_epoch_int() < other.get_epoch_int()) {
        return true;
    } else if (get_epoch_int() > other.get_epoch_int()) {
        return false;
    }

    // compare versions
    std::stringstream versionThis(get_version());
    std::stringstream versionOther(other.get_version());

    std::string bufferThis;
    std::string bufferOther;
    while (std::getline(versionThis, bufferThis, '.') && std::getline(versionOther, bufferOther, '.')) {
        int subVersionThis = std::stoi(bufferThis);
        int subVersionOther = std::stoi(bufferOther);
        if (subVersionThis == subVersionOther) {
            continue;
        }
        return subVersionOther > subVersionThis;
    }
    return false;
}

/*
std::vector<int64_t> Package::searchTransactions(
    libdnf5::utils::SQLite3 & conn, const std::vector<std::string> & patterns) {
    std::vector<int64_t> result;

    const char * sql = R"**(
        SELECT DISTINCT
            t.id
        FROM
            trans t
        JOIN
            trans_item ti ON ti.trans_id = t.id
        JOIN
            rpm i USING (item_id)
        WHERE
            t.state = 1
            AND (
                i.name = ?
                OR i.epoch = ?
                OR i.version = ?
                OR i.release = ?
                OR i.arch = ?
            )
        ORDER BY
           trans_id DESC
    )**";
    libdnf5::utils::SQLite3::Query query(conn, sql);
    for (auto pattern : patterns) {
        query.bindv(pattern, pattern, pattern, pattern, pattern);
        while (query.step() == libdnf5::utils::SQLite3::Statement::StepResult::ROW) {
            result.push_back(query.get<int64_t>("id"));
        }
    }
    std::sort(result.begin(), result.end());
    auto last = std::unique(result.begin(), result.end());
    result.erase(last, result.end());
    return result;
}
*/

}  // namespace libdnf5::transaction