Also provide static methods to retrieve base product and registration data.
authorMichael Andres <ma@suse.de>
Wed, 7 Oct 2009 12:04:23 +0000 (14:04 +0200)
committerMichael Andres <ma@suse.de>
Wed, 7 Oct 2009 12:04:23 +0000 (14:04 +0200)
This allows e.g the media backend to access those data even if the target is not initialized when downloading.

zypp/Target.cc
zypp/Target.h
zypp/target/TargetImpl.cc
zypp/target/TargetImpl.h

index fc219f4..411cc24 100644 (file)
@@ -94,24 +94,33 @@ namespace zypp
   Date Target::timestamp() const
   { return _pimpl->timestamp(); }
 
-  std::string Target::distributionFlavor() const
-  { return _pimpl->distributionFlavor(); }
+  Product::constPtr Target::baseProduct() const
+  { return _pimpl->baseProduct(); }
 
   std::string Target::targetDistribution() const
   { return _pimpl->targetDistribution(); }
+  std::string Target::targetDistribution( const Pathname & root_r )
+  { return target::TargetImpl::targetDistribution( root_r ); }
 
   std::string Target::targetDistributionRelease() const
   { return _pimpl->targetDistributionRelease(); }
-
-  std::string Target::anonymousUniqueId() const
-  { return _pimpl->anonymousUniqueId(); }
+  std::string Target::targetDistributionRelease( const Pathname & root_r )
+  { return target::TargetImpl::targetDistributionRelease( root_r ); }
 
   std::string Target::distributionVersion() const
   { return _pimpl->distributionVersion(); }
+  std::string Target::distributionVersion( const Pathname & root_r )
+  { return target::TargetImpl::distributionVersion( root_r ); }
 
-  Product::constPtr Target::baseProduct() const
-  { return _pimpl->baseProduct(); }
+  std::string Target::distributionFlavor() const
+  { return _pimpl->distributionFlavor(); }
+  std::string Target::distributionFlavor( const Pathname & root_r )
+  { return target::TargetImpl::distributionFlavor( root_r ); }
 
+  std::string Target::anonymousUniqueId() const
+  { return _pimpl->anonymousUniqueId(); }
+  std::string Target::anonymousUniqueId( const Pathname & root_r )
+  { return target::TargetImpl::anonymousUniqueId( root_r ); }
 
   /////////////////////////////////////////////////////////////////
 } // namespace zypp
index a463e14..b7c7694 100644 (file)
@@ -119,35 +119,47 @@ namespace zypp
      */
     Product::constPtr baseProduct() const;
 
-    /** \name Base product and registration. */
+  public:
+    /** \name Base product and registration.
+     *
+     * Static methods herein allow to retrieve the values without explicitly
+     * initializing the \ref Target. They take a targets root directory as
+     * argument. If an empty \ref Pathname is passed, an already existing
+     * Targets root is used, otherwise \c "\" is assumed.
+     */
     //@{
     /** This is \c register.target attribute of the installed base product.
      * Used for registration and \ref Service refresh.
      */
     std::string targetDistribution() const;
+    /** \overload */
+    static std::string targetDistribution( const Pathname & root_r );
 
     /** This is \c register.release attribute of the installed base product.
      * Used for registration.
      */
     std::string targetDistributionRelease() const;
-    //@}
+    /** \overload */
+    static std::string targetDistributionRelease( const Pathname & root_r );
 
     /** This is \c version attribute of the installed base product.
      * For example http://download.opensue.org/update/11.0
      * The 11.0 corresponds to the base product version.
      */
     std::string distributionVersion() const;
-    //@}
-
+    /** \overload */
+    static std::string distributionVersion( const Pathname & root_r );
 
     /**
-     * This is \flavor attribute of the installed base product
+     * 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;
+    /** \overload */
+    static std::string distributionFlavor( const Pathname & root_r );
 
     /**
      * anonymous unique id
@@ -158,6 +170,9 @@ namespace zypp
      *
      */
     std::string anonymousUniqueId() const;
