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 void PackageManagerProgressFd::StartDpkg()
85 // FIXME: use SetCloseExec here once it taught about throwing
86 // exceptions instead of doing _exit(100) on failure
87 fcntl(OutStatusFd
,F_SETFD
,FD_CLOEXEC
);
89 // send status information that we are about to fork dpkg
91 strprintf(status
, "pmstatus:dpkg-exec:%.4f:%s\n",
92 (StepsDone
/float(StepsTotal
)*100.0), _("Running dpkg"));
93 WriteToStatusFd(std::move(status
));
96 APT_CONST
void PackageManagerProgressFd::Stop()
100 void PackageManagerProgressFd::Error(std::string PackageName
,
101 unsigned int StepsDone
,
102 unsigned int TotalSteps
,
103 std::string ErrorMessage
)
106 strprintf(status
, "pmerror:%s:%.4f:%s\n", PackageName
.c_str(),
107 (StepsDone
/float(TotalSteps
)*100.0), ErrorMessage
.c_str());
108 WriteToStatusFd(std::move(status
));
111 void PackageManagerProgressFd::ConffilePrompt(std::string PackageName
,
112 unsigned int StepsDone
,
113 unsigned int TotalSteps
,
114 std::string ConfMessage
)
117 strprintf(status
, "pmconffile:%s:%.4f:%s\n", PackageName
.c_str(),
118 (StepsDone
/float(TotalSteps
)*100.0), ConfMessage
.c_str());
119 WriteToStatusFd(std::move(status
));
123 bool PackageManagerProgressFd::StatusChanged(std::string PackageName
,
124 unsigned int xStepsDone
,
125 unsigned int xTotalSteps
,
126 std::string pkg_action
)
128 StepsDone
= xStepsDone
;
129 StepsTotal
= xTotalSteps
;
131 // build the status str
133 strprintf(status
, "pmstatus:%s:%.4f:%s\n", StringSplit(PackageName
, ":")[0].c_str(),
134 (StepsDone
/float(StepsTotal
)*100.0), pkg_action
.c_str());
135 WriteToStatusFd(std::move(status
));
137 if(_config
->FindB("Debug::APT::Progress::PackageManagerFd", false) == true)
138 std::cerr
<< "progress: " << PackageName
<< " " << xStepsDone
139 << " " << xTotalSteps
<< " " << pkg_action
147 PackageManagerProgressDeb822Fd::PackageManagerProgressDeb822Fd(int progress_fd
)
148 : d(NULL
), StepsDone(0), StepsTotal(1)
150 OutStatusFd
= progress_fd
;
152 PackageManagerProgressDeb822Fd::~PackageManagerProgressDeb822Fd() {}
154 void PackageManagerProgressDeb822Fd::WriteToStatusFd(std::string s
)
156 FileFd::Write(OutStatusFd
, s
.c_str(), s
.size());
159 void PackageManagerProgressDeb822Fd::StartDpkg()
161 // FIXME: use SetCloseExec here once it taught about throwing
162 // exceptions instead of doing _exit(100) on failure
163 fcntl(OutStatusFd
,F_SETFD
,FD_CLOEXEC
);
165 // send status information that we are about to fork dpkg
167 strprintf(status
, "Status: %s\nPercent: %.4f\nMessage: %s\n\n", "progress",
168 (StepsDone
/float(StepsTotal
)*100.0), _("Running dpkg"));
169 WriteToStatusFd(std::move(status
));
172 APT_CONST
void PackageManagerProgressDeb822Fd::Stop()
176 void PackageManagerProgressDeb822Fd::Error(std::string PackageName
,
177 unsigned int StepsDone
,
178 unsigned int TotalSteps
,
179 std::string ErrorMessage
)
182 strprintf(status
, "Status: %s\nPackage: %s\nPercent: %.4f\nMessage: %s\n\n", "Error",
183 PackageName
.c_str(), (StepsDone
/float(TotalSteps
)*100.0), ErrorMessage
.c_str());
184 WriteToStatusFd(std::move(status
));
187 void PackageManagerProgressDeb822Fd::ConffilePrompt(std::string PackageName
,
188 unsigned int StepsDone
,
189 unsigned int TotalSteps
,
190 std::string ConfMessage
)
193 strprintf(status
, "Status: %s\nPackage: %s\nPercent: %.4f\nMessage: %s\n\n", "ConfFile",
194 PackageName
.c_str(), (StepsDone
/float(TotalSteps
)*100.0), ConfMessage
.c_str());
195 WriteToStatusFd(std::move(status
));
199 bool PackageManagerProgressDeb822Fd::StatusChanged(std::string PackageName
,
200 unsigned int xStepsDone
,
201 unsigned int xTotalSteps
,
204 StepsDone
= xStepsDone
;
205 StepsTotal
= xTotalSteps
;
208 strprintf(status
, "Status: %s\nPackage: %s\nPercent: %.4f\nMessage: %s\n\n", "progress",
209 PackageName
.c_str(), (StepsDone
/float(StepsTotal
)*100.0), message
.c_str());
210 WriteToStatusFd(std::move(status
));
215 PackageManagerFancy::PackageManagerFancy()
216 : d(NULL
), child_pty(-1)
218 // setup terminal size
219 old_SIGWINCH
= signal(SIGWINCH
, PackageManagerFancy::staticSIGWINCH
);
220 instances
.push_back(this);
222 std::vector
<PackageManagerFancy
*> PackageManagerFancy::instances
;
224 PackageManagerFancy::~PackageManagerFancy()
226 instances
.erase(find(instances
.begin(), instances
.end(), this));
227 signal(SIGWINCH
, old_SIGWINCH
);
230 void PackageManagerFancy::staticSIGWINCH(int signum
)
232 std::vector
<PackageManagerFancy
*>::const_iterator I
;
233 for(I
= instances
.begin(); I
!= instances
.end(); ++I
)
234 (*I
)->HandleSIGWINCH(signum
);
237 PackageManagerFancy::TermSize
238 PackageManagerFancy::GetTerminalSize()
241 PackageManagerFancy::TermSize s
= { 0, 0 };
243 // FIXME: get from "child_pty" instead?
244 if(ioctl(STDOUT_FILENO
, TIOCGWINSZ
, (char *)&win
) != 0)
247 if(_config
->FindB("Debug::InstallProgress::Fancy", false) == true)
248 std::cerr
<< "GetTerminalSize: " << win
.ws_row
<< " x " << win
.ws_col
<< std::endl
;
251 s
.columns
= win
.ws_col
;
255 void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows
)
257 if(_config
->FindB("Debug::InstallProgress::Fancy", false) == true)
258 std::cerr
<< "SetupTerminalScrollArea: " << nr_rows
<< std::endl
;
260 if (unlikely(nr_rows
<= 1))
263 // scroll down a bit to avoid visual glitch when the screen
264 // area shrinks by one row
268 std::cout
<< "\0337";
270 // set scroll region (this will place the cursor in the top left)
271 std::cout
<< "\033[0;" << std::to_string(nr_rows
- 1) << "r";
273 // restore cursor but ensure its inside the scrolling area
274 std::cout
<< "\0338";
275 static const char *move_cursor_up
= "\033[1A";
276 std::cout
<< move_cursor_up
;
278 // ensure its flushed
279 std::flush(std::cout
);
281 // setup tty size to ensure xterm/linux console are working properly too
284 if (ioctl(child_pty
, TIOCGWINSZ
, (char *)&win
) != -1)
286 win
.ws_row
= nr_rows
- 1;
287 ioctl(child_pty
, TIOCSWINSZ
, (char *)&win
);
291 void PackageManagerFancy::HandleSIGWINCH(int)
293 int const nr_terminal_rows
= GetTerminalSize().rows
;
294 SetupTerminalScrollArea(nr_terminal_rows
);
298 void PackageManagerFancy::Start(int a_child_pty
)
300 child_pty
= a_child_pty
;
301 int const nr_terminal_rows
= GetTerminalSize().rows
;
302 SetupTerminalScrollArea(nr_terminal_rows
);
305 void PackageManagerFancy::Stop()
307 int const nr_terminal_rows
= GetTerminalSize().rows
;
308 if (nr_terminal_rows
> 0)
310 SetupTerminalScrollArea(nr_terminal_rows
+ 1);
312 // override the progress line (sledgehammer)
313 static const char* clear_screen_below_cursor
= "\033[J";
314 std::cout
<< clear_screen_below_cursor
;
315 std::flush(std::cout
);
321 PackageManagerFancy::GetTextProgressStr(float Percent
, int OutputSize
)
326 // should we raise a exception here instead?
327 if (Percent
< 0.0 || Percent
> 1.0 || OutputSize
< 3)
330 int BarSize
= OutputSize
- 2; // bar without the leading "[" and trailing "]"
332 for(i
=0; i
< BarSize
*Percent
; i
++)
334 for (/*nothing*/; i
< BarSize
; i
++)
340 bool PackageManagerFancy::StatusChanged(std::string PackageName
,
341 unsigned int StepsDone
,
342 unsigned int TotalSteps
,
343 std::string HumanReadableAction
)
345 if (!PackageManager::StatusChanged(PackageName
, StepsDone
, TotalSteps
,
346 HumanReadableAction
))
349 return DrawStatusLine();
351 bool PackageManagerFancy::DrawStatusLine()
353 PackageManagerFancy::TermSize
const size
= GetTerminalSize();
354 if (unlikely(size
.rows
< 1 || size
.columns
< 1))
357 static std::string save_cursor
= "\0337";
358 static std::string restore_cursor
= "\0338";
361 static std::string set_bg_color
= DeQuoteString(
362 _config
->Find("Dpkg::Progress-Fancy::Progress-fg", "%1b[42m"));
364 static std::string set_fg_color
= DeQuoteString(
365 _config
->Find("Dpkg::Progress-Fancy::Progress-bg", "%1b[30m"));
367 static std::string restore_bg
= "\033[49m";
368 static std::string restore_fg
= "\033[39m";
370 std::cout
<< save_cursor
371 // move cursor position to last row
372 << "\033[" << std::to_string(size
.rows
) << ";0f"
378 std::flush(std::cout
);
380 // draw text progress bar
381 if (_config
->FindB("Dpkg::Progress-Fancy::Progress-Bar", true))
384 float progressbar_size
= size
.columns
- padding
- progress_str
.size();
385 float current_percent
= percentage
/ 100.0;
387 << GetTextProgressStr(current_percent
, progressbar_size
)
389 std::flush(std::cout
);
393 std::cout
<< restore_cursor
;
394 std::flush(std::cout
);
396 last_reported_progress
= percentage
;
401 bool PackageManagerText::StatusChanged(std::string PackageName
,
402 unsigned int StepsDone
,
403 unsigned int TotalSteps
,
404 std::string HumanReadableAction
)
406 if (!PackageManager::StatusChanged(PackageName
, StepsDone
, TotalSteps
, HumanReadableAction
))
409 std::cout
<< progress_str
<< "\r\n";
410 std::flush(std::cout
);
412 last_reported_progress
= percentage
;
417 PackageManagerText::PackageManagerText() : PackageManager(), d(NULL
) {}
418 PackageManagerText::~PackageManagerText() {}
423 } // namespace progress