Imported Upstream version 17.23.0
[platform/upstream/libzypp.git] / zypp / HistoryLogData.cc
index a66154a..0307a5d 100644 (file)
 
 using namespace std;
 
+///////////////////////////////////////////////////////////////////
 namespace zypp
 {
   using parser::ParseException;
 
-
   ///////////////////////////////////////////////////////////////////
   //
-  //    CLASS NAME : HistoryActionID
+  //   class HistoryActionID
   //
   ///////////////////////////////////////////////////////////////////
 
@@ -39,6 +39,8 @@ namespace zypp
   const HistoryActionID HistoryActionID::REPO_REMOVE           (HistoryActionID::REPO_REMOVE_e);
   const HistoryActionID HistoryActionID::REPO_CHANGE_ALIAS     (HistoryActionID::REPO_CHANGE_ALIAS_e);
   const HistoryActionID HistoryActionID::REPO_CHANGE_URL       (HistoryActionID::REPO_CHANGE_URL_e);
+  const HistoryActionID HistoryActionID::STAMP_COMMAND         (HistoryActionID::STAMP_COMMAND_e);
+  const HistoryActionID HistoryActionID::PATCH_STATE_CHANGE     (HistoryActionID::PATCH_STATE_CHANGE_e);
 
   HistoryActionID::HistoryActionID(const std::string & strval_r)
     : _id(parse(strval_r))
@@ -57,16 +59,17 @@ namespace zypp
       _table["rremove"]        = REPO_REMOVE_e;
       _table["ralias"] = REPO_CHANGE_ALIAS_e;
       _table["rurl"]   = REPO_CHANGE_URL_e;
+      _table["command"]        = STAMP_COMMAND_e;
+      _table["patch"]  = PATCH_STATE_CHANGE_e;
       _table["NONE"]   = _table["none"] = NONE_e;
     }
 
     MapType::const_iterator it = _table.find( strval_r );
-    if ( it == _table.end() )
-    {
-      WAR << "Unknown history action ID '" + strval_r + "'" << endl;
-      return NONE_e;
-    }
-    return it->second;
+    if ( it != _table.end() )
+      return it->second;
+    // else:
+    WAR << "Unknown history action ID '" + strval_r + "'" << endl;
+    return NONE_e;
   }
 
 
@@ -84,6 +87,8 @@ namespace zypp
       _table[REPO_REMOVE_e]       = PairType( "rremove"        , "rremove" );
       _table[REPO_CHANGE_ALIAS_e] = PairType( "ralias" , "ralias " );
       _table[REPO_CHANGE_URL_e]   = PairType( "rurl"   , "rurl   " );
+      _table[STAMP_COMMAND_e]     = PairType( "command"        , "command" );
+      _table[PATCH_STATE_CHANGE_e]= PairType( "patch"   , "patch  " );
       _table[NONE_e]              = PairType( "NONE"   , "NONE   " );
     }
 
@@ -95,237 +100,234 @@ namespace zypp
 
   ///////////////////////////////////////////////////////////////////
 
-
-  /////////////////////////////////////////////////////////////////////
-  //
-  // CLASS NAME: HistoryItem
-  //
-  /////////////////////////////////////////////////////////////////////
-
-  HistoryItem::HistoryItem(FieldVector & fields)
-  {
-    if (fields.size() <= 2)
-      ZYPP_THROW(ParseException(
-        str::form("Bad number of fields. Got %zd, expected more than %d.",
-          fields.size(), 2)));
-
-    date = Date(fields[0], HISTORY_LOG_DATE_FORMAT);
-    action = HistoryActionID(str::trim(fields[1]));
-  }
-
-  void HistoryItem::dumpTo(ostream & str) const
-  {
-    str << date.form(HISTORY_LOG_DATE_FORMAT) << "|" << action.asString();
-  }
-
-  ostream & operator<<(ostream & str, const HistoryItem & obj)
-  {
-    obj.dumpTo(str);
-    return str;
-  }
-
-
-  /////////////////////////////////////////////////////////////////////
-  //
-  // CLASS NAME: HistoryItemInstall
-  //
-  /////////////////////////////////////////////////////////////////////
-
-  HistoryItemInstall::HistoryItemInstall(FieldVector & fields)
-    : HistoryItem(fields)
-  {
-    if (fields.size() != 8)
-      ZYPP_THROW(ParseException(
-        str::form("Bad number of fields. Got %zu, expected %u.",
-          fields.size(), 8)));
-
-    name      = fields[2];
-    edition   = Edition(fields[3]);
-    arch      = Arch(fields[4]);
-    reqby     = fields[5];
-    repoalias = fields[6];
-    checksum  = CheckSum::sha(fields[7]);
-  }
-
-  void HistoryItemInstall::dumpTo(ostream & str) const
-  {
-    HistoryItem::dumpTo(str);
-    str << "|"
-      << name << "|"
-      << edition << "|"
-      << arch << "|"
-      << reqby << "|"
-      << repoalias << "|"
-      << checksum;
-  }
-
-  ostream & operator<<(ostream & str, const HistoryItemInstall & obj)
-  {
-    obj.dumpTo(str);
-    return str;
-  }
-
-
-  /////////////////////////////////////////////////////////////////////
+  ///////////////////////////////////////////////////////////////////
   //
