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>
23 PackageManager::PackageManager() : d(NULL
), percentage(0.0), last_reported_progress(-1) {}
24 PackageManager::~PackageManager() {}
26 /* Return a APT::Progress::PackageManager based on the global
27 * apt configuration (i.e. APT::Status-Fd and APT::Status-deb822-Fd)
29 PackageManager
* PackageManagerProgressFactory()
31 // select the right progress
32 int status_fd
= _config
->FindI("APT::Status-Fd", -1);
33 int status_deb822_fd
= _config
->FindI("APT::Status-deb822-Fd", -1);
35 APT::Progress::PackageManager
*progress
= NULL
;
36 if (status_deb822_fd
> 0)
37 progress
= new APT::Progress::PackageManagerProgressDeb822Fd(
39 else if (status_fd
> 0)
40 progress
= new APT::Progress::PackageManagerProgressFd(status_fd
);
41 else if(_config
->FindB("Dpkg::Progress-Fancy", false) == true)
42 progress
= new APT::Progress::PackageManagerFancy();
43 else if (_config
->FindB("Dpkg::Progress",
44 _config
->FindB("DpkgPM::Progress", false)) == true)
45 progress
= new APT::Progress::PackageManagerText();
47 progress
= new APT::Progress::PackageManager();
51 bool PackageManager::StatusChanged(std::string
/*PackageName*/,
52 unsigned int StepsDone
,
53 unsigned int TotalSteps
,
54 std::string
/*HumanReadableAction*/)
56 int reporting_steps
= _config
->FindI("DpkgPM::Reporting-Steps", 1);
57 percentage
= StepsDone
/(float)TotalSteps
* 100.0;
58 strprintf(progress_str
, _("Progress: [%3i%%]"), (int)percentage
);
60 if(percentage
< (last_reported_progress
+ reporting_steps
))
66 PackageManagerProgressFd::PackageManagerProgressFd(int progress_fd
)
67 : d(NULL
), StepsDone(0), StepsTotal(1)
69 OutStatusFd
= progress_fd
;
71 PackageManagerProgressFd::~PackageManagerProgressFd() {}
73 void PackageManagerProgressFd::WriteToStatusFd(std::string s
)
77 FileFd::Write(OutStatusFd
, s
.c_str(), s
.size());
80 static std::string
GetProgressFdString(char const * const status
,
81 char const * const pkg
, unsigned long long Done
,
82 unsigned long long Total
, char const * const msg
)
84 float const progress
{Done
/ static_cast<float>(Total
) * 100};
85 std::ostringstream str
;
86 str
.imbue(std::locale::classic());
88 str
<< status
<< ':' << pkg
<< ':' << std::fixed
<< progress
<< ':' << msg
<< '\n';
92 void PackageManagerProgressFd::StartDpkg()
97 // FIXME: use SetCloseExec here once it taught about throwing
98 // exceptions instead of doing _exit(100) on failure
99 fcntl(OutStatusFd
,F_SETFD
,FD_CLOEXEC
);
101 // send status information that we are about to fork dpkg
102 WriteToStatusFd(GetProgressFdString("pmstatus", "dpkg-exec", StepsDone
, StepsTotal
, _("Running dpkg")));
105 APT_CONST
void PackageManagerProgressFd::Stop()
109 void PackageManagerProgressFd::Error(std::string PackageName
,
110 unsigned int StepsDone
,
111 unsigned int TotalSteps
,
112 std::string ErrorMessage
)
114 WriteToStatusFd(GetProgressFdString("pmerror", PackageName
.c_str(),
115 StepsDone
, TotalSteps
, ErrorMessage
.c_str()));
118 void PackageManagerProgressFd::ConffilePrompt(std::string PackageName
,
119 unsigned int StepsDone
,
120 unsigned int TotalSteps
,
121 std::string ConfMessage
)
123 WriteToStatusFd(GetProgressFdString("pmconffile", PackageName
.c_str(),
124 StepsDone
, TotalSteps
, ConfMessage
.c_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 WriteToStatusFd(GetProgressFdString("pmstatus", StringSplit(PackageName
, ":")[0].c_str(),
137 StepsDone
, StepsTotal
, pkg_action
.c_str()));
139 if(_config
->FindB("Debug::APT::Progress::PackageManagerFd", false) == true)
140 std::cerr
<< "progress: " << PackageName
<< " " << xStepsDone
141 << " " << xTotalSteps
<< " " << pkg_action
149 PackageManagerProgressDeb822Fd::PackageManagerProgressDeb822Fd(int progress_fd
)
150 : d(NULL
), StepsDone(0), StepsTotal(1)
152 OutStatusFd
= progress_fd
;
154 PackageManagerProgressDeb822Fd::~PackageManagerProgressDeb822Fd() {}
156 void PackageManagerProgressDeb822Fd::WriteToStatusFd(std::string s
)
158 FileFd::Write(OutStatusFd
, s
.c_str(), s
.size());
161 static std::string
GetProgressDeb822String(char const * const status
,
162 char const * const pkg
, unsigned long long Done
,
163 unsigned long long Total
, char const * const msg
)
165 float const progress
{Done
/ static_cast<float>(Total
) * 100};
166 std::ostringstream str
;
167 str
.imbue(std::locale::classic());
169 str
<< "Status: " << status
<< '\n';
171 str
<< "Package: " << pkg
<< '\n';
172 str
<< "Percent: " << std::fixed
<< progress
<< '\n'
173 << "Message: " << msg
<< "\n\n";
177 void PackageManagerProgressDeb822Fd::StartDpkg()
179 // FIXME: use SetCloseExec here once it taught about throwing
180 // exceptions instead of doing _exit(100) on failure
181 fcntl(OutStatusFd
,F_SETFD
,FD_CLOEXEC
);
183 WriteToStatusFd(GetProgressDeb822String("progress", nullptr, StepsDone
, StepsTotal
, _("Running dpkg")));
186 APT_CONST
void PackageManagerProgressDeb822Fd::Stop()
190 void PackageManagerProgressDeb822Fd::Error(std::string PackageName
,
191 unsigned int StepsDone
,
192 unsigned int TotalSteps
,
193 std::string ErrorMessage
)
195 WriteToStatusFd(GetProgressDeb822String("Error", PackageName
.c_str(), StepsDone
, TotalSteps
, ErrorMessage
.c_str()));
198 void PackageManagerProgressDeb822Fd::ConffilePrompt(std::string PackageName
,
199 unsigned int StepsDone
,
200 unsigned int TotalSteps
,
201 std::string ConfMessage
)
203 WriteToStatusFd(GetProgressDeb822String("ConfFile", PackageName
.c_str(), StepsDone
, TotalSteps
, ConfMessage
.c_str()));
207 bool PackageManagerProgressDeb822Fd::StatusChanged(std::string PackageName
,
208 unsigned int xStepsDone
,
209 unsigned int xTotalSteps
,
212 StepsDone
= xStepsDone
;
213 StepsTotal
= xTotalSteps
;
215 WriteToStatusFd(GetProgressDeb822String("progress", PackageName
.c_str(), StepsDone
, StepsTotal
, message
.c_str()));
220 PackageManagerFancy::PackageManagerFancy()
221 : d(NULL
), child_pty(-1)
223 // setup terminal size
224 old_SIGWINCH
= signal(SIGWINCH
, PackageManagerFancy::staticSIGWINCH
);
225 instances
.push_back(this);
227 std::vector
<PackageManagerFancy
*> PackageManagerFancy::instances
;
229 PackageManagerFancy::~PackageManagerFancy()
231 instances
.erase(find(instances
.begin(), instances
.end(), this));
232 signal(SIGWINCH
, old_SIGWINCH
);
235 void PackageManagerFancy::staticSIGWINCH(int signum
)
237 std::vector
<PackageManagerFancy
*>::const_iterator I
;
238 for(I
= instances
.begin(); I
!= instances
.end(); ++I
)
239 (*I
)->HandleSIGWINCH(signum
);
242 PackageManagerFancy::TermSize
243 PackageManagerFancy::GetTerminalSize()
246 PackageManagerFancy::TermSize s
= { 0, 0 };
248 // FIXME: get from "child_pty" instead?
249 if(ioctl(STDOUT_FILENO
, TIOCGWINSZ
, (char *)&win
) != 0)
252 if(_config
->FindB("Debug::InstallProgress::Fancy", false) == true)
253 std::cerr
<< "GetTerminalSize: " << win
.ws_row
<< " x " << win
.ws_col
<< std::endl
;
256 s
.columns
= win
.ws_col
;
260 void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows
)
262 if(_config
->FindB("Debug::InstallProgress::Fancy", false) == true)
263 std::cerr
<< "SetupTerminalScrollArea: " << nr_rows
<< std::endl
;
265 if (unlikely(nr_rows
<= 1))
268 // scroll down a bit to avoid visual glitch when the screen
269 // area shrinks by one row
273 std::cout
<< "\0337";
275 // set scroll region (this will place the cursor in the top left)
276 std::cout
<< "\033[0;" << std::to_string(nr_rows
- 1) << "r";
278 // restore cursor but ensure its inside the scrolling area
279 std::cout
<< "\0338";
280 static const char *move_cursor_up
= "\033[1A";
281 std::cout
<< move_cursor_up
;
283 // ensure its flushed
284 std::flush(std::cout
);
286 // setup tty size to ensure xterm/linux console are working properly too
289 if (ioctl(child_pty
, TIOCGWINSZ
, (char *)&win
) != -1)
291 win
.ws_row
= nr_rows
- 1;
292 ioctl(child_pty
, TIOCSWINSZ
, (char *)&win
);
296 void PackageManagerFancy::HandleSIGWINCH(int)
298 int const nr_terminal_rows
= GetTerminalSize().rows
;
299 SetupTerminalScrollArea(nr_terminal_rows
);
303 void PackageManagerFancy::Start(int a_child_pty
)
305 child_pty
= a_child_pty
;
306 int const nr_terminal_rows
= GetTerminalSize().rows
;
307 SetupTerminalScrollArea(nr_terminal_rows
);
310 void PackageManagerFancy::Stop()
312 int const nr_terminal_rows
= GetTerminalSize().rows
;
313 if (nr_terminal_rows
> 0)
315 SetupTerminalScrollArea(nr_terminal_rows
+ 1);
317 // override the progress line (sledgehammer)
318 static const char* clear_screen_below_cursor
= "\033[J";
319 std::cout
<< clear_screen_below_cursor
;
320 std::flush(std::cout
);
326 PackageManagerFancy::GetTextProgressStr(float Percent
, int OutputSize
)
331 // should we raise a exception here instead?
332 if (Percent
< 0.0 || Percent
> 1.0 || OutputSize
< 3)
335 int BarSize
= OutputSize
- 2; // bar without the leading "[" and trailing "]"
337 for(i
=0; i
< BarSize
*Percent
; i
++)
339 for (/*nothing*/; i
< BarSize
; i
++)
345 bool PackageManagerFancy::StatusChanged(std::string PackageName
,
346 unsigned int StepsDone
,
347 unsigned int TotalSteps
,
348 std::string HumanReadableAction
)
350 if (!PackageManager::StatusChanged(PackageName
, StepsDone
, TotalSteps
,
351 HumanReadableAction
))
354 return DrawStatusLine();
356 bool PackageManagerFancy::DrawStatusLine()
358 PackageManagerFancy::TermSize
const size
= GetTerminalSize();
359 if (unlikely(size
.rows
< 1 || size
.columns
< 1))
362 static std::string save_cursor
= "\0337";
363 static std::string restore_cursor
= "\0338";
366 static std::string set_bg_color
= DeQuoteString(
367 _config
->Find("Dpkg::Progress-Fancy::Progress-fg", "%1b[42m"));
369 static std::string set_fg_color
= DeQuoteString(
370 _config
->Find("Dpkg::Progress-Fancy::Progress-bg", "%1b[30m"));
372 static std::string restore_bg
= "\033[49m";
373 static std::string restore_fg
= "\033[39m";
375 std::cout
<< save_cursor
376 // move cursor position to last row
377 << "\033[" << std::to_string(size
.rows
) << ";0f"
383 std::flush(std::cout
);
385 // draw text progress bar
386 if (_config
->FindB("Dpkg::Progress-Fancy::Progress-Bar", true))
389 float progressbar_size
= size
.columns
- padding
- progress_str
.size();
390 float current_percent
= percentage
/ 100.0;
392 << GetTextProgressStr(current_percent
, progressbar_size
)
394 std::flush(std::cout
);
398 std::cout
<< restore_cursor
;
399 std::flush(std::cout
);
401 last_reported_progress
= percentage
;
406 bool PackageManagerText::StatusChanged(std::string PackageName
,
407 unsigned int StepsDone
,
408 unsigned int TotalSteps
,
409 std::string HumanReadableAction
)
411 if (!PackageManager::StatusChanged(PackageName
, StepsDone
, TotalSteps
, HumanReadableAction
))
414 std::cout
<< progress_str
<< "\r\n";
415 std::flush(std::cout
);
417 last_reported_progress
= percentage
;
422 PackageManagerText::PackageManagerText() : PackageManager(), d(NULL
) {}
423 PackageManagerText::~PackageManagerText() {}
428 } // namespace progress