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