add distrbutionFlavor that does not require the target to be loaded, and it as a...
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Thu, 6 Nov 2008 17:36:39 +0000 (17:36 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Thu, 6 Nov 2008 17:36:39 +0000 (17:36 +0000)
zypp/Target.cc
zypp/Target.h
zypp/base/Functional.h
zypp/media/MediaAria2c.cc
zypp/media/MediaCurl.cc
zypp/target/TargetImpl.cc
zypp/target/TargetImpl.h

index 5a894dd..c50bbf4 100644 (file)
@@ -97,6 +97,9 @@ namespace zypp
   std::string Target::release() const
   { return _pimpl->release(); }
 
+  std::string Target::distributionFlavor() const
+  { return _pimpl->distributionFlavor(); }
+
   std::string Target::targetDistribution() const
   { return _pimpl->targetDistribution(); }
 
index 252c7bc..2dd9d3c 100644 (file)
@@ -153,6 +153,15 @@ namespace zypp
     
 
     /**
+     * This is \flavor attribute of the installed base product
+     * but does not require the target to be loaded as it remembers
+     * the last used one. It can be empty is the target has never
+     * been loaded, as the value is not present in the system
+     * but computer from a package provides
+     */
+    std::string distributionFlavor() const;    
+    
+    /**
      * anonymous unique id
      *
      * This id is generated once and stays in the
index 3f36374..9d6de50 100644 (file)
@@ -214,6 +214,29 @@ namespace zypp
     */
     //@{
 
+    /* functor that always returns a copied
+       value */
+    template<class T>
+    struct Constant
+    {
+      Constant( const T &value )
+        : _value(value)
+      {}
+
+      template<class _Tp>
+      T operator()( _Tp ) const
+      { return _value; }
+
+      T operator()() const
+      { return _value; }
+
+      T _value;
+    };
+
+    template<class T>
+    inline Constant<T> constant( const T &value )
+    { return Constant<T>(value); }
+
     /** Logical functor always \c true.
     */
     struct True
index 1aec7bc..b8c79e2 100644 (file)
@@ -104,6 +104,31 @@ static const char *const anonymousIdHeader()
   return _value.c_str();
 }
 
+static const char *const distributionFlavorHeader()
+{
+  // we need to add the release and identifier to the
+  // agent string.
+  // The target could be not initialized, and then this information
+  // is not available.
+  Target_Ptr target;
+  // FIXME this has to go away as soon as the target
+  // does not throw when not initialized.
+  try {
+      target = zypp::getZYpp()->target();
+  }
+  catch ( const Exception &e )
+  {
+      // nothing to do
+  }
+
+  static const std::string _value(
+      str::trim( str::form(
+          "X-ZYpp-DistributionFlavor: %s",
+          target ? target->distributionFlavor().c_str() : "" ) )
+  );
+  return _value.c_str();
+}
+
 const char *const MediaAria2c::agentString()
 {
   // we need to add the release and identifier to the
@@ -200,6 +225,7 @@ void MediaAria2c::attachTo (bool next)
   
    // add the anonymous id.
    _args.push_back(str::form("--header=%s", anonymousIdHeader() ));
+   _args.push_back(str::form("--header=%s", distributionFlavorHeader() ));
   // TODO add debug option
    
   // Transfer timeout
index d1f9799..9263f4a 100644 (file)
@@ -209,6 +209,32 @@ static const char *const anonymousIdHeader()
   );
   return _value.c_str();
 }
+
+static const char *const distributionFlavorHeader()
+{
+  // we need to add the release and identifier to the
+  // agent string.
+  // The target could be not initialized, and then this information
+  // is not available.
+  Target_Ptr target;
+  // FIXME this has to go away as soon as the target
+  // does not throw when not initialized.
+  try {
+      target = zypp::getZYpp()->target();
+  }
+  catch ( const Exception &e )
+  {
+      // nothing to do
+  }
+
+  static const std::string _value(
+      str::trim( str::form(
+          "X-ZYpp-DistributionFlavor: %s",
+          target ? target->distributionFlavor().c_str() : "" ) )
+  );
+  return _value.c_str();
+}
+
      
 static const char *const agentString()
 {
@@ -718,7 +744,14 @@ void MediaCurl::attachTo (bool next)
   if ( !_customHeaders ) {
       ZYPP_THROW(MediaCurlInitException(_url));
   }
-  
+
+  // now add the product flavor header
+  _customHeaders = curl_slist_append(_customHeaders, distributionFlavorHeader());
+
+  if ( !_customHeaders ) {
+      ZYPP_THROW(MediaCurlInitException(_url));
+  }
+
   ret = curl_easy_setopt ( _curl, CURLOPT_HTTPHEADER, _customHeaders );
     
   if ( ret != 0) {
index 03ad5ef..3139e6e 100644 (file)
@@ -390,26 +390,26 @@ namespace zypp
                          generateRandomId );
     }
 
-    void TargetImpl::createLastBaseProductFlavorCache() const
+    void TargetImpl::createLastDistributionFlavorCache() const
     {
       // create the anonymous unique id
       // this value is used for statistics
-      Pathname flavorpath( home() / "LastBaseProductFlavor");
+      Pathname flavorpath( home() / "LastDistributionFlavor");
 
       // is there a product
       Product::constPtr p = baseProduct();
       if ( ! p )
       {
-          WAR << "No base product, can't create flavor cache" << endl;
+          WAR << "No base product, I won't create flavor cache" << endl;
           return;
       }
 
       string flavor = p->flavor();
 
-      //updateFileContent( flavorpath,
-      //                   // only if flavor is not empty
-      //                   ( flavor.empty() ? functor::False() : functor::True() ),
-      //                   flavor );
+      updateFileContent( flavorpath,
+                         // only if flavor is not empty
+                         functor::Constant<bool>( ! flavor.empty() ),
+                         functor::Constant<string>(flavor) );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -587,7 +587,9 @@ namespace zypp
           ResPool::instance().setHardLockQueries( hardLocks );
         }
       }
-
+      
+      // now that the target is loaded, we can cache the flavor
+      createLastDistributionFlavorCache();
 
       MIL << "Target loaded: " << system.solvablesSize() << " resolvables" << endl;
     }
@@ -1030,6 +1032,18 @@ namespace zypp
       return _distributionVersion;
     }
 
+    std::string TargetImpl::distributionFlavor() const
+    {
+        std::ifstream idfile( ( home() / "LastDistributionFlavor" ).c_str() );
+        for( iostr::EachLine in( idfile ); in; in.next() )
+        {
+            std::string line( str::trim( *in ) );
+            if ( ! line.empty() )
+                return line;
+        }
+        return std::string();
+    }
+
     ///////////////////////////////////////////////////////////////////
 
     std::string TargetImpl::anonymousUniqueId() const
index d2221eb..b87553b 100644 (file)
@@ -79,7 +79,7 @@ namespace zypp
       /**
        * generates a cache of the last product flavor
        */
-      void createLastBaseProductFlavorCache() const;
+      void createLastDistributionFlavorCache() const;
 
       void load();
 
@@ -164,6 +164,9 @@ namespace zypp
       /** \copydoc Target::distributionVersion()*/
       std::string distributionVersion() const;
 
+      /** \copydoc Target::distributionFlavor() */
+      std::string distributionFlavor() const;
+
     protected:
       /** Path to the target */
       Pathname _root;