- Avoid superfluous file copying and gpg invocation in keyring handling.
authorMichael Andres <ma@suse.de>
Tue, 11 Nov 2008 17:49:55 +0000 (17:49 +0000)
committerMichael Andres <ma@suse.de>
Tue, 11 Nov 2008 17:49:55 +0000 (17:49 +0000)
VERSION.cmake
devel/devel.ma/NewPool.cc
package/libzypp.changes
zypp/FileChecker.cc
zypp/FileChecker.h
zypp/KeyRing.cc
zypp/PublicKey.cc
zypp/PublicKey.h
zypp/target/rpm/RpmDb.cc

index a871b84..8280486 100644 (file)
@@ -60,7 +60,7 @@
 #
 SET(LIBZYPP_MAJOR "5")
 SET(LIBZYPP_COMPATMINOR "21")
-SET(LIBZYPP_MINOR "22")
+SET(LIBZYPP_MINOR "23")
 SET(LIBZYPP_PATCH "0")
 #
 # LAST RELEASED: 5.22.0 (21)
index 18d325a..ed3ba9d 100644 (file)
@@ -497,7 +497,7 @@ try {
     Measure x( "INIT TARGET" );
     {
       {
-        zypp::base::LogControl::TmpLineWriter shutUp;
+        //zypp::base::LogControl::TmpLineWriter shutUp;
         getZYpp()->initializeTarget( sysRoot );
       }
       getZYpp()->target()->load();
@@ -507,6 +507,10 @@ try {
     }
   }
 
+  ///////////////////////////////////////////////////////////////////
+  INT << "===[END]============================================" << endl << endl;
+  zypp::base::LogControl::instance().logNothing();
+  return 0;
   if ( 1 )
   {
     RepoManager repoManager( makeRepoManager( sysRoot ) );
index 5afb115..41c8e10 100644 (file)
@@ -1,4 +1,10 @@
 -------------------------------------------------------------------
+Tue Nov 11 18:36:33 CET 2008 - ma@suse.de
+
+- Avoid superfluous file copying and gpg invocation in keyring handling.
+- revision 11650
+
+-------------------------------------------------------------------
 Tue Nov 11 14:48:52 CET 2008 - ma@suse.de
 
 - Prevent fetcher from processing the same index file twice. (bnc #443644)
index 1802579..44a98da 100644 (file)
@@ -32,7 +32,7 @@ namespace zypp
       //MIL << "checking " << file << " file against checksum '" << _checksum << "'" << endl;
     callback::SendReport<DigestReport> report;
     CheckSum real_checksum( _checksum.type(), filesystem::checksum( file, _checksum.type() ));
-    
+
     if ( _checksum.empty() )
     {
       MIL << "File " <<  file << " has no checksum available." << std::endl;
@@ -85,13 +85,13 @@ namespace zypp
       }
     }
   }
-  
+
   void CompositeFileChecker::add( const FileChecker &checker )
   {
     //MIL << "||# " << _checkers.size() << endl;
     _checkers.push_back(checker);
     //MIL << "||* " << _checkers.size() << endl;
-    
+
   }
 
    SignatureFileChecker::SignatureFileChecker( const Pathname &signature )
@@ -105,10 +105,12 @@ namespace zypp
   {
   }
 
-  void SignatureFileChecker::addPublicKey( const Pathname &publickey, const KeyContext & keycontext )
+  void SignatureFileChecker::addPublicKey( const Pathname & publickey, const KeyContext & keycontext )
+  { addPublicKey( PublicKey(publickey), keycontext ); }
+
+  void SignatureFileChecker::addPublicKey( const PublicKey & publickey, const KeyContext & keycontext )
   {
-    ZYpp::Ptr z = getZYpp();
-    z->keyRing()->importKey(publickey, false);
+    getZYpp()->keyRing()->importKey(publickey, false);
     _context = keycontext;
   }
 
index 4b96126..746dd69 100644 (file)
@@ -24,6 +24,8 @@
 namespace zypp
 { /////////////////////////////////////////////////////////////////
 
+  class PublicKey;
+
   /**
    * Functor signature used to check files.
    * \param file File to check.
@@ -32,7 +34,7 @@ namespace zypp
    * validate and the user don't want to continue.
    */
   typedef function<void ( const Pathname &file )> FileChecker;
-  
+
   class FileCheckException : public Exception
   {
   public:
@@ -40,21 +42,21 @@ namespace zypp
       : Exception(msg)
     {}
   };
-  
+
   class CheckSumCheckException : public FileCheckException
   {
     //TODO
   };
-  
+
   class SignatureCheckException : public FileCheckException
   {
     //TODO
   };
-  
+
   /**
    * Built in file checkers
    */
-  
+
   /**
    * \short Checks for a valid checksum and interacts with the user.
    */
@@ -76,7 +78,7 @@ namespace zypp
    private:
      CheckSum _checksum;
    };
-   
+
    /**
     * \short Checks for the validity of a signature
     */
@@ -101,7 +103,10 @@ namespace zypp
       /**
        * add a public key to the list of known keys
        */
-      void addPublicKey( const Pathname &publickey, const KeyContext & keycontext = KeyContext());
+      void addPublicKey( const PublicKey & publickey, const KeyContext & keycontext = KeyContext());
+      /** \overload Convenience taking the public keys pathname. */
+      void addPublicKey( const Pathname & publickey, const KeyContext & keycontext = KeyContext());
+
       /**
       * \short Try to validate the file
       * \param file File to validate.
@@ -124,10 +129,10 @@ namespace zypp
    public:
      void operator()( const Pathname &file )  const;
    };
-    
+
    /**
     * \short Checker composed of more checkers.
-    * 
+    *
     * Allows to create a checker composed of various
     * checkers altothether. It will only
     * validate if all the checkers validate.
index 62068f7..44d253a 100644 (file)
@@ -258,15 +258,13 @@ namespace zypp
   PublicKey KeyRing::Impl::exportKey( string id, const Pathname &keyring)
   {
     TmpFile tmp_file( _base_dir, "pubkey-"+id+"-" );
-    Pathname keyfile = tmp_file.path();
-    MIL << "Going to export key " << id << " from " << keyring << " to " << keyfile << endl;
+    MIL << "Going to export key " << id << " from " << keyring << " to " << tmp_file.path() << endl;
 
     try {
-      ofstream os(keyfile.asString().c_str());
+      ofstream os(tmp_file.path().c_str());
       dumpPublicKey( id, keyring, os );
       os.close();
-      PublicKey key(keyfile);
-      return key;
+      return PublicKey( tmp_file );
     }
     catch (BadKeyException &e)
     {
@@ -279,7 +277,7 @@ namespace zypp
     }
     catch (exception &e)
     {
-      ERR << "Cannot export key " << id << " from " << keyring << " keyring  to file " << keyfile << endl;
+      ERR << "Cannot export key " << id << " from " << keyring << " keyring  to file " << tmp_file.path() << endl;
     }
     return PublicKey();
   }
@@ -418,7 +416,7 @@ namespace zypp
         {
           MIL << "User does not want to trust key " << id << " " << key.name() << endl;
           return false;
-        } 
+        }
       }
       else
       {
index b787ed8..80671f6 100644 (file)
@@ -32,6 +32,8 @@ using std::endl;
 ///////////////////////////////////////////////////////////////////
 namespace zypp
 { /////////////////////////////////////////////////////////////////
+
+  /////////////////////////////////////////////////////////////////
   //
   //   CLASS NAME : PublicKey::Impl
   //
@@ -41,12 +43,24 @@ namespace zypp
     Impl()
     {}
 
-    Impl(const Pathname &file)
+    Impl( const Pathname & keyfile )
     {
-      readFromFile(file);
-      MIL << "Done reading key" << std::endl;
+      PathInfo info( keyfile );
+      MIL << "Takeing pubkey from " << keyfile << " of size " << info.size() << " and sha1 " << filesystem::checksum(keyfile, "sha1") << endl;
+
+      if ( !info.isExist() )
+        ZYPP_THROW(Exception("Can't read public key from " + keyfile.asString() + ", file not found"));
+
+      if ( copy( keyfile, _data_file.path() ) != 0 )
+        ZYPP_THROW(Exception("Can't copy public key data from " + keyfile.asString() + " to " +  _data_file.path().asString() ));
+
+      readFromFile();
     }
 
+    Impl( const filesystem::TmpFile & sharedfile )
+      : _data_file( sharedfile )
+    { readFromFile(); }
+
     public:
       /** Offer default Impl. */
       static shared_ptr<Impl> nullimpl()
@@ -55,50 +69,38 @@ namespace zypp
         return _nullimpl;
       }
 
