libzypp  15.3.0
SystemCheck.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 #include <fstream>
14 #include <vector>
15 
16 #include "zypp/base/LogTools.h"
17 #include "zypp/base/IOStream.h"
18 #include "zypp/base/String.h"
19 
20 #include "zypp/ZYppFactory.h"
21 #include "zypp/ZConfig.h"
22 #include "zypp/Pathname.h"
23 #include "zypp/PathInfo.h"
25 
26 using namespace std;
27 
29 namespace zypp
30 {
31 
32  Pathname _file = "";
33  Pathname _dir = "";
36 
37  typedef vector<string> CapList;
38 
39  const SystemCheck & SystemCheck::instance()
40  {
41  static SystemCheck _val;
42  return _val;
43  }
44 
45 
46  SystemCheck::SystemCheck() {
47  if (_file.empty()) {
48  _file = ZConfig::instance().solver_checkSystemFile();
49  loadFile(_file);
50  }
51  if (_dir.empty()) {
52  _dir = ZConfig::instance().solver_checkSystemFileDir();
53  loadFiles();
54  }
55  }
56 
57  bool SystemCheck::setFile(const Pathname & file) const{
58  MIL << "Setting checkFile to : " << file << endl;
59  _file = file;
60  loadFile(_file);
61  return true;
62  }
63 
64  bool SystemCheck::setDir(const Pathname & dir) const {
65  MIL << "Setting checkFile directory to : " << dir << endl;
66  loadFile(_file);
67  _dir = dir;
68  loadFiles();
69  return true;
70  }
71 
72  const Pathname & SystemCheck::file() {
73  return _file;
74  }
75 
76  const Pathname & SystemCheck::dir() {
77  return _dir;
78  }
79 
80  const CapabilitySet & SystemCheck::requiredSystemCap() const{
81  return _require;
82  }
83 
84  const CapabilitySet & SystemCheck::conflictSystemCap() const{
85  return _conflict;
86  }
87 
88  bool SystemCheck::loadFile(Pathname & file, bool reset_caps) const{
89  Target_Ptr trg( getZYpp()->getTarget() );
90  if ( trg )
91  file = trg->assertRootPrefix( file );
92 
93  PathInfo pi( file );
94  if ( ! pi.isFile() ) {
95  WAR << "Can't read " << file << " " << pi << endl;
96  return false;
97  }
98 
99  if (reset_caps) {
100  _require.clear();
101  _conflict.clear();
102  }
103 
104  std::ifstream infile( file.c_str() );
105  for( iostr::EachLine in( infile ); in; in.next() ) {
106  std::string l( str::trim(*in) );
107  if ( ! l.empty() && l[0] != '#' )
108  {
109  CapList capList;
110  str::split( l, back_inserter(capList), ":" );
111  if (capList.size() == 2 ) {
112  CapList::iterator it = capList.begin();
113  if (*it == "requires") {
114  _require.insert(Capability(*(it+1)));
115  } else if (*it == "conflicts") {
116  _conflict.insert(Capability(*(it+1)));
117  } else {
118  ERR << "Wrong parameter: " << l << endl;
119  }
120  } else {
121  ERR << "Wrong line: " << l << endl;
122  }
123  }
124  }
125  MIL << "Read " << pi << endl;
126  return true;
127  }
128 
129  bool SystemCheck::loadFiles() const {
130 
132  [this](const Pathname & dir_r, const char *const & name_r)->bool
133  {
134  const std::string wanted = ".check";
135  Pathname pth = dir_r/name_r;
136  if (pth.extension() != wanted) {
137  MIL << "Skipping " << pth << " (not a *.check file)" << endl;
138  return true;
139  }
140  else {
141  MIL << "Reading " << pth << endl;
142  return loadFile(pth, false /* do not reset caps */);
143  }
144  });
145  return true;
146  }
147 
148 
149  /******************************************************************
150  **
151  ** FUNCTION NAME : operator<<
152  ** FUNCTION TYPE : std::ostream &
153  */
154  std::ostream & operator<<( std::ostream & str, const SystemCheck & obj )
155  {
156  str << _file << endl;
157  str << "requires" << endl;
158  for (CapabilitySet::const_iterator it = _require.begin(); it != _require.end(); ++it)
159  str << " " << *it << endl;
160 
161  str << "conflicts" << endl;
162  for (CapabilitySet::const_iterator it = _conflict.begin(); it != _conflict.end(); ++it)
163  str << " " << *it << endl;
164 
165  return str;
166  }
167 
169 } // namespace zypp
vector< string > CapList
Definition: SystemCheck.cc:37
#define MIL
Definition: Logger.h:64
bool next()
Advance to next line.
Definition: IOStream.cc:72
unsigned split(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=" \t")
Split line_r into words.
Definition: String.h:471
String related utilities and Regular expression matching.
Definition: Arch.h:330
Simple lineparser: Traverse each line in a file.
Definition: IOStream.h:111
std::tr1::unordered_set< Capability > CapabilitySet
Definition: Capability.h:33
#define ERR
Definition: Logger.h:66
CapabilitySet _require
Definition: SystemCheck.cc:34
std::string trim(const std::string &s, const Trim trim_r)
Definition: String.cc:213
#define WAR
Definition: Logger.h:65
Pathname _dir
Definition: SystemCheck.cc:33
std::ostream & operator<<(std::ostream &str, const zypp::shared_ptr< void > &obj)
Definition: PtrTypes.h:134
Save and restore locale set from file.
Definition: SystemCheck.h:29
A sat capability.
Definition: Capability.h:59
CapabilitySet _conflict
Definition: SystemCheck.cc:35
int dirForEach(const Pathname &dir_r, function< bool(const Pathname &, const char *const)> fnc_r)
Invoke callback function fnc_r for each entry in directory dir_r.
Definition: PathInfo.cc:552
Pathname _file
Definition: SystemCheck.cc:32
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1