From 8d176f9713fc605ca0f702fac443a38056059907 Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Thu, 6 Nov 2008 17:36:39 +0000 Subject: [PATCH] add distrbutionFlavor that does not require the target to be loaded, and it as a header --- zypp/Target.cc | 3 +++ zypp/Target.h | 9 +++++++++ zypp/base/Functional.h | 23 +++++++++++++++++++++++ zypp/media/MediaAria2c.cc | 26 ++++++++++++++++++++++++++ zypp/media/MediaCurl.cc | 35 ++++++++++++++++++++++++++++++++++- zypp/target/TargetImpl.cc | 30 ++++++++++++++++++++++-------- zypp/target/TargetImpl.h | 5 ++++- 7 files changed, 121 insertions(+), 10 deletions(-) diff --git a/zypp/Target.cc b/zypp/Target.cc index 5a894dd..c50bbf4 100644 --- a/zypp/Target.cc +++ b/zypp/Target.cc @@ -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(); } diff --git a/zypp/Target.h b/zypp/Target.h index 252c7bc..2dd9d3c 100644 --- a/zypp/Target.h +++ b/zypp/Target.h @@ -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 diff --git a/zypp/base/Functional.h b/zypp/base/Functional.h index 3f36374..9d6de50 100644 --- a/zypp/base/Functional.h +++ b/zypp/base/Functional.h @@ -214,6 +214,29 @@ namespace zypp */ //@{ + /* functor that always returns a copied + value */ + template + struct Constant + { + Constant( const T &value ) + : _value(value) + {} + + template + T operator()( _Tp ) const + { return _value; } + + T operator()() const + { return _value; } + + T _value; + }; + + template + inline Constant constant( const T &value ) + { return Constant(value); } + /** Logical functor always \c true. */ struct True diff --git a/zypp/media/MediaAria2c.cc b/zypp/media/MediaAria2c.cc index 1aec7bc..b8c79e2 100644 --- a/zypp/media/MediaAria2c.cc +++ b/zypp/media/MediaAria2c.cc @@ -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 diff --git a/zypp/media/MediaCurl.cc b/zypp/media/MediaCurl.cc index d1f9799..9263f4a 100644 --- a/zypp/media/MediaCurl.cc +++ b/zypp/media/MediaCurl.cc @@ -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) { diff --git a/zypp/target/TargetImpl.cc b/zypp/target/TargetImpl.cc index 03ad5ef..3139e6e 100644 --- a/zypp/target/TargetImpl.cc +++ b/zypp/target/TargetImpl.cc @@ -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( ! flavor.empty() ), + functor::Constant(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 diff --git a/zypp/target/TargetImpl.h b/zypp/target/TargetImpl.h index d2221eb..b87553b 100644 --- a/zypp/target/TargetImpl.h +++ b/zypp/target/TargetImpl.h @@ -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; -- 2.7.4