/
SystemCheck.cc
170 lines (144 loc) · 4.87 KB
/
SystemCheck.cc
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
/*---------------------------------------------------------------------\
| ____ _ __ __ ___ |
| |__ / \ / / . \ . \ |
| / / \ V /| _/ _/ |
| / /__ | | | | | | |
| /_____||_| |_| |_| |
| |
\---------------------------------------------------------------------*/
/** \file zypp/solver/detail/SystemCheck.cc
*
*/
#include <iostream>
#include <fstream>
#include <vector>
#include "zypp/base/LogTools.h"
#include "zypp/base/IOStream.h"
#include "zypp/base/String.h"
#include "zypp/ZYppFactory.h"
#include "zypp/ZConfig.h"
#include "zypp/Pathname.h"
#include "zypp/PathInfo.h"
#include "zypp/solver/detail/SystemCheck.h"
using namespace std;
///////////////////////////////////////////////////////////////////
namespace zypp
{ /////////////////////////////////////////////////////////////////
Pathname _file = "";
Pathname _dir = "";
CapabilitySet _require;
CapabilitySet _conflict;
typedef vector<string> CapList;
const SystemCheck & SystemCheck::instance()
{
static SystemCheck _val;
return _val;
}
SystemCheck::SystemCheck() {
if (_file.empty()) {
_file = ZConfig::instance().solver_checkSystemFile();
loadFile(_file);
}
if (_dir.empty()) {
_dir = ZConfig::instance().solver_checkSystemFileDir();
loadFiles();
}
}
bool SystemCheck::setFile(const Pathname & file) const{
MIL << "Setting checkFile to : " << file << endl;
_file = file;
loadFile(_file);
return true;
}
bool SystemCheck::setDir(const Pathname & dir) const {
MIL << "Setting checkFile directory to : " << dir << endl;
loadFile(_file);
_dir = dir;
loadFiles();
return true;
}
const Pathname & SystemCheck::file() {
return _file;
}
const Pathname & SystemCheck::dir() {
return _dir;
}
const CapabilitySet & SystemCheck::requiredSystemCap() const{
return _require;
}
const CapabilitySet & SystemCheck::conflictSystemCap() const{
return _conflict;
}
bool SystemCheck::loadFile(Pathname & file, bool reset_caps) const{
Target_Ptr trg( getZYpp()->getTarget() );
if ( trg )
file = trg->assertRootPrefix( file );
PathInfo pi( file );
if ( ! pi.isFile() ) {
WAR << "Can't read " << file << " " << pi << endl;
return false;
}
if (reset_caps) {
_require.clear();
_conflict.clear();
}
std::ifstream infile( file.c_str() );
for( iostr::EachLine in( infile ); in; in.next() ) {
std::string l( str::trim(*in) );
if ( ! l.empty() && l[0] != '#' )
{
CapList capList;
str::split( l, back_inserter(capList), ":" );
if (capList.size() == 2 ) {
CapList::iterator it = capList.begin();
if (*it == "requires") {
_require.insert(Capability(*(it+1)));
} else if (*it == "conflicts") {
_conflict.insert(Capability(*(it+1)));
} else {
ERR << "Wrong parameter: " << l << endl;
}
} else {
ERR << "Wrong line: " << l << endl;
}
}
}
MIL << "Read " << pi << endl;
return true;
}
bool SystemCheck::loadFiles() const {
filesystem::dirForEach(_dir,
[this](const Pathname & dir_r, const char *const & name_r)->bool
{
const std::string wanted = ".check";
Pathname pth = dir_r/name_r;
if (pth.extension() != wanted) {
MIL << "Skipping " << pth << " (not a *.check file)" << endl;
return true;
}
else {
MIL << "Reading " << pth << endl;
return loadFile(pth, false /* do not reset caps */);
}
});
return true;
}
/******************************************************************
**
** FUNCTION NAME : operator<<
** FUNCTION TYPE : std::ostream &
*/
std::ostream & operator<<( std::ostream & str, const SystemCheck & obj )
{
str << _file << endl;
str << "requires" << endl;
for (CapabilitySet::const_iterator it = _require.begin(); it != _require.end(); ++it)
str << " " << *it << endl;
str << "conflicts" << endl;
for (CapabilitySet::const_iterator it = _conflict.begin(); it != _conflict.end(); ++it)
str << " " << *it << endl;
return str;
}
/////////////////////////////////////////////////////////////////
} // namespace zypp
///////////////////////////////////////////////////////////////////