-  // CLASS NAME: HistoryItemRemove
+  //   class HistoryLogData::Impl
   //
-  /////////////////////////////////////////////////////////////////////
-
-  HistoryItemRemove::HistoryItemRemove(FieldVector & fields)
-    : HistoryItem(fields)
+  ///////////////////////////////////////////////////////////////////
+  class HistoryLogData::Impl
   {
-    if (fields.size() != 6)
-      ZYPP_THROW(ParseException(
-        str::form("Bad number of fields. Got %zu, expected %u.",
-          fields.size(), 6)));
-
-    name      = fields[2];
-    edition   = Edition(fields[3]);
-    arch      = Arch(fields[4]);
-    reqby     = fields[5];
-  }
+  public:
+    Impl( FieldVector & fields_r, size_type expect_r )
+    {
+      _checkFields( fields_r, expect_r );
+      _field.swap( fields_r );
+      // For whatever reason writer is ' '-padding the action field
+      // but we don't want to modify the vector before we moved it.
+      _field[ACTION_INDEX] = str::trim( _field[ACTION_INDEX] );
+      _action = HistoryActionID( _field[ACTION_INDEX] );
+    }
 
-  void HistoryItemRemove::dumpTo(ostream & str) const
-  {
-    HistoryItem::dumpTo(str);
-    str << "|"
-      << name << "|"
-      << edition << "|"
-      << arch << "|"
-      << reqby;
-  }
+    Impl( FieldVector & fields_r, HistoryActionID action_r, size_type expect_r )
+    {
+      _checkFields( fields_r, expect_r );
+      // For whatever reason writer is ' '-padding the action field
+      // but we don't want to modify the vector before we moved it.
+      std::string trimmed( str::trim( fields_r[ACTION_INDEX] ) );
+      _action = HistoryActionID( trimmed );
+      if ( _action != action_r )
+      {
+       ZYPP_THROW( ParseException( str::form( "Bad action id. Got %s, expected %s.",
+                                              _action.asString().c_str(),
+                                              action_r.asString().c_str() ) ) );
+      }
+      _field.swap( fields_r );
+      // now adjust action field:
+      _field[ACTION_INDEX] = trimmed;
+    }
 
-  ostream & operator<<(ostream & str, const HistoryItemRemove & obj)
-  {
-    obj.dumpTo(str);
-    return str;
-  }
+    void _checkFields( const FieldVector & fields_r, size_type expect_r )
+    {
+      if ( expect_r < 2 )      // at least 2 fields (date and action) are required
+       expect_r = 2;
+      if ( fields_r.size() < expect_r )
+      {
+       ZYPP_THROW( ParseException( str::form( "Bad number of fields. Got %zd, expected at least %zd.",
+                                              fields_r.size(),
+                                              expect_r ) ) );
+      }
+      try
+      {
+       _date = Date( fields_r[DATE_INDEX], HISTORY_LOG_DATE_FORMAT );
+      }
+      catch ( const std::exception & excpt )
+      {
+       ZYPP_THROW( ParseException( excpt.what() ) );   // invalid date format
+      }
+      // _action handled later
+    }
 
+  public:
+    FieldVector        _field;
+    Date               _date;
+    HistoryActionID    _action;
+  };
 
-  /////////////////////////////////////////////////////////////////////
+  ///////////////////////////////////////////////////////////////////
   //
-  // CLASS NAME: HistoryItemRepoAdd
+  //   class HistoryLogData
   //
-  /////////////////////////////////////////////////////////////////////
+  ///////////////////////////////////////////////////////////////////
 