+    /** \overload */
+    static std::string anonymousUniqueId( const Pathname & root_r );
+    //@}
 
   public:
     /** Ctor. If \c doRebuild_r is \c true, an already existing
index f1f4b73..8480a1d 100644 (file)
@@ -1410,6 +1410,8 @@ namespace zypp
       return 0L;
     }
 
+    ///////////////////////////////////////////////////////////////////
+
     namespace
     {
       parser::ProductFileData baseproductdata( const Pathname & root_r )
@@ -1429,13 +1431,45 @@ namespace zypp
         return parser::ProductFileData();
       }
 
+      inline Pathname staticGuessRoot( const Pathname & root_r )
+      {
+        if ( root_r.empty() )
+        {
+          // empty root: use existing Target or assume "/"
+          Pathname ret ( ZConfig::instance().systemRoot() );
+          if ( ret.empty() )
+            return Pathname("/");
+          return ret;
+        }
+        return root_r;
+      }
+
+      inline std::string firstNonEmptyLineIn( const Pathname & file_r )
+      {
+        std::ifstream idfile( file_r.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::targetDistribution() const
     { return baseproductdata( _root ).registerTarget(); }
+    // static version:
+    std::string TargetImpl::targetDistribution( const Pathname & root_r )
+    { return baseproductdata( staticGuessRoot(root_r) ).registerTarget(); }
+
 
     std::string TargetImpl::targetDistributionRelease() const
     { return baseproductdata( _root ).registerRelease(); }
+    // static version:
+    std::string TargetImpl::targetDistributionRelease( const Pathname & root_r )
+    { return baseproductdata( staticGuessRoot(root_r) ).registerRelease();}
+
 
     std::string TargetImpl::distributionVersion() const
     {
@@ -1460,31 +1494,31 @@ namespace zypp
       }
       return _distributionVersion;
     }
+    // static version: (no fallback to init and read rpm db here)
+    std::string TargetImpl::distributionVersion( const Pathname & root_r )
+    { return baseproductdata( staticGuessRoot(root_r) ).edition().version(); }
+
 
     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();
+      return firstNonEmptyLineIn( home() / "LastDistributionFlavor" );
+    }
+    // static version:
+    std::string TargetImpl::distributionFlavor( const Pathname & root_r )
+    {
+      return firstNonEmptyLineIn( staticGuessRoot(root_r) / "/var/lib/zypp/LastDistributionFlavor" );
     }
 
     ///////////////////////////////////////////////////////////////////
 
     std::string TargetImpl::anonymousUniqueId() const
     {
-        std::ifstream idfile( ( home() / "AnonymousUniqueId" ).c_str() );
-        for( iostr::EachLine in( idfile ); in; in.next() )
-        {
-            std::string line( str::trim( *in ) );
-            if ( ! line.empty() )
-                return line;
-        }
-        return std::string();
+      return firstNonEmptyLineIn( home() / "AnonymousUniqueId" );
+    }
+    // static version:
+    std::string TargetImpl::anonymousUniqueId( const Pathname & root_r )
+    {
+      return firstNonEmptyLineIn( staticGuessRoot(root_r) / "/var/lib/zypp/AnonymousUniqueId" );
     }
 
     ///////////////////////////////////////////////////////////////////
index adc6c47..63fdc14 100644 (file)
@@ -111,8 +111,6 @@ namespace zypp
       void buildCache();
       //@}
 
-      std::string anonymousUniqueId() const;
-
     public:
 
       /** The root set for this target */
@@ -153,20 +151,31 @@ namespace zypp
       /** \copydoc Target::baseProduct() */
       Product::constPtr baseProduct() const;
 
-      /** \copydoc Target::release() */
-      std::string release() const;
 
       /** \copydoc Target::targetDistribution() */
       std::string targetDistribution() const;
+      /** \overload */
+      static std::string targetDistribution( const Pathname & root_r );
 
       /** \copydoc Target::targetDistributionRelease()*/
       std::string targetDistributionRelease() const;
+      /** \overload */
+      static std::string targetDistributionRelease( const Pathname & root_r );
 
       /** \copydoc Target::distributionVersion()*/
       std::string distributionVersion() const;
+      /** \overload */
+      static std::string distributionVersion( const Pathname & root_r );
 
       /** \copydoc Target::distributionFlavor() */
       std::string distributionFlavor() const;
+      /** \overload */
+      static std::string distributionFlavor( const Pathname & root_r );
+
+      /** \copydoc Target::anonymousUniqueId() */
+      std::string anonymousUniqueId() const;
+      /** \overload */
+      static std::string anonymousUniqueId( const Pathname & root_r );
 
     private:
       /** Commit ordered changes (internal helper) */