Imported Upstream version 15.0.0
[platform/upstream/libzypp.git] / zypp / RepoInfo.cc
index fcf49d1..0ad8cf4 100644 (file)
 #include <iostream>
 #include <vector>
 
-#include "zypp/base/Logger.h"
+#include "zypp/base/LogTools.h"
 #include "zypp/base/DefaultIntegral.h"
 #include "zypp/parser/xml/XmlEscape.h"
 
 #include "zypp/RepoInfo.h"
-#include "zypp/repo/RepoInfoBaseImpl.h"
+#include "zypp/TriBool.h"
+#include "zypp/Pathname.h"
 #include "zypp/repo/RepoMirrorList.h"
 #include "zypp/ExternalProgram.h"
 #include "zypp/media/MediaAccess.h"
 
-using namespace std;
+#include "zypp/base/IOStream.h"
+#include "zypp/base/InputStream.h"
+#include "zypp/parser/xml/Reader.h"
+
+using std::endl;
 using zypp::xml::escape;
 
 ///////////////////////////////////////////////////////////////////
@@ -34,11 +39,10 @@ namespace zypp
   //   CLASS NAME : RepoInfo::Impl
   //
   /** RepoInfo implementation. */
-  struct RepoInfo::Impl : public repo::RepoInfoBase::Impl
+  struct RepoInfo::Impl
   {
     Impl()
-      : repo::RepoInfoBase::Impl()
-      , gpgcheck(indeterminate)
+      : gpgcheck(indeterminate)
       ,        keeppackages(indeterminate)
       , type(repo::RepoType::NONE_e)
       , emptybaseurls(false)
@@ -64,52 +68,79 @@ namespace zypp
     Pathname licenseTgz() const
     { return metadatapath.empty() ? Pathname() : metadatapath / path / "license.tar.gz"; }
 
-    Url getmirrorListUrl() const
-    {
-      return replacer(mirrorlist_url);
-    }
-
-    Url &setmirrorListUrl()
-    {
-      return mirrorlist_url;
-    }
-
-    const std::set<Url> &baseUrls() const
+    const RepoVariablesReplacedUrlList & baseUrls() const
     {
-      if ( _baseUrls.empty() && ! (getmirrorListUrl().asString().empty()) )
+      const Url & mlurl( _mirrorListUrl.transformed() );       // Variables replaced!
+      if ( _baseUrls.empty() && ! mlurl.asString().empty() )
       {
         emptybaseurls = true;
-        repo::RepoMirrorList *rmirrorlist = NULL;
-
         DBG << "MetadataPath: " << metadatapath << endl;
-        if( metadatapath.empty() )
-          rmirrorlist = new repo::RepoMirrorList (getmirrorListUrl() );
-        else
-          rmirrorlist = new repo::RepoMirrorList (getmirrorListUrl(), metadatapath );
-
-        std::vector<Url> rmurls = rmirrorlist->getUrls();
-        delete rmirrorlist;
-        rmirrorlist = NULL;
-        _baseUrls.insert(rmurls.begin(), rmurls.end());
+       repo::RepoMirrorList rmurls( mlurl, metadatapath );
+       _baseUrls.raw().insert( _baseUrls.raw().end(), rmurls.getUrls().begin(), rmurls.getUrls().end() );
       }
       return _baseUrls;
     }
 
-    std::set<Url> &baseUrls()
-    {
-      return _baseUrls;
-    }
+    RepoVariablesReplacedUrlList & baseUrls()
+    { return _baseUrls; }
 
     bool baseurl2dump() const
+    { return !emptybaseurls && !_baseUrls.empty(); }
+
+
+    void addContent( const std::string & keyword_r )
+    { _keywords.insert( keyword_r ); }
+
+    bool hasContent( const std::string & keyword_r ) const
     {
-      return !emptybaseurls && !_baseUrls.empty();
+      if ( _keywords.empty() && ! metadatapath.empty() )
+      {
+       // HACK directly check master index file until RepoManager offers
+       // some content probing ans zypepr uses it.
+       /////////////////////////////////////////////////////////////////
+       MIL << "Empty keywords...." << metadatapath << endl;
+       Pathname master;
+       if ( PathInfo( (master=metadatapath/"/repodata/repomd.xml") ).isFile() )
+       {
+         //MIL << "GO repomd.." << endl;
+         xml::Reader reader( master );
+         while ( reader.seekToNode( 2, "content" ) )
+         {
+           _keywords.insert( reader.nodeText().asString() );
+           reader.seekToEndNode( 2, "content" );
+         }
+         _keywords.insert( "" );       // valid content in _keywords even if empty
+       }
+       else if ( PathInfo( (master=metadatapath/"/content") ).isFile() )
+       {
+         //MIL << "GO content.." << endl;
+         iostr::forEachLine( InputStream( master ),
+                            [this]( int num_r, std::string line_r )->bool
+                            {
+                              if ( str::startsWith( line_r, "REPOKEYWORDS" ) )
+                             {
+                               std::vector<std::string> words;
+                               if ( str::split( line_r, std::back_inserter(words) ) > 1
+                                 && words[0].length() == 12 /*"REPOKEYWORDS"*/ )
+                               {
+                                 this->_keywords.insert( ++words.begin(), words.end() );
+                               }
+                               return true; // mult. occurrances are ok.
+                             }
+                             return( ! str::startsWith( line_r, "META " ) );   // no need to parse into META section.
+                           } );
+         _keywords.insert( "" );
+       }
+       /////////////////////////////////////////////////////////////////
+      }
+      return( _keywords.find( keyword_r ) != _keywords.end() );
     }
 
-
   public:
     TriBool gpgcheck;
     TriBool keeppackages;
-    Url gpgkey_url;
+    RepoVariablesReplacedUrl _gpgKeyUrl;
+    RepoVariablesReplacedUrl _mirrorListUrl;
     repo::RepoType type;
     Pathname path;
     std::string service;
@@ -121,8 +152,8 @@ namespace zypp
     repo::RepoVariablesUrlReplacer replacer;
 
   private:
-    Url mirrorlist_url;
-    mutable std::set<Url> _baseUrls;
+    mutable RepoVariablesReplacedUrlList _baseUrls;
+    mutable std::set<std::string> _keywords;
 
     friend Impl * rwcowClone<Impl>( const Impl * rhs );
     /** clone for RWCOW_pointer */
@@ -166,81 +197,66 @@ namespace zypp
 
   unsigned RepoInfo::priority() const
   { return _pimpl->priority; }
+
   unsigned RepoInfo::defaultPriority()
   { return Impl::defaultPriority; }
+
   void RepoInfo::setPriority( unsigned newval_r )
-  {
-    _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority;
-  }
+  { _pimpl->priority = newval_r ? newval_r : Impl::defaultPriority; }
 
   void RepoInfo::setGpgCheck( bool check )
-  {
-    _pimpl->gpgcheck = check;
-  }
+  { _pimpl->gpgcheck = check; }
 
-  void RepoInfo::setMirrorListUrl( const Url &url )
-  {
-    _pimpl->setmirrorListUrl() = url;
-  }
+  void RepoInfo::setMirrorListUrl( const Url & url_r ) // Raw
+  { _pimpl->_mirrorListUrl.raw() = url_r; }
 
-  void RepoInfo::setGpgKeyUrl( const Url &url )
-  {
-    _pimpl->gpgkey_url = url;
-  }
+  void RepoInfo::setGpgKeyUrl( const Url & url_r )
+  { _pimpl->_gpgKeyUrl.raw() = url_r; }
 
-  void RepoInfo::addBaseUrl( const Url &url )
+  void RepoInfo::addBaseUrl( const Url & url_r )
   {
-    _pimpl->baseUrls().insert(url);
+    for ( const auto & url : _pimpl->baseUrls().raw() )        // Raw unique!
+      if ( url == url_r )
+       return;
+    _pimpl->baseUrls().raw().push_back( url_r );
   }
 
-  void RepoInfo::setBaseUrl( const Url &url )
+  void RepoInfo::setBaseUrl( const Url & url_r )
   {
-    _pimpl->baseUrls().clear();
-    addBaseUrl(url);
+    _pimpl->baseUrls().raw().clear();
+    _pimpl->baseUrls().raw().push_back( url_r );
   }
 
   void RepoInfo::setPath( const Pathname &path )
-  {
-    _pimpl->path = path;
-  }
+  { _pimpl->path = path; }
 
   void RepoInfo::setType( const repo::RepoType &t )
-  {
-    _pimpl->type = t;
-  }
+  { _pimpl->type = t; }
 
   void RepoInfo::setProbedType( const repo::RepoType &t ) const
   { _pimpl->setProbedType( t ); }
 
 
   void RepoInfo::setMetadataPath( const Pathname &path )
-  {
-    _pimpl->metadatapath = path;
-  }
+  { _pimpl->metadatapath = path; }
 
   void RepoInfo::setPackagesPath( const Pathname &path )
-  {
-    _pimpl->packagespath = path;
-  }
+  { _pimpl->packagespath = path; }
 
   void RepoInfo::setKeepPackages( bool keep )
-  {
-    _pimpl->keeppackages = keep;
-  }
+  { _pimpl->keeppackages = keep; }
 
   void RepoInfo::setService( const std::string& name )
-  {
-    _pimpl->service = name;
-  }
+  { _pimpl->service = name; }
 
-  void RepoInfo::setTargetDistribution(
-      const std::string & targetDistribution)
-  {
-    _pimpl->targetDistro = targetDistribution;
-  }
+  void RepoInfo::setTargetDistribution( const std::string & targetDistribution )
+  { _pimpl->targetDistro = targetDistribution; }
 
   bool RepoInfo::gpgCheck() const
-  { return indeterminate(_pimpl->gpgcheck) ? true : (bool) _pimpl->gpgcheck; }
+  { return indeterminate(_pimpl->gpgcheck) ? true : (bool)_pimpl->gpgcheck; }
+
+  bool RepoInfo::keepPackages() const
+  { return indeterminate(_pimpl->keeppackages) ? false : (bool)_pimpl->keeppackages; }
 
   Pathname RepoInfo::metadataPath() const
   { return _pimpl->metadatapath; }
@@ -251,25 +267,23 @@ namespace zypp
   repo::RepoType RepoInfo::type() const
   { return _pimpl->type; }
 
-  Url RepoInfo::mirrorListUrl() const
-  {
-    return _pimpl->getmirrorListUrl();
-  }
+  Url RepoInfo::mirrorListUrl() const                  // Variables replaced!
+  { return _pimpl->_mirrorListUrl.transformed(); }
 
-  Url RepoInfo::gpgKeyUrl() const
-  { return _pimpl->gpgkey_url; }
+  Url RepoInfo::rawMirrorListUrl() const               // Raw
+  { return _pimpl->_mirrorListUrl.raw(); }
 
-  std::set<Url> RepoInfo::baseUrls() const
-  {
-    RepoInfo::url_set replaced_urls;
-    for ( url_set::const_iterator it = _pimpl->baseUrls().begin();
-          it != _pimpl->baseUrls().end();
-          ++it )
-    {
-      replaced_urls.insert(_pimpl->replacer(*it));
-    }
-    return replaced_urls;
-  }
+  Url RepoInfo::gpgKeyUrl() const                      // Variables replaced!
+  { return _pimpl->_gpgKeyUrl.transformed(); }
+
+  Url RepoInfo::rawGpgKeyUrl() const                   // Raw
+  {  return _pimpl->_gpgKeyUrl.raw(); }
+
+  RepoInfo::url_set RepoInfo::baseUrls() const         // Variables replaced!
+  { return _pimpl->baseUrls().transformed(); }
+
+  RepoInfo::url_set RepoInfo::rawBaseUrls() const      // Raw
+  { return _pimpl->baseUrls().raw(); }
 
   Pathname RepoInfo::path() const
   { return _pimpl->path; }
@@ -280,19 +294,14 @@ namespace zypp
   std::string RepoInfo::targetDistribution() const
   { return _pimpl->targetDistro; }
 
+  Url RepoInfo::rawUrl() const
+  { return( _pimpl->baseUrls().empty() ? Url() : *_pimpl->baseUrls().rawBegin() ); }
+
   RepoInfo::urls_const_iterator RepoInfo::baseUrlsBegin() const
-  {
-    return make_transform_iterator( _pimpl->baseUrls().begin(),
-                                    _pimpl->replacer );
-    //return _pimpl->baseUrls.begin();
-  }
+  { return _pimpl->baseUrls().transformedBegin(); }
 
   RepoInfo::urls_const_iterator RepoInfo::baseUrlsEnd() const
-  {
-    //return _pimpl->baseUrls.end();
-    return make_transform_iterator( _pimpl->baseUrls().end(),
-                                    _pimpl->replacer );
-  }
+  { return _pimpl->baseUrls().transformedEnd(); }
 
   RepoInfo::urls_size_type RepoInfo::baseUrlsSize() const
   { return _pimpl->baseUrls().size(); }
@@ -300,34 +309,56 @@ namespace zypp
   bool RepoInfo::baseUrlsEmpty() const
   { return _pimpl->baseUrls().empty(); }
 
-  // false by default (if not set by setKeepPackages)
-  bool RepoInfo::keepPackages() const
-  {
-    if (indeterminate(_pimpl->keeppackages))
-    {
-      if (_pimpl->baseUrls().empty())
-        return false;
-      else if ( baseUrlsBegin()->schemeIsDownloading() )
-        return true;
-      else
-        return false;
-    }
+  bool RepoInfo::baseUrlSet() const
+  { return _pimpl->baseurl2dump(); }
 
-    return (bool) _pimpl->keeppackages;
-  }
+
+  void RepoInfo::addContent( const std::string & keyword_r )
+  { _pimpl->addContent( keyword_r ); }
+
+  bool RepoInfo::hasContent( const std::string & keyword_r ) const
+  { return _pimpl->hasContent( keyword_r ); }
 
   ///////////////////////////////////////////////////////////////////
 
   bool RepoInfo::hasLicense() const
   {
     Pathname licenseTgz( _pimpl->licenseTgz() );
-    SEC << licenseTgz << endl;
-    SEC << PathInfo(licenseTgz) << endl;
-
     return ! licenseTgz.empty() &&  PathInfo(licenseTgz).isFile();
   }
 
+  bool RepoInfo::needToAcceptLicense() const
+  {
+    static const std::string noAcceptanceFile = "no-acceptance-needed\n";
+    bool accept = true;
+
+    Pathname licenseTgz( _pimpl->licenseTgz() );
+    if ( licenseTgz.empty() || ! PathInfo( licenseTgz ).isFile() )
+      return false;     // no licenses at all
+
+    ExternalProgram::Arguments cmd;
+    cmd.push_back( "tar" );
+    cmd.push_back( "-t" );
+    cmd.push_back( "-z" );
+    cmd.push_back( "-f" );
+    cmd.push_back( licenseTgz.asString() );
+
+    ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
+    for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() )
+    {
+      if ( output == noAcceptanceFile )
+      {
+        accept = false;
+      }
+    }
+    MIL << "License for " << name() << " has to be accepted: " << (accept?"true":"false" ) << endl;
+    return accept;
+  }
+
   std::string RepoInfo::getLicense( const Locale & lang_r )
