Disable use if rpm-4.4 legacy interface (dropped in 4.9) (bnc#691089)
[platform/upstream/libzypp.git] / zypp / target / rpm / RpmHeader.cc
index ae827f3..b83bcba 100644 (file)
  *
 */
 #include "librpm.h"
+#ifdef _RPM_4_4
+#include <rpm/ugid.h>
+#else
+////////////////////////////////////////////////////////////////////
+// unameToUid and gnameToGid are shamelessly stolen from rpm-4.4.
+// (rpmio/ugid.c) Those functions were dropped in RPM_4_7
+extern "C"
+{
+#include <pwd.h>
+#include <grp.h>
+}
+/* unameToUid(), uidTouname() and the group variants are really poorly
+   implemented. They really ought to use hash tables. I just made the
+   guess that most files would be owned by root or the same person/group
+   who owned the last file. Those two values are cached, everything else
+   is looked up via getpw() and getgr() functions.  If this performs
+   too poorly I'll have to implement it properly :-( */
+
+int unameToUid(const char * thisUname, uid_t * uid)
+{
+/*@only@*/ static char * lastUname = NULL;
+    static size_t lastUnameLen = 0;
+    static size_t lastUnameAlloced;
+    static uid_t lastUid;
+    struct passwd * pwent;
+    size_t thisUnameLen;
+
+    if (!thisUname) {
+       lastUnameLen = 0;
+       return -1;
+    } else if (strcmp(thisUname, "root") == 0) {
+/*@-boundswrite@*/
+       *uid = 0;
+/*@=boundswrite@*/
+       return 0;
+    }
+
+    thisUnameLen = strlen(thisUname);
+    if (lastUname == NULL || thisUnameLen != lastUnameLen ||
+       strcmp(thisUname, lastUname) != 0)
+    {
+       if (lastUnameAlloced < thisUnameLen + 1) {
+           lastUnameAlloced = thisUnameLen + 10;
+           lastUname = (char *)realloc(lastUname, lastUnameAlloced);   /* XXX memory leak */
+       }
+/*@-boundswrite@*/
+       strcpy(lastUname, thisUname);
+/*@=boundswrite@*/
+
+       pwent = getpwnam(thisUname);
+       if (pwent == NULL) {
+           /*@-internalglobs@*/ /* FIX: shrug */
+           endpwent();
+           /*@=internalglobs@*/
+           pwent = getpwnam(thisUname);
+           if (pwent == NULL) return -1;
+       }
+
+       lastUid = pwent->pw_uid;
+    }
+
+/*@-boundswrite@*/
+    *uid = lastUid;
+/*@=boundswrite@*/
+
+    return 0;
+}
+
+int gnameToGid(const char * thisGname, gid_t * gid)
+{
+/*@only@*/ static char * lastGname = NULL;
+    static size_t lastGnameLen = 0;
+    static size_t lastGnameAlloced;
+    static gid_t lastGid;
+    size_t thisGnameLen;
+    struct group * grent;
+
+    if (thisGname == NULL) {
+       lastGnameLen = 0;
+       return -1;
+    } else if (strcmp(thisGname, "root") == 0) {
+/*@-boundswrite@*/
+       *gid = 0;
+/*@=boundswrite@*/
+       return 0;
+    }
+
+    thisGnameLen = strlen(thisGname);
+    if (lastGname == NULL || thisGnameLen != lastGnameLen ||
+       strcmp(thisGname, lastGname) != 0)
+    {
+       if (lastGnameAlloced < thisGnameLen + 1) {
+           lastGnameAlloced = thisGnameLen + 10;
+           lastGname = (char *)realloc(lastGname, lastGnameAlloced);   /* XXX memory leak */
+       }
+/*@-boundswrite@*/
+       strcpy(lastGname, thisGname);
+/*@=boundswrite@*/
+
+       grent = getgrnam(thisGname);
+       if (grent == NULL) {
+           /*@-internalglobs@*/ /* FIX: shrug */
+           endgrent();
+           /*@=internalglobs@*/
+           grent = getgrnam(thisGname);
+           if (grent == NULL) {
+               /* XXX The filesystem package needs group/lock w/o getgrnam. */
+               if (strcmp(thisGname, "lock") == 0) {
+/*@-boundswrite@*/
+                   *gid = lastGid = 54;
+/*@=boundswrite@*/
+                   return 0;
+               } else
+               if (strcmp(thisGname, "mail") == 0) {
+/*@-boundswrite@*/
+                   *gid = lastGid = 12;
+/*@=boundswrite@*/
+                   return 0;
+               } else
+               return -1;
+           }
+       }
+       lastGid = grent->gr_gid;
+    }
+
+/*@-boundswrite@*/
+    *gid = lastGid;
+/*@=boundswrite@*/
+
+    return 0;
+}
+////////////////////////////////////////////////////////////////////
+#endif
 
 #include <iostream>
 #include <map>
 
 #include "zypp/base/Easy.h"
 #include "zypp/base/Logger.h"
-#include "zypp/PathInfo.h"
+#include "zypp/base/Exception.h"
 
+#include "zypp/target/rpm/librpmDb.h"
 #include "zypp/target/rpm/RpmHeader.h"
-#include "zypp/CapFactory.h"
-#include "zypp/Rel.h"
 #include "zypp/Package.h"
-#include "zypp/base/Exception.h"
+#include "zypp/PathInfo.h"
 
-using namespace std;
-using namespace zypp::capability;
+using std::endl;
 
 namespace zypp
 {
@@ -78,7 +209,7 @@ RpmHeader::~RpmHeader()
 //        METHOD TYPE : constRpmHeaderPtr
 //
 RpmHeader::constPtr RpmHeader::readPackage( const Pathname & path_r,
-    VERIFICATION verification_r )
+                                            VERIFICATION verification_r )
 {
   PathInfo file( path_r );
   if ( ! file.isFile() )
@@ -96,6 +227,7 @@ RpmHeader::constPtr RpmHeader::readPackage( const Pathname & path_r,
     return (RpmHeader*)0;
   }
 
+  librpmDb::globalInit();
   rpmts ts = ::rpmtsCreate();
   unsigned vsflag = RPMVSF_DEFAULT;
   if ( verification_r & NODIGEST )
@@ -107,7 +239,7 @@ RpmHeader::constPtr RpmHeader::readPackage( const Pathname & path_r,
   Header nh = 0;
   int res = ::rpmReadPackageFile( ts, fd, path_r.asString().c_str(), &nh );
 
-  ts = ::rpmtsFree(ts);
+  ts = rpmtsFree(ts);
 
   ::Fclose( fd );
 
@@ -128,16 +260,16 @@ RpmHeader::constPtr RpmHeader::readPackage( const Pathname & path_r,
 //
 //
 //        METHOD NAME : RpmHeader::dumpOn
-//        METHOD TYPE : ostream &
+//        METHOD TYPE : std::ostream &
 //
 //        DESCRIPTION :
 //
-ostream & RpmHeader::dumpOn( ostream & str ) const
+std::ostream & RpmHeader::dumpOn( std::ostream & str ) const
 {
   return BinHeader::dumpOn( str ) << '{' << tag_name() << "-"
-         << (tag_epoch().empty()?"":(tag_epoch()+":"))
+         << (tag_epoch()==0?"":(tag_epoch()+":"))
          << tag_version()
-         << (tag_release().empty()?"":(string("-")+tag_release()))
+         << (tag_release().empty()?"":(std::string("-")+tag_release()))
          << ( isSrc() ? ".src}" : "}");
 }
 
@@ -157,11 +289,11 @@ bool RpmHeader::isSrc() const
 //
 //
 //        METHOD NAME : RpmHeader::tag_name
-//        METHOD TYPE : string
+//        METHOD TYPE : std::string
 //
 //        DESCRIPTION :
 //
-string RpmHeader::tag_name() const
+std::string RpmHeader::tag_name() const
 {
   return string_val( RPMTAG_NAME );
 }
@@ -170,24 +302,24 @@ string RpmHeader::tag_name() const
 //
 //
 //        METHOD NAME : RpmHeader::tag_epoch
-//        METHOD TYPE : string
+//        METHOD TYPE : Edition::epoch_t
 //
 //        DESCRIPTION :
 //
-string RpmHeader::tag_epoch() const
+Edition::epoch_t RpmHeader::tag_epoch() const
 {
-  return string_val ( RPMTAG_EPOCH );
+  return int_val ( RPMTAG_EPOCH );
 }
 
 ///////////////////////////////////////////////////////////////////
 //
 //
 //        METHOD NAME : RpmHeader::tag_version
-//        METHOD TYPE : string
+//        METHOD TYPE : std::string
 //
 //        DESCRIPTION :
 //
-string RpmHeader::tag_version() const
+std::string RpmHeader::tag_version() const
 {
   return string_val ( RPMTAG_VERSION );
 }
@@ -196,11 +328,11 @@ string RpmHeader::tag_version() const
 //
 //
 //        METHOD NAME : RpmHeader::tag_release
-//        METHOD TYPE : string
+//        METHOD TYPE : std::string
 //
 //        DESCRIPTION :
 //
-string RpmHeader::tag_release() const
+std::string RpmHeader::tag_release() const
 {
   return string_val( RPMTAG_RELEASE );
 }
@@ -215,29 +347,20 @@ string RpmHeader::tag_release() const
 //
 Edition RpmHeader::tag_edition () const
 {
-  try
-  {
-    return Edition( tag_version(), tag_release(), tag_epoch());
-  }
-  catch (Exception & excpt_r)
-  {
-    WAR << "Package " << tag_name() << "has an invalid edition";
-    ZYPP_CAUGHT (excpt_r);
-  }
-  return Edition();
+  return Edition( tag_version(), tag_release(), tag_epoch() );
 }
 
 ///////////////////////////////////////////////////////////////////
 //
 //
 //        METHOD NAME : RpmHeader::tag_arch
-//        METHOD TYPE : string
+//        METHOD TYPE : Arch
 //
 //        DESCRIPTION :
 //
-string RpmHeader::tag_arch() const
+Arch RpmHeader::tag_arch() const
 {
-  return string_val( RPMTAG_ARCH );
+  return Arch( string_val( RPMTAG_ARCH ) );
 }
 
 ///////////////////////////////////////////////////////////////////
@@ -265,21 +388,21 @@ Date RpmHeader::tag_buildtime() const
 {
   return int_val( RPMTAG_BUILDTIME );
 }
-
+#warning CHECK IF FILE REQUIRES HANDLING IS OBSOLETE
 ///////////////////////////////////////////////////////////////////
 //
 //
 //        METHOD NAME : RpmHeader::PkgRelList_val
-//        METHOD TYPE : unsigned
+//        METHOD TYPE : CapabilitySet
 //
 //        DESCRIPTION :
 //
-CapabilityImplPtrSet RpmHeader::PkgRelList_val( tag tag_r, bool pre, set<string> * freq_r ) const
+CapabilitySet RpmHeader::PkgRelList_val( tag tag_r, bool pre, std::set<std::string> * freq_r ) const
   {
-    CapabilityImplPtrSet ret;
+    CapabilitySet ret;
 
-    int_32  kindFlags   = 0;
-    int_32  kindVersion = 0;
+    rpmTag  kindFlags   = rpmTag(0);
+    rpmTag  kindVersion = rpmTag(0);
 
     switch ( tag_r )
     {
@@ -299,19 +422,14 @@ CapabilityImplPtrSet RpmHeader::PkgRelList_val( tag tag_r, bool pre, set<string>
       kindFlags   = RPMTAG_CONFLICTFLAGS;
       kindVersion = RPMTAG_CONFLICTVERSION;
       break;
-#ifdef HAVE_RPM_ENHANCES
     case RPMTAG_ENHANCESNAME:
       kindFlags   = RPMTAG_ENHANCESFLAGS;
       kindVersion = RPMTAG_ENHANCESVERSION;
       break;
-#endif
-#warning NEEDS RPMTAG_SUPPLEMENTSNAME
-#if 0
-    case RPMTAG_SUPPLEMENTSNAME:
-      kindFlags   = RPMTAG_SUPPLEMENTSFLAGS;
-      kindVersion = RPMTAG_SUPPLEMENTSVERSION;
+    case RPMTAG_SUGGESTSNAME:
+      kindFlags   = RPMTAG_SUGGESTSFLAGS;
+      kindVersion = RPMTAG_SUGGESTSVERSION;
       break;
-#endif
     default:
       INT << "Illegal RPMTAG_dependencyNAME " << tag_r << endl;
       return ret;
@@ -332,11 +450,11 @@ CapabilityImplPtrSet RpmHeader::PkgRelList_val( tag tag_r, bool pre, set<string>
     for ( unsigned i = 0; i < count; ++i )
     {
 
-      string n( names[i] );
+      std::string n( names[i] );
 
       Rel op = Rel::ANY;
-      int_32 f  = flags[i];
-      string v  = versions[i];
+      int32_t f = flags[i];
+      std::string v = versions[i];
 
       if ( n[0] == '/' )
       {
@@ -374,13 +492,7 @@ CapabilityImplPtrSet RpmHeader::PkgRelList_val( tag tag_r, bool pre, set<string>
       {
         try
         {
-          CapabilityImpl::Ptr cap = capability::buildVersioned(
-                             ResTraits<Package>::kind,
-                             n,
-                             op,
-                             Edition(v)
-                           );
-          ret.insert(cap);
+          ret.insert( Capability( n, op, Edition(v) ) );
         }
         catch (Exception & excpt_r)
         {
@@ -398,11 +510,11 @@ CapabilityImplPtrSet RpmHeader::PkgRelList_val( tag tag_r, bool pre, set<string>
 //
 //
 //        METHOD NAME : RpmHeader::tag_provides
-//        METHOD TYPE : CapabilityImplPtrSet
+//        METHOD TYPE : CapabilitySet
 //
 //        DESCRIPTION :
 //
-CapabilityImplPtrSet RpmHeader::tag_provides( set<string> * freq_r ) const
+CapabilitySet RpmHeader::tag_provides( std::set<std::string> * freq_r ) const
   {
     return PkgRelList_val( RPMTAG_PROVIDENAME, false, freq_r );
   }
@@ -411,11 +523,11 @@ CapabilityImplPtrSet RpmHeader::tag_provides( set<string> * freq_r ) const
 //
 //
 //        METHOD NAME : RpmHeader::tag_requires
-//        METHOD TYPE : CapabilityImplPtrSet
+//        METHOD TYPE : CapabilitySet
 //
 //        DESCRIPTION :
 //
-CapabilityImplPtrSet RpmHeader::tag_requires( set<string> * freq_r ) const
+CapabilitySet RpmHeader::tag_requires( std::set<std::string> * freq_r ) const
   {
     return PkgRelList_val( RPMTAG_REQUIRENAME, false, freq_r );
   }
@@ -424,11 +536,11 @@ CapabilityImplPtrSet RpmHeader::tag_requires( set<string> * freq_r ) const
 //
 //
 //        METHOD NAME : RpmHeader::tag_requires
-//        METHOD TYPE : CapabilityImplPtrSet
+//        METHOD TYPE : CapabilitySet
 //
 //        DESCRIPTION :
 //
-CapabilityImplPtrSet RpmHeader::tag_prerequires( set<string> * freq_r ) const
+CapabilitySet RpmHeader::tag_prerequires( std::set<std::string> * freq_r ) const
   {
     return PkgRelList_val( RPMTAG_REQUIRENAME, true, freq_r );
   }
@@ -437,11 +549,11 @@ CapabilityImplPtrSet RpmHeader::tag_prerequires( set<string> * freq_r ) const
 //
 //
 //        METHOD NAME : RpmHeader::tag_conflicts
-//        METHOD TYPE : CapabilityImplPtrSet
+//        METHOD TYPE : CapabilitySet
 //
 //        DESCRIPTION :
 //
-CapabilityImplPtrSet RpmHeader::tag_conflicts( set<string> * freq_r ) const
+CapabilitySet RpmHeader::tag_conflicts( std::set<std::string> * freq_r ) const
   {
     return PkgRelList_val( RPMTAG_CONFLICTNAME, false, freq_r );
   }
@@ -450,11 +562,11 @@ CapabilityImplPtrSet RpmHeader::tag_conflicts( set<string> * freq_r ) const
 //
 //
 //        METHOD NAME : RpmHeader::tag_obsoletes
-//        METHOD TYPE : CapabilityImplPtrSet
+//        METHOD TYPE : CapabilitySet
 //
 //        DESCRIPTION :
 //
-CapabilityImplPtrSet RpmHeader::tag_obsoletes( set<string> * freq_r ) const
+CapabilitySet RpmHeader::tag_obsoletes( std::set<std::string> * freq_r ) const
   {
     return PkgRelList_val( RPMTAG_OBSOLETENAME, false, freq_r );
   }
@@ -463,34 +575,26 @@ CapabilityImplPtrSet RpmHeader::tag_obsoletes( set<string> * freq_r ) const
 //
 //
 //        METHOD NAME : RpmHeader::tag_enhances
-//        METHOD TYPE : CapabilityImplPtrSet
+//        METHOD TYPE : CapabilitySet
 //
 //        DESCRIPTION :
 //
-CapabilityImplPtrSet RpmHeader::tag_enhances( set<string> * freq_r ) const
+CapabilitySet RpmHeader::tag_enhances( std::set<std::string> * freq_r ) const
   {
-#ifdef HAVE_RPM_ENHANCES
     return PkgRelList_val( RPMTAG_ENHANCESNAME, false, freq_r );
-#else
-    return CapabilityImplPtrSet();
-#endif
   }
 
 ///////////////////////////////////////////////////////////////////
 //
 //
-//        METHOD NAME : RpmHeader::tag_supplements
-//        METHOD TYPE : CapabilityImplPtrSet
+//        METHOD NAME : RpmHeader::tag_suggests
+//        METHOD TYPE : CapabilitySet
 //
 //        DESCRIPTION :
 //
-CapabilityImplPtrSet RpmHeader::tag_supplements( set<string> * freq_r ) const
+CapabilitySet RpmHeader::tag_suggests( std::set<std::string> * freq_r ) const
   {
-    return CapabilityImplPtrSet();
-#warning NEEDS RPMTAG_SUPPLEMENTSNAME
-#if 0
-    return PkgRelList_val( RPMTAG_SUPPLEMENTSNAME, false, freq_r );
-#endif
+    return PkgRelList_val( RPMTAG_SUGGESTSNAME, false, freq_r );
   }
 
 ///////////////////////////////////////////////////////////////////
@@ -884,7 +988,7 @@ DiskUsage & RpmHeader::tag_du( DiskUsage & dudata_r ) const
     // filter out hardliks ( different name but same device and inode ).
     ///////////////////////////////////////////////////////////////////
     filesystem::DevInoCache trace;
-    vector<DiskUsage::Entry> entries;
+    std::vector<DiskUsage::Entry> entries;
     entries.resize( dirnames.size() );
     for ( unsigned i = 0; i < dirnames.size(); ++i )
     {