libzypp  13.10.6
InstanceId.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 //#include <iostream>
13 //#include "zypp/base/LogTools.h"
14 #include "zypp/base/String.h"
15 
16 #include "zypp/InstanceId.h"
17 #include "zypp/ResPool.h"
18 
19 using std::endl;
20 
22 namespace zypp
23 {
24 
25  std::string InstanceId::getIdFor( sat::Solvable slv_r ) const
26  {
27  if ( ! slv_r )
28  return std::string();
29 
30  std::string ret( _namespace );
31  if ( ! ret.empty() )
32  ret += ':';
33 
34  if ( slv_r.isKind<SrcPackage>() ) // libsolv uses no namespace in SrcPackage ident!
35  {
36  ret += ResKind::srcpackage.c_str();
37  ret += ':';
38  }
39 
40  ret += str::form( "%s-%s-%s.%s@%s",
41  slv_r.ident().c_str(),
42  slv_r.edition().version().c_str(),
43  slv_r.edition().release().c_str(),
44  slv_r.arch().c_str(),
45  slv_r.repository().alias().c_str() );
46  return ret;
47  }
48 
49  PoolItem InstanceId::findPoolItem( const std::string str_r ) const
50  {
51  // [namespace:]<name>-<version>-<release>.<arch>@<repoalias>
52  std::string::size_type namespaceOff( _namespace.size() );
53 
54  if ( namespaceOff )
55  {
56  if ( ! str::hasPrefix( str_r, _namespace ) || str_r[namespaceOff] != ':' )
57  return PoolItem();
58  ++namespaceOff; // for the ':'
59  }
60 
61  // check repo
62  std::string::size_type rdelim( str_r.find( "@" ) );
63  if ( rdelim == std::string::npos )
64  return PoolItem();
65 
66  Repository repo( sat::Pool::instance().reposFind( str_r.substr( rdelim+1) ) );
67  if ( ! repo )
68  return PoolItem();
69 
70  // check n-v-r.a from behind
71  std::string::size_type delim = str_r.rfind( ".", rdelim );
72  if ( delim == std::string::npos )
73  return PoolItem();
74 
75  Arch arch( str_r.substr( delim+1, rdelim-delim-1 ) );
76 
77  // v-r starts at one but last '-'
78  rdelim = delim;
79  delim = str_r.rfind( "-", rdelim );
80  if ( delim == std::string::npos )
81  return PoolItem();
82 
83  if ( delim == rdelim-1 ) // supress an empty release
84  rdelim = delim;
85 
86  delim = str_r.rfind( "-", delim-1 );
87  if ( delim == std::string::npos )
88  return PoolItem();
89 
90  Edition ed( str_r.substr( delim+1, rdelim-delim-1 ) );
91 
92  // eveythig before is name (except the leading "<namespace>:")
93  std::string identstring( str_r.substr( namespaceOff, delim-namespaceOff ) );
94 
95  // now lookup in pool..
96  sat::Solvable::SplitIdent ident( (IdString(identstring)) );
97  ResPool pool( ResPool::instance() );
98  for_( it, pool.byIdentBegin( ident.kind(), ident.name() ), pool.byIdentEnd( ident.kind(), ident.name() ) )
99  {
100  sat::Solvable solv( (*it).satSolvable() );
101  if ( solv.repository() == repo && solv.arch() == arch && solv.edition() == ed )
102  {
103  return *it;
104  }
105  }
106  return PoolItem();
107  }
108 
109  bool InstanceId::isSystemId( const std::string str_r ) const
110  { return str::hasSuffix( str_r, Repository::systemRepoAlias() ); }
111 
113 } // namespace zypp
Repository repository() const
The Repository this Solvable belongs to.
Definition: Solvable.cc:148
A Solvable object within the sat Pool.
Definition: Solvable.h:55
Arch arch() const
Definition: Solvable.cc:378
std::string alias() const
Short unique string to identify a repo.
Definition: Repository.cc:57
IdString ident() const
The identifier.
Definition: Solvable.cc:165
Architecture.
Definition: Arch.h:36
PoolItem findPoolItem(const std::string str_r) const
InstanceId string to PoolItem.
Definition: InstanceId.cc:49
std::string release() const
Release.
Definition: Edition.cc:110
static const ResKind srcpackage
Definition: ResKind.h:47
Access to the sat-pools string space.
Definition: IdString.h:39
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
Edition represents [epoch:]version[-release]
Definition: Edition.h:60
byIdent_iterator byIdentEnd(const ByIdent &ident_r) const
Definition: ResPool.h:178
static Pool instance()
Singleton ctor.
Definition: Pool.h:51
bool isSystemId(const std::string str_r) const
Quick test whether the InstanceId string would refer to a system (installed) Solvable.
Definition: InstanceId.cc:109
std::string version() const
Version.
Definition: Edition.cc:94
const char * c_str() const
Conversion to const char *
Definition: IdString.cc:42
SolvableIdType size_type
Definition: PoolMember.h:99
const char * c_str() const
Definition: Arch.h:59
std::string form(const char *format,...)
Printf style construction of std::string.
Definition: String.cc:34
const char * c_str() const
Definition: IdStringType.h:105
SrcPackage interface.
Definition: SrcPackage.h:29
Global ResObject pool.
Definition: ResPool.h:48
bool hasSuffix(const C_Str &str_r, const C_Str &suffix_r)
Return whether str_r has suffix suffix_r.
Definition: String.h:836
Helper that splits an identifier into kind and name or vice versa.
Definition: Solvable.h:264
Reference to a PoolItem connecting ResObject and ResStatus.
Definition: PoolItem.h:50
std::string _namespace
Definition: InstanceId.h:102
static const std::string & systemRepoAlias()
Reserved system repository alias .
Definition: Repository.cc:35
bool isKind(const ResKind &kind_r) const
Test whether a Solvable is of a certain ResKind.
Definition: Solvable.cc:337
byIdent_iterator byIdentBegin(const ByIdent &ident_r) const
Definition: ResPool.h:147
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
Definition: String.h:828
std::string getIdFor(sat::Solvable slv_r) const
Solvable to InstanceId string.
Definition: InstanceId.cc:25
Edition edition() const
Definition: Solvable.cc:372
static ResPool instance()
Singleton ctor.
Definition: ResPool.cc:33