+  { return const_cast<const RepoInfo *>(this)->getLicense( lang_r );  }
+
+  std::string RepoInfo::getLicense( const Locale & lang_r ) const
   {
     LocaleSet avlocales( getLicenseLocales() );
     if ( avlocales.empty() )
@@ -395,10 +426,6 @@ namespace zypp
         else
           ret.insert( Locale( std::string( output.c_str()+license.size(), output.size()- license.size() - dotTxt.size() ) ) );
       }
-      else
-      {
-        WAR << "  " << output;
-      }
     }
     prog.close();
     return ret;
@@ -411,34 +438,32 @@ namespace zypp
     RepoInfoBase::dumpOn(str);
     if ( _pimpl->baseurl2dump() )
     {
-      for ( urls_const_iterator it = baseUrlsBegin();
-            it != baseUrlsEnd();
-            ++it )
+      for ( const auto & url : _pimpl->baseUrls().raw() )
       {
-        str << "- url         : " << *it << std::endl;
+        str << "- url         : " << url << std::endl;
       }
     }
-    if ( ! (_pimpl->getmirrorListUrl().asString().empty())  )
-    {
-      str << "- mirrorlist  : " << _pimpl->getmirrorListUrl() << std::endl;
-    }
-    str << "- path        : " << path() << std::endl;
+
+    // print if non empty value
+    auto strif( [&] ( const std::string & tag_r, const std::string & value_r ) {
+      if ( ! value_r.empty() )
+       str << tag_r << value_r << std::endl;
+    });
+
+    strif( "- mirrorlist  : ", rawMirrorListUrl().asString() );
+    strif( "- path        : ", path().asString() );
     str << "- type        : " << type() << std::endl;
     str << "- priority    : " << priority() << std::endl;
