]> git.saurik.com Git - apt.git/blame - apt-pkg/statechanges.cc
allow methods to be disabled and redirected via config
[apt.git] / apt-pkg / statechanges.cc
CommitLineData
b49068c5
DK
1#include <apt-pkg/pkgcache.h>
2#include <apt-pkg/cacheset.h>
3#include <apt-pkg/debsystem.h>
4#include <apt-pkg/fileutl.h>
5#include <apt-pkg/statechanges.h>
27f38567 6#include <apt-pkg/prettyprinters.h>
b49068c5
DK
7
8#include <algorithm>
9#include <memory>
10
11namespace APT
12{
13
14class StateChanges::Private
15{
16public:
17 APT::VersionVector hold;
64e3414e 18 APT::VersionVector unhold;
b49068c5 19 APT::VersionVector install;
64e3414e
DK
20 APT::VersionVector deinstall;
21 APT::VersionVector purge;
b49068c5
DK
22 APT::VersionVector error;
23};
24
64e3414e
DK
25#define APT_GETTERSETTER(Name, Container) \
26void StateChanges::Name(pkgCache::VerIterator const &Ver) \
27{ \
28 Container.push_back(Ver); \
29}\
30APT::VersionVector& StateChanges::Name() \
31{ \
32 return Container; \
b49068c5 33}
64e3414e
DK
34APT_GETTERSETTER(Hold, d->hold)
35APT_GETTERSETTER(Unhold, d->unhold)
36APT_GETTERSETTER(Install, d->install)
37APT_GETTERSETTER(Remove, d->deinstall)
38APT_GETTERSETTER(Purge, d->purge)
39#undef APT_GETTERSETTER
b49068c5
DK
40APT::VersionVector& StateChanges::Error()
41{
42 return d->error;
43}
44
64e3414e 45void StateChanges::clear()
b49068c5
DK
46{
47 d->hold.clear();
64e3414e 48 d->unhold.clear();
b49068c5 49 d->install.clear();
64e3414e
DK
50 d->deinstall.clear();
51 d->purge.clear();
b49068c5
DK
52 d->error.clear();
53}
54
64e3414e
DK
55bool StateChanges::empty() const
56{
57 return d->hold.empty() &&
58 d->unhold.empty() &&
59 d->install.empty() &&
60 d->deinstall.empty() &&
61 d->purge.empty() &&
62 d->error.empty();
63}
64
b49068c5
DK
65bool StateChanges::Save(bool const DiscardOutput)
66{
27f38567 67 bool const Debug = _config->FindB("Debug::pkgDpkgPm", false);
b49068c5 68 d->error.clear();
64e3414e 69 if (d->hold.empty() && d->unhold.empty() && d->install.empty() && d->deinstall.empty() && d->purge.empty())
b49068c5
DK
70 return true;
71
72 std::vector<std::string> Args = debSystem::GetDpkgBaseCommand();
73 // ensure dpkg knows about the package so that it keeps the status we set
64e3414e 74 if (d->hold.empty() == false || d->install.empty() == false)
b49068c5
DK
75 {
76 APT::VersionVector makeDpkgAvailable;
77 auto const notInstalled = [](pkgCache::VerIterator const &V) { return V.ParentPkg()->CurrentVer == 0; };
78 std::copy_if(d->hold.begin(), d->hold.end(), std::back_inserter(makeDpkgAvailable), notInstalled);
79 std::copy_if(d->install.begin(), d->install.end(), std::back_inserter(makeDpkgAvailable), notInstalled);
80
81 if (makeDpkgAvailable.empty() == false)
82 {
83 auto const BaseArgs = Args.size();
84 Args.push_back("--merge-avail");
85 // FIXME: supported only since 1.17.7 in dpkg
86 Args.push_back("-");
87 int dummyAvail = -1;
27f38567
DK
88 if (Debug)
89 {
90 for (auto const &V: makeDpkgAvailable)
91 {
92 std::clog << "echo 'Dummy record for " << V.ParentPkg().FullName(false) << "' | ";
93 std::copy(Args.begin(), Args.end(), std::ostream_iterator<std::string>(std::clog, " "));
94 std::clog << std::endl;
95 }
96 }
97 else
98 {
99 pid_t const dpkgMergeAvail = debSystem::ExecDpkg(Args, &dummyAvail, nullptr, true);
100
101 FILE* dpkg = fdopen(dummyAvail, "w");
102 for (auto const &V: makeDpkgAvailable)
103 fprintf(dpkg, "Package: %s\nVersion: 0~\nArchitecture: %s\nMaintainer: Dummy Example <dummy@example.org>\n"
104 "Description: dummy package record\n A record is needed to put a package on hold, so here it is.\n\n", V.ParentPkg().Name(), V.Arch());
105 fclose(dpkg);
106
107 ExecWait(dpkgMergeAvail, "dpkg --merge-avail", true);
108 }
b49068c5
DK
109 Args.erase(Args.begin() + BaseArgs, Args.end());
110 }
111 }
112 bool const dpkgMultiArch = _system->MultiArchSupported();
113
114 Args.push_back("--set-selections");
27f38567 115 if (Debug)
64e3414e 116 {
27f38567
DK
117 std::string state;
118 auto const dpkgName = [&](pkgCache::VerIterator const &V) {
119 pkgCache::PkgIterator P = V.ParentPkg();
120 if (strcmp(V.Arch(), "none") == 0)
121 ioprintf(std::clog, "echo '%s %s' | ", P.Name(), state.c_str());
122 else if (dpkgMultiArch == false)
123 ioprintf(std::clog, "echo '%s %s' | ", P.FullName(true).c_str(), state.c_str());
124 else
125 ioprintf(std::clog, "echo '%s:%s %s' | ", P.Name(), V.Arch(), state.c_str());
126 std::copy(Args.begin(), Args.end(), std::ostream_iterator<std::string>(std::clog, " "));
127 std::clog << std::endl;
128 };
129 for (auto const &V: d->unhold)
130 {
131 if (V.ParentPkg()->CurrentVer != 0)
132 state = "install";
133 else
134 state = "deinstall";
135 dpkgName(V);
136 }
137 if (d->purge.empty() == false)
138 {
139 state = "purge";
140 std::for_each(d->purge.begin(), d->purge.end(), dpkgName);
141 }
142 if (d->deinstall.empty() == false)
143 {
64e3414e 144 state = "deinstall";
27f38567
DK
145 std::for_each(d->deinstall.begin(), d->deinstall.end(), dpkgName);
146 }
147 if (d->hold.empty() == false)
148 {
149 state = "hold";
150 std::for_each(d->hold.begin(), d->hold.end(), dpkgName);
151 }
152 if (d->install.empty() == false)
153 {
154 state = "install";
155 std::for_each(d->install.begin(), d->install.end(), dpkgName);
156 }
b49068c5 157 }
27f38567 158 else
b49068c5 159 {
27f38567
DK
160 int selections = -1;
161 pid_t const dpkgSelections = debSystem::ExecDpkg(Args, &selections, nullptr, DiscardOutput);
162
163 FILE* dpkg = fdopen(selections, "w");
164 std::string state;
165 auto const dpkgName = [&](pkgCache::VerIterator const &V) {
166 pkgCache::PkgIterator P = V.ParentPkg();
167 if (strcmp(V.Arch(), "none") == 0)
168 fprintf(dpkg, "%s %s\n", P.Name(), state.c_str());
169 else if (dpkgMultiArch == false)
170 fprintf(dpkg, "%s %s\n", P.FullName(true).c_str(), state.c_str());
171 else
172 fprintf(dpkg, "%s:%s %s\n", P.Name(), V.Arch(), state.c_str());
173 };
174 for (auto const &V: d->unhold)
175 {
176 if (V.ParentPkg()->CurrentVer != 0)
177 state = "install";
178 else
179 state = "deinstall";
180 dpkgName(V);
181 }
182 if (d->purge.empty() == false)
183 {
184 state = "purge";
185 std::for_each(d->purge.begin(), d->purge.end(), dpkgName);
186 }
187 if (d->deinstall.empty() == false)
188 {
189 state = "deinstall";
190 std::for_each(d->deinstall.begin(), d->deinstall.end(), dpkgName);
191 }
192 if (d->hold.empty() == false)
193 {
194 state = "hold";
195 std::for_each(d->hold.begin(), d->hold.end(), dpkgName);
196 }
197 if (d->install.empty() == false)
198 {
199 state = "install";
200 std::for_each(d->install.begin(), d->install.end(), dpkgName);
201 }
202 fclose(dpkg);
b49068c5 203
27f38567
DK
204 if (ExecWait(dpkgSelections, "dpkg --set-selections") == false)
205 {
206 std::move(d->purge.begin(), d->purge.end(), std::back_inserter(d->error));
207 d->purge.clear();
208 std::move(d->deinstall.begin(), d->deinstall.end(), std::back_inserter(d->error));
209 d->deinstall.clear();
210 std::move(d->hold.begin(), d->hold.end(), std::back_inserter(d->error));
211 d->hold.clear();
212 std::move(d->unhold.begin(), d->unhold.end(), std::back_inserter(d->error));
213 d->unhold.clear();
214 std::move(d->install.begin(), d->install.end(), std::back_inserter(d->error));
215 d->install.clear();
216 }
b49068c5
DK
217 }
218 return d->error.empty();
219}
220
221StateChanges::StateChanges() : d(new StateChanges::Private()) {}
222StateChanges::StateChanges(StateChanges&&) = default;
223StateChanges& StateChanges::operator=(StateChanges&&) = default;
224StateChanges::~StateChanges() = default;
225
226}