File: dnf4convert.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 (350 lines) | stat: -rw-r--r-- 15,286 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright Contributors to the DNF5 project.
// Copyright (C) 2022 Red Hat, Inc.
// SPDX-License-Identifier: LGPL-2.0-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/dnf5/
//
// 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 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 "dnf4convert.hpp"

#include "config_module.hpp"
#include "utils/sqlite3/sqlite3.hpp"

#include "libdnf5/base/base.hpp"
#include "libdnf5/conf/config_parser.hpp"

#include <filesystem>

namespace libdnf5::dnf4convert {

// Query to get list of installed packages with versions and repoid.
// The inner query selects id of the latest successful transaction item that touched
// each NEVRA. Only REINSTALLED (10) action is skipped because it can be ordered
// after respective REINSTALL action overwriting potential repoid change.
// Packages with the latest action 3 (DOWNGRADED), 5 (OBSOLETED), 7 (UPGRADED), and
// 8 (REMOVE) are eventually skipped as removed from the system.
static constexpr const char * SQL_CURRENTLY_INSTALLED_PACKAGES = R"**(
    SELECT
        "ti"."item_id",
        "ti"."action",
        "ti"."reason",
        "rpm"."name",
        "rpm"."epoch",
        "rpm"."version",
        "rpm"."release",
        "rpm"."arch",
        "repo"."repoid"
    FROM
        "trans_item" "ti"
    JOIN
        "rpm" USING ("item_id")
    JOIN
        "repo" ON ("repo"."id" = "ti"."repo_id")
    WHERE
        "ti"."id" IN (
            SELECT MAX("trans_item"."id")
            FROM "trans_item"
            JOIN "rpm" USING ("item_id")
            JOIN "trans" ON ("trans"."id" = "trans_item"."trans_id")
            WHERE "trans_item"."action" != 10
                  AND "trans_item"."state" = 1
                  AND "trans"."state" = 1
            GROUP BY "rpm"."name", "rpm"."epoch", "rpm"."version", "rpm"."release", "rpm"."arch")
    AND "ti"."action" NOT IN (3, 5, 7, 8)
    ORDER BY "ti"."item_id" DESC
)**";


// Query to get list of currently installed comps groups.
// The inner query select the latest successful transaction item touching each group.
// Only actions 1 (INSTALL), 2 (UPGRADE), and 8 (REMOVE) are taken into account.
static constexpr const char * SQL_CURRENTLY_INSTALLED_GROUPS = R"**(
    SELECT
        "ti"."item_id",
        "ti"."reason",
        "cg"."groupid",
        "cg"."pkg_types"
    FROM
        "trans_item" "ti"
    JOIN
        "comps_group" "cg" USING ("item_id")
    WHERE
        "ti"."id" IN (
            SELECT MAX("trans_item"."id")
            FROM "trans_item"
            JOIN "comps_group" USING ("item_id")
            JOIN "trans" ON ("trans"."id" = "trans_item"."trans_id")
            WHERE "trans_item"."action" IN (1, 6, 8)
                  AND "trans_item"."state" = 1
                  AND "trans"."state" = 1
            GROUP BY "comps_group"."groupid")
        AND "ti"."action" <> 8
)**";


// Query to get list of packages that were installed with given group.
static constexpr const char * SQL_GROUP_PACKAGES = R"**(
SELECT
    DISTINCT "name"
FROM
    "comps_group_package"
WHERE
    "installed" = 1
    AND "group_id" = ?
)**";


// Query to get list of currently installed comps environments.
// The inner query select the latest successful transaction item touching each group.
// Only actions 1 (INSTALL), 2 (UPGRADE), and 8 (REMOVE) are taken into account.
static constexpr const char * SQL_CURRENTLY_INSTALLED_ENVIRONMENTS = R"**(
    SELECT
        "ti"."item_id",
        "ce"."environmentid"
    FROM
        "trans_item" "ti"
    JOIN
        "comps_environment" "ce" USING ("item_id")
    WHERE
        "ti"."id" IN (
            SELECT MAX("trans_item"."id")
            FROM "trans_item"
            JOIN "comps_environment" USING ("item_id")
            JOIN "trans" ON ("trans"."id" = "trans_item"."trans_id")
            WHERE "trans_item"."action" IN (1, 6, 8)
                  AND "trans_item"."state" = 1
                  AND "trans"."state" = 1
            GROUP BY "comps_environment"."environmentid")
        AND "ti"."action" <> 8
)**";


// Query to get list of packages that were installed with given group.
static constexpr const char * SQL_ENVIRONMENT_GROUPS = R"**(
SELECT
    DISTINCT "groupid"
FROM
    "comps_environment_group"
WHERE
    "installed" = 1
    AND "environment_id" = ?
)**";


