1 #include <apt-pkg/configuration.h>
2 #include <apt-pkg/fileutl.h>
3 #include <apt-pkg/strutl.h>
4 #include <apt-pkg/install-progress.h>
16 PackageManager
* PackageManagerProgressFactory()
18 // select the right progress
19 int status_fd
= _config
->FindI("APT::Status-Fd", -1);
20 int status_deb822_fd
= _config
->FindI("APT::Status-deb822-Fd", -1);
22 APT::Progress::PackageManager
*progress
= NULL
;
23 if (status_deb822_fd
> 0)
24 progress
= new APT::Progress::PackageManagerProgressDeb822Fd(
26 else if (status_fd
> 0)
27 progress
= new APT::Progress::PackageManagerProgressFd(status_fd
);
28 else if(_config
->FindB("Dpkg::Progress-Fancy", false) == true)
29 progress
= new APT::Progress::PackageManagerFancy();
30 else if (_config
->FindB("Dpkg::Progress",
31 _config
->FindB("DpkgPM::Progress", false)) == true)
32 progress
= new APT::Progress::PackageManagerText();
34 progress
= new APT::Progress::PackageManager();
38 bool PackageManager::StatusChanged(std::string PackageName
,
39 unsigned int StepsDone
,
40 unsigned int TotalSteps
,
41 std::string HumanReadableAction
)
43 int reporting_steps
= _config
->FindI("DpkgPM::Reporting-Steps", 1);
44 percentage
= StepsDone
/(float)TotalSteps
* 100.0;
45 strprintf(progress_str
, _("Progress: [%3i%%]"), (int)percentage
);
47 if(percentage
< (last_reported_progress
+ reporting_steps
))
53 PackageManagerProgressFd::PackageManagerProgressFd(int progress_fd
)
54 : StepsDone(0), StepsTotal(1)
56 OutStatusFd
= progress_fd
;
59 void PackageManagerProgressFd::WriteToStatusFd(std::string s
)
63 FileFd::Write(OutStatusFd
, s
.c_str(), s
.size());
66 void PackageManagerProgressFd::Start()
71 // FIXME: use SetCloseExec here once it taught about throwing
72 // exceptions instead of doing _exit(100) on failure
73 fcntl(OutStatusFd
,F_SETFD
,FD_CLOEXEC
);
75 // send status information that we are about to fork dpkg
76 std::ostringstream status
;
77 status
<< "pmstatus:dpkg-exec:"
78 << (StepsDone
/float(StepsTotal
)*100.0)
79 << ":" << _("Running dpkg")
81 WriteToStatusFd(status
.str());
84 void PackageManagerProgressFd::Stop()
86 // clear the Keep-Fd again
87 _config
->Clear("APT::Keep-Fds", OutStatusFd
);
90 void PackageManagerProgressFd::Error(std::string PackageName
,
91 unsigned int StepsDone
,
92 unsigned int TotalSteps
,
93 std::string ErrorMessage
)
95 std::ostringstream status
;
96 status
<< "pmerror:" << PackageName
97 << ":" << (StepsDone
/float(TotalSteps
)*100.0)
98 << ":" << ErrorMessage
100 WriteToStatusFd(status
.str());
103 void PackageManagerProgressFd::ConffilePrompt(std::string PackageName
,
104 unsigned int StepsDone
,
105 unsigned int TotalSteps
,
106 std::string ConfMessage
)
108 std::ostringstream status
;
109 status
<< "pmconffile:" << PackageName
110 << ":" << (StepsDone
/float(TotalSteps
)*100.0)
111 << ":" << ConfMessage
113 WriteToStatusFd(status
.str());
117 bool PackageManagerProgressFd::StatusChanged(std::string PackageName
,
118 unsigned int xStepsDone
,
119 unsigned int xTotalSteps
,
120 std::string pkg_action
)
122 StepsDone
= xStepsDone
;
123 StepsTotal
= xTotalSteps
;
125 // build the status str
126 std::ostringstream status
;
127 status
<< "pmstatus:" << StringSplit(PackageName
, ":")[0]
128 << ":" << (StepsDone
/float(StepsTotal
)*100.0)
131 WriteToStatusFd(status
.str());
133 if(_config
->FindB("Debug::APT::Progress::PackageManagerFd", false) == true)
134 std::cerr
<< "progress: " << PackageName
<< " " << xStepsDone
135 << " " << xTotalSteps
<< " " << pkg_action
143 PackageManagerProgressDeb822Fd::PackageManagerProgressDeb822Fd(int progress_fd
)
144 : StepsDone(0), StepsTotal(1)
146 OutStatusFd
= progress_fd
;
149 void PackageManagerProgressDeb822Fd::WriteToStatusFd(std::string s
)
151 FileFd::Write(OutStatusFd
, s
.c_str(), s
.size());
154 void PackageManagerProgressDeb822Fd::Start()
156 // FIXME: use SetCloseExec here once it taught about throwing
157 // exceptions instead of doing _exit(100) on failure
158 fcntl(OutStatusFd
,F_SETFD
,FD_CLOEXEC
);
160 // send status information that we are about to fork dpkg
161 std::ostringstream status
;
162 status
<< "Status: " << "progress" << std::endl
163 << "Percent: " << (StepsDone
/float(StepsTotal
)*100.0) << std::endl
164 << "Message: " << _("Running dpkg") << std::endl
166 WriteToStatusFd(status
.str());
169 void PackageManagerProgressDeb822Fd::Stop()
171 // clear the Keep-Fd again
172 _config
->Clear("APT::Keep-Fds", OutStatusFd
);
175 void PackageManagerProgressDeb822Fd::Error(std::string PackageName
,
176 unsigned int StepsDone
,
177 unsigned int TotalSteps
,
178 std::string ErrorMessage
)
180 std::ostringstream status
;
181 status
<< "Status: " << "Error" << std::endl
182 << "Package:" << PackageName
<< std::endl
183 << "Percent: " << (StepsDone
/float(TotalSteps
)*100.0) << std::endl
184 << "Message: " << ErrorMessage
<< std::endl
186 WriteToStatusFd(status
.str());
189 void PackageManagerProgressDeb822Fd::ConffilePrompt(std::string PackageName
,
190 unsigned int StepsDone
,
191 unsigned int TotalSteps
,
192 std::string ConfMessage
)
194 std::ostringstream status
;
195 status
<< "Status: " << "ConfFile" << std::endl
196 << "Package:" << PackageName
<< std::endl
197 << "Percent: " << (StepsDone
/float(TotalSteps
)*100.0) << std::endl
198 << "Message: " << ConfMessage
<< std::endl
200 WriteToStatusFd(status
.str());
204 bool PackageManagerProgressDeb822Fd::StatusChanged(std::string PackageName
,
205 unsigned int xStepsDone
,
206 unsigned int xTotalSteps
,
209 StepsDone
= xStepsDone
;
210 StepsTotal
= xTotalSteps
;
212 // build the status str
213 std::ostringstream status
;
214 status
<< "Status: " << "progress" << std::endl
215 << "Package: " << PackageName
<< std::endl
216 << "Percent: " << (StepsDone
/float(StepsTotal
)*100.0) << std::endl
217 << "Message: " << message
<< std::endl
219 WriteToStatusFd(status
.str());
225 void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows
)
227 // scroll down a bit to avoid visual glitch when the screen
228 // area shrinks by one row
232 std::cout
<< "\033[s";
234 // set scroll region (this will place the cursor in the top left)
235 std::cout
<< "\033[1;" << nr_rows
- 1 << "r";
237 // restore cursor but ensure its inside the scrolling area
238 std::cout
<< "\033[u";
239 static const char *move_cursor_up
= "\033[1A";
240 std::cout
<< move_cursor_up
;
242 std::flush(std::cout
);
245 PackageManagerFancy::PackageManagerFancy()
246 : nr_terminal_rows(-1)
249 if(ioctl(STDOUT_FILENO
, TIOCGWINSZ
, (char *)&win
) == 0)
251 nr_terminal_rows
= win
.ws_row
;
255 void PackageManagerFancy::Start()
257 if (nr_terminal_rows
> 0)
258 SetupTerminalScrollArea(nr_terminal_rows
);
261 void PackageManagerFancy::Stop()
263 if (nr_terminal_rows
> 0)
265 SetupTerminalScrollArea(nr_terminal_rows
+ 1);
267 // override the progress line (sledgehammer)
268 static const char* clear_screen_below_cursor
= "\033[J";
269 std::cout
<< clear_screen_below_cursor
;
273 bool PackageManagerFancy::StatusChanged(std::string PackageName
,
274 unsigned int StepsDone
,
275 unsigned int TotalSteps
,
276 std::string HumanReadableAction
)
278 if (!PackageManager::StatusChanged(PackageName
, StepsDone
, TotalSteps
,
279 HumanReadableAction
))
282 int row
= nr_terminal_rows
;
284 static string save_cursor
= "\033[s";
285 static string restore_cursor
= "\033[u";
287 static string set_bg_color
= "\033[42m"; // green
288 static string set_fg_color
= "\033[30m"; // black
290 static string restore_bg
= "\033[49m";
291 static string restore_fg
= "\033[39m";
293 std::cout
<< save_cursor
294 // move cursor position to last row
295 << "\033[" << row
<< ";0f"
302 std::flush(std::cout
);
303 last_reported_progress
= percentage
;
308 bool PackageManagerText::StatusChanged(std::string PackageName
,
309 unsigned int StepsDone
,
310 unsigned int TotalSteps
,
311 std::string HumanReadableAction
)
313 if (!PackageManager::StatusChanged(PackageName
, StepsDone
, TotalSteps
, HumanReadableAction
))
316 std::cout
<< progress_str
<< "\r\n";
317 std::flush(std::cout
);
319 last_reported_progress
= percentage
;
326 }; // namespace progress