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
202 { ++myPool().snapshotClaimingRef(); }
203
206
207 bool Pool::mapSnapshot( const Pathname & path_r, const std::string & cookie_r )
208 {
209 if ( cookie_r.empty() )
210 return false;
211 if ( ! myPool().mapSnapshot( path_r, cookie_r ) )
212 return false;
213 snapshotMappedFlag() = true;
214 return true;
215 }
216
217 bool Pool::writeSnapshot( const Pathname & path_r, const std::string & cookie_r ) const
218 {
219 // pid-unique tempname: concurrent writers must not interleave
220 // into the same file, and the final file may only ever appear
221 // via rename (live readers borrow pages from it)
222 Pathname tmp( path_r.extend( ".new." + str::numstring( getpid() ) ) );
223 AutoFILE fp { ::fopen( tmp.c_str(), "we" ) };
224 if ( !fp )
225 return false;
226 // The reader mmaps this file and trusts the sizes in its header, so
227 // it must be complete on disk before it is reachable under its final
228 // name: flush, fsync, and only then rename. Skipping that would not
229 // corrupt a reader (the header checksum and the size check reject a
230 // short file), but every later run would reject and rewrite it.
231 bool ok = ( ::pool_snapshot_write( get(), fp, reinterpret_cast<const unsigned char*>(cookie_r.data()), cookie_r.size() ) == 0
232 && ::fflush( fp ) == 0
233 && ::fsync( ::fileno( fp ) ) == 0 );
234 ::fchmod( ::fileno( fp ), 0644 ); // cosmetic, never a reason to drop the file
235 // fclose() reports write errors the buffered writes above could not
236 FILE * raw = fp;
237 fp.resetDispose(); // we close it ourselves, to see the result
238 ok = ( ::fclose( raw ) == 0 && ok );
239 if ( ! ok || ::rename( tmp.c_str(), path_r.c_str() ) != 0 )
240 {
241 ::unlink( tmp.c_str() );
242 return false;
243 }
244 MIL << "Wrote pool snapshot " << path_r << endl;
245 return true;
246 }
247
248 Repository Pool::addRepoSolv( const Pathname & file_r, const std::string & alias_r )
249 {
250 // Using a temporay repo! (The additional parenthesis are required.)
252 *tmprepo = reposInsert( alias_r );
253 tmprepo->addSolv( file_r );
254
255 // no exceptions so we keep it:
256 tmprepo.resetDispose();
257 return tmprepo;
258 }
259
261 { return addRepoSolv( file_r, file_r.basename() ); }
262
263 Repository Pool::addRepoSolv( const Pathname & file_r, const RepoInfo & info_r )
264 {
265 Repository ret( addRepoSolv( file_r, info_r.alias() ) );
266 ret.setInfo( info_r );
267 return ret;
268 }
269
271
272 Repository Pool::addRepoHelix( const Pathname & file_r, const std::string & alias_r )
273 {
274 // Using a temporay repo! (The additional parenthesis are required.)
276 *tmprepo = reposInsert( alias_r );
277 tmprepo->addHelix( file_r );
278
279 // no exceptions so we keep it:
280 tmprepo.resetDispose();
281 return tmprepo;
282 }
283
285 { return addRepoHelix( file_r, file_r.basename() ); }
286
287 Repository Pool::addRepoHelix( const Pathname & file_r, const RepoInfo & info_r )
288 {
289 Repository ret( addRepoHelix( file_r, info_r.alias() ) );
290 ret.setInfo( info_r );
291 return ret;
292 }
293
295
296 void Pool::setTextLocale( const Locale & locale_r )
297 { myPool().setTextLocale( locale_r ); }
298
299 void Pool::setRequestedLocales( const LocaleSet & locales_r )
300 { myPool().setRequestedLocales( locales_r ); }
301
302 bool Pool::addRequestedLocale( const Locale & locale_r )
303 { return myPool().addRequestedLocale( locale_r ); }
304
305 bool Pool::eraseRequestedLocale( const Locale & locale_r )
306 { return myPool().eraseRequestedLocale( locale_r ); }
307
310
311 bool Pool::isRequestedLocale( const Locale & locale_r ) const
312 { return myPool().isRequestedLocale( locale_r ); }
313
314 void Pool::initRequestedLocales( const LocaleSet & locales_r ) { myPool().initRequestedLocales( locales_r ); }
317
320
321 bool Pool::isAvailableLocale( const Locale & locale_r ) const
322 { return myPool().isAvailableLocale( locale_r ); }
323
326
328 void Pool::setAutoInstalled( const Queue & autoInstalled_r ){ myPool().setAutoInstalled( autoInstalled_r ); }
329
330 void Pool::setNeedrebootSpec( sat::SolvableSpec needrebootSpec_r ) { myPool().setNeedrebootSpec( std::move(needrebootSpec_r) ); }
331
332 /******************************************************************
333 **
334 ** FUNCTION NAME : operator<<
335 ** FUNCTION TYPE : std::ostream &
336 */
337 std::ostream & operator<<( std::ostream & str, const Pool & obj )
338 {
339 return str << "sat::pool(" << obj.serial() << ")["
340 << obj.capacity() << "]{"
341 << obj.reposSize() << "repos|"
342 << obj.solvablesSize() << "solv}";
343 }
344
346 #undef ZYPP_BASE_LOGGER_LOGGROUP
347 #define ZYPP_BASE_LOGGER_LOGGROUP "solvidx"
348
349 void updateSolvFileIndex( const Pathname & solvfile_r )
350 {
351 AutoDispose<FILE*> solv( ::fopen( solvfile_r.c_str(), "re" ), ::fclose );
352 if ( solv == NULL )
353 {
354 solv.resetDispose();
355 ERR << "Can't open solv-file: " << solv << endl;
356 return;
357 }
358
359 std::string solvidxfile( solvfile_r.extend(".idx").asString() );
360 if ( ::unlink( solvidxfile.c_str() ) == -1 && errno != ENOENT )
361 {
362 ERR << "Can't unlink solv-idx: " << Errno() << endl;
363 return;
364 }
365 {
366 int fd = ::open( solvidxfile.c_str(), O_CREAT|O_EXCL|O_WRONLY|O_TRUNC, 0644 );
367 if ( fd == -1 )
368 {
369 ERR << "Can't create solv-idx: " << Errno() << endl;
370 return;
371 }
372 ::close( fd );
373 }
374 std::ofstream idx( solvidxfile.c_str() );
375
376
377 detail::CPool * _pool = ::pool_create();
378 detail::CRepo * _repo = ::repo_create( _pool, "" );
379 if ( ::repo_add_solv( _repo, solv, 0 ) == 0 )
380 {
381 int _id = 0;
382 detail::CSolvable * _solv = nullptr;
383 FOR_REPO_SOLVABLES( _repo, _id, _solv )
384 {
385 if ( _solv )
386 {
387#define SEP '\t'
388#define idstr(V) pool_id2str( _pool, _solv->V )
389 if ( _solv->arch == ARCH_SRC || _solv->arch == ARCH_NOSRC )
390 idx << "srcpackage:" << idstr(name) << SEP << idstr(evr) << SEP << "noarch" << endl;
391 else
392 idx << idstr(name) << SEP << idstr(evr) << SEP << idstr(arch) << endl;
393 }
394 }
395 }
396 else
397 {
398 ERR << "Can't read solv-file: " << ::pool_errstr( _pool ) << endl;
399 }
400 ::repo_free( _repo, 0 );
401 ::pool_free( _pool );
402 }
403
405 } // namespace sat
408} // 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.
A sat capability.
Definition Capability.h:63
sat::detail::IdType id() const
Expert backdoor.
Definition Capability.h:289
Errno()
Default ctor: errno.
Definition Errno.h:29
IdType id() const
'Language[_Country]' codes.
Definition Locale.h:51
What is known about a repository.
Definition RepoInfo.h:72
Repository()
Default ctor creates noRepository.
Definition Repository.h:53
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:327
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:302
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:324
const LocaleSet & getAvailableLocales() const
Get the set of available locales.
Definition Pool.cc:318
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:328
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:316
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:311
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:296
Pathname rootDir() const
Get rootdir (for file conflicts check).
Definition Pool.cc:67
SolvableSet MultiversionList
Definition Pool.h:304
detail::size_type size_type
Definition Pool.h:52
RepositoryIterator reposBegin() const
Iterator to the first Repository.
Definition Pool.cc:79
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:248
bool eraseRequestedLocale(const Locale &locale_r)
Erase one Locale from the set of requested locales.
Definition Pool.cc:305
bool isAvailableLocale(const Locale &locale_r) const
Whether this Locale is in the set of available locales.
Definition Pool.cc:321
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:272
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:217
bool mapSnapshot(const Pathname &path_r, const std::string &cookie_r)
Definition Pool.cc:207
void setNeedrebootSpec(sat::SolvableSpec needrebootSpec_r)
Solvables which should trigger the reboot-needed hint if installed/updated.
Definition Pool.cc:330
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:299
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:308
const LocaleSet & getAddedRequestedLocales() const
Added since last initRequestedLocales.
Definition Pool.cc:315
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:314
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
unsigned & snapshotClaimingRef() const
Nesting level of Pool::ClaimingSnapshotRepos.
Definition PoolImpl.h:92
CPool * getPool() const
Definition PoolImpl.h:188
Pathname rootDir() const
Get rootdir (for file conflicts check).
Definition PoolImpl.h:124
StringQueue autoInstalled() const
Get ident list of all autoinstalled solvables.
Definition PoolImpl.h:328
void setTextLocale(const Locale &locale_r)
Definition PoolImpl.cc:619
void initRequestedLocales(const LocaleSet &locales_r)
Start tracking changes based on this locales_r.
Definition PoolImpl.cc:644
const LocaleSet & getAvailableLocales() const
All Locales occurring in any repo.
Definition PoolImpl.cc:754
bool isRequestedLocale(const Locale &locale_r) const
Definition PoolImpl.h:286
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:673
const LocaleSet & getRequestedLocales() const
Current set of requested Locales.
Definition PoolImpl.h:283
void setRequestedLocales(const LocaleSet &locales_r)
User change (tracked).
Definition PoolImpl.cc:653
const LocaleSet & getRemovedRequestedLocales() const
Removed since last initRequestedLocales.
Definition PoolImpl.h:279
void setNeedrebootSpec(sat::SolvableSpec needrebootSpec_r)
Set new Solvable specs.
Definition PoolImpl.h:346
const MultiversionList & multiversionList() const
Definition PoolImpl.cc:800
bool isAvailableLocale(const Locale &locale_r) const
Definition PoolImpl.h:299
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:275
void setAutoInstalled(const StringQueue &autoInstalled_r)
Set ident list of all autoinstalled solvables.
Definition PoolImpl.h:332
bool addRequestedLocale(const Locale &locale_r)
User change (tracked).
Definition PoolImpl.cc:662
void prepare() const
Update housekeeping data (e.g.
Definition PoolImpl.cc:398
static const std::string & systemRepoAlias()
Reserved system repository alias @System .
Definition PoolImpl.cc:108
const SerialNumber & serial() const
Serial number changing whenever the content changes.
Definition PoolImpl.h:74
CURL handle void p CURLversion age struct curl_slist list CURLM multi CURLM CURL handle CURLM curl_socket_t int int running_handles CURLINFO info
Definition curl_dl.cc:160
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:349
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