File: statechanges.cc

package info (click to toggle)
apt 1.8.2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 19,680 kB
  • sloc: cpp: 67,708; sh: 24,865; xml: 5,187; perl: 577; ansic: 190; makefile: 28; python: 28
file content (227 lines) | stat: -rw-r--r-- 6,930 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <apt-pkg/cacheset.h>
#include <apt-pkg/debsystem.h>
#include <apt-pkg/fileutl.h>
#include <apt-pkg/pkgcache.h>
#include <apt-pkg/prettyprinters.h>
#include <apt-pkg/statechanges.h>

#include <algorithm>
#include <memory>

namespace APT
{

class StateChanges::Private
{
public:
   APT::VersionVector hold;
   APT::VersionVector unhold;
   APT::VersionVector install;
   APT::VersionVector deinstall;
   APT::VersionVector purge;
   APT::VersionVector error;
};

#define APT_GETTERSETTER(Name, Container) \
void StateChanges::Name(pkgCache::VerIterator const &Ver) \
{ \
   if (Ver.end() == false) \
      Container.push_back(Ver); \
}\
APT::VersionVector& StateChanges::Name() \
{ \
   return Container; \
}
APT_GETTERSETTER(Hold, d->hold)
APT_GETTERSETTER(Unhold, d->unhold)
APT_GETTERSETTER(Install, d->install)
APT_GETTERSETTER(Remove, d->deinstall)
APT_GETTERSETTER(Purge, d->purge)
#undef APT_GETTERSETTER
APT::VersionVector& StateChanges::Error()
{
   return d->error;
}

void StateChanges::clear()
{
   d->hold.clear();
   d->unhold.clear();
   d->install.clear();
   d->deinstall.clear();
   d->purge.clear();
   d->error.clear();
}

bool StateChanges::empty() const
{
   return d->hold.empty() &&
      d->unhold.empty() &&
      d->install.empty() &&
      d->deinstall.empty() &&
      d->purge.empty() &&
      d->error.empty();
}

bool StateChanges::Save(bool const DiscardOutput)
{
   bool const Debug = _config->FindB("Debug::pkgDpkgPm", false);
   d->error.clear();
   if (d->hold.empty() && d->unhold.empty() && d->install.empty() && d->deinstall.empty() && d->purge.empty())
      return true;

   std::vector<std::string> Args = debSystem::GetDpkgBaseCommand();
   // ensure dpkg knows about the package so that it keeps the status we set
   if (d->hold.empty() == false || d->install.empty() == false)
   {
      APT::VersionVector makeDpkgAvailable;
      auto const notInstalled = [](pkgCache::VerIterator const &V) { return V.ParentPkg()->CurrentVer == 0; };
      std::copy_if(d->hold.begin(), d->hold.end(), std::back_inserter(makeDpkgAvailable), notInstalled);
      std::copy_if(d->install.begin(), d->install.end(), std::back_inserter(makeDpkgAvailable), notInstalled);

      if (makeDpkgAvailable.empty() == false)
      {
	 auto const BaseArgs = Args.size();
	 Args.push_back("--merge-avail");
	 // FIXME: supported only since 1.17.7 in dpkg
	 Args.push_back("-");
	 int dummyAvail = -1;
	 if (Debug)
	 {
	    for (auto const &V: makeDpkgAvailable)
	    {
	       std::clog << "echo 'Dummy record for " << V.ParentPkg().FullName(false) << "' | ";
	       std::copy(Args.begin(), Args.end(), std::ostream_iterator<std::string>(std::clog, " "));
	       std::clog << std::endl;
	    }
	 }
	 else
	 {
	    pid_t const dpkgMergeAvail = debSystem::ExecDpkg(Args, &dummyAvail, nullptr, true);

	    FILE* dpkg = fdopen(dummyAvail, "w");
	    for (auto const &V: makeDpkgAvailable)
	       fprintf(dpkg, "Package: %s\nVersion: 0~\nArchitecture: %s\nMaintainer: Dummy Example <dummy@example.org>\n"
		     "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());
	    fclose(dpkg);

	    ExecWait(dpkgMergeAvail, "dpkg --merge-avail", true);
	 }
	 Args.erase(Args.begin() + BaseArgs, Args.end());
      }
   }
   bool const dpkgMultiArch = _system->MultiArchSupported();

   Args.push_back("--set-selections");
   if (Debug)
   {
      std::string state;
      auto const dpkgName = [&](pkgCache::VerIterator const &V) {
	 pkgCache::PkgIterator P = V.ParentPkg();
	 if (strcmp(V.Arch(), "none") == 0)
	    ioprintf(std::clog, "echo '%s %s' | ", P.Name(), state.c_str());
	 else if (dpkgMultiArch == false)
	    ioprintf(std::clog, "echo '%s %s' | ", P.FullName(true).c_str(), state.c_str());
	 else
	    ioprintf(std::clog, "echo '%s:%s %s' | ", P.Name(), V.Arch(), state.c_str());
	 std::copy(Args.begin(), Args.end(), std::ostream_iterator<std::string>(std::clog, " "));
	 std::clog << std::endl;
      };
      for (auto const &V: d->unhold)
      {
	 if (V.ParentPkg()->CurrentVer != 0)
	    state = "install";
	 else
	    state = "deinstall";
	 dpkgName(V);
      }
      if (d->purge.empty() == false)
      {
	 state = "purge";
	 std::for_each(d->purge.begin(), d->purge.end(), dpkgName);
      }
      if (d->deinstall.empty() == false)
      {
	 state = "deinstall";
	 std::for_each(d->deinstall.begin(), d->deinstall.end(), dpkgName);
      }
      if (d->hold.empty() == false)
      {
	 state = "hold";
	 std::for_each(d->hold.begin(), d->hold.end(), dpkgName);
      }
      if (d->install.empty() == false)
      {
	 state = "install";
	 std::for_each(d->install.begin(), d->install.end(), dpkgName);
      }
   }
   else
   {
      int selections = -1;
      pid_t const dpkgSelections = debSystem::ExecDpkg(Args, &selections, nullptr, DiscardOutput);

      FILE* dpkg = fdopen(selections, "w");
      std::string state;
      auto const dpkgName = [&](pkgCache::VerIterator const &V) {
	 pkgCache::PkgIterator P = V.ParentPkg();
	 if (strcmp(V.Arch(), "none") == 0)
	    fprintf(dpkg, "%s %s\n", P.Name(), state.c_str());
	 else if (dpkgMultiArch == false)
	    fprintf(dpkg, "%s %s\n", P.FullName(true).c_str(), state.c_str());
	 else
	    fprintf(dpkg, "%s:%s %s\n", P.Name(), V.Arch(), state.c_str());
      };
      for (auto const &V: d->unhold)
      {
	 if (V.ParentPkg()->CurrentVer != 0)
	    state = "install";
	 else
	    state = "deinstall";
	 dpkgName(V);
      }
      if (d->purge.empty() == false)
      {
	 state = "purge";
	 std::for_each(d->purge.begin(), d->purge.end(), dpkgName);
      }
      if (d->deinstall.empty() == false)
      {
	 state = "deinstall";
	 std::for_each(d->deinstall.begin(), d->deinstall.end(), dpkgName);
      }
      if (d->hold.empty() == false)
      {
	 state = "hold";
	 std::for_each(d->hold.begin(), d->hold.end(), dpkgName);
      }
      if (d->install.empty() == false)
      {
	 state = "install";
	 std::for_each(d->install.begin(), d->install.end(), dpkgName);
      }
      fclose(dpkg);

      if (ExecWait(dpkgSelections, "dpkg --set-selections") == false)
      {
	 std::move(d->purge.begin(), d->purge.end(), std::back_inserter(d->error));
	 d->purge.clear();
	 std::move(d->deinstall.begin(), d->deinstall.end(), std::back_inserter(d->error));
	 d->deinstall.clear();
	 std::move(d->hold.begin(), d->hold.end(), std::back_inserter(d->error));
	 d->hold.clear();
	 std::move(d->unhold.begin(), d->unhold.end(), std::back_inserter(d->error));
	 d->unhold.clear();
	 std::move(d->install.begin(), d->install.end(), std::back_inserter(d->error));
	 d->install.clear();
      }
   }
   return d->error.empty();
}

StateChanges::StateChanges() : d(new StateChanges::Private()) {}
StateChanges::StateChanges(StateChanges&&) = default;
StateChanges& StateChanges::operator=(StateChanges&&) = default;
StateChanges::~StateChanges() = default;

}