]>
git.saurik.com Git - apt.git/blob - cmdline/apt-internal-solver.cc
e7faf88a97a7b45c4dbbe19248cbe9d5eca2b2df
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>
29 // ShowHelp - Show a help screen /*{{{*/
30 // ---------------------------------------------------------------------
32 bool ShowHelp(CommandLine
&CmdL
) {
33 ioprintf(std::cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
34 COMMON_ARCH
,__DATE__
,__TIME__
);
37 _("Usage: apt-internal-resolver\n"
39 "apt-internal-resolver is an interface to use the current internal\n"
40 "like an external resolver for the APT family for debugging or alike\n"
43 " -h This help text.\n"
44 " -q Loggable output - no progress indicator\n"
45 " -c=? Read this configuration file\n"
46 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
47 "apt.conf(5) manual pages for more information and options.\n"
48 " This APT has Super Cow Powers.\n");
52 int main(int argc
,const char *argv
[]) /*{{{*/
54 CommandLine::Args Args
[] = {
55 {'h',"help","help",0},
56 {'v',"version","version",0},
57 {'q',"quiet","quiet",CommandLine::IntLevel
},
58 {'q',"silent","quiet",CommandLine::IntLevel
},
59 {'c',"config-file",0,CommandLine::ConfigFile
},
60 {'o',"option",0,CommandLine::ArbItem
},
63 CommandLine
CmdL(Args
,_config
);
64 if (pkgInitConfig(*_config
) == false ||
65 CmdL
.Parse(argc
,argv
) == false) {
70 // See if the help should be shown
71 if (_config
->FindB("help") == true ||
72 _config
->FindB("version") == true) {
77 if (CmdL
.FileList
[0] != 0 && strcmp(CmdL
.FileList
[0], "scenario") == 0)
79 if (pkgInitSystem(*_config
,_system
) == false) {
80 std::cerr
<< "System could not be initialized!" << std::endl
;
83 pkgCacheFile CacheFile
;
84 CacheFile
.Open(NULL
, false);
85 APT::PackageSet pkgset
= APT::PackageSet::FromCommandLine(CacheFile
, CmdL
.FileList
+ 1);
86 FILE* output
= stdout
;
87 if (pkgset
.empty() == true)
88 EDSP::WriteScenario(CacheFile
, output
);
90 EDSP::WriteLimitedScenario(CacheFile
, output
, pkgset
);
92 _error
->DumpErrors(std::cerr
);
96 // Deal with stdout not being a tty
97 if (!isatty(STDOUT_FILENO
) && _config
->FindI("quiet", -1) == -1)
98 _config
->Set("quiet","1");
100 if (_config
->FindI("quiet", 0) < 1)
101 _config
->Set("Debug::EDSP::WriteSolution", true);
103 _config
->Set("APT::Solver", "internal");
104 _config
->Set("edsp::scenario", "stdin");
105 int input
= STDIN_FILENO
;
106 FILE* output
= stdout
;
107 SetNonBlock(input
, false);
109 EDSP::WriteProgress(0, "Start up solver…", output
);
111 if (pkgInitSystem(*_config
,_system
) == false) {
112 std::cerr
<< "System could not be initialized!" << std::endl
;
116 EDSP::WriteProgress(1, "Read request…", output
);
118 if (WaitFd(input
, false, 5) == false)
119 std::cerr
<< "WAIT timed out in the resolver" << std::endl
;
121 std::list
<std::string
> install
, remove
;
122 bool upgrade
, distUpgrade
, autoRemove
;
123 if (EDSP::ReadRequest(input
, install
, remove
, upgrade
, distUpgrade
, autoRemove
) == false) {
124 std::cerr
<< "Parsing the request failed!" << std::endl
;
128 EDSP::WriteProgress(5, "Read scenario…", output
);
130 pkgCacheFile CacheFile
;
131 CacheFile
.Open(NULL
, false);
133 EDSP::WriteProgress(50, "Apply request on scenario…", output
);
135 if (EDSP::ApplyRequest(install
, remove
, CacheFile
) == false) {
136 std::cerr
<< "Failed to apply request to depcache!" << std::endl
;
140 pkgProblemResolver
Fix(CacheFile
);
141 for (std::list
<std::string
>::const_iterator i
= remove
.begin();
142 i
!= remove
.end(); ++i
) {
143 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
149 for (std::list
<std::string
>::const_iterator i
= install
.begin();
150 i
!= install
.end(); ++i
) {
151 pkgCache::PkgIterator P
= CacheFile
->FindPkg(*i
);
156 for (std::list
<std::string
>::const_iterator i
= install
.begin();
157 i
!= install
.end(); ++i
)
158 CacheFile
->MarkInstall(CacheFile
->FindPkg(*i
), true);
160 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output
);
162 if (upgrade
== true) {
163 if (pkgAllUpgrade(CacheFile
) == false) {
164 EDSP::WriteError("ERR_UNSOLVABLE_UPGRADE", "An upgrade error occured", output
);
167 } else if (distUpgrade
== true) {
168 if (pkgDistUpgrade(CacheFile
) == false) {
169 EDSP::WriteError("ERR_UNSOLVABLE_DIST_UPGRADE", "An dist-upgrade error occured", output
);
172 } else if (Fix
.Resolve() == false) {
173 EDSP::WriteError("ERR_UNSOLVABLE", "An error occured", output
);
177 EDSP::WriteProgress(95, "Write solution…", output
);
179 if (EDSP::WriteSolution(CacheFile
, output
) == false) {
180 std::cerr
<< "Failed to output the solution!" << std::endl
;
184 EDSP::WriteProgress(100, "Done", output
);
186 bool const Errors
= _error
->PendingError();
187 if (_config
->FindI("quiet",0) > 0)
188 _error
->DumpErrors(std::cerr
);
190 _error
->DumpErrors(std::cerr
, GlobalError::DEBUG
);
191 return Errors
== true ? 100 : 0;