-
     str << "- gpgcheck    : " << gpgCheck() << std::endl;
-    str << "- gpgkey      : " << gpgKeyUrl() << std::endl;
-    str << "- keeppackages: " << keepPackages() << std::endl;
-    str << "- service     : " << service() << std::endl;
+    strif( "- gpgkey      : ", rawGpgKeyUrl().asString() );
 
-    if (!targetDistribution().empty())
-      str << "- targetdistro: " << targetDistribution() << std::endl;
+    if ( ! indeterminate(_pimpl->keeppackages) )
+      str << "- keeppackages: " << keepPackages() << std::endl;
 
-    if (!metadataPath().empty())
-      str << "- metadataPath: " << metadataPath() << std::endl;
-
-    if (!packagesPath().empty())
-      str << "- packagesPath: " << packagesPath() << std::endl;
+    strif( "- service     : ", service() );
+    strif( "- targetdistro: ", targetDistribution() );
+    strif( "- metadataPath: ", metadataPath().asString() );
+    strif( "- packagesPath: ", packagesPath().asString() );
 
     return str;
   }
@@ -450,19 +475,19 @@ namespace zypp
     if ( _pimpl->baseurl2dump() )
     {
       str << "baseurl=";
-      for ( url_set::const_iterator it = _pimpl->baseUrls().begin();
-            it != _pimpl->baseUrls().end();
-            ++it )
+      std::string indent;
+      for ( const auto & url : _pimpl->baseUrls().raw() )
       {
-        str << *it << endl;
+        str << indent << url << endl;
+       if ( indent.empty() ) indent = "        ";      // "baseurl="
       }
     }
 
     if ( ! _pimpl->path.empty() )
       str << "path="<< path() << endl;
 
