libzypp 17.31.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>
18#include <zypp-core/parser/ParseException>
19
20#include <zypp/HistoryLogData.h>
21
22using std::endl;
23
25namespace zypp
26{
27 using parser::ParseException;
28
30 //
31 // class HistoryActionID
32 //
34
35 const HistoryActionID HistoryActionID::NONE (HistoryActionID::NONE_e);
44
45 HistoryActionID::HistoryActionID(const std::string & strval_r)
46 : _id(parse(strval_r))
47 {}
48
49 HistoryActionID::ID HistoryActionID::parse( const std::string & strval_r )
50 {
51 typedef std::map<std::string,ID> MapType;
52 static MapType _table;
53 if ( _table.empty() )
54 {
55 // initialize it
56 _table["install"] = INSTALL_e;
57 _table["remove"] = REMOVE_e;
58 _table["radd"] = REPO_ADD_e;
59 _table["rremove"] = REPO_REMOVE_e;
60 _table["ralias"] = REPO_CHANGE_ALIAS_e;
61 _table["rurl"] = REPO_CHANGE_URL_e;
62 _table["command"] = STAMP_COMMAND_e;
64 _table["NONE"] = _table["none"] = NONE_e;
65 }
66
67 MapType::const_iterator it = _table.find( strval_r );
68 if ( it != _table.end() )
69 return it->second;
70 // else:
71 WAR << "Unknown history action ID '" + strval_r + "'" << endl;
72 return NONE_e;
73 }
74
75
76 const std::string & HistoryActionID::asString( bool pad ) const
77 {
78 typedef std::pair<std::string,std::string> PairType;
79 typedef std::map<ID, PairType> MapType;
80 static MapType _table;
81 if ( _table.empty() )
82 {
83 // initialize it pad(7) (for now)
84 _table[INSTALL_e] = PairType( "install" , "install" );
85 _table[REMOVE_e] = PairType( "remove" , "remove " );
86 _table[REPO_ADD_e] = PairType( "radd" , "radd " );
87 _table[REPO_REMOVE_e] = PairType( "rremove" , "rremove" );
88 _table[REPO_CHANGE_ALIAS_e] = PairType( "ralias" , "ralias " );
89 _table[REPO_CHANGE_URL_e] = PairType( "rurl" , "rurl " );
90 _table[STAMP_COMMAND_e] = PairType( "command" , "command" );
91 _table[PATCH_STATE_CHANGE_e]= PairType( "patch" , "patch " );
92 _table[NONE_e] = PairType( "NONE" , "NONE " );
93 }
94
95 return( pad ? _table[_id].second : _table[_id].first );
96 }
97
98 std::ostream & operator << (std::ostream & str, const HistoryActionID & id)
99 { return str << id.asString(); }
100
102
104 //
105 // class HistoryLogData::Impl
106 //
109 {
110 public:
111 Impl( FieldVector & fields_r, size_type expect_r )
112 {
113 _checkFields( fields_r, expect_r );
114 _field.swap( fields_r );
115 // For whatever reason writer is ' '-padding the action field
116 // but we don't want to modify the vector before we moved it.
119 }
120
121 Impl( FieldVector & fields_r, HistoryActionID action_r, size_type expect_r )
122 {
123 _checkFields( fields_r, expect_r );
124 // For whatever reason writer is ' '-padding the action field
125 // but we don't want to modify the vector before we moved it.
126 std::string trimmed( str::trim( fields_r[ACTION_INDEX] ) );
127 _action = HistoryActionID( trimmed );
128 if ( _action != action_r )
129 {
130 ZYPP_THROW( ParseException( str::form( "Bad action id. Got %s, expected %s.",
131 _action.asString().c_str(),
132 action_r.asString().c_str() ) ) );
133 }
134 _field.swap( fields_r );
135 // now adjust action field:
136 _field[ACTION_INDEX] = trimmed;
137 }
138
139 void _checkFields( const FieldVector & fields_r, size_type expect_r )
140 {
141 if ( expect_r < 2 ) // at least 2 fields (date and action) are required
142 expect_r = 2;
143 if ( fields_r.size() < expect_r )
144 {
145 ZYPP_THROW( ParseException( str::form( "Bad number of fields. Got %zd, expected at least %zd.",
146 fields_r.size(),
147 expect_r ) ) );
148 }
149 try
150 {
152 }
153 catch ( const std::exception & excpt )
154 {
155 ZYPP_THROW( ParseException( excpt.what() ) ); // invalid date format
156 }
157 // _action handled later
158 }
159
160 public:
164 };
165
167 //
168 // class HistoryLogData
169 //
171
173 : _pimpl( new Impl( fields_r, expect_r ) )
174 {}
175
177 : _pimpl( new Impl( fields_r, expectedId_r, expect_r ) )
178 {}
179
181 {}
182
184 {
185 if ( fields_r.size() >= 2 )
186 {
187 // str::trim( _field[ACTION_INDEX] );
188 switch ( HistoryActionID( str::trim( fields_r[ACTION_INDEX] ) ).toEnum() )
189 {
190#define OUTS(E,T) case HistoryActionID::E: return Ptr( new T( fields_r ) ); break;
191 OUTS( INSTALL_e, HistoryLogDataInstall );
192 OUTS( REMOVE_e, HistoryLogDataRemove );
193 OUTS( REPO_ADD_e, HistoryLogDataRepoAdd );
194 OUTS( REPO_REMOVE_e, HistoryLogDataRepoRemove );
195 OUTS( REPO_CHANGE_ALIAS_e, HistoryLogDataRepoAliasChange );
196 OUTS( REPO_CHANGE_URL_e, HistoryLogDataRepoUrlChange );
197 OUTS( STAMP_COMMAND_e, HistoryLogDataStampCommand );
198 OUTS( PATCH_STATE_CHANGE_e, HistoryLogPatchStateChange );
199#undef OUTS
200 // intentionally no default:
202 break;
203 }
204 }
205 // unknown action or invalid fields? Ctor will accept or throw.
206 return Ptr( new HistoryLogData( fields_r ) );
207 }
208
210 { return _pimpl->_field.empty(); }
211
213 { return _pimpl->_field.size(); }
214
216 { return _pimpl->_field.begin(); }
217
219 { return _pimpl->_field.end(); }
220
221 const std::string & HistoryLogData::optionalAt( size_type idx_r ) const
222 {
223 static const std::string _empty;
224 return( idx_r < size() ? _pimpl->_field[idx_r] : _empty );
225 }
226
227 const std::string & HistoryLogData::at( size_type idx_r ) const
228 { return _pimpl->_field.at( idx_r ); }
229
230
232 { return _pimpl->_date; }
233
235 { return _pimpl->_action; }
236
237
238 std::ostream & operator<<( std::ostream & str, const HistoryLogData & obj )
239 { return str << str::joinEscaped( obj.begin(), obj.end(), '|' ); }
240
242 // class HistoryLogDataInstall
245 : HistoryLogData( fields_r )
246 {}
247 std::string HistoryLogDataInstall::name() const { return optionalAt( NAME_INDEX ); }
250 std::string HistoryLogDataInstall::reqby() const { return optionalAt( REQBY_INDEX ); }
254
256 // class HistoryLogPatchStateChange
259 : HistoryLogData( fields_r )
260 {}
261 std::string HistoryLogPatchStateChange::name() const { return optionalAt( NAME_INDEX ); }
270
272 // class HistoryLogDataRemove
275 : HistoryLogData( fields_r )
276 {}
277 std::string HistoryLogDataRemove::name() const { return optionalAt( NAME_INDEX ); }
280 std::string HistoryLogDataRemove::reqby() const { return optionalAt( REQBY_INDEX ); }
282
284 // class HistoryLogDataRepoAdd
287 : HistoryLogData( fields_r )
288 {}
289 std::string HistoryLogDataRepoAdd::alias() const { return optionalAt( ALIAS_INDEX ); }
292
294 // class HistoryLogDataRepoRemove
297 : HistoryLogData( fields_r )
298 {}
299 std::string HistoryLogDataRepoRemove::alias() const { return optionalAt( ALIAS_INDEX ); }
301
303 // class HistoryLogDataRepoAliasChange
306 : HistoryLogData( fields_r )
307 {}
311
313 // class HistoryLogDataRepoUrlChange
316 : HistoryLogData( fields_r )
317 {}
321
323 // class HistoryLogDataStampCommand
326 : HistoryLogData( fields_r )
327 {}
331
332} // namespace zypp
#define OUTS(E, T)
#define HISTORY_LOG_DATE_FORMAT
Architecture.
Definition: Arch.h:37
Store and operate on date (time_t).
Definition: Date.h:33
Edition represents [epoch:]version[-release]
Definition: Edition.h:61
A zypp history log line for an installed packaged.
HistoryLogDataInstall(FieldVector &fields_r)
Ctor moving FieldVector (via swap).
std::string userdata() const
userdata/transactionID
@ EDITION_INDEX
package edition
@ REQBY_INDEX
requested by (user@hostname, pid:appname, or empty (solver))
@ ARCH_INDEX
package architecture
@ USERDATA_INDEX
userdata/transactionID
@ REPOALIAS_INDEX
repository providing the package
@ CHEKSUM_INDEX
package checksum
CheckSum checksum() const
package checksum
std::string name() const
package name
Arch arch() const
package architecture
std::string reqby() const
requested by (user@hostname, pid:appname, or empty (solver))
std::string repoAlias() const
repository providing the package
Edition edition() const
package edition
A zypp history log line for a removed packge.
Arch arch() const
package architecture
@ REQBY_INDEX
requested by (user@hostname, pid:appname, or empty (solver))
@ ARCH_INDEX
package architecture
@ USERDATA_INDEX
userdata/transactionID
@ EDITION_INDEX
package edition
HistoryLogDataRemove(FieldVector &fields_r)
Ctor moving FieldVector (via swap).
Edition edition() const
package edition
std::string name() const
package name
std::string userdata() const
userdata/transactionID
std::string reqby() const
requested by (user@hostname, pid:appname, or empty (solver))
A zypp history log line for an added repository.
@ ALIAS_INDEX
repository alias
@ USERDATA_INDEX
userdata/transactionID
std::string alias() const
repository alias
Url url() const
repository url
HistoryLogDataRepoAdd(FieldVector &fields_r)
Ctor moving FieldVector (via swap).
std::string userdata() const
userdata/transactionID
A zypp history log line for a repo alias change.
std::string userdata() const
userdata/transactionID
std::string oldAlias() const
repositories old alias
@ NEWALIAS_INDEX
repositories new alias
@ USERDATA_INDEX
userdata/transactionID
@ OLDALIAS_INDEX
repositories old alias
std::string newAlias() const
repositories new alias
HistoryLogDataRepoAliasChange(FieldVector &fields_r)
Ctor moving FieldVector (via swap).
A zypp history log line for a removed repository.
@ USERDATA_INDEX
userdata/transactionID
HistoryLogDataRepoRemove(FieldVector &fields_r)
Ctor moving FieldVector (via swap).
std::string alias() const
repository alias
std::string userdata() const
userdata/transactionID
A zypp history log line for a repo url change.
Url newUrl() const
repositories new url
std::string alias() const
repository alias
@ NEWURL_INDEX
repositories new url
@ USERDATA_INDEX
userdata/transactionID
std::string userdata() const
userdata/transactionID
HistoryLogDataRepoUrlChange(FieldVector &fields_r)
Ctor moving FieldVector (via swap).
A zypp history log line identifying the program that triggered the following commit.
std::string command() const
the commandline executed
std::string executedBy() const
executed by user@hostname
HistoryLogDataStampCommand(FieldVector &fields_r)
Ctor moving FieldVector (via swap).
@ COMMAND_INDEX
the commandline executed
@ USER_INDEX
executed by user@hostname
@ USERDATA_INDEX
userdata/transactionID
std::string userdata() const
userdata/transactionID
Impl(FieldVector &fields_r, size_type expect_r)
void _checkFields(const FieldVector &fields_r, size_type expect_r)
Impl(FieldVector &fields_r, HistoryActionID action_r, size_type expect_r)
A zypp history log line split into fields.
HistoryActionID action() const
HistoryActionID (or NONE_e if unknown)
FieldVector::const_iterator const_iterator
FieldVector::size_type size_type
shared_ptr< HistoryLogData > Ptr
static Ptr create(FieldVector &fields_r)
Factory method creating HistoryLogData classes.
const_iterator end() const
Iterator pointing behind the last element in vector.
virtual ~HistoryLogData()
Dtor.
const std::string & at(size_type idx_r) const
Access (required) field by number.
const std::string & optionalAt(size_type idx_r) const
Access (optional) field by number.
const_iterator begin() const
Iterator pointing to 1st element in vector (or end()).
@ ACTION_INDEX
HistoryActionID.
bool empty() const
Whether FieldVector is empty.
Date date() const
date
RWCOW_pointer< Impl > _pimpl
Implementation class.
HistoryLogData(FieldVector &fields_r, size_type expect_r=2)
Ctor moving FieldVector (via swap).
std::vector< std::string > FieldVector
size_type size() const
Number of fields in vector.
A zypp history log line for an installed packaged.
std::string repoAlias() const
repository providing the package
std::string name() const
package name
std::string userdata() const
userdata/transactionID
Patch::Category category() const
Arch arch() const
package architecture
@ OLDSTATE_INDEX
the state of the patch before the change
@ NEWSTATE_INDEX
the state of the patch after the change
@ REPOALIAS_INDEX
repository providing the patch
@ USERDATA_INDEX
userdata/transactionID
Edition edition() const
package edition
Patch::SeverityFlag severity() const
HistoryLogPatchStateChange(FieldVector &fields_r)
Ctor moving FieldVector (via swap).
Category categoryEnum() const
This patch's category as enum of wellknown categories.
Definition: Patch.cc:49
SeverityFlag
Possible severity levels for (security) patches.
Definition: Patch.h:76
SeverityFlag severityFlag() const
Severity string mapped to an enum.
Definition: Patch.cc:142
Url manipulation class.
Definition: Url.h:92
String related utilities and Regular expression matching.
static std::map< std::string, ServiceType::Type > _table
Definition: ServiceType.cc:21
std::string joinEscaped(TIterator begin, TIterator end, const char sep_r=' ')
Join strings using separator sep_r, quoting or escaping the values.
Definition: String.h:798
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:36
std::string trim(const std::string &s, const Trim trim_r)
Definition: String.cc:223
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition: SerialNumber.cc:52
Enumeration of known history actions.
static const HistoryActionID REPO_REMOVE
static const HistoryActionID PATCH_STATE_CHANGE
const std::string & asString(bool pad=false) const
static const HistoryActionID REMOVE
static const HistoryActionID NONE
static const HistoryActionID STAMP_COMMAND
static const HistoryActionID REPO_ADD
static const HistoryActionID REPO_CHANGE_ALIAS
static const HistoryActionID REPO_CHANGE_URL
static HistoryActionID::ID parse(const std::string &strval_r)
static const HistoryActionID INSTALL
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:428
#define WAR
Definition: Logger.h:97