libzypp 17.38.15
Pool.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <sys/types.h>
13#include <sys/stat.h>
14#include <fcntl.h>
15#include <unistd.h>
16
17extern "C"
18{
19#include <solv/pool.h>
20#include <solv/pool_snapshot.h>
21#include <solv/repo.h>
22#include <solv/solvable.h>
23}
24
25#include <iostream>
26#include <fstream>
27
28#include <zypp-core/base/Easy.h>
33
35
37#include <zypp/sat/Pool.h>
38#include <zypp/sat/LookupAttr.h>
39
40using std::endl;
41
43namespace zypp
44{
46 namespace sat
47 {
48
49 const std::string & Pool::systemRepoAlias()
51
53 { return myPool().getPool(); }
54
56 { return myPool()->nsolvables; }
57
58 const SerialNumber & Pool::serial() const
59 { return myPool().serial(); }
60
62 { return myPool().serialIDs(); }
63
64 void Pool::prepare() const
65 { return myPool().prepare(); }
66
68 { return myPool().rootDir(); }
69
70 void Pool::rootDir( const Pathname & root_r )
71 { return myPool().rootDir( root_r ); }
72
73 bool Pool::reposEmpty() const
74 { return ! myPool()->urepos; }
75
77 { return myPool()->urepos; }
78
80 {
81 if ( myPool()->urepos )
82 { // repos[0] == NULL
83 for_( it, myPool()->repos+1, myPool()->repos+myPool()->nrepos )
84 if ( *it )
85 return RepositoryIterator( it );
86 }
87 return reposEnd();
88 }
89
92
94 {
95 // return myPool()->nsolvables;
96 // nsolvables is the array size including
97 // invalid Solvables.
98 for_( it, reposBegin(), reposEnd() )
99 {
100 if ( ! it->solvablesEmpty() )
101 return false;
102 }
103 return true;
104 }
105
107 {
108 // Do not return myPool()->nsolvables;
109 // nsolvables is the array size including
110 // invalid Solvables.
111 size_type ret = 0;
112 for_( it, reposBegin(), reposEnd() )
113 {
114 ret += it->solvablesSize();
115 }
116 return ret;
117 }
118
120 { return SolvableIterator( myPool().getFirstId() ); }
121
124
125 sat::Queue Pool::whatMatchesDep( const SolvAttr &attr, const Capability &cap ) const
126 {
127 sat::Queue q;
128 pool_whatmatchesdep( get(), attr.id(), cap.id(), q, 0);
129 return q;
130 }
131
132 Queue Pool::whatMatchesSolvable( const SolvAttr &attr, const Solvable &solv ) const
133 {
134 sat::Queue q;
135 pool_whatmatchessolvable( get(), attr.id(), static_cast<Id>( solv.id() ), q, 0 );
136 return q;
137 }
138
139 Queue Pool::whatContainsDep( const SolvAttr &attr, const Capability &cap ) const
140 {
141 sat::Queue q;
142 pool_whatcontainsdep( get(), attr.id(), cap.id(), q, 0 );
143 return q;
144 }
145
146 Repository Pool::reposInsert( const std::string & alias_r )
147 {
148 Repository ret( reposFind( alias_r ) );
149 if ( ret )
150 return ret;
151
152 ret = Repository( myPool()._createRepo( alias_r ) );
153 if ( ret.isSystemRepo() )
154 {
155 // autoprovide (dummy) RepoInfo
157 info.setAlias( alias_r );
158 info.setName( alias_r );
159 info.setAutorefresh( true );
160 info.setEnabled( true );
161 ret.setInfo( info );
162 }
163 return ret;
164 }
165
166 Repository Pool::reposFind( const std::string & alias_r ) const
167 {
168 for_( it, reposBegin(), reposEnd() )
169 {
170 if ( alias_r == it->alias() )
171 return *it;
172 }
173 return Repository();
174 }
175
177 {
178 return Repository( myPool().systemRepo() );
179 }
180
182 {
183 if ( myPool().systemRepo() )
184 return Repository( myPool().systemRepo() );
185 return reposInsert( systemRepoAlias() );
186 }
187
188 void Pool::reserveIds( unsigned numid_r, unsigned numrel_r )
189 {
190 ::pool_reserve_ids( get(), numid_r, numrel_r );
191 }
192
193 namespace { bool & snapshotMappedFlag() { static bool _f = false; return _f; } }
194
196 { return snapshotMappedFlag(); }
197
198 void Pool::setSnapshotCandidate( const Pathname & path_r, const std::string & cookie_r, const std::vector<std::string> & aliases_r )
199 { myPool().setSnapshotCandidate( path_r, cookie_r, aliases_r ); }
200
201 std::string Pool::snapshotCookie( const Pathname & path_r )
202 {
203 AutoFILE fp { ::fopen( path_r.c_str(), "re" ) };
204 if ( !fp )
205 return std::string();
206 unsigned char buf[4096];
207 unsigned int len = sizeof(buf);
208 if ( ::pool_snapshot_read_cookie( fp, buf, &len ) != 0 )
209 return std::string();
210 return std::string( reinterpret_cast<char*>(buf), len );
211 }
212
213 bool Pool::mapSnapshot( const Pathname & path_r, const std::string & cookie_r )
214 {
215 if ( cookie_r.empty() )
216 return false;
217 if ( ! myPool().mapSnapshot( path_r, cookie_r ) )
218 return false;
219 snapshotMappedFlag() = true;
220 return true;
221 }
222
223 bool Pool::writeSnapshot( const Pathname & path_r, const std::string & cookie_r ) const
224 {
225 // pid-unique tempname: concurrent writers must not interleave
226 // into the same file, and the final file may only ever appear
227 // via rename (live readers borrow pages from it)
228 Pathname tmp( path_r.extend( ".new." + str::numstring( getpid() ) ) );
229 AutoFILE fp { ::fopen( tmp.c_str(), "we" ) };
230 if ( !fp )
231 return false;
232 if ( ::pool_snapshot_write( get(), fp, reinterpret_cast<const unsigned char*>(cookie_r.data()), cookie_r.size() ) != 0
233 || ::fflush( fp ) != 0 )
234 {
235 ::unlink( tmp.c_str() );
236 return false;
237 }
238 ::fchmod( ::fileno( fp ), 0644 );
239 fp.reset();
240 if ( ::rename( tmp.c_str(), path_r.c_str() ) != 0 )
241 {
242 ::unlink( tmp.c_str() );
243 return false;
244 }
245 MIL << "Wrote pool snapshot " << path_r << endl;
246 return true;
247 }
248
249 Repository Pool::addRepoSolv( const Pathname & file_r, const std::string & alias_r )
250 {
251 // Using a temporay repo! (The additional parenthesis are required.)
253 *tmprepo = reposInsert( alias_r );
254 tmprepo->addSolv( file_r );
255
256 // no exceptions so we keep it:
257 tmprepo.resetDispose();
258 return tmprepo;
259 }
260
262 { return addRepoSolv( file_r, file_r.basename() ); }
263
264 Repository Pool::addRepoSolv( const Pathname & file_r, const RepoInfo & info_r )
265 {
266 Repository ret( addRepoSolv( file_r, info_r.alias() ) );
267 ret.setInfo( info_r );
268 return ret;
269 }
270
272
273 Repository Pool::addRepoHelix( const Pathname & file_r, const std::string & alias_r )
274 {
275 // Using a temporay repo! (The additional parenthesis are required.)
277 *tmprepo = reposInsert( alias_r );
278 tmprepo->addHelix( file_r );
279
280 // no exceptions so we keep it:
281 tmprepo.resetDispose();
282 return tmprepo;
283 }
284
286 { return addRepoHelix( file_r, file_r.basename() ); }
287
288 Repository Pool::addRepoHelix( const Pathname & file_r, const RepoInfo & info_r )
289 {
290 Repository ret( addRepoHelix( file_r, info_r.alias() ) );
291 ret.setInfo( info_r );
292 return ret;
293 }
294
296
297 void Pool::setTextLocale( const Locale & locale_r )
298 { myPool().setTextLocale( locale_r ); }
299
300 void Pool::setRequestedLocales( const LocaleSet & locales_r )
301 { myPool().setRequestedLocales( locales_r ); }
302
303 bool Pool::addRequestedLocale( const Locale & locale_r )
304 { return myPool().addRequestedLocale( locale_r ); }
305
306 bool Pool::eraseRequestedLocale( const Locale & locale_r )
307 { return myPool().eraseRequestedLocale( locale_r ); }
308
311
312 bool Pool::isRequestedLocale( const Locale & locale_r ) const
313 { return myPool().isRequestedLocale( locale_r ); }
314
315 void Pool::initRequestedLocales( const LocaleSet & locales_r ) { myPool().initRequestedLocales( locales_r ); }
318
321
322 bool Pool::isAvailableLocale( const Locale & locale_r ) const
323 { return myPool().isAvailableLocale( locale_r ); }
324
327
329 void Pool::setAutoInstalled( const Queue & autoInstalled_r ){ myPool().setAutoInstalled( autoInstalled_r ); }
330
331 void Pool::setNeedrebootSpec( sat::SolvableSpec needrebootSpec_r ) { myPool().setNeedrebootSpec( std::move(needrebootSpec_r) ); }
332
333 /******************************************************************
334 **
335 ** FUNCTION NAME : operator<<
336 ** FUNCTION TYPE : std::ostream &
337 */
338 std::ostream & operator<<( std::ostream & str, const Pool & obj )
339 {
340 return str << "sat::pool(" << obj.serial() << ")["
341 << obj.capacity() << "]{"
342 << obj.reposSize() << "repos|"
343 << obj.solvablesSize() << "solv}";
344 }
345
347 #undef ZYPP_BASE_LOGGER_LOGGROUP
348 #define ZYPP_BASE_LOGGER_LOGGROUP "solvidx"
349
350 void updateSolvFileIndex( const Pathname & solvfile_r )
351 {
352 AutoDispose<FILE*> solv( ::fopen( solvfile_r.c_str(), "re" ), ::fclose );
353 if ( solv == NULL )
354 {
355 solv.resetDispose();
356 ERR << "Can't open solv-file: " << solv << endl;
357 return;
358 }
359
360 std::string solvidxfile( solvfile_r.extend(".idx").asString() );
361 if ( ::unlink( solvidxfile.c_str() ) == -1 && errno != ENOENT )
362 {
363 ERR << "Can't unlink solv-idx: " << Errno() << endl;
364 return;
365 }
366 {
367 int fd = ::open( solvidxfile.c_str(), O_CREAT|O_EXCL|O_WRONLY|O_TRUNC, 0644 );
368 if ( fd == -1 )
369 {
370 ERR << "Can't create solv-idx: " << Errno() << endl;
371 return;
372 }
373 ::close( fd );
374 }
375 std::ofstream idx( solvidxfile.c_str() );
376
377
378 detail::CPool * _pool = ::pool_create();
379 detail::CRepo * _repo = ::repo_create( _pool, "" );
380 if ( ::repo_add_solv( _repo, solv, 0 ) == 0 )
381 {
382 int _id = 0;
383 detail::CSolvable * _solv = nullptr;
384 FOR_REPO_SOLVABLES( _repo, _id, _solv )
385 {
386 if ( _solv )
387 {
388#define SEP '\t'
389#define idstr(V) pool_id2str( _pool, _solv->V )
390 if ( _solv->arch == ARCH_SRC || _solv->arch == ARCH_NOSRC )
391 idx << "srcpackage:" << idstr(name) << SEP << idstr(evr) << SEP << "noarch" << endl;
392 else
393 idx << idstr(name) << SEP << idstr(evr) << SEP << idstr(arch) << endl;
394 }
395 }
396 }
397 else
398 {
399 ERR << "Can't read solv-file: " << ::pool_errstr( _pool ) << endl;
400 }
401 ::repo_free( _repo, 0 );
402 ::pool_free( _pool );
403 }
404
406 } // namespace sat
409} // namespace zypp
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition Easy.h:27
#define MIL
Definition Logger.h:130
#define ERR
Definition Logger.h:132
#define idstr(V)
#define SEP
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition AutoDispose.h:95
void resetDispose()
Set no dispose function.
void reset()
Reset to default Ctor values.
A sat capability.
Definition Capability.h:63
sat::detail::IdType id() const
Expert backdoor.
Definition Capability.h:289
Convenience errno wrapper.
Definition Errno.h:26
IdType id() const
'Language[_Country]' codes.
Definition Locale.h:51
What is known about a repository.
Definition RepoInfo.h:72
void setInfo(const RepoInfo &info_r)
Set RepoInfo for this repository.
bool isSystemRepo() const
Return whether this is the system repository.
Definition Repository.cc:59
Simple serial number provider.
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
Definition Pathname.h:192
const char * c_str() const
String representation.
Definition Pathname.h:113
const std::string & asString() const
String representation.
Definition Pathname.h:94
std::string basename() const
Return the last component of this path.
Definition Pathname.h:137
std::string alias() const
unique identifier for this source.
Global sat-pool.
Definition Pool.h:48
const SerialNumber & serialIDs() const
Serial number changing whenever resusePoolIDs==true was used.
Definition Pool.cc:61
Queue autoInstalled() const
Get ident list of all autoinstalled solvables.
Definition Pool.cc:328
RepositoryIterator reposEnd() const
Iterator behind the last Repository.
Definition Pool.cc:90
bool addRequestedLocale(const Locale &locale_r)
Add one Locale to the set of requested locales.
Definition Pool.cc:303
detail::SolvableIterator SolvableIterator
Definition Pool.h:50
size_type reposSize() const
Number of repos in Pool.
Definition Pool.cc:76
const MultiversionList & multiversion() const
Definition Pool.cc:325
const LocaleSet & getAvailableLocales() const
Get the set of available locales.
Definition Pool.cc:319
void setSnapshotCandidate(const Pathname &path_r, const std::string &cookie_r, const std::vector< std::string > &aliases_r)
Arm snapshot writing: once the pool holds exactly the repos in aliases_r (sorted),...
Definition Pool.cc:198
void setAutoInstalled(const Queue &autoInstalled_r)
Set ident list of all autoinstalled solvables.
Definition Pool.cc:329
Queue whatMatchesSolvable(const SolvAttr &attr, const Solvable &solv) const
Definition Pool.cc:132
Iterable< RepositoryIterator > repos() const
Iterate the repositories.
Definition Pool.h:96
const LocaleSet & getRemovedRequestedLocales() const
Removed since last initRequestedLocales.
Definition Pool.cc:317
Queue whatContainsDep(const SolvAttr &attr, const Capability &cap) const
Definition Pool.cc:139
bool solvablesEmpty() const
Whether Pool contains solvables.
Definition Pool.cc:93
bool isRequestedLocale(const Locale &locale_r) const
Whether this Locale is in the set of requested locales.
Definition Pool.cc:312
Repository findSystemRepo() const
Return the system repository if it is on the pool.
Definition Pool.cc:176
size_type capacity() const
Internal array size for stats only.
Definition Pool.cc:55
SolvableIterator solvablesEnd() const
Iterator behind the last Solvable.
Definition Pool.cc:122
void setTextLocale(const Locale &locale_r)
Set the default language for retrieving translated texts.
Definition Pool.cc:297
Pathname rootDir() const
Get rootdir (for file conflicts check)
Definition Pool.cc:67
SolvableSet MultiversionList
Definition Pool.h:286
detail::size_type size_type
Definition Pool.h:52
RepositoryIterator reposBegin() const
Iterator to the first Repository.
Definition Pool.cc:79
static std::string snapshotCookie(const Pathname &path_r)
Cookie stored in the snapshot, empty if unreadable.
Definition Pool.cc:201
Repository reposFind(const std::string &alias_r) const
Find a Repository named alias_r.
Definition Pool.cc:166
size_type solvablesSize() const
Number of solvables in Pool.
Definition Pool.cc:106
const SerialNumber & serial() const
Housekeeping data serial number.
Definition Pool.cc:58
Repository addRepoSolv(const Pathname &file_r, const std::string &name_r)
Load Solvables from a solv-file into a Repository named name_r.
Definition Pool.cc:249
bool eraseRequestedLocale(const Locale &locale_r)
Erase one Locale from the set of requested locales.
Definition Pool.cc:306
bool isAvailableLocale(const Locale &locale_r) const
Whether this Locale is in the set of available locales.
Definition Pool.cc:322
Queue whatMatchesDep(const SolvAttr &attr, const Capability &cap) const
Definition Pool.cc:125
static const std::string & systemRepoAlias()
Reserved system repository alias @System .
Definition Pool.cc:49
Repository addRepoHelix(const Pathname &file_r, const std::string &name_r)
Load Solvables from a helix-file into a Repository named name_r.
Definition Pool.cc:273
zypp::detail::RepositoryIterator RepositoryIterator
Definition Pool.h:51
SolvableIterator solvablesBegin() const
Iterator to the first Solvable.
Definition Pool.cc:119
Repository reposInsert(const std::string &alias_r)
Return a Repository named alias_r.
Definition Pool.cc:146
void prepare() const
Update housekeeping data if necessary (e.g.
Definition Pool.cc:64
bool writeSnapshot(const Pathname &path_r, const std::string &cookie_r) const
Definition Pool.cc:223
bool mapSnapshot(const Pathname &path_r, const std::string &cookie_r)
Definition Pool.cc:213
void setNeedrebootSpec(sat::SolvableSpec needrebootSpec_r)
Solvables which should trigger the reboot-needed hint if installed/updated.
Definition Pool.cc:331
detail::CPool * get() const
Expert backdoor.
Definition Pool.cc:52
void reserveIds(unsigned numid_r, unsigned numrel_r)
Size the pool's id hash tables for the given number of new string and rel ids that upcoming addRepoSo...
Definition Pool.cc:188
static bool snapshotMapped()
Whether mapSnapshot succeeded in this process.
Definition Pool.cc:195
void setRequestedLocales(const LocaleSet &locales_r)
Set the requested locales.
Definition Pool.cc:300
Repository systemRepo()
Return the system repository, create it if missing.
Definition Pool.cc:181
const LocaleSet & getRequestedLocales() const
Return the requested locales.
Definition Pool.cc:309
const LocaleSet & getAddedRequestedLocales() const
Added since last initRequestedLocales.
Definition Pool.cc:316
bool reposEmpty() const
Whether Pool contains repos.
Definition Pool.cc:73
void initRequestedLocales(const LocaleSet &locales_r)
Start tracking changes based on this locales_r.
Definition Pool.cc:315
Libsolv Id queue wrapper.
Definition Queue.h:36
Solvable attribute keys.
Definition SolvAttr.h:41
A Solvable object within the sat Pool.
Definition Solvable.h:54
IdType id() const
Expert backdoor.
Definition Solvable.h:464
CPool * getPool() const
Definition PoolImpl.h:184
Pathname rootDir() const
Get rootdir (for file conflicts check)
Definition PoolImpl.h:120
StringQueue autoInstalled() const
Get ident list of all autoinstalled solvables.
Definition PoolImpl.h:324
void setTextLocale(const Locale &locale_r)
Definition PoolImpl.cc:584
void initRequestedLocales(const LocaleSet &locales_r)
Start tracking changes based on this locales_r.
Definition PoolImpl.cc:609
const LocaleSet & getAvailableLocales() const
All Locales occurring in any repo.
Definition PoolImpl.cc:719
bool isRequestedLocale(const Locale &locale_r) const
Definition PoolImpl.h:282
const SerialNumber & serialIDs() const
Serial number changing whenever resusePoolIDs==true was used.
Definition PoolImpl.h:78
bool eraseRequestedLocale(const Locale &locale_r)
User change (tracked).
Definition PoolImpl.cc:638
const LocaleSet & getRequestedLocales() const
Current set of requested Locales.
Definition PoolImpl.h:279
void setRequestedLocales(const LocaleSet &locales_r)
User change (tracked).
Definition PoolImpl.cc:618
const LocaleSet & getRemovedRequestedLocales() const
Removed since last initRequestedLocales.
Definition PoolImpl.h:275
void setNeedrebootSpec(sat::SolvableSpec needrebootSpec_r)
Set new Solvable specs.
Definition PoolImpl.h:342
const MultiversionList & multiversionList() const
Definition PoolImpl.cc:765
bool isAvailableLocale(const Locale &locale_r) const
Definition PoolImpl.h:295
void setSnapshotCandidate(const Pathname &path_r, const std::string &cookie_r, const std::vector< std::string > &aliases_r)
Definition PoolImpl.h:88
const LocaleSet & getAddedRequestedLocales() const
Added since last initRequestedLocales.
Definition PoolImpl.h:271
void setAutoInstalled(const StringQueue &autoInstalled_r)
Set ident list of all autoinstalled solvables.
Definition PoolImpl.h:328
bool addRequestedLocale(const Locale &locale_r)
User change (tracked).
Definition PoolImpl.cc:627
void prepare() const
Update housekeeping data (e.g.
Definition PoolImpl.cc:402
static const std::string & systemRepoAlias()
Reserved system repository alias @System .
Definition PoolImpl.cc:116
const SerialNumber & serial() const
Serial number changing whenever the content changes.
Definition PoolImpl.h:74
nullptr CURL handle void p CURLversion nullptr struct curl_slist list CURLM CURLM_INTERNAL_ERROR CURLM CURL CURLM_INTERNAL_ERROR CURLM curl_socket_t int int CURLM_INTERNAL_ERROR CURLINFO info
Definition curl_dl.cc:117
String related utilities and Regular expression matching.
::s_Repo CRepo
Wrapped libsolv C data type exposed as backdoor.
Definition PoolDefines.h:38
::s_Solvable CSolvable
Wrapped libsolv C data type exposed as backdoor.
Definition PoolDefines.h:39
::s_Pool CPool
Wrapped libsolv C data type exposed as backdoor.
Definition PoolDefines.h:36
Libsolv interface
std::ostream & operator<<(std::ostream &str, const FileConflicts &obj)
relates: FileConflicts Stream output
void updateSolvFileIndex(const Pathname &solvfile_r)
Create solv file content digest for zypper bash completion.
Definition Pool.cc:350
std::string numstring(char n, int w=0)
Definition String.h:290
Easy-to use interface to the ZYPP dependency resolver.
std::unordered_set< Locale > LocaleSet
Definition Locale.h:29
AutoDispose<FILE*> calling fclose
Functor removing Repository from its Pool.
Definition Repository.h:444
static PoolImpl & myPool()
Definition PoolMember.cc:41