+      std::string asString() const
+      { return "[" + id() + "-" + str::hexstring(created(),8).substr(2) + "] [" + name() + "] [" + fingerprint() + "]"; }
 
-    std::string asString() const
-    {
-      return "[" + id() + "-" + str::hexstring(created(),8).substr(2) + "] [" + name() + "] [" + fingerprint() + "]";
-    }
-
-    std::string armoredData() const
-    { return _data; }
+      std::string armoredData() const
+      { return _data; }
 
-    std::string id() const
-    { return _id; }
+      std::string id() const
+      { return _id; }
 
-    std::string name() const
-    { return _name; }
+      std::string name() const
+      { return _name; }
 
-    std::string fingerprint() const
-    { return _fingerprint; }
+      std::string fingerprint() const
+      { return _fingerprint; }
 
-    Date created() const
-    { return _created; }
+      Date created() const
+      { return _created; }
 
-    Date expires() const
-    { return _expires; }
+      Date expires() const
+      { return _expires; }
 
-    Pathname path() const
-    {
-      return _data_file.path();
-      //return _data_file;
-    }
+      Pathname path() const
+      { return _data_file.path(); }
 
     protected:
 
-     void readFromFile( const Pathname &keyfile)
-     {
-
-       PathInfo info(keyfile);
-       MIL << "Reading pubkey from " << keyfile << " of size " << info.size() << " and sha1 " << filesystem::checksum(keyfile, "sha1")<< endl;
-       if ( !info.isExist() )
-         ZYPP_THROW(Exception("Can't read public key from " + keyfile.asString() + ", file not found"));
-
-       if ( copy( keyfile, _data_file.path() ) != 0 )
-         ZYPP_THROW(Exception("Can't copy public key data from " + keyfile.asString() + " to " +  _data_file.path().asString() ));
+      void readFromFile()
+      {
+        PathInfo info( _data_file.path() );
+        MIL << "Reading pubkey from " << info.path() << " of size " << info.size() << " and sha1 " << filesystem::checksum(info.path(), "sha1") << endl;
 
-        filesystem::TmpDir dir;
+        static filesystem::TmpDir dir;
         const char* argv[] =
         {
           "gpg",
@@ -183,26 +185,29 @@ namespace zypp
         prog.close();
 
         if ( _id.size() == 0 )
-          ZYPP_THROW( BadKeyException( "File " + keyfile.asString() + " doesn't contain public key data" , keyfile ) );
+          ZYPP_THROW( BadKeyException( "File " + _data_file.path().asString() + " doesn't contain public key data" , _data_file.path() ) );
 
         //replace all escaped semicolon with real ':'
         str::replaceAll( _name, "\\x3a", ":" );
-     }
-
-  private:
-    std::string _id;
-    std::string _name;
-    std::string _fingerprint;
-    std::string _data;
-    filesystem::TmpFile _data_file;
-    Date _created;
-    Date _expires;
-    //Pathname _data_file;
-  private:
-    friend Impl * rwcowClone<Impl>( const Impl * rhs );
-    /** clone for RWCOW_pointer */
-    Impl * clone() const
-    { return new Impl( *this ); }
+
+        MIL << "Read pubkey from " << info.path() << ": " << asString() << endl;
+      }
+
+    private:
+      filesystem::TmpFile _data_file;
+
+      std::string _id;
+      std::string _name;
+      std::string _fingerprint;
+      std::string _data;
+      Date        _created;
+      Date        _expires;
+
+    private:
+      friend Impl * rwcowClone<Impl>( const Impl * rhs );
+      /** clone for RWCOW_pointer */
+      Impl * clone() const
+      { return new Impl( *this ); }
   };
   ///////////////////////////////////////////////////////////////////
 