-    if ( ! (_pimpl->getmirrorListUrl().asString().empty()) )
-      str << "mirrorlist=" << _pimpl->getmirrorListUrl() << endl;
+    if ( ! (rawMirrorListUrl().asString().empty()) )
+      str << "mirrorlist=" << rawMirrorListUrl() << endl;
 
     str << "type=" << type().asString() << endl;
 
@@ -471,8 +496,9 @@ namespace zypp
 
     if (!indeterminate(_pimpl->gpgcheck))
       str << "gpgcheck=" << (gpgCheck() ? "1" : "0") << endl;
-    if ( ! (gpgKeyUrl().asString().empty()) )
-      str << "gpgkey=" <<gpgKeyUrl() << endl;
+
+    if ( ! (rawGpgKeyUrl().asString().empty()) )
+      str << "gpgkey=" << rawGpgKeyUrl() << endl;
 
     if (!indeterminate(_pimpl->keeppackages))
       str << "keeppackages=" << keepPackages() << endl;
@@ -483,12 +509,9 @@ namespace zypp
     return str;
   }
 
-  std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str) const
-  { return dumpAsXMLOn(str, ""); }
-
-  std::ostream & RepoInfo::dumpAsXMLOn( std::ostream & str, const std::string & content) const
+  std::ostream & RepoInfo::dumpAsXmlOn( std::ostream & str, const std::string & content ) const
   {
-    string tmpstr;
+    std::string tmpstr;
     str
       << "<repo"
       << " alias=\"" << escape(alias()) << "\""
@@ -496,6 +519,7 @@ namespace zypp
     if (type() != repo::RepoType::NONE)
       str << " type=\"" << type().asString() << "\"";
     str
+      << " priority=\"" << priority() << "\""
       << " enabled=\"" << enabled() << "\""
       << " autorefresh=\"" << autorefresh() << "\""
       << " gpgcheck=\"" << gpgCheck() << "\"";
@@ -507,9 +531,8 @@ namespace zypp
 
     if ( _pimpl->baseurl2dump() )
     {
-      for (RepoInfo::urls_const_iterator urlit = baseUrlsBegin();
-           urlit != baseUrlsEnd(); ++urlit)
-        str << "<url>" << escape(urlit->asString()) << "</url>" << endl;
+      for_( it, baseUrlsBegin(), baseUrlsEnd() )       // !transform iterator replaces variables
+       str << "<url>" << escape((*it).asString()) << "</url>" << endl;
     }
 
     str << "</repo>" << endl;