3 #include <apt-pkg/configuration.h>
4 #include <apt-pkg/fileutl.h>
5 #include <apt-pkg/strutl.h>
6 #include <apt-pkg/install-progress.h>
12 #include <sys/ioctl.h>
22 PackageManager::PackageManager() : d(NULL
), percentage(0.0), last_reported_progress(-1) {}
23 PackageManager::~PackageManager() {}
25 /* Return a APT::Progress::PackageManager based on the global
26 * apt configuration (i.e. APT::Status-Fd and APT::Status-deb822-Fd)
28 PackageManager
* PackageManagerProgressFactory()
30 // select the right progress
31 int status_fd
= _config
->FindI("APT::Status-Fd", -1);
32 int status_deb822_fd
= _config
->FindI("APT::Status-deb822-Fd", -1);
34 APT::Progress::PackageManager
*progress
= NULL
;
35 if (status_deb822_fd
> 0)
36 progress
= new APT::Progress::PackageManagerProgressDeb822Fd(
38 else if (status_fd
> 0)
39 progress
= new APT::Progress::PackageManagerProgressFd(status_fd
);
40 else if(_config
->FindB("Dpkg::Progress-Fancy", false) == true)
41 progress
= new APT::Progress::PackageManagerFancy();
42 else if (_config
->FindB("Dpkg::Progress",
43 _config
->FindB("DpkgPM::Progress", false)) == true)
44 progress
= new APT::Progress::PackageManagerText();
46 progress
= new APT::Progress::PackageManager();
50 bool PackageManager::StatusChanged(std::string
/*PackageName*/,
51 unsigned int StepsDone
,
52 unsigned int TotalSteps
,
53 std::string
/*HumanReadableAction*/)
55 int reporting_steps
= _config
->FindI("DpkgPM::Reporting-Steps", 1);
56 percentage
= StepsDone
/(float)TotalSteps
* 100.0;
57 strprintf(progress_str
, _("Progress: [%3i%%]"), (int)percentage
);
59 if(percentage
< (last_reported_progress
+ reporting_steps
))
65 PackageManagerProgressFd::PackageManagerProgressFd(int progress_fd
)
66 : d(NULL
), StepsDone(0), StepsTotal(1)
68 OutStatusFd
= progress_fd
;
70 PackageManagerProgressFd::~PackageManagerProgressFd() {}
72 void PackageManagerProgressFd::WriteToStatusFd(std::string s
)
76 FileFd::Write(OutStatusFd
, s
.c_str(), s
.size());
79 void PackageManagerProgressFd::StartDpkg()
84 // FIXME: use SetCloseExec here once it taught about throwing
85 // exceptions instead of doing _exit(100) on failure
86 fcntl(OutStatusFd
,F_SETFD
,FD_CLOEXEC
);
88 // send status information that we are about to fork dpkg
89 std::ostringstream status
;
90 status
<< "pmstatus:dpkg-exec:"
91 << (StepsDone
/float(StepsTotal
)*100.0)
92 << ":" << _("Running dpkg")
94 WriteToStatusFd(status
.str());
97 APT_CONST
void PackageManagerProgressFd::Stop()
101 void PackageManagerProgressFd::Error(std::string PackageName
,
102 unsigned int StepsDone
,
103 unsigned int TotalSteps
,
104 std::string ErrorMessage
)
106 std::ostringstream status
;
107 status
<< "pmerror:" << PackageName
108 << ":" << (StepsDone
/float(TotalSteps
)*100.0)
109 << ":" << ErrorMessage
111 WriteToStatusFd(status
.str());
114 void PackageManagerProgressFd::ConffilePrompt(std::string PackageName
,
115 unsigned int StepsDone
,
116 unsigned int TotalSteps
,
117 std::string ConfMessage
)
119 std::ostringstream status
;
120 status
<< "pmconffile:" << PackageName
121 << ":" << (StepsDone
/float(TotalSteps
)*100.0)
122 << ":" << ConfMessage
124 WriteToStatusFd(status
.str());
128 bool PackageManagerProgressFd::StatusChanged(std::string PackageName
,
129 unsigned int xStepsDone
,
130 unsigned int xTotalSteps
,
131 std::string pkg_action
)
133 StepsDone
= xStepsDone
;
134 StepsTotal
= xTotalSteps
;
136 // build the status str
137 std::ostringstream status
;
138 status
<< "pmstatus:" << StringSplit(PackageName
, ":")[0]
139 << ":" << (StepsDone
/float(StepsTotal
)*100.0)
142 WriteToStatusFd(status
.str());
144 if(_config
->FindB("Debug::APT::Progress::PackageManagerFd", false) == true)
145 std::cerr
<< "progress: " << PackageName
<< " " << xStepsDone
146 << " " << xTotalSteps
<< " " << pkg_action
154 PackageManagerProgressDeb822Fd::PackageManagerProgressDeb822Fd(int progress_fd
)
155 : d(NULL
), StepsDone(0), StepsTotal(1)
157 OutStatusFd
= progress_fd
;
159 PackageManagerProgressDeb822Fd::~PackageManagerProgressDeb822Fd() {}
161 void PackageManagerProgressDeb822Fd::WriteToStatusFd(std::string s
)
163 FileFd::Write(OutStatusFd
, s
.c_str(), s
.size());
166 void PackageManagerProgressDeb822Fd::StartDpkg()
168 // FIXME: use SetCloseExec here once it taught about throwing
169 // exceptions instead of doing _exit(100) on failure
170 fcntl(OutStatusFd
,F_SETFD
,FD_CLOEXEC
);
172 // send status information that we are about to fork dpkg
173 std::ostringstream status
;
174 status
<< "Status: " << "progress" << std::endl
175 << "Percent: " << (StepsDone
/float(StepsTotal
)*100.0) << std::endl
176 << "Message: " << _("Running dpkg") << std::endl
178 WriteToStatusFd(status
.str());
181 APT_CONST
void PackageManagerProgressDeb822Fd::Stop()
185 void PackageManagerProgressDeb822Fd::Error(std::string PackageName
,
186 unsigned int StepsDone
,
187 unsigned int TotalSteps
,
188 std::string ErrorMessage
)
190 std::ostringstream status
;
191 status
<< "Status: " << "Error" << std::endl
192 << "Package:" << PackageName
<< std::endl
193 << "Percent: " << (StepsDone
/float(TotalSteps
)*100.0) << std::endl
194 << "Message: " << ErrorMessage
<< std::endl
196 WriteToStatusFd(status
.str());
199 void PackageManagerProgressDeb822Fd::ConffilePrompt(std::string PackageName
,
200 unsigned int StepsDone
,
201 unsigned int TotalSteps
,
202 std::string ConfMessage
)
204 std::ostringstream status
;
205 status
<< "Status: " << "ConfFile" << std::endl
206 << "Package:" << PackageName
<< std::endl
207 << "Percent: " << (StepsDone
/float(TotalSteps
)*100.0) << std::endl
208 << "Message: " << ConfMessage
<< std::endl
210 WriteToStatusFd(status
.str());
214 bool PackageManagerProgressDeb822Fd::StatusChanged(std::string PackageName
,
215 unsigned int xStepsDone
,
216 unsigned int xTotalSteps
,
219 StepsDone
= xStepsDone
;
220 StepsTotal
= xTotalSteps
;
222 // build the status str
223 std::ostringstream status
;
224 status
<< "Status: " << "progress" << std::endl
225 << "Package: " << PackageName
<< std::endl
226 << "Percent: " << (StepsDone
/float(StepsTotal
)*100.0) << std::endl
227 << "Message: " << message
<< std::endl
229 WriteToStatusFd(status
.str());
235 PackageManagerFancy::PackageManagerFancy()
236 : d(NULL
), child_pty(-1)
238 // setup terminal size
239 old_SIGWINCH
= signal(SIGWINCH
, PackageManagerFancy::staticSIGWINCH
);
240 instances
.push_back(this);
242 std::vector
<PackageManagerFancy
*> PackageManagerFancy::instances
;
244 PackageManagerFancy::~PackageManagerFancy()
246 instances
.erase(find(instances
.begin(), instances
.end(), this));
247 signal(SIGWINCH
, old_SIGWINCH
);
250 void PackageManagerFancy::staticSIGWINCH(int signum
)
252 std::vector
<PackageManagerFancy
*>::const_iterator I
;
253 for(I
= instances
.begin(); I
!= instances
.end(); ++I
)
254 (*I
)->HandleSIGWINCH(signum
);
257 PackageManagerFancy::TermSize
258 PackageManagerFancy::GetTerminalSize()
261 PackageManagerFancy::TermSize s
= { 0, 0 };
263 // FIXME: get from "child_pty" instead?
264 if(ioctl(STDOUT_FILENO
, TIOCGWINSZ
, (char *)&win
) != 0)
267 if(_config
->FindB("Debug::InstallProgress::Fancy", false) == true)
268 std::cerr
<< "GetTerminalSize: " << win
.ws_row
<< " x " << win
.ws_col
<< std::endl
;
271 s
.columns
= win
.ws_col
;
275 void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows
)
277 if(_config
->FindB("Debug::InstallProgress::Fancy", false) == true)
278 std::cerr
<< "SetupTerminalScrollArea: " << nr_rows
<< std::endl
;
280 if (unlikely(nr_rows
<= 1))
283 // scroll down a bit to avoid visual glitch when the screen
284 // area shrinks by one row
288 std::cout
<< "\0337";
290 // set scroll region (this will place the cursor in the top left)
291 std::cout
<< "\033[0;" << nr_rows
- 1 << "r";
293 // restore cursor but ensure its inside the scrolling area
294 std::cout
<< "\0338";
295 static const char *move_cursor_up
= "\033[1A";
296 std::cout
<< move_cursor_up
;
298 // ensure its flushed
299 std::flush(std::cout
);
301 // setup tty size to ensure xterm/linux console are working properly too
304 if (ioctl(child_pty
, TIOCGWINSZ
, (char *)&win
) != -1)
306 win
.ws_row
= nr_rows
- 1;
307 ioctl(child_pty
, TIOCSWINSZ
, (char *)&win
);
311 void PackageManagerFancy::HandleSIGWINCH(int)
313 int const nr_terminal_rows
= GetTerminalSize().rows
;
314 SetupTerminalScrollArea(nr_terminal_rows
);
318 void PackageManagerFancy::Start(int a_child_pty
)
320 child_pty
= a_child_pty
;
321 int const nr_terminal_rows
= GetTerminalSize().rows
;
322 SetupTerminalScrollArea(nr_terminal_rows
);
325 void PackageManagerFancy::Stop()
327 int const nr_terminal_rows
= GetTerminalSize().rows
;
328 if (nr_terminal_rows
> 0)
330 SetupTerminalScrollArea(nr_terminal_rows
+ 1);
332 // override the progress line (sledgehammer)
333 static const char* clear_screen_below_cursor
= "\033[J";
334 std::cout
<< clear_screen_below_cursor
;
340 PackageManagerFancy::GetTextProgressStr(float Percent
, int OutputSize
)
345 // should we raise a exception here instead?
346 if (Percent
< 0.0 || Percent
> 1.0 || OutputSize
< 3)
349 int BarSize
= OutputSize
- 2; // bar without the leading "[" and trailing "]"
351 for(i
=0; i
< BarSize
*Percent
; i
++)
353 for (/*nothing*/; i
< BarSize
; i
++)
359 bool PackageManagerFancy::StatusChanged(std::string PackageName
,
360 unsigned int StepsDone
,
361 unsigned int TotalSteps
,
362 std::string HumanReadableAction
)
364 if (!PackageManager::StatusChanged(PackageName
, StepsDone
, TotalSteps
,
365 HumanReadableAction
))
368 return DrawStatusLine();
370 bool PackageManagerFancy::DrawStatusLine()
372 PackageManagerFancy::TermSize
const size
= GetTerminalSize();
373 if (unlikely(size
.rows
< 1 || size
.columns
< 1))
376 static std::string save_cursor
= "\0337";
377 static std::string restore_cursor
= "\0338";
380 static std::string set_bg_color
= DeQuoteString(
381 _config
->Find("Dpkg::Progress-Fancy::Progress-fg", "%1b[42m"));
383 static std::string set_fg_color
= DeQuoteString(
384 _config
->Find("Dpkg::Progress-Fancy::Progress-bg", "%1b[30m"));
386 static std::string restore_bg
= "\033[49m";
387 static std::string restore_fg
= "\033[39m";
389 std::cout
<< save_cursor
390 // move cursor position to last row
391 << "\033[" << size
.rows
<< ";0f"
397 std::flush(std::cout
);
399 // draw text progress bar
400 if (_config
->FindB("Dpkg::Progress-Fancy::Progress-Bar", true))
403 float progressbar_size
= size
.columns
- padding
- progress_str
.size();
404 float current_percent
= percentage
/ 100.0;
406 << GetTextProgressStr(current_percent
, progressbar_size
)
408 std::flush(std::cout
);
412 std::cout
<< restore_cursor
;
413 std::flush(std::cout
);
415 last_reported_progress
= percentage
;
420 bool PackageManagerText::StatusChanged(std::string PackageName
,
421 unsigned int StepsDone
,
422 unsigned int TotalSteps
,
423 std::string HumanReadableAction
)
425 if (!PackageManager::StatusChanged(PackageName
, StepsDone
, TotalSteps
, HumanReadableAction
))
428 std::cout
<< progress_str
<< "\r\n";
429 std::flush(std::cout
);
431 last_reported_progress
= percentage
;
436 PackageManagerText::PackageManagerText() : PackageManager(), d(NULL
) {}
437 PackageManagerText::~PackageManagerText() {}
442 } // namespace progress