1 /*
2  * Copyright (C) 2018 Red Hat, Inc.
3  *
4  * Licensed under the GNU Lesser General Public License Version 2.1
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <glib.h>
22 
23 #include "dnf-db.h"
24 #include "dnf-package.h"
25 #include "transaction/Swdb.hpp"
26 
27 /**
28  * dnf_db_ensure_origin_pkg:
29  * @db: a #DnfDb instance.
30  * @pkg: A package to set
31  *
32  * Sets the repo origin on a package if not already set.
33  *
34  * Since: 0.1.0
35  */
36 void
dnf_db_ensure_origin_pkg(DnfDb * db,DnfPackage * pkg)37 dnf_db_ensure_origin_pkg(DnfDb *db, DnfPackage *pkg)
38 {
39     /* already set */
40     if (dnf_package_get_origin(pkg) != NULL)
41         return;
42     if (!dnf_package_installed(pkg))
43         return;
44 
45     /* set from the database if available */
46     auto tmp = db->getRPMRepo(dnf_package_get_nevra(pkg));
47     if (tmp.empty()) {
48         g_debug("no origin for %s", dnf_package_get_package_id(pkg));
49     } else {
50         dnf_package_set_origin(pkg, tmp.c_str());
51     }
52 }
53 
54 /**
55  * dnf_db_ensure_origin_pkglist:
56  * @db: a #DnfDb instance.
57  * @pkglist: A package list to set
58  *
59  * Sets the repo origin on several package if not already set.
60  *
61  * Since: 0.1.0
62  */
63 void
dnf_db_ensure_origin_pkglist(DnfDb * db,GPtrArray * pkglist)64 dnf_db_ensure_origin_pkglist(DnfDb *db, GPtrArray *pkglist)
65 {
66     DnfPackage * pkg;
67     guint i;
68     for (i = 0; i < pkglist->len; i++) {
69         pkg = static_cast<DnfPackage *>(g_ptr_array_index(pkglist, i));
70         dnf_db_ensure_origin_pkg(db, pkg);
71     }
72 }
73