-  HistoryItemRepoAdd::HistoryItemRepoAdd(FieldVector & fields)
-    : HistoryItem(fields)
-  {
-    if (fields.size() != 4)
-      ZYPP_THROW(ParseException(
-        str::form("Bad number of fields. Got %zu, expected %u.",
-          fields.size(), 4)));
+  HistoryLogData::HistoryLogData( FieldVector & fields_r, size_type expect_r )
+  : _pimpl( new Impl( fields_r, expect_r ) )
+  {}
 
-    alias = fields[2];
-    url = Url(fields[3]);
-  }
+  HistoryLogData::HistoryLogData( FieldVector & fields_r, HistoryActionID expectedId_r, size_type expect_r )
+  : _pimpl( new Impl( fields_r, expectedId_r, expect_r ) )
+  {}
 
-  void HistoryItemRepoAdd::dumpTo(ostream & str) const
-  {
-    HistoryItem::dumpTo(str);
-    str << "|"
-      << alias << "|"
-      << url;
-  }
+  HistoryLogData::~HistoryLogData()
+  {}
 
-  ostream & operator<<(ostream & str, const HistoryItemRepoAdd & obj)
+  HistoryLogData::Ptr HistoryLogData::create( FieldVector & fields_r )
   {
-    obj.dumpTo(str);
-    return str;
+    if ( fields_r.size() >= 2 )
+    {
+      // str::trim( _field[ACTION_INDEX] );
+      switch ( HistoryActionID( str::trim( fields_r[ACTION_INDEX] ) ).toEnum() )
+      {
+#define OUTS(E,T) case HistoryActionID::E: return Ptr( new T( fields_r ) ); break;
+       OUTS( INSTALL_e,                HistoryLogDataInstall );
+       OUTS( REMOVE_e,                 HistoryLogDataRemove );
+       OUTS( REPO_ADD_e,               HistoryLogDataRepoAdd );
+       OUTS( REPO_REMOVE_e,            HistoryLogDataRepoRemove );
+       OUTS( REPO_CHANGE_ALIAS_e,      HistoryLogDataRepoAliasChange );
+       OUTS( REPO_CHANGE_URL_e,        HistoryLogDataRepoUrlChange );
+       OUTS( STAMP_COMMAND_e,          HistoryLogDataStampCommand );
+        OUTS( PATCH_STATE_CHANGE_e,     HistoryLogPatchStateChange );
+#undef OUTS
+       // intentionally no default:
+       case HistoryActionID::NONE_e:
+         break;
+      }
+    }
+    // unknown action or invalid fields? Ctor will accept or throw.
+    return Ptr( new HistoryLogData( fields_r ) );
   }
 
+  bool HistoryLogData::empty() const
+  { return _pimpl->_field.empty(); }
 
-  /////////////////////////////////////////////////////////////////////
-  //
-  // CLASS NAME: HistoryItemRepoRemove
-  //
-  /////////////////////////////////////////////////////////////////////
-
-  HistoryItemRepoRemove::HistoryItemRepoRemove(FieldVector & fields)
-    : HistoryItem(fields)
-  {
-    if (fields.size() != 3)
-      ZYPP_THROW(ParseException(
-        str::form("Bad number of fields. Got %zu, expected %u.",
-          fields.size(), 3)));
+  HistoryLogData::size_type HistoryLogData::size() const
+  { return _pimpl->_field.size(); }
 
-    alias = fields[2];
-  }
+  HistoryLogData::const_iterator HistoryLogData::begin() const
+  { return _pimpl->_field.begin(); }
 
-  void HistoryItemRepoRemove::dumpTo(ostream & str) const
-  {
-    HistoryItem::dumpTo(str);
-    str << "|" << alias;
-  }
+  HistoryLogData::const_iterator HistoryLogData::end() const
+  { return _pimpl->_field.end(); }
 
-  ostream & operator<<(ostream & str, const HistoryItemRepoRemove & obj)
+  const std::string & HistoryLogData::optionalAt( size_type idx_r ) const
   {
-    obj.dumpTo(str);
-    return str;
+    static const std::string _empty;
+    return( idx_r < size() ? _pimpl->_field[idx_r] : _empty );
   }
 
+  const std::string & HistoryLogData::at( size_type idx_r ) const
+  { return _pimpl->_field.at( idx_r ); }
 
-  /////////////////////////////////////////////////////////////////////
-  //
-  // CLASS NAME: HistoryItemRepoAliasChange
-  //
-  /////////////////////////////////////////////////////////////////////
 
