]>
git.saurik.com Git - apt.git/blob - apt-pkg/edsp.cc
1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
4 Set of methods to help writing and reading everything needed for EDSP
5 ##################################################################### */
7 // Include Files /*{{{*/
8 #include <apt-pkg/edsp.h>
9 #include <apt-pkg/error.h>
10 #include <apt-pkg/configuration.h>
11 #include <apt-pkg/version.h>
12 #include <apt-pkg/policy.h>
13 #include <apt-pkg/tagfile.h>
21 // EDSP::WriteScenario - to the given file descriptor /*{{{*/
22 bool EDSP::WriteScenario(pkgDepCache
&Cache
, FILE* output
)
24 // we could use pkgCache::DepType and ::Priority, but these would be lokalized stringsā¦
25 const char * const PrioMap
[] = {0, "important", "required", "standard",
27 const char * const DepMap
[] = {"", "Depends", "PreDepends", "Suggests",
28 "Recommends" , "Conflicts", "Replaces",
29 "Obsoletes", "Breaks", "Enhances"};
31 for (pkgCache::PkgIterator Pkg
= Cache
.PkgBegin(); Pkg
.end() == false; ++Pkg
)
33 for (pkgCache::VerIterator Ver
= Pkg
.VersionList(); Ver
.end() == false; ++Ver
)
35 fprintf(output
, "Package: %s\n", Pkg
.Name());
36 fprintf(output
, "Architecture: %s\n", Ver
.Arch());
37 fprintf(output
, "Version: %s\n", Ver
.VerStr());
38 if (Pkg
.CurrentVer() == Ver
)
39 fprintf(output
, "Installed: yes\n");
40 if (Pkg
->SelectedState
== pkgCache::State::Hold
)
41 fprintf(output
, "Hold: yes\n");
42 fprintf(output
, "APT-ID: %u\n", Ver
->ID
);
43 fprintf(output
, "Priority: %s\n", PrioMap
[Ver
->Priority
]);
44 if ((Pkg
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
45 fprintf(output
, "Essential: yes\n");
46 fprintf(output
, "Section: %s\n", Ver
.Section());
47 if (Ver
->MultiArch
== pkgCache::Version::Allowed
|| Ver
->MultiArch
== pkgCache::Version::AllAllowed
)
48 fprintf(output
, "Multi-Arch: allowed\n");
49 else if (Ver
->MultiArch
== pkgCache::Version::Foreign
|| Ver
->MultiArch
== pkgCache::Version::AllForeign
)
50 fprintf(output
, "Multi-Arch: foreign\n");
51 else if (Ver
->MultiArch
== pkgCache::Version::Same
)
52 fprintf(output
, "Multi-Arch: same\n");
53 signed short Pin
= std::numeric_limits
<signed short>::min();
54 for (pkgCache::VerFileIterator File
= Ver
.FileList(); File
.end() == false; ++File
) {
55 signed short const p
= Cache
.GetPolicy().GetPriority(File
.File());
59 fprintf(output
, "APT-Pin: %d\n", Pin
);
60 if (Cache
.GetCandidateVer(Pkg
) == Ver
)
61 fprintf(output
, "APT-Candidate: yes\n");
62 if ((Cache
[Pkg
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
)
63 fprintf(output
, "APT-Automatic: yes\n");
64 std::string dependencies
[pkgCache::Dep::Enhances
+ 1];
66 for (pkgCache::DepIterator Dep
= Ver
.DependsList(); Dep
.end() == false; ++Dep
)
68 // Ignore implicit dependencies for multiarch here
69 if (strcmp(Pkg
.Arch(), Dep
.TargetPkg().Arch()) != 0)
72 dependencies
[Dep
->Type
].append(", ");
73 dependencies
[Dep
->Type
].append(Dep
.TargetPkg().Name());
74 if (Dep
->Version
!= 0)
75 dependencies
[Dep
->Type
].append(" (").append(pkgCache::CompTypeDeb(Dep
->CompareOp
)).append(" ").append(Dep
.TargetVer()).append(")");
76 if ((Dep
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
)
78 dependencies
[Dep
->Type
].append(" | ");
84 for (int i
= 1; i
< pkgCache::Dep::Enhances
+ 1; ++i
)
85 if (dependencies
[i
].empty() == false)
86 fprintf(output
, "%s: %s\n", DepMap
[i
], dependencies
[i
].c_str()+2);
88 for (pkgCache::PrvIterator Prv
= Ver
.ProvidesList(); Prv
.end() == false; ++Prv
)
90 // Ignore implicit provides for multiarch here
91 if (strcmp(Pkg
.Arch(), Prv
.ParentPkg().Arch()) != 0 || strcmp(Pkg
.Name(),Prv
.Name()) == 0)
93 provides
.append(", ").append(Prv
.Name());
95 if (provides
.empty() == false)
96 fprintf(output
, "Provides: %s\n", provides
.c_str()+2);
99 fprintf(output
, "\n");
105 // EDSP::WriteRequest - to the given file descriptor /*{{{*/
106 bool EDSP::WriteRequest(pkgDepCache
&Cache
, FILE* output
, bool const Upgrade
,
107 bool const DistUpgrade
, bool const AutoRemove
)
110 for (pkgCache::PkgIterator Pkg
= Cache
.PkgBegin(); Pkg
.end() == false; ++Pkg
)
113 if (Cache
[Pkg
].Delete() == true)
115 else if (Cache
[Pkg
].NewInstall() == true || Cache
[Pkg
].Upgrade() == true)
119 req
->append(" ").append(Pkg
.FullName());
121 fprintf(output
, "Request: EDSP 0.2\n");
122 if (del
.empty() == false)
123 fprintf(output
, "Remove: %s\n", del
.c_str()+1);
124 if (inst
.empty() == false)
125 fprintf(output
, "Install: %s\n", inst
.c_str()+1);
127 fprintf(output
, "Upgrade: yes\n");
128 if (DistUpgrade
== true)
129 fprintf(output
, "Dist-Upgrade: yes\n");
130 if (AutoRemove
== true)
131 fprintf(output
, "Autoremove: yes\n");
132 if (_config
->FindB("APT::Solver::Strict-Pinning", true) == false)
133 fprintf(output
, "Strict-Pinning: no\n");
134 string
solverpref("APT::Solver::");
135 solverpref
.append(_config
->Find("APT::Solver::Name", "internal")).append("::Preferences");
136 if (_config
->Exists(solverpref
) == true)
137 fprintf(output
, "Preferences: %s\n", _config
->Find(solverpref
,"").c_str());
138 fprintf(output
, "\n");
143 // EDSP::ReadResponse - from the given file descriptor /*{{{*/
144 bool EDSP::ReadResponse(int const input
, pkgDepCache
&Cache
) {
146 in
.OpenDescriptor(input
, FileFd::ReadOnly
);
147 pkgTagFile
response(&in
);
148 pkgTagSection section
;
149 while (response
.Step(section
) == true) {
151 if (section
.Exists("Install") == true)
153 else if (section
.Exists("Remove") == true)
155 //FIXME: handle progress
159 int const id
= section
.FindI(type
.c_str(), -1);
161 return _error
->Error("Unable to parse %s request!", type
.c_str());
163 //FIXME: find version by id and mark it correctly
168 // EDSP::ReadLine - first line from the given file descriptor /*{{{*/
169 // ---------------------------------------------------------------------
170 /* Little helper method to read a complete line into a string. Similar to
171 fgets but we need to use the low-level read() here as otherwise the
172 listparser will be confused later on as mixing of fgets and read isn't
173 a supported action according to the manpages and results are undefined */
174 bool EDSP::ReadLine(int const input
, std::string
&line
) {
179 while ((data
= read(input
, &one
, sizeof(one
))) != -1) {
186 if (line
.empty() == true && isblank(one
) != 0)
193 // EDSP::StringToBool - convert yes/no to bool /*{{{*/
194 // ---------------------------------------------------------------------
195 /* we are not as lazy as we are in the global StringToBool as we really
196 only accept yes/no here - but we will ignore leading spaces */
197 bool EDSP::StringToBool(char const *answer
, bool const defValue
) {
198 for (; isspace(*answer
) != 0; ++answer
);
199 if (strncasecmp(answer
, "yes", 3) == 0)
201 else if (strncasecmp(answer
, "no", 2) == 0)
204 _error
->Warning("Value '%s' is not a boolean 'yes' or 'no'!", answer
);
208 // EDSP::ReadRequest - first stanza from the given file descriptor /*{{{*/
209 bool EDSP::ReadRequest(int const input
, std::list
<std::string
> &install
,
210 std::list
<std::string
> &remove
, bool &upgrade
,
211 bool &distUpgrade
, bool &autoRemove
)
219 while (ReadLine(input
, line
) == true)
221 // Skip empty lines before request
222 if (line
.empty() == true)
224 // The first Tag must be a request, so search for it
225 if (line
.compare(0, 8, "Request:") != 0)
228 while (ReadLine(input
, line
) == true)
230 // empty lines are the end of the request
231 if (line
.empty() == true)
234 std::list
<std::string
> *request
= NULL
;
235 if (line
.compare(0, 8, "Install:") == 0)
240 else if (line
.compare(0, 7, "Remove:") == 0)
245 else if (line
.compare(0, 8, "Upgrade:") == 0)
246 upgrade
= EDSP::StringToBool(line
.c_str() + 9, false);
247 else if (line
.compare(0, 13, "Dist-Upgrade:") == 0)
248 distUpgrade
= EDSP::StringToBool(line
.c_str() + 14, false);
249 else if (line
.compare(0, 11, "Autoremove:") == 0)
250 autoRemove
= EDSP::StringToBool(line
.c_str() + 12, false);
252 _error
->Warning("Unknown line in EDSP Request stanza: %s", line
.c_str());
256 size_t end
= line
.length();
258 size_t begin
= line
.rfind(' ');
259 if (begin
== std::string::npos
)
261 request
->push_back(line
.substr(0, end
));
264 else if (begin
< end
)
265 request
->push_back(line
.substr(begin
+ 1, end
));
267 end
= line
.find_last_not_of(' ');
268 } while (end
!= std::string::npos
);
274 // EDSP::ApplyRequest - first stanza from the given file descriptor /*{{{*/
275 bool EDSP::ApplyRequest(std::list
<std::string
> const &install
,
276 std::list
<std::string
> const &remove
,
279 for (std::list
<std::string
>::const_iterator i
= install
.begin();
280 i
!= install
.end(); ++i
)
281 Cache
.MarkInstall(Cache
.FindPkg(*i
), false);
283 for (std::list
<std::string
>::const_iterator i
= remove
.begin();
284 i
!= remove
.end(); ++i
)
285 Cache
.MarkDelete(Cache
.FindPkg(*i
));
289 // EDSP::WriteSolution - to the given file descriptor /*{{{*/
290 bool EDSP::WriteSolution(pkgDepCache
&Cache
, FILE* output
)
292 bool const Debug
= _config
->FindB("Debug::EDSP::WriteSolution", false);
293 for (pkgCache::PkgIterator Pkg
= Cache
.PkgBegin(); Pkg
.end() == false; ++Pkg
)
295 if (Cache
[Pkg
].Delete() == true)
296 fprintf(output
, "Remove: %d\n", Cache
.GetCandidateVer(Pkg
)->ID
);
297 else if (Cache
[Pkg
].NewInstall() == true || Cache
[Pkg
].Upgrade() == true)
298 fprintf(output
, "Install: %d\n", Cache
.GetCandidateVer(Pkg
)->ID
);
302 fprintf(output
, "Package: %s\nVersion: %s\n", Pkg
.FullName().c_str(), Cache
.GetCandidateVer(Pkg
).VerStr());
303 fprintf(output
, "\n");
309 bool EDSP::WriteError(std::string
const &message
, FILE* output
) { return false; }