1 // -*- mode: cpp; mode: fold -*-
3 /* #####################################################################
5 cover around the internal solver to be able to run it like an external
7 ##################################################################### */
9 // Include Files /*{{{*/
12 #include <apt-pkg/error.h>
13 #include <apt-pkg/cmndline.h>
14 #include <apt-pkg/init.h>
15 #include <apt-pkg/cachefile.h>
16 #include <apt-pkg/cacheset.h>
17 #include <apt-pkg/strutl.h>
18 #include <apt-pkg/edsp.h>
19 #include <apt-pkg/algorithms.h>
20 #include <apt-pkg/fileutl.h>
21 #include <apt-pkg/pkgsystem.h>
22 #include <apt-pkg/upgrade.h>
23 #include <apt-pkg/configuration.h>
24 #include <apt-pkg/depcache.h>
25 #include <apt-pkg/pkgcache.h>
26 #include <apt-pkg/cacheiterators.h>
28 #include <apt-private/private-output.h>
29 #include <apt-private/private-cmndline.h>
30 #include <apt-private/private-main.h>
44 static bool ShowHelp(CommandLine
&) /*{{{*/
47 _("Usage: apt-internal-solver\n"
49 "apt-internal-solver is an interface to use the current internal\n"
50 "resolver for the APT family like an external one, for debugging or\n"
55 APT_NORETURN
static void DIE(std::string
const &message
) { /*{{{*/
56 std::cerr
<< "ERROR: " << message
<< std::endl
;
57 _error
->DumpErrors(std::cerr
);
61 static std::vector
<aptDispatchWithHelp
> GetCommands() /*{{{*/
66 static bool WriteSolution(pkgDepCache
&Cache
, FileFd
&output
) /*{{{*/
68 bool Okay
= output
.Failed() == false;
69 for (pkgCache::PkgIterator Pkg
= Cache
.PkgBegin(); Pkg
.end() == false && likely(Okay
); ++Pkg
)
72 if (Cache
[Pkg
].Delete() == true)
73 Okay
&= EDSP::WriteSolutionStanza(output
, "Remove", Pkg
.CurrentVer());
74 else if (Cache
[Pkg
].NewInstall() == true || Cache
[Pkg
].Upgrade() == true)
75 Okay
&= EDSP::WriteSolutionStanza(output
, "Install", Cache
.GetCandidateVersion(Pkg
));
76 else if (Cache
[Pkg
].Garbage
== true)
77 Okay
&= EDSP::WriteSolutionStanza(output
, "Autoremove", Pkg
.CurrentVer());
82 int main(int argc
,const char *argv
[]) /*{{{*/
84 // we really don't need anything
88 ParseCommandLine(CmdL
, APT_CMD::APT_INTERNAL_SOLVER
, &_config
, NULL
, argc
, argv
, &ShowHelp
, &GetCommands
);
90 if (CmdL
.FileList
[0] != 0 && strcmp(CmdL
.FileList
[0], "scenario") == 0)
92 if (pkgInitSystem(*_config
,_system
) == false) {
93 std::cerr
<< "System could not be initialized!" << std::endl
;
96 pkgCacheFile CacheFile
;
97 CacheFile
.Open(NULL
, false);
98 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
100 if (output
.OpenDescriptor(STDOUT_FILENO
, FileFd::WriteOnly
| FileFd::BufferedWrite
, true) == false)
102 if (pkgset
.empty() == true)
103 EDSP::WriteScenario(CacheFile
, output
);
106 std::vector
<bool> pkgvec(CacheFile
->Head().PackageCount
, false);
107 for (auto const &p
: pkgset
)
108 pkgvec
[p
->ID
] = true;
109 EDSP::WriteLimitedScenario(CacheFile
, output
, pkgvec
);
112 _error
->DumpErrors(std::cerr
);
116 // Deal with stdout not being a tty
117 if (!isatty(STDOUT_FILENO
) && _config
->FindI("quiet", -1) == -1)
118 _config
->Set("quiet","1");
120 if (_config
->FindI("quiet", 0) < 1)
121 _config
->Set("Debug::EDSP::WriteSolution", true);
123 _config
->Set("APT::System", "Debian APT solver interface");
124 _config
->Set("APT::Solver", "internal");
125 _config
->Set("edsp::scenario", "/nonexistent/stdin");
126 _config
->Clear("Dir::Log");
128 if (output
.OpenDescriptor(STDOUT_FILENO
, FileFd::WriteOnly
| FileFd::BufferedWrite
, true) == false)
129 DIE("stdout couldn't be opened");
130 int const input
= STDIN_FILENO
;
131 SetNonBlock(input
, false);
133 EDSP::WriteProgress(0, "Start up solver…", output
);
135 if (pkgInitSystem(*_config
,_system
) == false)
136 DIE("System could not be initialized!");
138 EDSP::WriteProgress(1, "Read request…", output
);
140 if (WaitFd(input
, false, 5) == false)
141 DIE("WAIT timed out in the resolver");
143 std::list
<std::string
> install
, remove
;
145 if (EDSP::ReadRequest(input
, install
, remove
, flags
) == false)
146 DIE("Parsing the request failed!");
148 EDSP::WriteProgress(5, "Read scenario…", output
);
150 pkgCacheFile CacheFile
;
151 if (CacheFile
.Open(NULL
, false) == false)
152 DIE("Failed to open CacheFile!");
154 EDSP::WriteProgress(50, "Apply request on scenario…", output
);
156 if (EDSP::ApplyRequest(install
, remove
, CacheFile
) == false)
157 DIE("Failed to apply request to depcache!");
159 pkgProblemResolver
Fix(CacheFile
);
160 for (std::list
<std::string
>::const_iterator i
= remove
.begin();
161 i
!= remove
.end(); ++i
) {
162 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
168 for (std::list
<std::string
>::const_iterator i
= install
.begin();
169 i
!= install
.end(); ++i
) {
170 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
175 for (std::list
<std::string
>::const_iterator i
= install
.begin();
176 i
!= install
.end(); ++i
)
177 CacheFile
->MarkInstall(CacheFile
->FindPkg(*i
), true);
179 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output
);
182 if (flags
& EDSP::Request::UPGRADE_ALL
) {
183 int upgrade_flags
= APT::Upgrade::ALLOW_EVERYTHING
;
184 if (flags
& EDSP::Request::FORBID_NEW_INSTALL
)
185 upgrade_flags
|= APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES
;
186 if (flags
& EDSP::Request::FORBID_REMOVE
)
187 upgrade_flags
|= APT::Upgrade::FORBID_REMOVE_PACKAGES
;
189 if (APT::Upgrade::Upgrade(CacheFile
, upgrade_flags
))
191 else if (upgrade_flags
== APT::Upgrade::ALLOW_EVERYTHING
)
192 failure
= "ERR_UNSOLVABLE_FULL_UPGRADE";
194 failure
= "ERR_UNSOLVABLE_UPGRADE";
195 } else if (Fix
.Resolve() == false)
196 failure
= "ERR_UNSOLVABLE";
198 if (failure
.empty() == false) {
199 std::ostringstream broken
;
200 ShowBroken(broken
, CacheFile
, false);
201 EDSP::WriteError(failure
.c_str(), broken
.str(), output
);
205 EDSP::WriteProgress(95, "Write solution…", output
);
207 if (WriteSolution(CacheFile
, output
) == false)
208 DIE("Failed to output the solution!");
210 EDSP::WriteProgress(100, "Done", output
);
212 return DispatchCommandLine(CmdL
, {});