1 /*
2  * Copyright (C) 2017 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 "nevra.hpp"
22 #include "hy-nevra.h"
23 #include "dnf-sack.h"
24 
25 #include "regex/regex.hpp"
26 
27 namespace libdnf {
28 
29 #define PKG_NAME "([^:(/=<> ]+)"
30 #define PKG_EPOCH "(([0-9]+):)?"
31 #define PKG_VERSION "([^-:(/=<> ]+)"
32 #define PKG_RELEASE PKG_VERSION
33 #define PKG_ARCH "([^-:.(/=<> ]+)"
34 
35 static const Regex NEVRA_FORM_REGEX[]{
36     Regex("^" PKG_NAME "-" PKG_EPOCH PKG_VERSION "-" PKG_RELEASE "\\." PKG_ARCH "$", REG_EXTENDED),
37     Regex("^" PKG_NAME "-" PKG_EPOCH PKG_VERSION "-" PKG_RELEASE          "()"  "$", REG_EXTENDED),
38     Regex("^" PKG_NAME "-" PKG_EPOCH PKG_VERSION        "()"              "()"  "$", REG_EXTENDED),
39     Regex("^" PKG_NAME      "()()"      "()"            "()"     "\\." PKG_ARCH "$", REG_EXTENDED),
40     Regex("^" PKG_NAME      "()()"      "()"            "()"              "()"  "$", REG_EXTENDED)
41 };
42 
parse(const char * nevraStr,HyForm form)43 bool Nevra::parse(const char * nevraStr, HyForm form)
44 {
45     enum { NAME = 1, EPOCH = 3, VERSION = 4, RELEASE = 5, ARCH = 6, _LAST_ };
46     auto matchResult = NEVRA_FORM_REGEX[form - 1].match(nevraStr, false, _LAST_);
47     if (!matchResult.isMatched() || matchResult.getMatchedLen(NAME) == 0)
48         return false;
49     name = matchResult.getMatchedString(NAME);
50     if (matchResult.getMatchedLen(EPOCH) > 0)
51         epoch = atoi(matchResult.getMatchedString(EPOCH).c_str());
52     else
53         epoch = EPOCH_NOT_SET;
54     version = matchResult.getMatchedString(VERSION);
55     release = matchResult.getMatchedString(RELEASE);
56     arch = matchResult.getMatchedString(ARCH);
57     return true;
58 }
59 
60 void
clear()61 Nevra::clear() noexcept
62 {
63     name.clear();
64     epoch = EPOCH_NOT_SET;
65     version.clear();
66     release.clear();
67     arch.clear();
68 }
69 
70 std::string
getEvr() const71 Nevra::getEvr() const
72 {
73     if (epoch == EPOCH_NOT_SET)
74         return version + "-" + release;
75     return std::to_string(epoch) + ":" + version + "-" + release;
76 }
77 
78 bool
hasJustName() const79 Nevra::hasJustName() const
80 {
81     return !name.empty() && epoch == EPOCH_NOT_SET &&
82         version.empty() && release.empty() && arch.empty();
83 }
84 
85 int
compareEvr(const Nevra & nevra2,DnfSack * sack) const86 Nevra::compareEvr(const Nevra & nevra2, DnfSack *sack) const
87 {
88     return dnf_sack_evr_cmp(sack, getEvr().c_str(), nevra2.getEvr().c_str());
89 }
90 
91 int
compare(const Nevra & nevra2) const92 Nevra::compare(const Nevra & nevra2) const
93 {
94     auto ret = name.compare(nevra2.name);
95     if (ret != 0)
96         return ret;
97     ret = compareEvr(nevra2, nullptr);
98     if (ret != 0)
99         return ret;
100     return arch.compare(nevra2.arch);
101 }
102 
103 }
104 
105 void
hy_nevra_free(HyNevra nevra)106 hy_nevra_free(HyNevra nevra)
107 {
108     delete nevra;
109 }
110