@@ -215,9 +220,14 @@ namespace zypp
   : _pimpl( Impl::nullimpl() )
   {}
 
-  PublicKey::PublicKey( const Pathname &file )
+  PublicKey::PublicKey( const Pathname & file )
   : _pimpl( new Impl(file) )
   {}
+
+  PublicKey::PublicKey( const filesystem::TmpFile & sharedfile )
+  : _pimpl( new Impl(sharedfile) )
+  {}
+
   ///////////////////////////////////////////////////////////////////
   //
   //   METHOD NAME : PublicKey::~PublicKey
@@ -233,9 +243,7 @@ namespace zypp
   ///////////////////////////////////////////////////////////////////
 
   std::string PublicKey::asString() const
-  {
-    return _pimpl->asString();
-  }
+  { return _pimpl->asString(); }
 
   std::string PublicKey::armoredData() const
   { return _pimpl->armoredData(); }
index 79ba4b5..d2544b4 100644 (file)
 namespace zypp
 { /////////////////////////////////////////////////////////////////
 
+  namespace filesystem
+  {
+    class TmpFile;
+  }
+
   /**
    * Exception thrown when the supplied key is
    * not a valid gpg key
@@ -64,7 +69,9 @@ namespace zypp
   //   CLASS NAME : PublicKey
   //
   /**
-   * Class that represent a GPG Public Key
+   * Class that represent a GPG Public Key.
+   *
+   *
    */
   class PublicKey
   {
@@ -75,11 +82,28 @@ namespace zypp
     class Impl;
 
   public:
+    /** Default ctor. */
     PublicKey();
-   /** Ctor
-    * \throws when data does not make a key
-    */
-    PublicKey(const Pathname &file);
+
+    /** Ctor taking the key from a file.
+     *
+     * This is quite expensive, as a copy of the file is created and
+     * used. If you can construct PublicKey from a \ref filesystem::TmpFile,
+     * this prevents copying.
+     *
+     * \throws when data does not make a key
+     */
+    explicit
+    PublicKey( const Pathname & file );
+
+    /** Ctor reading the key from a \ref TmpFile.
+     *
+     * PublicKey holds a reference on the TmpFile providing the key.
+     *
+     * \throws when data does not make a key
+     */
+    explicit
+    PublicKey( const filesystem::TmpFile & sharedfile );
 
     ~PublicKey();
 
index 376a4c5..a44aaad 100644 (file)
@@ -912,8 +912,7 @@ void RpmDb::importZyppKeyRingTrustedKeys()
     return;
 
   std::list<PublicKey> rpm_keys = pubkeys();
-
-  for ( std::list<PublicKey>::const_iterator it = zypp_keys.begin(); it != zypp_keys.end(); ++it)
+  for_( it, zypp_keys.begin(), zypp_keys.end() )
     {
       // we find only the left part of the long gpg key, as rpm does not support long ids
       std::list<PublicKey>::iterator ik = find( rpm_keys.begin(), rpm_keys.end(), (*it));
@@ -926,7 +925,7 @@ void RpmDb::importZyppKeyRingTrustedKeys()
           // now import the key in rpm
           try
             {
-              importPubkey((*it).path());
+              importPubkey( *it );
               MIL << "Trusted key " << (*it).id() << " (" << (*it).name() << ") imported in rpm database." << std::endl;
             }
           catch (RpmException &e)
@@ -941,17 +940,15 @@ void RpmDb::exportTrustedKeysInZyppKeyRing()
 {
   MIL << "Exporting rpm keyring into zypp trusted keyring" <<endl;
 
-  set<Edition> rpm_keys = pubkeyEditions();
-
-  list<PublicKey> zypp_keys;
-  zypp_keys = getZYpp()->keyRing()->trustedPublicKeys();
+  set<Edition>    rpm_keys( pubkeyEditions() );
+  list<PublicKey> zypp_keys( getZYpp()->keyRing()->trustedPublicKeys() );
 
-  for ( set<Edition>::const_iterator it = rpm_keys.begin(); it != rpm_keys.end(); ++it)
+  for_( it, rpm_keys.begin(), rpm_keys.end() )
   {
     // search the zypp key into the rpm keys
     // long id is edition version + release
     string id = str::toUpper( (*it).version() + (*it).release());
-    list<PublicKey>::iterator ik = find( zypp_keys.begin(), zypp_keys.end(), id);
+    list<PublicKey>::iterator ik( find( zypp_keys.begin(), zypp_keys.end(), id) );
     if ( ik != zypp_keys.end() )
     {
       MIL << "Key " << (*it) << " is already in zypp database." << endl;
@@ -959,7 +956,7 @@ void RpmDb::exportTrustedKeysInZyppKeyRing()
     else
     {
       // we export the rpm key into a file
-      RpmHeader::constPtr result = new RpmHeader();
+      RpmHeader::constPtr result( new RpmHeader() );
       getData( string("gpg-pubkey"), *it, result );
       TmpFile file(getZYpp()->tmpPath());
       ofstream os;
@@ -982,7 +979,7 @@ void RpmDb::exportTrustedKeysInZyppKeyRing()
       // now import the key in zypp
       try
       {
-        getZYpp()->keyRing()->importKey( file.path(), true /*trusted*/);
+        getZYpp()->keyRing()->importKey( PublicKey(file), true /*trusted*/);
         MIL << "Trusted key " << (*it) << " imported in zypp keyring." << endl;
       }
       catch (Exception &e)
@@ -1181,7 +1178,7 @@ list<PublicKey> RpmDb::pubkeys() const
         //MIL << "-----------------------------------------------" << endl;
         os.close();
         // read the public key from the dumped file
-        PublicKey key(file.path());
+        PublicKey key(file);
         ret.push_back(key);
       }
       catch (exception &e)