e2b1d6fa16b8ddd844d76cf39795abbcb58d448e
[platform/upstream/libzypp.git] / zypp / target / TargetImpl.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/target/TargetImpl.cc
10  *
11 */
12 #include <iostream>
13 #include <fstream>
14 #include <sstream>
15 #include <string>
16 #include <list>
17 #include <set>
18
19 #include <sys/types.h>
20 #include <dirent.h>
21
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"
28
29 #include "zypp/ZConfig.h"
30
31 #include "zypp/PoolItem.h"
32 #include "zypp/ResObjects.h"
33 #include "zypp/Url.h"
34 #include "zypp/TmpPath.h"
35 #include "zypp/RepoStatus.h"
36 #include "zypp/ExternalProgram.h"
37 #include "zypp/Repository.h"
38
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"
45
46 #include "zypp/pool/GetResolvablesToInsDel.h"
47 #include "zypp/solver/detail/Helper.h"
48
49 #include "zypp/repo/DeltaCandidates.h"
50 #include "zypp/repo/PackageProvider.h"
51 #include "zypp/repo/ScriptProvider.h"
52 #include "zypp/repo/SrcPackageProvider.h"
53
54 #include "zypp/sat/Pool.h"
55
56 using std::endl;
57
58 ///////////////////////////////////////////////////////////////////
59 namespace zypp
60 { /////////////////////////////////////////////////////////////////
61   ///////////////////////////////////////////////////////////////////
62   namespace target
63   { /////////////////////////////////////////////////////////////////
64
65     ///////////////////////////////////////////////////////////////////
66     namespace
67     { /////////////////////////////////////////////////////////////////
68
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>).
74        *
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.
78        */
79       std::pair<bool,PatchScriptReport::Action> doExecuteScript( const Pathname & root_r,
80                                                                  const Pathname & script_r,
81                                                                  callback::SendReport<PatchScriptReport> & report_r )
82       {
83         MIL << "Execute script " << PathInfo(script_r) << endl;
84
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*/ );
88
89         for ( std::string output = prog.receiveLine(); output.length(); output = prog.receiveLine() )
90         {
91           progresslog() << output;
92           if ( ! report_r->progress( PatchScriptReport::OUTPUT, output ) )
93           {
94             WAR << "User request to abort script " << script_r << endl;
95             prog.kill();
96             // the rest is handled by exit code evaluation
97             // in case the script has meanwhile finished.
98           }
99         }
100
101         std::pair<bool,PatchScriptReport::Action> ret( std::make_pair( false, PatchScriptReport::ABORT ) );
102
103         if ( prog.close() != 0 )
104         {
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;
108           return ret;
109         }
110
111         report_r->finish();
112         ret.first = true;
113         return ret;
114       }
115
116       /** Execute script and report against report_r.
117        * Return \c false if user requested \c ABORT.
118        */
119       bool executeScript( const Pathname & root_r,
120                           const Pathname & script_r,
121                           callback::SendReport<PatchScriptReport> & report_r )
122       {
123         std::pair<bool,PatchScriptReport::Action> action( std::make_pair( false, PatchScriptReport::ABORT ) );
124
125         do {
126           action = doExecuteScript( root_r, script_r, report_r );
127           if ( action.first )
128             return true; // success
129
130           switch ( action.second )
131           {
132             case PatchScriptReport::ABORT:
133               WAR << "User request to abort at script " << script_r << endl;
134               return false; // requested abort.
135               break;
136
137             case PatchScriptReport::IGNORE:
138               WAR << "User request to skip script " << script_r << endl;
139               return true; // requested skip.
140               break;
141
142             case PatchScriptReport::RETRY:
143               break; // again
144           }
145         } while ( action.second == PatchScriptReport::RETRY );
146
147         // THIS is not intended to be reached:
148         INT << "Abort on unknown ACTION request " << action.second << " returned" << endl;
149         return false; // abort.
150       }
151
152       /** Look for patch scripts named 'name-version-release-*' and
153        *  execute them. Return \c false if \c ABORT was requested.
154        */
155       bool RunUpdateScripts( const Pathname & root_r,
156                              const Pathname & scriptsPath_r,
157                              const std::vector<sat::Solvable> & checkPackages_r,
158                              bool aborting_r )
159       {
160         if ( checkPackages_r.empty() )
161           return true; // no installed packages to check
162
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
167
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
172
173         // Now collect and execute all matching scripts.
174         // On ABORT: at least log all outstanding scripts.
175         bool abort = false;
176         for_( it, checkPackages_r.begin(), checkPackages_r.end() )
177         {
178           std::string prefix( str::form( "%s-%s-", it->name().c_str(), it->edition().c_str() ) );
179           for_( sit, scripts.begin(), scripts.end() )
180           {
181             if ( ! str::hasPrefix( *sit, prefix ) )
182               continue;
183
184             PathInfo script( scriptsDir / *sit );
185             if ( ! script.isFile() )
186               continue;
187
188             if ( abort || aborting_r )
189             {
190               WAR << "Aborting: Skip patch script " << *sit << endl;
191               CommitLog()(/*timestamp*/true) << script.path() << _(" execution skipped while aborting") << endl;
192             }
193             else
194             {
195               MIL << "Found patch script " << *sit << endl;
196               callback::SendReport<PatchScriptReport> report;
197               report->start( make<Package>( *it ), script.path() );
198
199               if ( ! executeScript( root_r, script.path(), report ) )
200                 abort = true; // requested abort.
201             }
202           }
203         }
204         return !abort;
205       }
206
207       /////////////////////////////////////////////////////////////////
208     } // namespace
209     ///////////////////////////////////////////////////////////////////
210
211     /** Helper for PackageProvider queries during commit. */
212     struct QueryInstalledEditionHelper
213     {
214       bool operator()( const std::string & name_r,
215                        const Edition &     ed_r,
216                        const Arch &        arch_r ) const
217       {
218         rpm::librpmDb::db_const_iterator it;
219         for ( it.findByName( name_r ); *it; ++it )
220           {
221             if ( arch_r == it->tag_arch()
222                  && ( ed_r == Edition::noedition || ed_r == it->tag_edition() ) )
223               {
224                 return true;
225               }
226           }
227         return false;
228       }
229     };
230
231     /**
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
235     */
236     struct RepoProvidePackage
237     {
238       ResPool _pool;
239       repo::RepoMediaAccess &_access;
240
241       RepoProvidePackage( repo::RepoMediaAccess &access, ResPool pool_r )
242         : _pool(pool_r), _access(access)
243       {
244
245       }
246
247       ManagedFile operator()( const PoolItem & pi )
248       {
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() );
253
254         Package::constPtr p = asKind<Package>(pi.resolvable());
255
256
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();
263       }
264     };
265     ///////////////////////////////////////////////////////////////////
266
267     IMPL_PTR_TYPE(TargetImpl);
268
269     TargetImpl_Ptr TargetImpl::_nullimpl;
270
271     /** Null implementation */
272     TargetImpl_Ptr TargetImpl::nullimpl()
273     {
274       if (_nullimpl == 0)
275         _nullimpl = new TargetImpl;
276       return _nullimpl;
277     }
278
279     ///////////////////////////////////////////////////////////////////
280     //
281     //  METHOD NAME : TargetImpl::TargetImpl
282     //  METHOD TYPE : Ctor
283     //
284     TargetImpl::TargetImpl( const Pathname & root_r, bool doRebuild_r )
285     : _root( root_r )
286     , _requestedLocalesFile( home() / "RequestedLocales" )
287     , _softLocksFile( home() / "SoftLocks" )
288     , _hardLocksFile( Pathname::assertprefix( _root, ZConfig::instance().locksFile() ) )
289     {
290       _rpm.initDatabase( root_r, Pathname(), doRebuild_r );
291       MIL << "Initialized target on " << _root << endl;
292     }
293
294     ///////////////////////////////////////////////////////////////////
295     //
296     //  METHOD NAME : TargetImpl::~TargetImpl
297     //  METHOD TYPE : Dtor
298     //
299     TargetImpl::~TargetImpl()
300     {
301       _rpm.closeDatabase();
302       MIL << "Targets closed" << endl;
303     }
304
305     void TargetImpl::clearCache()
306     {
307       Pathname base = Pathname::assertprefix( _root,
308                                               ZConfig::instance().repoSolvfilesPath() / sat::Pool::instance().systemRepoAlias() );
309       filesystem::recursive_rmdir( base );
310     }
311
312     void TargetImpl::buildCache()
313     {
314       Pathname base = Pathname::assertprefix( _root,
315                                               ZConfig::instance().repoSolvfilesPath() / sat::Pool::instance().systemRepoAlias() );
316       Pathname rpmsolv       = base/"solv";
317       Pathname rpmsolvcookie = base/"cookie";
318
319       bool build_rpm_solv = true;
320       // lets see if the rpm solv cache exists
321
322       RepoStatus rpmstatus(_root + "/var/lib/rpm/Name");
323       bool solvexisted = PathInfo(rpmsolv).isExist();
324       if ( solvexisted )
325       {
326         // see the status of the cache
327         PathInfo cookie( rpmsolvcookie );
328         MIL << "Read cookie: " << cookie << endl;
329         if ( cookie.isExist() )
330         {
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;
337         }
338       }
339
340       if ( build_rpm_solv )
341       {
342         // Take care we unlink the solvfile on exception
343         ManagedFile guard( base, filesystem::recursive_rmdir );
344
345         // if it does not exist yet, we better create it
346         filesystem::assert_dir( base );
347
348         filesystem::TmpFile tmpsolv( filesystem::TmpFile::makeSibling( rpmsolv ) );
349         if (!tmpsolv)
350         {
351           Exception ex("Failed to cache rpm database.");
352           ex.remember(str::form(
353               "Cannot create temporary file under %s.", base.c_str()));
354           ZYPP_THROW(ex);
355         }
356
357         std::ostringstream cmd;
358         cmd << "rpmdb2solv";
359         if ( ! _root.empty() )
360           cmd << " -r '" << _root << "'";
361
362         if ( solvexisted )
363           cmd << " '" << rpmsolv << "'";
364
365         cmd << "  > '" << tmpsolv.path() << "'";
366
367         MIL << "Executing: " << cmd << endl;
368         ExternalProgram prog( cmd.str(), ExternalProgram::Stderr_To_Stdout );
369
370         cmd << endl;
371         for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
372           WAR << "  " << output;
373           cmd << "     " << output;
374         }
375
376         int ret = prog.close();
377         if ( ret != 0 )
378         {
379           Exception ex(str::form("Failed to cache rpm database (%d).", ret));
380           ex.remember( cmd.str() );
381           ZYPP_THROW(ex);
382         }
383
384         ret = filesystem::rename( tmpsolv, rpmsolv );
385         if ( ret != 0 )
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 );
389
390         rpmstatus.saveToCookieFile(rpmsolvcookie);
391
392         // We keep it.
393         guard.resetDispose();
394       }
395     }
396
397     void TargetImpl::unload()
398     {
399       Repository system( sat::Pool::instance().findSystemRepo() );
400       if ( system )
401         system.eraseFromPool();
402     }
403
404
405     void TargetImpl::load()
406     {
407       buildCache();
408
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;
414
415       // Providing an empty system repo, unload any old content
416       Repository system( sat::Pool::instance().findSystemRepo() );
417       if ( system && ! system.solvablesEmpty() )
418       {
419         system.eraseFromPool(); // invalidates system
420       }
421       if ( ! system )
422       {
423         system = satpool.systemRepo();
424       }
425
426       try
427       {
428         system.addSolv( rpmsolv );
429       }
430       catch ( const Exception & exp )
431       {
432         ZYPP_CAUGHT( exp );
433         MIL << "Try to handle exception by rebuilding the solv-file" << endl;
434         clearCache();
435         buildCache();
436
437         system.addSolv( rpmsolv );
438       }
439
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.
445       {
446         const LocaleSet & requestedLocales( _requestedLocalesFile.locales() );
447         if ( ! requestedLocales.empty() )
448         {
449           satpool.setRequestedLocales( requestedLocales );
450         }
451       }
452       {
453         const SoftLocksFile::Data & softLocks( _softLocksFile.data() );
454         if ( ! softLocks.empty() )
455         {
456           ResPool::instance().setAutoSoftLocks( softLocks );
457         }
458       }
459       if ( ZConfig::instance().apply_locks_file() )
460       {
461         const HardLocksFile::Data & hardLocks( _hardLocksFile.data() );
462         if ( ! hardLocks.empty() )
463         {
464           ResPool::instance().setHardLockQueries( hardLocks );
465         }
466       }
467
468
469       MIL << "Target loaded: " << system.solvablesSize() << " resolvables" << endl;
470     }
471
472     ZYppCommitResult TargetImpl::commit( ResPool pool_r, const ZYppCommitPolicy & policy_rX )
473     {
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 )
479         policy_r.allMedia();
480       // ----------------------------------------------------------------- //
481
482       MIL << "TargetImpl::commit(<pool>, " << policy_r << ")" << endl;
483
484       ///////////////////////////////////////////////////////////////////
485       // Store non-package data:
486       ///////////////////////////////////////////////////////////////////
487       filesystem::assert_dir( home() );
488       // requested locales
489       _requestedLocalesFile.setLocales( pool_r.getRequestedLocales() );
490       // weak locks
491       {
492         SoftLocksFile::Data newdata;
493         pool_r.getActiveSoftLocks( newdata );
494         _softLocksFile.setData( newdata );
495       }
496       // hard locks
497       if ( ZConfig::instance().apply_locks_file() )
498       {
499         HardLocksFile::Data newdata;
500         pool_r.getHardLockQueries( newdata );
501         _hardLocksFile.setData( newdata );
502       }
503
504       ///////////////////////////////////////////////////////////////////
505       // Process packages:
506       ///////////////////////////////////////////////////////////////////
507       if ( root() == "/" && CommitLog::fname().empty() )
508       {
509         // Yes, we simply hijack /var/log/YaST2/y2logRPM
510         // until we maintain some zypp history database.
511         CommitLog::setFname( "/var/log/YaST2/y2logRPM" );
512       }
513
514       ZYppCommitResult result;
515
516       TargetImpl::PoolItemList to_uninstall;
517       TargetImpl::PoolItemList to_install;
518       TargetImpl::PoolItemList to_srcinstall;
519       {
520
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 );
528       }
529
530       if ( policy_r.restrictToMedia() )
531       {
532         MIL << "Restrict to media number " << policy_r.restrictToMedia() << endl;
533       }
534
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() )
540       {
541         if ( ! isKind<Patch>(it->resolvable()) )
542           continue;
543         if ( ! it->status().isToBeInstalled() )
544           continue;
545
546         Patch::constPtr patch( asKind<Patch>(it->resolvable()) );
547         if ( ! patch->message().empty() )
548         {
549           MIL << "Show message for " << patch << endl;
550           callback::SendReport<target::PatchMessageReport> report;
551           if ( ! report->show( patch ) )
552           {
553             WAR << "commit aborted by the user" << endl;
554             ZYPP_THROW( TargetAbortedException( N_("Installation has been aborted as directed.") ) );
555           }
556         }
557       }
558
559       ///////////////////////////////////////////////////////////////////
560       // Remove/install packages.
561       ///////////////////////////////////////////////////////////////////
562      commit (to_uninstall, policy_r, pool_r );
563
564       if (policy_r.restrictToMedia() == 0)
565       {                 // commit all
566         result._remaining = commit( to_install, policy_r, pool_r );
567         result._srcremaining = commit( to_srcinstall, policy_r, pool_r );
568       }
569       else
570       {
571         TargetImpl::PoolItemList current_install;
572         TargetImpl::PoolItemList current_srcinstall;
573
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)
578         {
579           ResObject::constPtr res( it->resolvable() );
580
581           if ( hitUnwantedMedia
582                || ( res->mediaNr() && res->mediaNr() != policy_r.restrictToMedia() ) )
583           {
584             hitUnwantedMedia = true;
585             result._remaining.push_back( *it );
586           }
587           else
588           {
589             current_install.push_back( *it );
590           }
591         }
592
593         TargetImpl::PoolItemList bad = commit( current_install, policy_r, pool_r );
594         result._remaining.insert(result._remaining.end(), bad.begin(), bad.end());
595
596         for (TargetImpl::PoolItemList::iterator it = to_srcinstall.begin(); it != to_srcinstall.end(); ++it)
597         {
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
601           {
602             XXX << "Package " << *pkg << ", wrong media " << pkg->mediaNr() << endl;
603             result._srcremaining.push_back( *it );
604           }
605           else
606           {
607             current_srcinstall.push_back( *it );
608           }
609         }
610         bad = commit( current_srcinstall, policy_r, pool_r );
611         result._srcremaining.insert(result._srcremaining.end(), bad.begin(), bad.end());
612       }
613
614       ///////////////////////////////////////////////////////////////////
615       // Try to rebuild solv file while rpm database is still in cache.
616       ///////////////////////////////////////////////////////////////////
617       buildCache();
618
619       result._result = (to_install.size() - result._remaining.size());
620       MIL << "TargetImpl::commit(<pool>, " << policy_r << ") returns: " << result << endl;
621       return result;
622     }
623
624
625     TargetImpl::PoolItemList
626     TargetImpl::commit( const TargetImpl::PoolItemList & items_r,
627                         const ZYppCommitPolicy & policy_r,
628                         const ResPool & pool_r )
629     {
630       TargetImpl::PoolItemList remaining;
631       repo::RepoMediaAccess access;
632       MIL << "TargetImpl::commit(<list>" << policy_r << ")" << items_r.size() << endl;
633
634       bool abort = false;
635       std::vector<sat::Solvable> successfullyInstalledPackages;
636
637       RepoProvidePackage repoProvidePackage( access, pool_r);
638       // prepare the package cache.
639       CommitPackageCache packageCache( items_r.begin(), items_r.end(),
640                                        root() / "tmp", repoProvidePackage );
641
642       for (TargetImpl::PoolItemList::const_iterator it = items_r.begin(); it != items_r.end(); it++)
643       {
644         if (isKind<Package>(it->resolvable()))
645         {
646           Package::constPtr p = asKind<Package>(it->resolvable());
647           if (it->status().isToBeInstalled())
648           {
649             ManagedFile localfile;
650             try
651             {
652               localfile = packageCache.get( it );
653             }
654             catch ( const SkipRequestException &e )
655             {
656               ZYPP_CAUGHT( e );
657               WAR << "Skipping package " << p << " in commit" << endl;
658               continue;
659             }
660 #if 0
661             // bnc #395704: missing catch causes abort. see if packageCache fails to handle
662             // errors correctly.
663             catch ( const Exception &e )
664             {
665               ZYPP_CAUGHT( e );
666               SEC << e << endl;
667             }
668 #endif
669
670 #warning Exception handling
671             // create a installation progress report proxy
672             RpmInstallPackageReceiver progress( it->resolvable() );
673             progress.connect();
674             bool success = true;
675             unsigned flags = 0;
676             // Why force and nodeps?
677             //
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;
685             //
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;
689
690             try
691             {
692               progress.tryLevel( target::rpm::InstallResolvableReport::RPM_NODEPS_FORCE );
693               rpm().installPackage( localfile, flags );
694
695               if ( progress.aborted() )
696               {
697                 WAR << "commit aborted by the user" << endl;
698                 progress.disconnect();
699                 success = false;
700                 abort = true;
701                 break;
702               }
703             }
704             catch (Exception & excpt_r)
705             {
706               ZYPP_CAUGHT(excpt_r);
707               if ( policy_r.dryRun() )
708               {
709                 WAR << "dry run failed" << endl;
710                 progress.disconnect();
711                 break;
712               }
713               // else
714               WAR << "Install failed" << endl;
715               remaining.push_back( *it );
716               progress.disconnect();
717               success = false;
718               break;
719             }
720
721             if ( success && !policy_r.dryRun() )
722             {
723               it->status().resetTransact( ResStatus::USER );
724               // Remember to check this package for presence of patch scripts.
725               successfullyInstalledPackages.push_back( it->satSolvable() );
726             }
727             progress.disconnect();
728           }
729           else
730           {
731             bool success = true;
732
733             RpmRemovePackageReceiver progress( it->resolvable() );
734             progress.connect();
735             unsigned flags = rpm::RpmDb::RPMINST_NODEPS;
736             if (policy_r.dryRun()) flags |= rpm::RpmDb::RPMINST_TEST;
737             try
738             {
739               rpm().removePackage( p, flags );
740
741               if ( progress.aborted() )
742               {
743                 WAR << "commit aborted by the user" << endl;
744                 progress.disconnect();
745                 success = false;
746                 abort = true;
747                 break;
748               }
749             }
750             catch (Exception & excpt_r)
751             {
752               WAR << "removal of " << p << " failed";
753               success = false;
754               ZYPP_CAUGHT( excpt_r );
755             }
756             if (success
757                 && !policy_r.dryRun())
758             {
759               it->status().resetTransact( ResStatus::USER );
760             }
761             progress.disconnect();
762           }
763         }
764         else if (!policy_r.dryRun()) // other resolvables (non-Package)
765         {
766           it->status().resetTransact( ResStatus::USER );
767         }  // other resolvables
768
769       } // for
770
771       // Check presence of patch scripts. If aborting, at least log
772       // omitted scripts.
773       if ( ! successfullyInstalledPackages.empty() )
774       {
775         if ( ! RunUpdateScripts( _root, ZConfig::instance().update_scriptsPath(),
776                                  successfullyInstalledPackages, abort ) )
777         {
778           WAR << "Commit aborted by the user" << endl;
779           abort = true;
780         }
781       }
782
783       if ( abort )
784       {
785         ZYPP_THROW( TargetAbortedException( N_("Installation has been aborted as directed.") ) );
786       }
787
788      return remaining;
789     }
790
791     rpm::RpmDb & TargetImpl::rpm()
792     {
793       return _rpm;
794     }
795
796     bool TargetImpl::providesFile (const std::string & path_str, const std::string & name_str) const
797     {
798       return _rpm.hasFile(path_str, name_str);
799     }
800
801     /** Set the log file for target */
802     bool TargetImpl::setInstallationLogfile(const Pathname & path_r)
803     {
804       CommitLog::setFname(path_r);
805       return true;
806     }
807
808     Date TargetImpl::timestamp() const
809     {
810       return _rpm.timestamp();
811     }
812
813     std::string TargetImpl::release() const
814     {
815       std::ifstream suseRelease( (_root / "/etc/SuSE-release").c_str() );
816       for( iostr::EachLine in( suseRelease ); in; in.next() )
817       {
818         std::string line( str::trim( *in ) );
819         if ( ! line.empty() )
820           return line;
821       }
822
823       return _("Unknown Distribution");
824     }
825
826     void TargetImpl::installSrcPackage( const SrcPackage_constPtr & srcPackage_r )
827     {
828       // provide on local disk
829       repo::RepoMediaAccess access_r;
830       repo::SrcPackageProvider prov( access_r );
831       ManagedFile localfile = prov.provideSrcPackage( srcPackage_r );
832       // install it
833       rpm().installPackage ( localfile );
834     }
835
836     /////////////////////////////////////////////////////////////////
837   } // namespace target
838   ///////////////////////////////////////////////////////////////////
839   /////////////////////////////////////////////////////////////////
840 } // namespace zypp
841 ///////////////////////////////////////////////////////////////////