added preliminary commit function for target
[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 "zypp/base/Logger.h"
14
15 #include "zypp/target/TargetImpl.h"
16
17 #include <vector>
18
19 using namespace std;
20
21 ///////////////////////////////////////////////////////////////////
22 namespace zypp
23 { /////////////////////////////////////////////////////////////////
24   ///////////////////////////////////////////////////////////////////
25   namespace target
26   { /////////////////////////////////////////////////////////////////
27
28     IMPL_PTR_TYPE(TargetImpl);
29
30     TargetImpl_Ptr TargetImpl::_nullimpl;
31
32     /** Null implementation */
33     TargetImpl_Ptr TargetImpl::nullimpl()
34     {
35       if (_nullimpl == 0)
36         _nullimpl = new TargetImpl;
37       return _nullimpl;
38     }
39
40
41     ///////////////////////////////////////////////////////////////////
42     //
43     //  METHOD NAME : TargetImpl::TargetImpl
44     //  METHOD TYPE : Ctor
45     //
46     TargetImpl::TargetImpl(const Pathname & root_r)
47     : _root(root_r)
48     {
49       _rpm.initDatabase();
50       MIL << "Initialized target on " << _root << endl;
51     }
52
53     ///////////////////////////////////////////////////////////////////
54     //
55     //  METHOD NAME : TargetImpl::~TargetImpl
56     //  METHOD TYPE : Dtor
57     //
58     TargetImpl::~TargetImpl()
59     {
60       _rpm.closeDatabase();
61       MIL << "Targets closed" << endl;
62     }
63
64     const ResStore & TargetImpl::resolvables()
65     {
66       _store.clear();
67       // RPM objects
68       std::list<Package::Ptr> packages = _rpm.getPackages();
69       for (std::list<Package::Ptr>::const_iterator it = packages.begin();
70            it != packages.end();
71            it++)
72       {
73         _store.insert(*it);
74       }
75       // TODO objects from the XML store
76       return _store;
77     }
78
79     void TargetImpl::commit(ResPool & pool_r)
80     {
81 #warning FIXME this orderding doesn't honor the dependencies
82       vector<ResObject::constPtr> to_uninstall;
83       vector<ResObject::constPtr> to_install;
84       for (ResPool::const_iterator it = pool_r.begin();
85            it != pool_r.end(); it++)
86       {
87         if (it->status().isToBeInstalled())
88         {
89           to_install.push_back(it->resolvable());
90         }
91         else if (it->status().isToBeUninstalled())
92         {
93           to_uninstall.push_back(it->resolvable());
94         }
95       }
96       // first uninstall what is to be uninstalled
97       for (vector<ResObject::constPtr>::const_iterator it = to_uninstall.begin();
98            it != to_uninstall.end();
99            it++)
100       {
101         if (isKind<Package>(*it))
102         {
103           Package::constPtr p = dynamic_pointer_cast<const Package>(*it);
104           rpm().removePackage(p);
105         }
106 #warning FIXME other resolvables (once more below)
107       }
108       // now install what is to be installed
109       for (vector<ResObject::constPtr>::const_iterator it = to_install.begin();
110            it != to_install.end();
111            it++)
112       {
113         if (isKind<Package>(*it))
114         {
115           Package::constPtr p = dynamic_pointer_cast<const Package>(*it);
116           rpm().installPackage(p->getPlainRpm(), rpm::RpmDb::RPMINST_NOUPGRADE);
117         }
118       }
119     }
120
121     rpm::RpmDb & TargetImpl::rpm()
122     { return _rpm; }
123
124     /////////////////////////////////////////////////////////////////
125   } // namespace target
126   ///////////////////////////////////////////////////////////////////
127   /////////////////////////////////////////////////////////////////
128 } // namespace zypp
129 ///////////////////////////////////////////////////////////////////