fix target not cachedsometimes
[platform/upstream/libzypp.git] / zypp / zypp_detail / ZYppImpl.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/zypp_detail/ZYppImpl.cc
10  *
11 */
12
13 #include <iostream>
14 #include "zypp/TmpPath.h"
15 #include "zypp/base/Logger.h"
16 #include "zypp/base/String.h"
17
18 #include "zypp/zypp_detail/ZYppImpl.h"
19 #include "zypp/solver/detail/Helper.h"
20 #include "zypp/target/TargetImpl.h"
21 #include "zypp/ZYpp.h"
22 #include "zypp/NVRAD.h"
23 #include "zypp/DiskUsageCounter.h"
24 #include "zypp/NameKindProxy.h"
25 #include "zypp/Locks.h"
26
27 using std::endl;
28
29 ///////////////////////////////////////////////////////////////////
30 namespace zypp
31 { /////////////////////////////////////////////////////////////////
32   ///////////////////////////////////////////////////////////////////
33   namespace zypp_detail
34   { /////////////////////////////////////////////////////////////////
35
36     ///////////////////////////////////////////////////////////////////
37     //
38     //  METHOD NAME : ZYppImpl::ZYppImpl
39     //  METHOD TYPE : Constructor
40     //
41     ZYppImpl::ZYppImpl()
42     : _target(0)
43     , _resolver( new Resolver( ResPool::instance()) )
44     {
45       MIL << "Initializing keyring..." << std::endl;
46       //_keyring = new KeyRing(homePath() + Pathname("/keyring/all"), homePath() + Pathname("/keyring/trusted"));
47       _keyring = new KeyRing(tmpPath());
48     }
49
50     ///////////////////////////////////////////////////////////////////
51     //
52     //  METHOD NAME : ZYppImpl::~ZYppImpl
53     //  METHOD TYPE : Destructor
54     //
55     ZYppImpl::~ZYppImpl()
56     {}
57
58     //------------------------------------------------------------------------
59     // add/remove resolvables
60
61     DiskUsageCounter::MountPointSet ZYppImpl::diskUsage()
62     {
63       if ( ! _disk_usage )
64       {
65         setPartitions( DiskUsageCounter::detectMountPoints() );
66       }
67       return _disk_usage->disk_usage(pool());
68     }
69
70     void ZYppImpl::setPartitions(const DiskUsageCounter::MountPointSet &mp)
71     {
72       _disk_usage.reset(new DiskUsageCounter());
73       _disk_usage->setMountPoints(mp);
74     }
75
76     DiskUsageCounter::MountPointSet ZYppImpl::getPartitions() const
77     {
78       if (_disk_usage)
79         return _disk_usage->getMountPoints();
80       else
81         return DiskUsageCounter::detectMountPoints();
82     }
83
84     //------------------------------------------------------------------------
85     // target
86
87     Target_Ptr ZYppImpl::target() const
88     {
89       if (! _target)
90         ZYPP_THROW(Exception("Target not initialized."));
91       return _target;
92      }
93
94     void ZYppImpl::initializeTarget(const Pathname & root)
95     {
96       MIL << "initTarget( " << root << endl;
97       if (_target) {
98         if (_target->root() == root) {
99             MIL << "Repeated call to initializeTarget()" << endl;
100             return;
101         }
102 #warning NEED SOME NEW WAY TO INDICATE NEDD OF TARGET RELOAD
103 #if 0
104         removeInstalledResolvables( );
105 #endif
106       }
107       _target = new Target( root );
108       _target->buildCache();
109     }
110
111     void ZYppImpl::finishTarget()
112     {
113 #warning NEED SOME NEW WAY TO UNLOAD THE POOL
114 #if 0
115       if (_target)
116         removeInstalledResolvables();
117 #endif
118       _target = 0;
119     }
120
121     //------------------------------------------------------------------------
122     // commit
123
124     /** \todo Remove workflow from target, lot's of it could be done here,
125      * and target used for transact. */
126     ZYppCommitResult ZYppImpl::commit( const ZYppCommitPolicy & policy_r )
127     {
128       if ( getenv("ZYPP_TESTSUITE_FAKE_ARCH") )
129       {
130         ZYPP_THROW( Exception("ZYPP_TESTSUITE_FAKE_ARCH set. Commit not allowed and disabled.") );
131       }
132
133       MIL << "Attempt to commit (" << policy_r << ")" << endl;
134       if (! _target)
135         ZYPP_THROW( Exception("Target not initialized.") );
136
137       ZYppCommitResult res = _target->_pimpl->commit( pool(), policy_r );
138
139       if (! policy_r.dryRun() ) {
140         // Tag target data invalid, so they are reloaded on the next call to
141         // target->resolvables(). Actually the target should do this without
142         // foreign help.
143         _target->reset();
144 #warning NEED SOME NEW WAY TO INDICATE NEDD OF TARGET RELOAD
145 #if 0
146         removeInstalledResolvables();
147         if ( policy_r.syncPoolAfterCommit() )
148           {
149             // reload new status from target
150             addResolvables( _target->resolvables(), true );
151           }
152 #endif
153       }
154
155       MIL << "Commit (" << policy_r << ") returned: "
156           << res << endl;
157       return res;
158     }
159
160     void ZYppImpl::installSrcPackage( const SrcPackage_constPtr & srcPackage_r )
161     {
162       if (! _target)
163         ZYPP_THROW( Exception("Target not initialized.") );
164       _target->_pimpl->installSrcPackage( srcPackage_r );
165     }
166
167     //------------------------------------------------------------------------
168     // target store path
169
170     Pathname ZYppImpl::homePath() const
171     { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
172
173     void ZYppImpl::setHomePath( const Pathname & path )
174     { _home_path = path; }
175
176     Pathname ZYppImpl::tmpPath() const
177     {
178       static TmpDir zypp_tmp_dir( TmpPath::defaultLocation(), "zypp." );
179       return zypp_tmp_dir.path();
180     }
181
182     int ZYppImpl::applyLocks()
183     {
184 #warning make the /etc/zypp/locks path an option zconfig.
185       Pathname locksrcPath( "/etc/zypp/locks" );
186       try
187       {
188         Target_Ptr trg( target() );
189         if ( trg )
190           locksrcPath = trg->root() / locksrcPath;
191       }
192       catch ( ... )
193       {
194         // noop: Someone decided to let target() throw if the ptr is NULL ;(
195       }
196
197       int num=0;
198       PathInfo locksrc( locksrcPath );
199       if ( locksrc.isFile() )
200       {
201         MIL << "Reading locks from '" << locksrcPath << "'" << endl;
202         num = zypp::locks::readLocks( pool(), locksrcPath );
203         MIL << num << " items locked." << endl;
204       }
205       else
206       {
207         MIL << "No file '" << locksrcPath << "' to read locks from" << endl;
208       }
209       return num;
210     }
211     /******************************************************************
212      **
213      ** FUNCTION NAME : operator<<
214      ** FUNCTION TYPE : std::ostream &
215     */
216     std::ostream & operator<<( std::ostream & str, const ZYppImpl & obj )
217     {
218       return str << "ZYppImpl";
219     }
220
221     /////////////////////////////////////////////////////////////////
222   } // namespace zypp_detail
223   ///////////////////////////////////////////////////////////////////
224   /////////////////////////////////////////////////////////////////
225 } // namespace zypp
226 ///////////////////////////////////////////////////////////////////