1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/target/TargetImpl.cc
19 #include <sys/types.h>
22 #include "zypp/base/LogTools.h"
23 #include "zypp/base/Exception.h"
24 #include "zypp/base/Iterator.h"
25 #include "zypp/base/Gettext.h"
26 #include "zypp/base/IOStream.h"
27 #include "zypp/base/UserRequestException.h"
29 #include "zypp/ZConfig.h"
31 #include "zypp/PoolItem.h"
32 #include "zypp/ResObjects.h"
34 #include "zypp/TmpPath.h"
35 #include "zypp/RepoStatus.h"
36 #include "zypp/ExternalProgram.h"
37 #include "zypp/Repository.h"
39 #include "zypp/ResFilters.h"
40 #include "zypp/target/CommitLog.h"
41 #include "zypp/target/TargetImpl.h"
42 #include "zypp/target/TargetCallbackReceiver.h"
43 #include "zypp/target/rpm/librpmDb.h"
44 #include "zypp/target/CommitPackageCache.h"
46 #include "zypp/pool/GetResolvablesToInsDel.h"
47 #include "zypp/solver/detail/Helper.h"
49 #include "zypp/repo/DeltaCandidates.h"
50 #include "zypp/repo/PackageProvider.h"
51 #include "zypp/repo/ScriptProvider.h"
52 #include "zypp/repo/SrcPackageProvider.h"
54 #include "zypp/sat/Pool.h"
58 ///////////////////////////////////////////////////////////////////
60 { /////////////////////////////////////////////////////////////////
61 ///////////////////////////////////////////////////////////////////
63 { /////////////////////////////////////////////////////////////////
65 ///////////////////////////////////////////////////////////////////
67 { /////////////////////////////////////////////////////////////////
69 /** Execute script and report against report_r.
70 * Return \c std::pair<bool,PatchScriptReport::Action> to indicate if
71 * execution was successfull (<tt>first = true</tt>), or the desired
72 * \c PatchScriptReport::Action in case execution failed
73 * (<tt>first = false</tt>).
75 * \note The packager is responsible for setting the correct permissions
76 * of the script. If the script is not executable it is reported as an
77 * error. We must not modify the permessions.
79 std::pair<bool,PatchScriptReport::Action> doExecuteScript( const Pathname & root_r,
80 const Pathname & script_r,
81 callback::SendReport<PatchScriptReport> & report_r )
83 MIL << "Execute script " << PathInfo(script_r) << endl;
85 CommitLog progresslog;
86 progresslog(/*timestamp*/true) << script_r << _(" executed") << endl;
87 ExternalProgram prog( script_r.asString(), ExternalProgram::Stderr_To_Stdout, false, -1, true /*, root_r*/ );
89 for ( std::string output = prog.receiveLine(); output.length(); output = prog.receiveLine() )
91 progresslog() << output;
92 if ( ! report_r->progress( PatchScriptReport::OUTPUT, output ) )
94 WAR << "User request to abort script " << script_r << endl;
96 // the rest is handled by exit code evaluation
97 // in case the script has meanwhile finished.
101 std::pair<bool,PatchScriptReport::Action> ret( std::make_pair( false, PatchScriptReport::ABORT ) );
103 if ( prog.close() != 0 )
105 ret.second = report_r->problem( prog.execError() );
106 WAR << "ACTION" << ret.second << "(" << prog.execError() << ")" << endl;
107 progresslog(/*timestamp*/true)<< script_r << _(" execution failed") << " (" << prog.execError() << ")" << endl;
116 /** Execute script and report against report_r.
117 * Return \c false if user requested \c ABORT.
119 bool executeScript( const Pathname & root_r,
120 const Pathname & script_r,
121 callback::SendReport<PatchScriptReport> & report_r )
123 std::pair<bool,PatchScriptReport::Action> action( std::make_pair( false, PatchScriptReport::ABORT ) );
126 action = doExecuteScript( root_r, script_r, report_r );
128 return true; // success
130 switch ( action.second )
132 case PatchScriptReport::ABORT:
133 WAR << "User request to abort at script " << script_r << endl;
134 return false; // requested abort.
137 case PatchScriptReport::IGNORE:
138 WAR << "User request to skip script " << script_r << endl;
139 return true; // requested skip.
142 case PatchScriptReport::RETRY:
145 } while ( action.second == PatchScriptReport::RETRY );
147 // THIS is not intended to be reached:
148 INT << "Abort on unknown ACTION request " << action.second << " returned" << endl;
149 return false; // abort.
152 /** Look for patch scripts named 'name-version-release-*' and
153 * execute them. Return \c false if \c ABORT was requested.
155 bool RunUpdateScripts( const Pathname & root_r,
156 const Pathname & scriptsPath_r,
157 const std::vector<sat::Solvable> & checkPackages_r,
160 if ( checkPackages_r.empty() )
161 return true; // no installed packages to check
163 MIL << "Looking for new patch scripts in (" << root_r << ")" << scriptsPath_r << endl;
164 Pathname scriptsDir( Pathname::assertprefix( root_r, scriptsPath_r ) );
165 if ( ! PathInfo( scriptsDir ).isDir() )
166 return true; // no script dir
168 std::list<std::string> scripts;
169 filesystem::readdir( scripts, scriptsDir, /*dots*/false );
170 if ( scripts.empty() )
171 return true; // no scripts in script dir
173 // Now collect and execute all matching scripts.
174 // On ABORT: at least log all outstanding scripts.
176 for_( it, checkPackages_r.begin(), checkPackages_r.end() )
178 std::string prefix( str::form( "%s-%s-", it->name().c_str(), it->edition().c_str() ) );
179 for_( sit, scripts.begin(), scripts.end() )
181 if ( ! str::hasPrefix( *sit, prefix ) )
184 PathInfo script( scriptsDir / *sit );
185 if ( ! script.isFile() )
188 if ( abort || aborting_r )
190 WAR << "Aborting: Skip patch script " << *sit << endl;
191 CommitLog()(/*timestamp*/true) << script.path() << _(" execution skipped while aborting") << endl;
195 MIL << "Found patch script " << *sit << endl;
196 callback::SendReport<PatchScriptReport> report;
197 report->start( make<Package>( *it ), script.path() );
199 if ( ! executeScript( root_r, script.path(), report ) )
200 abort = true; // requested abort.
207 /////////////////////////////////////////////////////////////////
209 ///////////////////////////////////////////////////////////////////
211 /** Helper for PackageProvider queries during commit. */
212 struct QueryInstalledEditionHelper
214 bool operator()( const std::string & name_r,
215 const Edition & ed_r,
216 const Arch & arch_r ) const
218 rpm::librpmDb::db_const_iterator it;
219 for ( it.findByName( name_r ); *it; ++it )
221 if ( arch_r == it->tag_arch()
222 && ( ed_r == Edition::noedition || ed_r == it->tag_edition() ) )
232 * \short Let the Source provide the package.
233 * \p pool_r \ref ResPool used to get candidates
234 * \p pi item to be commited
236 struct RepoProvidePackage
239 repo::RepoMediaAccess &_access;
241 RepoProvidePackage( repo::RepoMediaAccess &access, ResPool pool_r )
242 : _pool(pool_r), _access(access)
247 ManagedFile operator()( const PoolItem & pi )
249 // Redirect PackageProvider queries for installed editions
250 // (in case of patch/delta rpm processing) to rpmDb.
251 repo::PackageProviderPolicy packageProviderPolicy;
252 packageProviderPolicy.queryInstalledCB( QueryInstalledEditionHelper() );
254 Package::constPtr p = asKind<Package>(pi.resolvable());
257 // Build a repository list for repos
258 // contributing to the pool
259 std::list<Repository> repos( _pool.knownRepositoriesBegin(), _pool.knownRepositoriesEnd() );
260 repo::DeltaCandidates deltas(repos, p->name());
261 repo::PackageProvider pkgProvider( _access, p, deltas, packageProviderPolicy );
262 return pkgProvider.providePackage();
265 ///////////////////////////////////////////////////////////////////
267 IMPL_PTR_TYPE(TargetImpl);
269 TargetImpl_Ptr TargetImpl::_nullimpl;
271 /** Null implementation */
272 TargetImpl_Ptr TargetImpl::nullimpl()
275 _nullimpl = new TargetImpl;
279 ///////////////////////////////////////////////////////////////////
281 // METHOD NAME : TargetImpl::TargetImpl
282 // METHOD TYPE : Ctor
284 TargetImpl::TargetImpl( const Pathname & root_r, bool doRebuild_r )
286 , _requestedLocalesFile( home() / "RequestedLocales" )
287 , _softLocksFile( home() / "SoftLocks" )
288 , _hardLocksFile( Pathname::assertprefix( _root, ZConfig::instance().locksFile() ) )
290 _rpm.initDatabase( root_r, Pathname(), doRebuild_r );
291 MIL << "Initialized target on " << _root << endl;
294 ///////////////////////////////////////////////////////////////////
296 // METHOD NAME : TargetImpl::~TargetImpl
297 // METHOD TYPE : Dtor
299 TargetImpl::~TargetImpl()
301 _rpm.closeDatabase();
302 MIL << "Targets closed" << endl;
305 void TargetImpl::clearCache()
307 Pathname base = Pathname::assertprefix( _root,
308 ZConfig::instance().repoSolvfilesPath() / sat::Pool::instance().systemRepoAlias() );
309 filesystem::recursive_rmdir( base );
312 void TargetImpl::buildCache()
314 Pathname base = Pathname::assertprefix( _root,
315 ZConfig::instance().repoSolvfilesPath() / sat::Pool::instance().systemRepoAlias() );
316 Pathname rpmsolv = base/"solv";
317 Pathname rpmsolvcookie = base/"cookie";
319 bool build_rpm_solv = true;
320 // lets see if the rpm solv cache exists
322 RepoStatus rpmstatus(_root + "/var/lib/rpm/Name");
323 bool solvexisted = PathInfo(rpmsolv).isExist();
326 // see the status of the cache
327 PathInfo cookie( rpmsolvcookie );
328 MIL << "Read cookie: " << cookie << endl;
329 if ( cookie.isExist() )
331 RepoStatus status = RepoStatus::fromCookieFile(rpmsolvcookie);
332 // now compare it with the rpm database
333 if ( status.checksum() == rpmstatus.checksum() )
334 build_rpm_solv = false;
335 MIL << "Read cookie: " << rpmsolvcookie << " says: "
336 << (build_rpm_solv ? "outdated" : "uptodate") << endl;
340 if ( build_rpm_solv )
342 // Take care we unlink the solvfile on exception
343 ManagedFile guard( base, filesystem::recursive_rmdir );
345 // if it does not exist yet, we better create it
346 filesystem::assert_dir( base );
348 filesystem::TmpFile tmpsolv( filesystem::TmpFile::makeSibling( rpmsolv ) );
351 Exception ex("Failed to cache rpm database.");
352 ex.remember(str::form(
353 "Cannot create temporary file under %s.", base.c_str()));
357 std::ostringstream cmd;
359 if ( ! _root.empty() )
360 cmd << " -r '" << _root << "'";
363 cmd << " '" << rpmsolv << "'";
365 cmd << " > '" << tmpsolv.path() << "'";
367 MIL << "Executing: " << cmd << endl;
368 ExternalProgram prog( cmd.str(), ExternalProgram::Stderr_To_Stdout );
371 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
372 WAR << " " << output;
373 cmd << " " << output;
376 int ret = prog.close();
379 Exception ex(str::form("Failed to cache rpm database (%d).", ret));
380 ex.remember( cmd.str() );
384 ret = filesystem::rename( tmpsolv, rpmsolv );
386 ZYPP_THROW(Exception("Failed to move cache to final destination"));
387 // if this fails, don't bother throwing exceptions
388 filesystem::chmod( rpmsolv, 0644 );
390 rpmstatus.saveToCookieFile(rpmsolvcookie);
393 guard.resetDispose();
397 void TargetImpl::unload()
399 Repository system( sat::Pool::instance().findSystemRepo() );
401 system.eraseFromPool();
405 void TargetImpl::load()
409 // now add the repos to the pool
410 sat::Pool satpool( sat::Pool::instance() );
411 Pathname rpmsolv( Pathname::assertprefix( _root,
412 ZConfig::instance().repoSolvfilesPath() / satpool.systemRepoAlias() / "solv" ) );
413 MIL << "adding " << rpmsolv << " to pool(" << satpool.systemRepoAlias() << ")" << endl;
415 // Providing an empty system repo, unload any old content
416 Repository system( sat::Pool::instance().findSystemRepo() );
417 if ( system && ! system.solvablesEmpty() )
419 system.eraseFromPool(); // invalidates system
423 system = satpool.systemRepo();
428 system.addSolv( rpmsolv );
430 catch ( const Exception & exp )
433 MIL << "Try to handle exception by rebuilding the solv-file" << endl;
437 system.addSolv( rpmsolv );
440 // (Re)Load the requested locales et al.
441 // If the requested locales are empty, we leave the pool untouched
442 // to avoid undoing changes the application applied. We expect this
443 // to happen on a bare metal installation only. An already existing
444 // target should be loaded before its settings are changed.
446 const LocaleSet & requestedLocales( _requestedLocalesFile.locales() );
447 if ( ! requestedLocales.empty() )
449 satpool.setRequestedLocales( requestedLocales );
453 const SoftLocksFile::Data & softLocks( _softLocksFile.data() );
454 if ( ! softLocks.empty() )
456 ResPool::instance().setAutoSoftLocks( softLocks );
459 if ( ZConfig::instance().apply_locks_file() )
461 const HardLocksFile::Data & hardLocks( _hardLocksFile.data() );
462 if ( ! hardLocks.empty() )
464 ResPool::instance().setHardLockQueries( hardLocks );
469 MIL << "Target loaded: " << system.solvablesSize() << " resolvables" << endl;
472 ZYppCommitResult TargetImpl::commit( ResPool pool_r, const ZYppCommitPolicy & policy_rX )
474 // ----------------------------------------------------------------- //
475 // Fake outstanding YCP fix: Honour restriction to media 1
476 // at installation, but install all remaining packages if post-boot.
477 ZYppCommitPolicy policy_r( policy_rX );
478 if ( policy_r.restrictToMedia() > 1 )
480 // ----------------------------------------------------------------- //
482 MIL << "TargetImpl::commit(<pool>, " << policy_r << ")" << endl;
484 ///////////////////////////////////////////////////////////////////
485 // Store non-package data:
486 ///////////////////////////////////////////////////////////////////
487 filesystem::assert_dir( home() );
489 _requestedLocalesFile.setLocales( pool_r.getRequestedLocales() );
492 SoftLocksFile::Data newdata;
493 pool_r.getActiveSoftLocks( newdata );
494 _softLocksFile.setData( newdata );
497 if ( ZConfig::instance().apply_locks_file() )
499 HardLocksFile::Data newdata;
500 pool_r.getHardLockQueries( newdata );
501 _hardLocksFile.setData( newdata );
504 ///////////////////////////////////////////////////////////////////
506 ///////////////////////////////////////////////////////////////////
507 if ( root() == "/" && CommitLog::fname().empty() )
509 // Yes, we simply hijack /var/log/YaST2/y2logRPM
510 // until we maintain some zypp history database.
511 CommitLog::setFname( "/var/log/YaST2/y2logRPM" );
514 ZYppCommitResult result;
516 TargetImpl::PoolItemList to_uninstall;
517 TargetImpl::PoolItemList to_install;
518 TargetImpl::PoolItemList to_srcinstall;
521 pool::GetResolvablesToInsDel
522 collect( pool_r, policy_r.restrictToMedia() ? pool::GetResolvablesToInsDel::ORDER_BY_MEDIANR
523 : pool::GetResolvablesToInsDel::ORDER_BY_SOURCE );
524 MIL << "GetResolvablesToInsDel: " << endl << collect << endl;
525 to_uninstall.swap( collect._toDelete );
526 to_install.swap( collect._toInstall );
527 to_srcinstall.swap( collect._toSrcinstall );
530 if ( policy_r.restrictToMedia() )
532 MIL << "Restrict to media number " << policy_r.restrictToMedia() << endl;
535 ///////////////////////////////////////////////////////////////////
536 // First collect and display all messages
537 // associated with patches to be installed.
538 ///////////////////////////////////////////////////////////////////
539 for_( it, to_install.begin(), to_install.end() )
541 if ( ! isKind<Patch>(it->resolvable()) )
543 if ( ! it->status().isToBeInstalled() )
546 Patch::constPtr patch( asKind<Patch>(it->resolvable()) );
547 if ( ! patch->message().empty() )
549 MIL << "Show message for " << patch << endl;
550 callback::SendReport<target::PatchMessageReport> report;
551 if ( ! report->show( patch ) )
553 WAR << "commit aborted by the user" << endl;
554 ZYPP_THROW( TargetAbortedException( N_("Installation has been aborted as directed.") ) );
559 ///////////////////////////////////////////////////////////////////
560 // Remove/install packages.
561 ///////////////////////////////////////////////////////////////////
562 commit (to_uninstall, policy_r, pool_r );
564 if (policy_r.restrictToMedia() == 0)
566 result._remaining = commit( to_install, policy_r, pool_r );
567 result._srcremaining = commit( to_srcinstall, policy_r, pool_r );
571 TargetImpl::PoolItemList current_install;
572 TargetImpl::PoolItemList current_srcinstall;
574 // Collect until the 1st package from an unwanted media occurs.
575 // Further collection could violate install order.
576 bool hitUnwantedMedia = false;
577 for (TargetImpl::PoolItemList::iterator it = to_install.begin(); it != to_install.end(); ++it)
579 ResObject::constPtr res( it->resolvable() );
581 if ( hitUnwantedMedia
582 || ( res->mediaNr() && res->mediaNr() != policy_r.restrictToMedia() ) )
584 hitUnwantedMedia = true;
585 result._remaining.push_back( *it );
589 current_install.push_back( *it );
593 TargetImpl::PoolItemList bad = commit( current_install, policy_r, pool_r );
594 result._remaining.insert(result._remaining.end(), bad.begin(), bad.end());
596 for (TargetImpl::PoolItemList::iterator it = to_srcinstall.begin(); it != to_srcinstall.end(); ++it)
598 Resolvable::constPtr res( it->resolvable() );
599 Package::constPtr pkg( asKind<Package>(res) );
600 if (pkg && policy_r.restrictToMedia() != pkg->mediaNr()) // check medianr for packages only
602 XXX << "Package " << *pkg << ", wrong media " << pkg->mediaNr() << endl;
603 result._srcremaining.push_back( *it );
607 current_srcinstall.push_back( *it );
610 bad = commit( current_srcinstall, policy_r, pool_r );
611 result._srcremaining.insert(result._srcremaining.end(), bad.begin(), bad.end());
614 ///////////////////////////////////////////////////////////////////
615 // Try to rebuild solv file while rpm database is still in cache.
616 ///////////////////////////////////////////////////////////////////
619 result._result = (to_install.size() - result._remaining.size());
620 MIL << "TargetImpl::commit(<pool>, " << policy_r << ") returns: " << result << endl;
625 TargetImpl::PoolItemList
626 TargetImpl::commit( const TargetImpl::PoolItemList & items_r,
627 const ZYppCommitPolicy & policy_r,
628 const ResPool & pool_r )
630 TargetImpl::PoolItemList remaining;
631 repo::RepoMediaAccess access;
632 MIL << "TargetImpl::commit(<list>" << policy_r << ")" << items_r.size() << endl;
635 std::vector<sat::Solvable> successfullyInstalledPackages;
637 RepoProvidePackage repoProvidePackage( access, pool_r);
638 // prepare the package cache.
639 CommitPackageCache packageCache( items_r.begin(), items_r.end(),
640 root() / "tmp", repoProvidePackage );
642 for (TargetImpl::PoolItemList::const_iterator it = items_r.begin(); it != items_r.end(); it++)
644 if (isKind<Package>(it->resolvable()))
646 Package::constPtr p = asKind<Package>(it->resolvable());
647 if (it->status().isToBeInstalled())
649 ManagedFile localfile;
652 localfile = packageCache.get( it );
654 catch ( const SkipRequestException &e )
657 WAR << "Skipping package " << p << " in commit" << endl;
661 // bnc #395704: missing catch causes abort. see if packageCache fails to handle
663 catch ( const Exception &e )
670 #warning Exception handling
671 // create a installation progress report proxy
672 RpmInstallPackageReceiver progress( it->resolvable() );
676 // Why force and nodeps?
678 // Because zypp builds the transaction and the resolver asserts that
679 // everything is fine.
680 // We use rpm just to unpack and register the package in the database.
681 // We do this step by step, so rpm is not aware of the bigger context.
682 // So we turn off rpms internal checks, because we do it inside zypp.
683 flags |= rpm::RpmDb::RPMINST_NODEPS;
684 flags |= rpm::RpmDb::RPMINST_FORCE;
686 if (p->installOnly()) flags |= rpm::RpmDb::RPMINST_NOUPGRADE;
687 if (policy_r.dryRun()) flags |= rpm::RpmDb::RPMINST_TEST;
688 if (policy_r.rpmNoSignature()) flags |= rpm::RpmDb::RPMINST_NOSIGNATURE;
692 progress.tryLevel( target::rpm::InstallResolvableReport::RPM_NODEPS_FORCE );
693 rpm().installPackage( localfile, flags );
695 if ( progress.aborted() )
697 WAR << "commit aborted by the user" << endl;
698 progress.disconnect();
704 catch (Exception & excpt_r)
706 ZYPP_CAUGHT(excpt_r);
707 if ( policy_r.dryRun() )
709 WAR << "dry run failed" << endl;
710 progress.disconnect();
714 WAR << "Install failed" << endl;
715 remaining.push_back( *it );
716 progress.disconnect();
721 if ( success && !policy_r.dryRun() )
723 it->status().resetTransact( ResStatus::USER );
724 // Remember to check this package for presence of patch scripts.
725 successfullyInstalledPackages.push_back( it->satSolvable() );
727 progress.disconnect();
733 RpmRemovePackageReceiver progress( it->resolvable() );
735 unsigned flags = rpm::RpmDb::RPMINST_NODEPS;
736 if (policy_r.dryRun()) flags |= rpm::RpmDb::RPMINST_TEST;
739 rpm().removePackage( p, flags );
741 if ( progress.aborted() )
743 WAR << "commit aborted by the user" << endl;
744 progress.disconnect();
750 catch (Exception & excpt_r)
752 WAR << "removal of " << p << " failed";
754 ZYPP_CAUGHT( excpt_r );
757 && !policy_r.dryRun())
759 it->status().resetTransact( ResStatus::USER );
761 progress.disconnect();
764 else if (!policy_r.dryRun()) // other resolvables (non-Package)
766 it->status().resetTransact( ResStatus::USER );
767 } // other resolvables
771 // Check presence of patch scripts. If aborting, at least log
773 if ( ! successfullyInstalledPackages.empty() )
775 if ( ! RunUpdateScripts( _root, ZConfig::instance().update_scriptsPath(),
776 successfullyInstalledPackages, abort ) )
778 WAR << "Commit aborted by the user" << endl;
785 ZYPP_THROW( TargetAbortedException( N_("Installation has been aborted as directed.") ) );
791 rpm::RpmDb & TargetImpl::rpm()
796 bool TargetImpl::providesFile (const std::string & path_str, const std::string & name_str) const
798 return _rpm.hasFile(path_str, name_str);
801 /** Set the log file for target */
802 bool TargetImpl::setInstallationLogfile(const Pathname & path_r)
804 CommitLog::setFname(path_r);
808 Date TargetImpl::timestamp() const
810 return _rpm.timestamp();
813 std::string TargetImpl::release() const
815 std::ifstream suseRelease( (_root / "/etc/SuSE-release").c_str() );
816 for( iostr::EachLine in( suseRelease ); in; in.next() )
818 std::string line( str::trim( *in ) );
819 if ( ! line.empty() )
823 return _("Unknown Distribution");
826 void TargetImpl::installSrcPackage( const SrcPackage_constPtr & srcPackage_r )
828 // provide on local disk
829 repo::RepoMediaAccess access_r;
830 repo::SrcPackageProvider prov( access_r );
831 ManagedFile localfile = prov.provideSrcPackage( srcPackage_r );
833 rpm().installPackage ( localfile );
836 /////////////////////////////////////////////////////////////////
837 } // namespace target
838 ///////////////////////////////////////////////////////////////////
839 /////////////////////////////////////////////////////////////////
841 ///////////////////////////////////////////////////////////////////