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