#ifdef WITH_MODULEMD
std::map<std::string, libdnf5::system::ModuleState> Dnf4Convert::read_module_states() {
    std::map<std::string, libdnf5::system::ModuleState> module_states;

    auto & config = base->get_config();
    std::filesystem::path path =
        config.get_installroot_option().get_value() / std::filesystem::path{MODULES_PERSIST_DIR};

    auto & logger = *base->get_logger();
    std::error_code ec;
    for (const auto & dir_entry : std::filesystem::directory_iterator(path, ec)) {
        auto & module_file = dir_entry.path();
        if ((dir_entry.is_regular_file() || dir_entry.is_symlink()) && (module_file.extension() == ".module")) {
            logger.debug("Loading module state from file \"{}\"", module_file.string());
            libdnf5::ConfigParser parser;
            parser.read(module_file);
            auto d = parser.get_data();
            // each module file contains only one section named by the module name
            auto name = module_file.stem();
            ConfigModule module_config{name};
            module_config.load_from_parser(parser, name, *base->get_vars(), logger);
            libdnf5::system::ModuleState state;
            state.enabled_stream = module_config.stream.get_value();
            state.installed_profiles = module_config.profiles.get_value();
            state.status = libdnf5::module::ModuleStatus::AVAILABLE;
            auto status_value = module_config.state.get_value();
            if (status_value == "enabled") {
                state.status = libdnf5::module::ModuleStatus::ENABLED;
            } else if (status_value == "disabled") {
                state.status = libdnf5::module::ModuleStatus::DISABLED;
            }
            module_states.emplace(name, state);
        }
    }
    return module_states;
}
#endif


