libzypp  13.10.6
HistoryLogData.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
9 
13 #include <sstream>
14 
15 #include "zypp/base/PtrTypes.h"
16 #include "zypp/base/String.h"
17 #include "zypp/base/Logger.h"
19 
20 #include "zypp/HistoryLogData.h"
21 
22 using namespace std;
23 
25 namespace zypp
26 {
27  using parser::ParseException;
28 
30  //
31  // class HistoryActionID
32  //
34 
35  const HistoryActionID HistoryActionID::NONE (HistoryActionID::NONE_e);
36  const HistoryActionID HistoryActionID::INSTALL (HistoryActionID::INSTALL_e);
37  const HistoryActionID HistoryActionID::REMOVE (HistoryActionID::REMOVE_e);
38  const HistoryActionID HistoryActionID::REPO_ADD (HistoryActionID::REPO_ADD_e);
39  const HistoryActionID HistoryActionID::REPO_REMOVE (HistoryActionID::REPO_REMOVE_e);
40  const HistoryActionID HistoryActionID::REPO_CHANGE_ALIAS (HistoryActionID::REPO_CHANGE_ALIAS_e);
41  const HistoryActionID HistoryActionID::REPO_CHANGE_URL (HistoryActionID::REPO_CHANGE_URL_e);
42 
43  HistoryActionID::HistoryActionID(const std::string & strval_r)
44  : _id(parse(strval_r))
45  {}
46 
47  HistoryActionID::ID HistoryActionID::parse( const std::string & strval_r )
48  {
49  typedef std::map<std::string,ID> MapType;
50  static MapType _table;
51  if ( _table.empty() )
52  {
53  // initialize it
54  _table["install"] = INSTALL_e;
55  _table["remove"] = REMOVE_e;
56  _table["radd"] = REPO_ADD_e;
57  _table["rremove"] = REPO_REMOVE_e;
58  _table["ralias"] = REPO_CHANGE_ALIAS_e;
59  _table["rurl"] = REPO_CHANGE_URL_e;
60  _table["NONE"] = _table["none"] = NONE_e;
61  }
62 
63  MapType::const_iterator it = _table.find( strval_r );
64  if ( it != _table.end() )
65  return it->second;
66  // else:
67  WAR << "Unknown history action ID '" + strval_r + "'" << endl;
68  return NONE_e;
69  }
70 
71 
72  const std::string & HistoryActionID::asString( bool pad ) const
73  {
74  typedef std::pair<std::string,std::string> PairType;
75  typedef std::map<ID, PairType> MapType;
76  static MapType _table;
77  if ( _table.empty() )
78  {
79  // initialize it pad(7) (for now)
80  _table[INSTALL_e] = PairType( "install" , "install" );
81  _table[REMOVE_e] = PairType( "remove" , "remove " );
82  _table[REPO_ADD_e] = PairType( "radd" , "radd " );
83  _table[REPO_REMOVE_e] = PairType( "rremove" , "rremove" );
84  _table[REPO_CHANGE_ALIAS_e] = PairType( "ralias" , "ralias " );
85  _table[REPO_CHANGE_URL_e] = PairType( "rurl" , "rurl " );
86  _table[NONE_e] = PairType( "NONE" , "NONE " );
87  }
88 
89  return( pad ? _table[_id].second : _table[_id].first );
90  }
91 
92  std::ostream & operator << (std::ostream & str, const HistoryActionID & id)
93  { return str << id.asString(); }
94 
96 
98  //
99  // class HistoryLogData::Impl
100  //
103  {
104  public:
105  Impl( FieldVector & fields_r, size_type expect_r )
106  {
107  _checkFields( fields_r, expect_r );
108  _field.swap( fields_r );
109  // For whatever reason writer is ' '-padding the action field
110  // but we don't want to modify the vector before we moved it.
112  _action = HistoryActionID( _field[ACTION_INDEX] );
113  }
114 
115  Impl( FieldVector & fields_r, HistoryActionID action_r, size_type expect_r )
116  {
117  _checkFields( fields_r, expect_r );
118  // For whatever reason writer is ' '-padding the action field
119  // but we don't want to modify the vector before we moved it.
120  std::string trimmed( str::trim( fields_r[ACTION_INDEX] ) );
121  _action = HistoryActionID( trimmed );
122  if ( _action != action_r )
123  {
124  ZYPP_THROW( ParseException( str::form( "Bad action id. Got %s, expected %s.",
125  _action.asString().c_str(),
126  action_r.asString().c_str() ) ) );
127  }
128  _field.swap( fields_r );
129  // now adjust action field:
130  _field[ACTION_INDEX] = trimmed;
131  }
132 
133  void _checkFields( const FieldVector & fields_r, size_type expect_r )
134  {
135  if ( expect_r < 2 ) // at least 2 fields (date and action) are required
136  expect_r = 2;
137  if ( fields_r.size() < expect_r )
138  {
139  ZYPP_THROW( ParseException( str::form( "Bad number of fields. Got %zd, expected at least %zd.",
140  fields_r.size(),
141  expect_r ) ) );
142  }
143  try
144  {
146  }
147  catch ( const std::exception & excpt )
148  {
149  ZYPP_THROW( ParseException( excpt.what() ) ); // invalid date format
150  }
151  // _action handled later
152  }
153 
154  public:
158  };
159 
161  //
162  // class HistoryLogData
163  //
165 
167  : _pimpl( new Impl( fields_r, expect_r ) )
168  {}
169 
171  : _pimpl( new Impl( fields_r, expectedId_r, expect_r ) )
172  {}
173 
175  {}
176 
178  {
179  if ( fields_r.size() >= 2 )
180  {
181  // str::trim( _field[ACTION_INDEX] );
182  switch ( HistoryActionID( str::trim( fields_r[ACTION_INDEX] ) ).toEnum() )
183  {
184 #define OUTS(E,T) case HistoryActionID::E: return Ptr( new T( fields_r ) ); break;
185  OUTS( INSTALL_e, HistoryLogDataInstall );
186  OUTS( REMOVE_e, HistoryLogDataRemove );
187  OUTS( REPO_ADD_e, HistoryLogDataRepoAdd );
188  OUTS( REPO_REMOVE_e, HistoryLogDataRepoRemove );
189  OUTS( REPO_CHANGE_ALIAS_e, HistoryLogDataRepoAliasChange );
190  OUTS( REPO_CHANGE_URL_e, HistoryLogDataRepoUrlChange );
191 #undef OUTS
192  // intentionally no default:
194  break;
195  }
196  }
197  // unknown action or invalid fields? Ctor will accept or throw.
198  return Ptr( new HistoryLogData( fields_r ) );
199  }
200 
202  { return _pimpl->_field.empty(); }
203 
205  { return _pimpl->_field.size(); }
206 
208  { return _pimpl->_field.begin(); }
209 
211  { return _pimpl->_field.end(); }
212 
213  const std::string & HistoryLogData::optionalAt( size_type idx_r ) const
214  {
215  static const std::string _empty;
216  return( idx_r < size() ? _pimpl->_field[idx_r] : _empty );
217  }
218 
219  const std::string & HistoryLogData::at( size_type idx_r ) const
220  { return _pimpl->_field.at( idx_r ); }
221 
222 
224  { return _pimpl->_date; }
225 
227  { return _pimpl->_action; }
228 
229 
230  std::ostream & operator<<( std::ostream & str, const HistoryLogData & obj )
231  { return str << str::joinEscaped( obj.begin(), obj.end(), '|' ); }
232 
234  // class HistoryLogDataInstall
237  : HistoryLogData( fields_r )
238  {}
239  std::string HistoryLogDataInstall::name() const { return optionalAt( NAME_INDEX ); }
242  std::string HistoryLogDataInstall::reqby() const { return optionalAt( REQBY_INDEX ); }
245  std::string HistoryLogDataInstall::userdata() const { return optionalAt( USERDATA_INDEX ); }
246 
248  // class HistoryLogDataRemove
251  : HistoryLogData( fields_r )
252  {}
253  std::string HistoryLogDataRemove::name() const { return optionalAt( NAME_INDEX ); }
256  std::string HistoryLogDataRemove::reqby() const { return optionalAt( REQBY_INDEX ); }
257  std::string HistoryLogDataRemove::userdata() const { return optionalAt( USERDATA_INDEX ); }
258 
260  // class HistoryLogDataRepoAdd
263  : HistoryLogData( fields_r )
264  {}
265  std::string HistoryLogDataRepoAdd::alias() const { return optionalAt( ALIAS_INDEX ); }
267  std::string HistoryLogDataRepoAdd::userdata() const { return optionalAt( USERDATA_INDEX ); }
268 
270  // class HistoryLogDataRepoRemove
273  : HistoryLogData( fields_r )
274  {}
275  std::string HistoryLogDataRepoRemove::alias() const { return optionalAt( ALIAS_INDEX ); }
277 
279  // class HistoryLogDataRepoAliasChange
282  : HistoryLogData( fields_r )
283  {}
287 
289  // class HistoryLogDataRepoUrlChange
292  : HistoryLogData( fields_r )
293  {}
294  std::string HistoryLogDataRepoUrlChange::alias() const { return optionalAt( ALIAS_INDEX ); }
297 
298 } // namespace zypp
repository providing the package
std::string alias() const
repository alias
std::vector< std::string > FieldVector
static std::map< std::string, ServiceType::Type > _table
Definition: ServiceType.cc:21
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:320
std::string reqby() const
requested by (user, pid:appname, or empty (solver))
std::string userdata() const
userdata/transactionID
Architecture.
Definition: Arch.h:36
Impl(FieldVector &fields_r, HistoryActionID action_r, size_type expect_r)
std::string reqby() const
requested by (user, pid:appname, or empty (solver))
const_iterator begin() const
Iterator pointing to 1st element in vector (or end()).
FieldVector::size_type size_type
Date date() const
date
HistoryLogDataRemove(FieldVector &fields_r)
Ctor moving FieldVector (via swap).
bool empty() const
Whether FieldVector is empty.
std::string repoAlias() const
repository providing the package
HistoryLogData(FieldVector &fields_r, size_type expect_r=2)
Ctor moving FieldVector (via swap).
CheckSum checksum() const
package checksum
std::string alias() const
repository alias
Url newUrl() const
repositories new url
Edition represents [epoch:]version[-release]
Definition: Edition.h:60
RWCOW_pointer< Impl > _pimpl
Implementation class.
A zypp history log line for a removed packge.
Arch arch() const
package architecture
std::string oldAlias() const
repositories old alias
Url url() const
repository url
Arch arch() const
package architecture
std::string joinEscaped(_Iterator begin, _Iterator end, const char sep_r= ' ')
Join strings using separator sep_r, quoting or escaping the values.
Definition: String.h:659
size_type size() const
Number of fields in vector.
HistoryLogDataRepoAliasChange(FieldVector &fields_r)
Ctor moving FieldVector (via swap).
const_iterator end() const
Iterator pointing behind the last element in vector.
virtual ~HistoryLogData()
Dtor.
std::string userdata() const
userdata/transactionID
HistoryLogDataRepoAdd(FieldVector &fields_r)
Ctor moving FieldVector (via swap).
#define OUTS(E, T)
FieldVector::const_iterator const_iterator
Store and operate on date (time_t).
Definition: Date.h:31
A zypp history log line for an installed packaged.
requested by (user, pid:appname, or empty (solver))
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:120
std::string trim(const std::string &s, const Trim trim_r)
Definition: String.cc:204
A zypp history log line for a repo url change.
const std::string & asString(bool pad=false) const
std::string name() const
package name
#define WAR
Definition: Logger.h:48
HistoryActionID action() const
HistoryActionID (or NONE_e if unknown)
HistoryLogDataInstall(FieldVector &fields_r)
Ctor moving FieldVector (via swap).
std::string userdata() const
userdata/transactionID
Edition edition() const
package edition
A zypp history log line for a removed repository.
static Ptr create(FieldVector &fields_r)
Factory method creating HistoryLogData classes.
const std::string & optionalAt(size_type idx_r) const
Access (optional) field by number.
std::string userdata() const
userdata/transactionID
HistoryLogDataRepoUrlChange(FieldVector &fields_r)
Ctor moving FieldVector (via swap).
A zypp history log line split into fieldsEach valid history log line starts with a date and HistoryAc...
static HistoryActionID::ID parse(const std::string &strval_r)
void _checkFields(const FieldVector &fields_r, size_type expect_r)
std::string form(const char *format,...)
Printf style construction of std::string.
Definition: String.cc:34
std::string alias() const
repository alias
requested by (user, pid:appname, or empty (solver))
Impl(FieldVector &fields_r, size_type expect_r)
A zypp history log line for a repo alias change.
std::string newAlias() const
repositories new alias
std::string userdata() const
userdata/transactionID
HistoryLogDataRepoRemove(FieldVector &fields_r)
Ctor moving FieldVector (via swap).
std::string name() const
package name
std::string userdata() const
userdata/transactionID
const std::string & at(size_type idx_r) const
Access (required) field by number.
Edition edition() const
package edition
A zypp history log line for an added repository.
Enumeration of known history actions.
#define HISTORY_LOG_DATE_FORMAT
shared_ptr< HistoryLogData > Ptr
Url manipulation class.
Definition: Url.h:87