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>
24 PackageManager::PackageManager() : d(NULL
), percentage(0.0), last_reported_progress(-1) {}
25 PackageManager::~PackageManager() {}
27 /* Return a APT::Progress::PackageManager based on the global
28 * apt configuration (i.e. APT::Status-Fd and APT::Status-deb822-Fd)
30 PackageManager
* PackageManagerProgressFactory()
32 // select the right progress
33 int status_fd
= _config
->FindI("APT::Status-Fd", -1);
34 int status_deb822_fd
= _config
->FindI("APT::Status-deb822-Fd", -1);
36 APT::Progress::PackageManager
*progress
= NULL
;
37 if (status_deb822_fd
> 0)
38 progress
= new APT::Progress::PackageManagerProgressDeb822Fd(
40 else if (status_fd
> 0)
41 progress
= new APT::Progress::PackageManagerProgressFd(status_fd
);
42 else if(_config
->FindB("Dpkg::Progress-Fancy", false) == true)
43 progress
= new APT::Progress::PackageManagerFancy();
44 else if (_config
->FindB("Dpkg::Progress",
45 _config
->FindB("DpkgPM::Progress", false)) == true)
46 progress
= new APT::Progress::PackageManagerText();
48 progress
= new APT::Progress::PackageManager();
52 bool PackageManager::StatusChanged(std::string
/*PackageName*/,
53 unsigned int StepsDone
,
54 unsigned int TotalSteps
,
55 std::string
/*HumanReadableAction*/)
57 int reporting_steps
= _config
->FindI("DpkgPM::Reporting-Steps", 1);
58 percentage
= StepsDone
/(float)TotalSteps
* 100.0;
59 strprintf(progress_str
, _("Progress: [%3i%%]"), (int)percentage
);
61 if(percentage
< (last_reported_progress
+ reporting_steps
))
67 PackageManagerProgressFd::PackageManagerProgressFd(int progress_fd
)
68 : d(NULL
), StepsDone(0), StepsTotal(1)
70 OutStatusFd
= progress_fd
;
72 PackageManagerProgressFd::~PackageManagerProgressFd() {}
74 void PackageManagerProgressFd::WriteToStatusFd(std::string s
)
78 FileFd::Write(OutStatusFd
, s
.c_str(), s
.size());
81 static std::string
GetProgressFdString(char const * const status
,
82 char const * const pkg
, unsigned long long Done
,
83 unsigned long long Total
, char const * const msg
)
85 float const progress
{Done
/ static_cast<float>(Total
) * 100};
86 std::ostringstream str
;
87 str
.imbue(std::locale("C.UTF-8"));
89 str
<< status
<< ':' << pkg
<< ':' << std::fixed
<< progress
<< ':' << msg
<< '\n';
93 void PackageManagerProgressFd::StartDpkg()
98 // FIXME: use SetCloseExec here once it taught about throwing
99 // exceptions instead of doing _exit(100) on failure
100 fcntl(OutStatusFd
,F_SETFD
,FD_CLOEXEC
);
102 // send status information that we are about to fork dpkg
103 WriteToStatusFd(GetProgressFdString("pmstatus", "dpkg-exec", StepsDone
, StepsTotal
, _("Running dpkg")));
106 APT_CONST
void PackageManagerProgressFd::Stop()
110 void PackageManagerProgressFd::Error(std::string PackageName
,
111 unsigned int StepsDone
,
112 unsigned int TotalSteps
,
113 std::string ErrorMessage
)
115 WriteToStatusFd(GetProgressFdString("pmerror", PackageName
.c_str(),
116 StepsDone
, TotalSteps
, ErrorMessage
.c_str()));
119 void PackageManagerProgressFd::ConffilePrompt(std::string PackageName
,
120 unsigned int StepsDone
,
121 unsigned int TotalSteps
,
122 std::string ConfMessage
)
124 WriteToStatusFd(GetProgressFdString("pmconffile", PackageName
.c_str(),
125 StepsDone
, TotalSteps
, ConfMessage
.c_str()));
129 bool PackageManagerProgressFd::StatusChanged(std::string PackageName
,
130 unsigned int xStepsDone
,
131 unsigned int xTotalSteps
,
132 std::string pkg_action
)
134 StepsDone
= xStepsDone
;
135 StepsTotal
= xTotalSteps
;
137 WriteToStatusFd(GetProgressFdString("pmstatus", StringSplit(PackageName
, ":")[0].c_str(),
138 StepsDone
, StepsTotal
, pkg_action
.c_str()));
140 if(_config
->FindB("Debug::APT::Progress::PackageManagerFd", false) == true)
141 std::cerr
<< "progress: " << PackageName
<< " " << xStepsDone
142 << " " << xTotalSteps
<< " " << pkg_action
150 PackageManagerProgressDeb822Fd::PackageManagerProgressDeb822Fd(int progress_fd
)
151 : d(NULL
), StepsDone(0), StepsTotal(1)
153 OutStatusFd
= progress_fd
;
155 PackageManagerProgressDeb822Fd::~PackageManagerProgressDeb822Fd() {}
157 void PackageManagerProgressDeb822Fd::WriteToStatusFd(std::string s
)
159 FileFd::Write(OutStatusFd
, s
.c_str(), s
.size());
162 static std::string
GetProgressDeb822String(char const * const status
,
163 char const * const pkg
, unsigned long long Done
,
164 unsigned long long Total
, char const * const msg
)
166 float const progress
{Done
/ static_cast<float>(Total
) * 100};
167 std::ostringstream str
;
168 str
.imbue(std::locale("C.UTF-8"));
170 str
<< "Status: " << status
<< '\n';
172 str
<< "Package: " << pkg
<< '\n';
173 str
<< "Percent: " << std::fixed
<< progress
<< '\n'
174 << "Message: " << msg
<< "\n\n";
178 void PackageManagerProgressDeb822Fd::StartDpkg()
180 // FIXME: use SetCloseExec here once it taught about throwing
181 // exceptions instead of doing _exit(100) on failure
182 fcntl(OutStatusFd
,F_SETFD
,FD_CLOEXEC
);
184 WriteToStatusFd(GetProgressDeb822String("progress", nullptr, StepsDone
, StepsTotal
, _("Running dpkg")));
187 APT_CONST
void PackageManagerProgressDeb822Fd::Stop()
191 void PackageManagerProgressDeb822Fd::Error(std::string PackageName
,
192 unsigned int StepsDone
,
193 unsigned int TotalSteps
,
194 std::string ErrorMessage
)
196 WriteToStatusFd(GetProgressDeb822String("Error", PackageName
.c_str(), StepsDone
, TotalSteps
, ErrorMessage
.c_str()));
199 void PackageManagerProgressDeb822Fd::ConffilePrompt(std::string PackageName
,
200 unsigned int StepsDone
,
201 unsigned int TotalSteps
,
202 std::string ConfMessage
)
204 WriteToStatusFd(GetProgressDeb822String("ConfFile", PackageName
.c_str(), StepsDone
, TotalSteps
, ConfMessage
.c_str()));
208 bool PackageManagerProgressDeb822Fd::StatusChanged(std::string PackageName
,
209 unsigned int xStepsDone
,
210 unsigned int xTotalSteps
,
213 StepsDone
= xStepsDone
;
214 StepsTotal
= xTotalSteps
;
216 WriteToStatusFd(GetProgressDeb822String("progress", PackageName
.c_str(), StepsDone
, StepsTotal
, message
.c_str()));
221 PackageManagerFancy::PackageManagerFancy()
222 : d(NULL
), child_pty(-1)
224 // setup terminal size
225 old_SIGWINCH
= signal(SIGWINCH
, PackageManagerFancy::staticSIGWINCH
);
226 instances
.push_back(this);
228 std::vector
<PackageManagerFancy
*> PackageManagerFancy::instances
;
230 PackageManagerFancy::~PackageManagerFancy()
232 instances
.erase(find(instances
.begin(), instances
.end(), this));
233 signal(SIGWINCH
, old_SIGWINCH
);
236 void PackageManagerFancy::staticSIGWINCH(int signum
)
238 std::vector
<PackageManagerFancy
*>::const_iterator I
;
239 for(I
= instances
.begin(); I
!= instances
.end(); ++I
)
240 (*I
)->HandleSIGWINCH(signum
);
243 PackageManagerFancy::TermSize
244 PackageManagerFancy::GetTerminalSize()
247 PackageManagerFancy::TermSize s
= { 0, 0 };
249 // FIXME: get from "child_pty" instead?
250 if(ioctl(STDOUT_FILENO
, TIOCGWINSZ
, (char *)&win
) != 0)
253 if(_config
->FindB("Debug::InstallProgress::Fancy", false) == true)
254 std::cerr
<< "GetTerminalSize: " << win
.ws_row
<< " x " << win
.ws_col
<< std::endl
;
257 s
.columns
= win
.ws_col
;
261 void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows
)
263 if(_config
->FindB("Debug::InstallProgress::Fancy", false) == true)
264 std::cerr
<< "SetupTerminalScrollArea: " << nr_rows
<< std::endl
;
266 if (unlikely(nr_rows
<= 1))
269 // scroll down a bit to avoid visual glitch when the screen
270 // area shrinks by one row
274 std::cout
<< "\0337";
276 // set scroll region (this will place the cursor in the top left)
277 std::cout
<< "\033[0;" << std::to_string(nr_rows
- 1) << "r";
279 // restore cursor but ensure its inside the scrolling area
280 std::cout
<< "\0338";
281 static const char *move_cursor_up
= "\033[1A";
282 std::cout
<< move_cursor_up
;
284 // ensure its flushed
285 std::flush(std::cout
);
287 // setup tty size to ensure xterm/linux console are working properly too
290 if (ioctl(child_pty
, TIOCGWINSZ
, (char *)&win
) != -1)
292 win
.ws_row
= nr_rows
- 1;
293 ioctl(child_pty
, TIOCSWINSZ
, (char *)&win
);
297 void PackageManagerFancy::HandleSIGWINCH(int)
299 int const nr_terminal_rows
= GetTerminalSize().rows
;
300 SetupTerminalScrollArea(nr_terminal_rows
);
304 void PackageManagerFancy::Start(int a_child_pty
)
306 child_pty
= a_child_pty
;
307 int const nr_terminal_rows
= GetTerminalSize().rows
;
308 SetupTerminalScrollArea(nr_terminal_rows
);
311 void PackageManagerFancy::Stop()
313 int const nr_terminal_rows
= GetTerminalSize().rows
;
314 if (nr_terminal_rows
> 0)
316 SetupTerminalScrollArea(nr_terminal_rows
+ 1);
318 // override the progress line (sledgehammer)
319 static const char* clear_screen_below_cursor
= "\033[J";
320 std::cout
<< clear_screen_below_cursor
;
321 std::flush(std::cout
);
327 PackageManagerFancy::GetTextProgressStr(float Percent
, int OutputSize
)
330 if (unlikely(OutputSize
< 3))
333 int const BarSize
= OutputSize
- 2; // bar without the leading "[" and trailing "]"
334 int const BarDone
= std::max(0, std::min(BarSize
, static_cast<int>(std::floor(Percent
* BarSize
))));
336 std::fill_n(std::fill_n(std::back_inserter(output
), BarDone
, '#'), BarSize
- BarDone
, '.');
341 bool PackageManagerFancy::StatusChanged(std::string PackageName
,
342 unsigned int StepsDone
,
343 unsigned int TotalSteps
,
344 std::string HumanReadableAction
)
346 if (!PackageManager::StatusChanged(PackageName
, StepsDone
, TotalSteps
,
347 HumanReadableAction
))
350 return DrawStatusLine();
352 bool PackageManagerFancy::DrawStatusLine()
354 PackageManagerFancy::TermSize
const size
= GetTerminalSize();
355 if (unlikely(size
.rows
< 1 || size
.columns
< 1))
358 static std::string save_cursor
= "\0337";
359 static std::string restore_cursor
= "\0338";
362 static std::string set_bg_color
= DeQuoteString(
363 _config
->Find("Dpkg::Progress-Fancy::Progress-fg", "%1b[42m"));
365 static std::string set_fg_color
= DeQuoteString(
366 _config
->Find("Dpkg::Progress-Fancy::Progress-bg", "%1b[30m"));
368 static std::string restore_bg
= "\033[49m";
369 static std::string restore_fg
= "\033[39m";
371 std::cout
<< save_cursor
372 // move cursor position to last row
373 << "\033[" << std::to_string(size
.rows
) << ";0f"
379 std::flush(std::cout
);
381 // draw text progress bar
382 if (_config
->FindB("Dpkg::Progress-Fancy::Progress-Bar", true))
385 float progressbar_size
= size
.columns
- padding
- progress_str
.size();
386 float current_percent
= percentage
/ 100.0;
388 << GetTextProgressStr(current_percent
, progressbar_size
)
390 std::flush(std::cout
);
394 std::cout
<< restore_cursor
;
395 std::flush(std::cout
);
397 last_reported_progress
= percentage
;
402 bool PackageManagerText::StatusChanged(std::string PackageName
,
403 unsigned int StepsDone
,
404 unsigned int TotalSteps
,
405 std::string HumanReadableAction
)
407 if (!PackageManager::StatusChanged(PackageName
, StepsDone
, TotalSteps
, HumanReadableAction
))
410 std::cout
<< progress_str
<< "\r\n";
411 std::flush(std::cout
);
413 last_reported_progress
= percentage
;
418 PackageManagerText::PackageManagerText() : PackageManager(), d(NULL
) {}
419 PackageManagerText::~PackageManagerText() {}
424 } // namespace progress