]> git.saurik.com Git - apt.git/blame - apt-pkg/install-progress.cc
Cleanup includes after running iwyu
[apt.git] / apt-pkg / install-progress.cc
CommitLineData
453b82a3
DK
1#include <config.h>
2
e6ad8031
MV
3#include <apt-pkg/configuration.h>
4#include <apt-pkg/fileutl.h>
31f97d7b 5#include <apt-pkg/strutl.h>
af36becc 6#include <apt-pkg/install-progress.h>
c7ea1eba 7
453b82a3
DK
8#include <signal.h>
9#include <unistd.h>
10#include <iostream>
453b82a3 11#include <vector>
31f97d7b 12#include <sys/ioctl.h>
5e9458e2 13#include <fcntl.h>
5ed88785 14#include <algorithm>
c23e6cd5 15#include <stdio.h>
e96e4e9c 16
453b82a3
DK
17#include <apti18n.h>
18
31f97d7b
MV
19namespace APT {
20namespace Progress {
21
440614ff
DK
22PackageManager::PackageManager() : d(NULL), percentage(0.0), last_reported_progress(-1) {}
23PackageManager::~PackageManager() {}
61f954bf
MV
24
25/* Return a APT::Progress::PackageManager based on the global
26 * apt configuration (i.e. APT::Status-Fd and APT::Status-deb822-Fd)
27 */
bd5f39b3
MV
28PackageManager* PackageManagerProgressFactory()
29{
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);
33
34 APT::Progress::PackageManager *progress = NULL;
35 if (status_deb822_fd > 0)
36 progress = new APT::Progress::PackageManagerProgressDeb822Fd(
37 status_deb822_fd);
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();
45 else
46 progress = new APT::Progress::PackageManager();
47 return progress;
48}
49
65512241 50bool PackageManager::StatusChanged(std::string /*PackageName*/,
e6ad8031
MV
51 unsigned int StepsDone,
52 unsigned int TotalSteps,
65512241 53 std::string /*HumanReadableAction*/)
6c5ae8ed
MV
54{
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);
58
59 if(percentage < (last_reported_progress + reporting_steps))
60 return false;
61
62 return true;
63}
64
e6ad8031 65PackageManagerProgressFd::PackageManagerProgressFd(int progress_fd)
6c55f07a 66 : d(NULL), StepsDone(0), StepsTotal(1)
e6ad8031
MV
67{
68 OutStatusFd = progress_fd;
69}
c8a4ce6c 70PackageManagerProgressFd::~PackageManagerProgressFd() {}
e6ad8031 71
f9935b1c
MV
72void PackageManagerProgressFd::WriteToStatusFd(std::string s)
73{
74 if(OutStatusFd <= 0)
75 return;
76 FileFd::Write(OutStatusFd, s.c_str(), s.size());
77}
78
e45c4617 79void PackageManagerProgressFd::StartDpkg()
e6ad8031 80{
5e9458e2
MV
81 if(OutStatusFd <= 0)
82 return;
83
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);
e6ad8031
MV
87
88 // send status information that we are about to fork dpkg
f9935b1c
MV
89 std::ostringstream status;
90 status << "pmstatus:dpkg-exec:"
91 << (StepsDone/float(StepsTotal)*100.0)
92 << ":" << _("Running dpkg")
93 << std::endl;
94 WriteToStatusFd(status.str());
e6ad8031
MV
95}
96
a02db58f 97APT_CONST void PackageManagerProgressFd::Stop()
e6ad8031 98{
e6ad8031
MV
99}
100
101void PackageManagerProgressFd::Error(std::string PackageName,
102 unsigned int StepsDone,
103 unsigned int TotalSteps,
104 std::string ErrorMessage)
105{
106 std::ostringstream status;
107 status << "pmerror:" << PackageName
108 << ":" << (StepsDone/float(TotalSteps)*100.0)
109 << ":" << ErrorMessage
110 << std::endl;
f9935b1c 111 WriteToStatusFd(status.str());
e6ad8031
MV
112}
113
114void PackageManagerProgressFd::ConffilePrompt(std::string PackageName,
115 unsigned int StepsDone,
116 unsigned int TotalSteps,
117 std::string ConfMessage)
118{
119 std::ostringstream status;
120 status << "pmconffile:" << PackageName
121 << ":" << (StepsDone/float(TotalSteps)*100.0)
122 << ":" << ConfMessage
123 << std::endl;
f9935b1c 124 WriteToStatusFd(status.str());
e6ad8031
MV
125}
126
127
128bool PackageManagerProgressFd::StatusChanged(std::string PackageName,
129 unsigned int xStepsDone,
130 unsigned int xTotalSteps,
131 std::string pkg_action)
132{
133 StepsDone = xStepsDone;
134 StepsTotal = xTotalSteps;
135
136 // build the status str
137 std::ostringstream status;
dd640f3c 138 status << "pmstatus:" << StringSplit(PackageName, ":")[0]
e6ad8031
MV
139 << ":" << (StepsDone/float(StepsTotal)*100.0)
140 << ":" << pkg_action
141 << std::endl;
f9935b1c 142 WriteToStatusFd(status.str());
65dbd5a1
MV
143
144 if(_config->FindB("Debug::APT::Progress::PackageManagerFd", false) == true)
145 std::cerr << "progress: " << PackageName << " " << xStepsDone
146 << " " << xTotalSteps << " " << pkg_action
147 << std::endl;
148
149
e6ad8031
MV
150 return true;
151}
152
c7ea1eba
MV
153
154PackageManagerProgressDeb822Fd::PackageManagerProgressDeb822Fd(int progress_fd)
6c55f07a 155 : d(NULL), StepsDone(0), StepsTotal(1)
c7ea1eba
MV
156{
157 OutStatusFd = progress_fd;
158}
c8a4ce6c 159PackageManagerProgressDeb822Fd::~PackageManagerProgressDeb822Fd() {}
c7ea1eba
MV
160
161void PackageManagerProgressDeb822Fd::WriteToStatusFd(std::string s)
162{
163 FileFd::Write(OutStatusFd, s.c_str(), s.size());
164}
165
790d41f6 166void PackageManagerProgressDeb822Fd::StartDpkg()
c7ea1eba
MV
167{
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);
171
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
177 << std::endl;
178 WriteToStatusFd(status.str());
179}
180
a02db58f 181APT_CONST void PackageManagerProgressDeb822Fd::Stop()
c7ea1eba 182{
c7ea1eba
MV
183}
184
185void PackageManagerProgressDeb822Fd::Error(std::string PackageName,
186 unsigned int StepsDone,
187 unsigned int TotalSteps,
188 std::string ErrorMessage)
189{
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
195 << std::endl;
196 WriteToStatusFd(status.str());
197}
198
199void PackageManagerProgressDeb822Fd::ConffilePrompt(std::string PackageName,
200 unsigned int StepsDone,
201 unsigned int TotalSteps,
202 std::string ConfMessage)
203{
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
209 << std::endl;
210 WriteToStatusFd(status.str());
211}
212
213
214bool PackageManagerProgressDeb822Fd::StatusChanged(std::string PackageName,
215 unsigned int xStepsDone,
216 unsigned int xTotalSteps,
217 std::string message)
218{
219 StepsDone = xStepsDone;
220 StepsTotal = xTotalSteps;
221
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
228 << std::endl;
229 WriteToStatusFd(status.str());
230
231 return true;
232}
233
5ed88785
MV
234
235PackageManagerFancy::PackageManagerFancy()
6c55f07a 236 : d(NULL), child_pty(-1)
5ed88785
MV
237{
238 // setup terminal size
239 old_SIGWINCH = signal(SIGWINCH, PackageManagerFancy::staticSIGWINCH);
240 instances.push_back(this);
241}
242std::vector<PackageManagerFancy*> PackageManagerFancy::instances;
243
244PackageManagerFancy::~PackageManagerFancy()
245{
246 instances.erase(find(instances.begin(), instances.end(), this));
247 signal(SIGWINCH, old_SIGWINCH);
248}
249
250void PackageManagerFancy::staticSIGWINCH(int signum)
251{
252 std::vector<PackageManagerFancy *>::const_iterator I;
9ce3cfc9 253 for(I = instances.begin(); I != instances.end(); ++I)
5ed88785
MV
254 (*I)->HandleSIGWINCH(signum);
255}
256
fa211e2d
MV
257PackageManagerFancy::TermSize
258PackageManagerFancy::GetTerminalSize()
e96e4e9c
MV
259{
260 struct winsize win;
76bd63e2 261 PackageManagerFancy::TermSize s = { 0, 0 };
fa211e2d 262
5ed88785 263 // FIXME: get from "child_pty" instead?
e96e4e9c 264 if(ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&win) != 0)
fa211e2d 265 return s;
5ed88785
MV
266
267 if(_config->FindB("Debug::InstallProgress::Fancy", false) == true)
76bd63e2 268 std::cerr << "GetTerminalSize: " << win.ws_row << " x " << win.ws_col << std::endl;
fa211e2d
MV
269
270 s.rows = win.ws_row;
271 s.columns = win.ws_col;
272 return s;
e96e4e9c 273}
c7ea1eba 274
db78c60c 275void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows)
31f97d7b 276{
5ed88785
MV
277 if(_config->FindB("Debug::InstallProgress::Fancy", false) == true)
278 std::cerr << "SetupTerminalScrollArea: " << nr_rows << std::endl;
279
76bd63e2
DK
280 if (unlikely(nr_rows <= 1))
281 return;
282
31f97d7b
MV
283 // scroll down a bit to avoid visual glitch when the screen
284 // area shrinks by one row
285 std::cout << "\n";
286
287 // save cursor
45b19add 288 std::cout << "\0337";
31f97d7b
MV
289
290 // set scroll region (this will place the cursor in the top left)
5ed88785 291 std::cout << "\033[0;" << nr_rows - 1 << "r";
31f97d7b
MV
292
293 // restore cursor but ensure its inside the scrolling area
45b19add 294 std::cout << "\0338";
31f97d7b
MV
295 static const char *move_cursor_up = "\033[1A";
296 std::cout << move_cursor_up;
db78c60c 297
5ed88785 298 // ensure its flushed
31f97d7b 299 std::flush(std::cout);
31f97d7b 300
5ed88785
MV
301 // setup tty size to ensure xterm/linux console are working properly too
302 // see bug #731738
303 struct winsize win;
76bd63e2
DK
304 if (ioctl(child_pty, TIOCGWINSZ, (char *)&win) != -1)
305 {
306 win.ws_row = nr_rows - 1;
307 ioctl(child_pty, TIOCSWINSZ, (char *)&win);
308 }
e96e4e9c
MV
309}
310
311void PackageManagerFancy::HandleSIGWINCH(int)
312{
76bd63e2 313 int const nr_terminal_rows = GetTerminalSize().rows;
e96e4e9c 314 SetupTerminalScrollArea(nr_terminal_rows);
76bd63e2 315 DrawStatusLine();
31f97d7b
MV
316}
317
5ed88785 318void PackageManagerFancy::Start(int a_child_pty)
31f97d7b 319{
5ed88785 320 child_pty = a_child_pty;
76bd63e2
DK
321 int const nr_terminal_rows = GetTerminalSize().rows;
322 SetupTerminalScrollArea(nr_terminal_rows);
31f97d7b
MV
323}
324
a22fdebf 325void PackageManagerFancy::Stop()
31f97d7b 326{
76bd63e2 327 int const nr_terminal_rows = GetTerminalSize().rows;
db78c60c
MV
328 if (nr_terminal_rows > 0)
329 {
330 SetupTerminalScrollArea(nr_terminal_rows + 1);
31f97d7b 331
db78c60c
MV
332 // override the progress line (sledgehammer)
333 static const char* clear_screen_below_cursor = "\033[J";
334 std::cout << clear_screen_below_cursor;
335 }
5ed88785 336 child_pty = -1;
31f97d7b
MV
337}
338
fa211e2d
MV
339std::string
340PackageManagerFancy::GetTextProgressStr(float Percent, int OutputSize)
341{
342 std::string output;
343 int i;
344
345 // should we raise a exception here instead?
346 if (Percent < 0.0 || Percent > 1.0 || OutputSize < 3)
347 return output;
348
349 int BarSize = OutputSize - 2; // bar without the leading "[" and trailing "]"
350 output += "[";
351 for(i=0; i < BarSize*Percent; i++)
352 output += "#";
353 for (/*nothing*/; i < BarSize; i++)
354 output += ".";
355 output += "]";
356 return output;
357}
358
6c5ae8ed 359bool PackageManagerFancy::StatusChanged(std::string PackageName,
31f97d7b 360 unsigned int StepsDone,
e6ad8031
MV
361 unsigned int TotalSteps,
362 std::string HumanReadableAction)
31f97d7b 363{
e6ad8031
MV
364 if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps,
365 HumanReadableAction))
6c5ae8ed 366 return false;
31f97d7b 367
76bd63e2
DK
368 return DrawStatusLine();
369}
370bool PackageManagerFancy::DrawStatusLine()
371{
372 PackageManagerFancy::TermSize const size = GetTerminalSize();
373 if (unlikely(size.rows < 1 || size.columns < 1))
374 return false;
31f97d7b 375
45b19add
JM
376 static std::string save_cursor = "\0337";
377 static std::string restore_cursor = "\0338";
453b82a3 378
c34d1202
MV
379 // green
380 static std::string set_bg_color = DeQuoteString(
381 _config->Find("Dpkg::Progress-Fancy::Progress-fg", "%1b[42m"));
382 // black
383 static std::string set_fg_color = DeQuoteString(
384 _config->Find("Dpkg::Progress-Fancy::Progress-bg", "%1b[30m"));
453b82a3
DK
385
386 static std::string restore_bg = "\033[49m";
387 static std::string restore_fg = "\033[39m";
388
31f97d7b
MV
389 std::cout << save_cursor
390 // move cursor position to last row
fa211e2d 391 << "\033[" << size.rows << ";0f"
31f97d7b
MV
392 << set_bg_color
393 << set_fg_color
394 << progress_str
31f97d7b
MV
395 << restore_bg
396 << restore_fg;
397 std::flush(std::cout);
fa211e2d
MV
398
399 // draw text progress bar
400 if (_config->FindB("Dpkg::Progress-Fancy::Progress-Bar", true))
401 {
402 int padding = 4;
403 float progressbar_size = size.columns - padding - progress_str.size();
76bd63e2 404 float current_percent = percentage / 100.0;
fa211e2d
MV
405 std::cout << " "
406 << GetTextProgressStr(current_percent, progressbar_size)
407 << " ";
408 std::flush(std::cout);
409 }
410
411 // restore
412 std::cout << restore_cursor;
413 std::flush(std::cout);
414
31f97d7b 415 last_reported_progress = percentage;
6c5ae8ed
MV
416
417 return true;
31f97d7b
MV
418}
419
6c5ae8ed 420bool PackageManagerText::StatusChanged(std::string PackageName,
31f97d7b 421 unsigned int StepsDone,
e6ad8031
MV
422 unsigned int TotalSteps,
423 std::string HumanReadableAction)
31f97d7b 424{
e6ad8031 425 if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps, HumanReadableAction))
6c5ae8ed 426 return false;
31f97d7b
MV
427
428 std::cout << progress_str << "\r\n";
429 std::flush(std::cout);
430
431 last_reported_progress = percentage;
6c5ae8ed
MV
432
433 return true;
31f97d7b
MV
434}
435
6c55f07a 436PackageManagerText::PackageManagerText() : PackageManager(), d(NULL) {}
c8a4ce6c
DK
437PackageManagerText::~PackageManagerText() {}
438
439
31f97d7b 440
c7ea1eba 441
d3e8fbb3
DK
442} // namespace progress
443} // namespace apt