-  HistoryItemRepoAliasChange::HistoryItemRepoAliasChange(FieldVector & fields)
-    : HistoryItem(fields)
-  {
-    if (fields.size() != 4)
-      ZYPP_THROW(ParseException(
-        str::form("Bad number of fields. Got %zu, expected %u.",
-          fields.size(), 4)));
+  Date HistoryLogData::date() const
+  { return _pimpl->_date; }
 
-    oldalias = fields[2];
-    newalias = fields[3];
-  }
+  HistoryActionID HistoryLogData::action() const
+  { return _pimpl->_action; }
 
-  void HistoryItemRepoAliasChange::dumpTo(ostream & str) const
-  {
-    HistoryItem::dumpTo(str);
-    str << "|" << oldalias << "|" << newalias;
-  }
 
-  ostream & operator<<(ostream & str, const HistoryItemRepoAliasChange & obj)
-  {
-    obj.dumpTo(str);
-    return str;
-  }
+  std::ostream & operator<<( std::ostream & str, const HistoryLogData & obj )
+  { return str << str::joinEscaped( obj.begin(), obj.end(), '|' ); }
 
+  ///////////////////////////////////////////////////////////////////
+  //   class HistoryLogDataInstall
+  ///////////////////////////////////////////////////////////////////
+    HistoryLogDataInstall::HistoryLogDataInstall( FieldVector & fields_r )
+    : HistoryLogData( fields_r )
+    {}
+    std::string        HistoryLogDataInstall::name()           const { return optionalAt( NAME_INDEX ); }
+    Edition    HistoryLogDataInstall::edition()        const { return Edition( optionalAt( EDITION_INDEX ) ); }
+    Arch       HistoryLogDataInstall::arch()           const { return Arch( optionalAt( ARCH_INDEX ) ); }
+    std::string        HistoryLogDataInstall::reqby()          const { return optionalAt( REQBY_INDEX ); }
+    std::string        HistoryLogDataInstall::repoAlias()      const { return optionalAt( REPOALIAS_INDEX ); }
+    CheckSum   HistoryLogDataInstall::checksum()       const { return optionalAt( CHEKSUM_INDEX ); }
+    std::string        HistoryLogDataInstall::userdata()       const { return optionalAt( USERDATA_INDEX ); }
+
+    ///////////////////////////////////////////////////////////////////
+    // class HistoryLogPatchStateChange
+    ///////////////////////////////////////////////////////////////////
+    HistoryLogPatchStateChange::HistoryLogPatchStateChange( FieldVector & fields_r )
+      : HistoryLogData( fields_r )
+    {}
+    std::string        HistoryLogPatchStateChange::name()      const { return optionalAt( NAME_INDEX ); }
+    Edition    HistoryLogPatchStateChange::edition()   const { return Edition( optionalAt( EDITION_INDEX ) ); }
+    Arch       HistoryLogPatchStateChange::arch()      const { return Arch( optionalAt( ARCH_INDEX ) ); }
+    std::string        HistoryLogPatchStateChange::repoAlias() const { return optionalAt( REPOALIAS_INDEX ); }
+    Patch::SeverityFlag HistoryLogPatchStateChange::severity() const { return Patch::severityFlag( optionalAt( SEVERITY_INDEX ) ); }
+    Patch::Category HistoryLogPatchStateChange::category() const { return Patch::categoryEnum( optionalAt( CATEGORY_INDEX ) ); }
+    string HistoryLogPatchStateChange::oldstate() const { return optionalAt( OLDSTATE_INDEX ); }
+    string HistoryLogPatchStateChange::newstate() const { return optionalAt( NEWSTATE_INDEX ); }
+    std::string        HistoryLogPatchStateChange::userdata()  const { return optionalAt( USERDATA_INDEX ); }
 