bool Dnf4Convert::read_package_states_from_history(
    std::map<std::string, libdnf5::system::PackageState> & package_states,
    std::map<std::string, libdnf5::system::NevraState> & nevra_states,
    std::map<std::string, libdnf5::system::GroupState> & group_states,
    std::map<std::string, libdnf5::system::EnvironmentState> & environment_states) {
    auto & logger = *base->get_logger();
    // get dnf4 history database path (installroot/persistdir/history.sqlite)
    auto & config = base->get_config();
    std::filesystem::path path{config.get_installroot_option().get_value()};
    path /= std::filesystem::path(config.get_persistdir_option().get_value()).relative_path();
    path /= "history.sqlite";

    if (!std::filesystem::exists(path.native())) {
        return false;
    }

    logger.debug("Loading system state from dnf4 history database \"{}\"", path.string());

    // These arrays temporarily hold data read from sqlite database. They are converted
    // to libdnf5::system::* objects once everything is read from db.
    std::vector<std::tuple<rpm::Nevra, transaction::TransactionItemReason, std::string>> installed_packages;
    std::vector<std::tuple<std::string, transaction::TransactionItemReason, std::set<std::string>, int64_t>>
        installed_groups;
    std::vector<std::tuple<std::string, std::set<std::string>>> installed_environments;

    // Auxiliary map to optimalize looking up groups for package
    // map package_name -> {set of group_id}
    std::map<std::string, std::set<std::string>> package_groups;
    // Auxiliary set of actually installed package names. Used to filter out group
    // packages that are not currently installed.
    std::set<std::string> installed_packages_names{};

    // First mine all the data from sqlite database.
    try {
        auto conn = std::make_unique<libdnf5::utils::SQLite3>(path.native());

        auto query_environments =
            std::make_unique<libdnf5::utils::SQLite3::Query>(*conn, SQL_CURRENTLY_INSTALLED_ENVIRONMENTS);
        std::set<std::string> groups_in_environments;
        while (query_environments->step() == libdnf5::utils::SQLite3::Statement::StepResult::ROW) {
            auto environment_id = query_environments->get<std::string>("environmentid");
            auto item_id = query_environments->get<int64_t>("item_id");
            auto grps_query = std::make_unique<libdnf5::utils::SQLite3::Query>(*conn, SQL_ENVIRONMENT_GROUPS);
            grps_query->bindv(item_id);
            std::set<std::string> groups;
            while (grps_query->step() == libdnf5::utils::SQLite3::Statement::StepResult::ROW) {
                std::string groupid = grps_query->get<std::string>("groupid");
                groups.emplace(groupid);
                groups_in_environments.insert(std::move(groupid));
            }
            installed_environments.emplace_back(environment_id, std::move(groups));
        }

        auto query_groups = std::make_unique<libdnf5::utils::SQLite3::Query>(*conn, SQL_CURRENTLY_INSTALLED_GROUPS);
        while (query_groups->step() == libdnf5::utils::SQLite3::Statement::StepResult::ROW) {
            auto group_id = query_groups->get<std::string>("groupid");
            auto item_id = query_groups->get<int64_t>("item_id");
            auto pkg_types = query_groups->get<int64_t>("pkg_types");
            auto pkgs_query = std::make_unique<libdnf5::utils::SQLite3::Query>(*conn, SQL_GROUP_PACKAGES);
            pkgs_query->bindv(item_id);
            std::set<std::string> packages;
            while (pkgs_query->step() == libdnf5::utils::SQLite3::Statement::StepResult::ROW) {
                auto pkg_name = pkgs_query->get<std::string>("name");
                packages.emplace(pkg_name);
                // keep track of every group the package is part of
                package_groups[pkg_name].insert(group_id);
            }
            // reason stored in dnf4 history db is always USER
            // if the group is part of installed environment, mark it as DEPENDENCY
            installed_groups.emplace_back(
                group_id,
                groups_in_environments.contains(group_id) ? transaction::TransactionItemReason::DEPENDENCY
                                                          : transaction::TransactionItemReason::USER,
                std::move(packages),
                pkg_types);
        }

        auto query_pkgs = std::make_unique<libdnf5::utils::SQLite3::Query>(*conn, SQL_CURRENTLY_INSTALLED_PACKAGES);
        while (query_pkgs->step() == libdnf5::utils::SQLite3::Statement::StepResult::ROW) {
            auto reason = static_cast<transaction::TransactionItemReason>(query_pkgs->get<int>("reason"));
            auto repo_id = query_pkgs->get<std::string>("repoid");
            rpm::Nevra pkg_nevra;
            pkg_nevra.set_name(query_pkgs->get<std::string>("name"));
            pkg_nevra.set_epoch(query_pkgs->get<std::string>("epoch"));
            pkg_nevra.set_version(query_pkgs->get<std::string>("version"));
            pkg_nevra.set_release(query_pkgs->get<std::string>("release"));
            pkg_nevra.set_arch(query_pkgs->get<std::string>("arch"));

            installed_packages_names.insert(pkg_nevra.get_name());
            installed_packages.emplace_back(pkg_nevra, reason, repo_id);
        }

    } catch (const libdnf5::utils::SQLite3Error & e) {
        logger.debug("Reading from history database failed: {}", e.what());
        return false;
    }

    // Convert all database data to system::state::* objects

    // Clear the current packages state
    package_states.clear();
    nevra_states.clear();
    group_states.clear();
    environment_states.clear();

    for (const auto & [pkg_nevra, original_reason, repo_id] : installed_packages) {
        // set from_repo attribute in nevras.toml
        libdnf5::system::NevraState nevra_state;
        nevra_state.from_repo = repo_id;
        nevra_states.emplace(rpm::to_nevra_string(pkg_nevra), std::move(nevra_state));

        transaction::TransactionItemReason reason = original_reason;
        if (reason == transaction::TransactionItemReason::GROUP) {
            // group packages need a group they belong to
            if (package_groups.find(pkg_nevra.get_name()) == package_groups.end()) {
                // in case there is no such group, change the reason to USER
                reason = transaction::TransactionItemReason::USER;
            } else {
                continue;
            }
        } else if (reason == transaction::TransactionItemReason::NONE) {
            // Packages installed by another tool (e.g. rpm) can have (once they get
            // upgraded) reason NONE. Change it to EXTERNAL_USER.
            reason = transaction::TransactionItemReason::EXTERNAL_USER;
        }
        // for non-group packages save the reason in packages.toml
        libdnf5::system::PackageState package_state;
        package_state.reason = transaction::transaction_item_reason_to_string(reason);
        package_states.emplace(pkg_nevra.get_name() + "." + pkg_nevra.get_arch(), std::move(package_state));
    }

    // Store the groups state in groups.toml.
    for (const auto & [group_id, reason, candidate_packages, pkg_types] : installed_groups) {
        libdnf5::system::GroupState group_state;
        if (reason == transaction::TransactionItemReason::USER) {
            group_state.userinstalled = true;
        } else {
            group_state.userinstalled = false;
        }
        group_state.package_types = static_cast<libdnf5::comps::PackageType>(pkg_types);
        for (const auto & candidate_pkg : candidate_packages) {
            // store only actually installed group packages
            if (installed_packages_names.contains(candidate_pkg)) {
                group_state.packages.emplace_back(std::move(candidate_pkg));
            }
        }
        group_states.emplace(group_id, std::move(group_state));
    }

    // Finally store the environmental groups state in environments.toml.
    for (const auto & [environment_id, candidate_groups] : installed_environments) {
        libdnf5::system::EnvironmentState environment_state;
        for (const auto & candidate_grp : candidate_groups) {
            // store only actually installed groups
            if (group_states.contains(candidate_grp)) {
                environment_state.groups.emplace_back(std::move(candidate_grp));
            }
        }
        environment_states.emplace(environment_id, std::move(environment_state));
    }

    return true;
}


}  // namespace libdnf5::dnf4convert