-  /////////////////////////////////////////////////////////////////////
-  //
-  // CLASS NAME: HistoryItemRepoUrlChange
-  //
-  /////////////////////////////////////////////////////////////////////
-
-  HistoryItemRepoUrlChange::HistoryItemRepoUrlChange(FieldVector & fields)
-    : HistoryItem(fields)
-  {
-    if (fields.size() != 4)
-      ZYPP_THROW(ParseException(
-        str::form("Bad number of fields. Got %zu, expected %u.",
-          fields.size(), 4)));
+  ///////////////////////////////////////////////////////////////////
+  //   class HistoryLogDataRemove
+  ///////////////////////////////////////////////////////////////////
+    HistoryLogDataRemove::HistoryLogDataRemove( FieldVector & fields_r )
+    : HistoryLogData( fields_r )
+    {}
+    std::string        HistoryLogDataRemove::name()            const { return optionalAt( NAME_INDEX ); }
+    Edition    HistoryLogDataRemove::edition()         const { return Edition( optionalAt( EDITION_INDEX ) ); }
+    Arch       HistoryLogDataRemove::arch()            const { return Arch( optionalAt( ARCH_INDEX ) ); }
+    std::string        HistoryLogDataRemove::reqby()           const { return optionalAt( REQBY_INDEX ); }
+    std::string        HistoryLogDataRemove::userdata()        const { return optionalAt( USERDATA_INDEX ); }
 
-    alias = fields[2];
-    newurl = Url(fields[3]);
-  }
+  ///////////////////////////////////////////////////////////////////
+  //   class HistoryLogDataRepoAdd
+  ///////////////////////////////////////////////////////////////////
+    HistoryLogDataRepoAdd::HistoryLogDataRepoAdd( FieldVector & fields_r )
+    : HistoryLogData( fields_r )
+    {}
+    std::string        HistoryLogDataRepoAdd::alias()          const { return optionalAt( ALIAS_INDEX ); }
+    Url                HistoryLogDataRepoAdd::url()            const { return optionalAt( URL_INDEX ); }
+    std::string        HistoryLogDataRepoAdd::userdata()       const { return optionalAt( USERDATA_INDEX ); }
 
-  void HistoryItemRepoUrlChange::dumpTo(ostream & str) const
-  {
-    HistoryItem::dumpTo(str);
-    str << "|" << alias << "|" << newurl;
-  }
+  ///////////////////////////////////////////////////////////////////
+  //   class HistoryLogDataRepoRemove
+  ///////////////////////////////////////////////////////////////////
+    HistoryLogDataRepoRemove::HistoryLogDataRepoRemove( FieldVector & fields_r )
+    : HistoryLogData( fields_r )
+    {}
+    std::string        HistoryLogDataRepoRemove::alias()       const { return optionalAt( ALIAS_INDEX ); }
+    std::string        HistoryLogDataRepoRemove::userdata()    const { return optionalAt( USERDATA_INDEX ); }
 
-  ostream & operator<<(ostream & str, const HistoryItemRepoUrlChange & obj)
-  {
-    obj.dumpTo(str);
-    return str;
-  }
+  ///////////////////////////////////////////////////////////////////
+  //   class HistoryLogDataRepoAliasChange
+  ///////////////////////////////////////////////////////////////////
+    HistoryLogDataRepoAliasChange::HistoryLogDataRepoAliasChange( FieldVector & fields_r )
+    : HistoryLogData( fields_r )
+    {}
+    std::string        HistoryLogDataRepoAliasChange::oldAlias()       const { return optionalAt( OLDALIAS_INDEX ); }
+    std::string        HistoryLogDataRepoAliasChange::newAlias()       const { return optionalAt( NEWALIAS_INDEX ); }
+    std::string        HistoryLogDataRepoAliasChange::userdata()       const { return optionalAt( USERDATA_INDEX ); }
 
+  ///////////////////////////////////////////////////////////////////
+  //   class HistoryLogDataRepoUrlChange
+  ///////////////////////////////////////////////////////////////////
+    HistoryLogDataRepoUrlChange::HistoryLogDataRepoUrlChange( FieldVector & fields_r )
+    : HistoryLogData( fields_r )
+    {}
+    std::string        HistoryLogDataRepoUrlChange::alias()    const { return optionalAt( ALIAS_INDEX ); }
+    Url                HistoryLogDataRepoUrlChange::newUrl()   const { return optionalAt( NEWURL_INDEX ); }
+    std::string        HistoryLogDataRepoUrlChange::userdata() const { return optionalAt( USERDATA_INDEX ); }
 
-}
+  ///////////////////////////////////////////////////////////////////
+  //   class HistoryLogDataStampCommand
+  ///////////////////////////////////////////////////////////////////
+    HistoryLogDataStampCommand::HistoryLogDataStampCommand( FieldVector & fields_r )
+    : HistoryLogData( fields_r )
+    {}
+    std::string        HistoryLogDataStampCommand::executedBy()        const { return optionalAt( USER_INDEX ); }
+    std::string HistoryLogDataStampCommand::command()          const { return optionalAt( COMMAND_INDEX ); }
+    std::string        HistoryLogDataStampCommand::userdata()          const { return optionalAt( USERDATA_INDEX ); }
+
+} // namespace zypp
+///////////////////////////////////////////////////////////////////