]> git.saurik.com Git - apt.git/blame - apt-pkg/install-progress.cc
CMake: Fix uninitialized variables
[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>
1cb04707 17#include <cmath>
e96e4e9c 18
453b82a3
DK
19#include <apti18n.h>
20
31f97d7b
MV
21namespace APT {
22namespace Progress {
23
440614ff
DK
24PackageManager::PackageManager() : d(NULL), percentage(0.0), last_reported_progress(-1) {}
25PackageManager::~PackageManager() {}
61f954bf
MV
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 */
bd5f39b3
MV
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
65512241 52bool PackageManager::StatusChanged(std::string /*PackageName*/,
e6ad8031
MV
53 unsigned int StepsDone,
54 unsigned int TotalSteps,
65512241 55 std::string /*HumanReadableAction*/)
6c5ae8ed
MV
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
e6ad8031 67PackageManagerProgressFd::PackageManagerProgressFd(int progress_fd)
6c55f07a 68 : d(NULL), StepsDone(0), StepsTotal(1)
e6ad8031
MV
69{
70 OutStatusFd = progress_fd;
71}
c8a4ce6c 72PackageManagerProgressFd::~PackageManagerProgressFd() {}
e6ad8031 73
f9935b1c
MV
74void PackageManagerProgressFd::WriteToStatusFd(std::string s)
75{
76 if(OutStatusFd <= 0)
77 return;
78 FileFd::Write(OutStatusFd, s.c_str(), s.size());
79}
80
e45c4617 81void PackageManagerProgressFd::StartDpkg()
e6ad8031 82{
5e9458e2
MV
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);
e6ad8031
MV
89
90 // send status information that we are about to fork dpkg
b58e2c7c
DK
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));
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{
b58e2c7c
DK
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));
e6ad8031
MV
110}
111
112void PackageManagerProgressFd::ConffilePrompt(std::string PackageName,
113 unsigned int StepsDone,
114 unsigned int TotalSteps,
115 std::string ConfMessage)
116{
b58e2c7c
DK
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));
e6ad8031
MV
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
b58e2c7c
DK
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));
65dbd5a1
MV
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
e6ad8031
MV
144 return true;
145}
146
c7ea1eba
MV
147
148PackageManagerProgressDeb822Fd::PackageManagerProgressDeb822Fd(int progress_fd)
6c55f07a 149 : d(NULL), StepsDone(0), StepsTotal(1)
c7ea1eba
MV
150{
151 OutStatusFd = progress_fd;
152}
c8a4ce6c 153PackageManagerProgressDeb822Fd::~PackageManagerProgressDeb822Fd() {}
c7ea1eba
MV
154
155void PackageManagerProgressDeb822Fd::WriteToStatusFd(std::string s)
156{
157 FileFd::Write(OutStatusFd, s.c_str(), s.size());
158}
159
790d41f6 160void PackageManagerProgressDeb822Fd::StartDpkg()
c7ea1eba
MV
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
b58e2c7c
DK
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));
c7ea1eba
MV
171}
172
a02db58f 173APT_CONST void PackageManagerProgressDeb822Fd::Stop()
c7ea1eba 174{
c7ea1eba
MV
175}
176
177void PackageManagerProgressDeb822Fd::Error(std::string PackageName,
178 unsigned int StepsDone,
179 unsigned int TotalSteps,
180 std::string ErrorMessage)
181{
b58e2c7c
DK
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));
c7ea1eba
MV
186}
187
188void PackageManagerProgressDeb822Fd::ConffilePrompt(std::string PackageName,
189 unsigned int StepsDone,
190 unsigned int TotalSteps,
191 std::string ConfMessage)
192{
b58e2c7c
DK
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));
c7ea1eba
MV
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
b58e2c7c
DK
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));
c7ea1eba
MV
212 return true;
213}
214
5ed88785
MV
215
216PackageManagerFancy::PackageManagerFancy()
6c55f07a 217 : d(NULL), child_pty(-1)
5ed88785
MV
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;
9ce3cfc9 234 for(I = instances.begin(); I != instances.end(); ++I)
5ed88785
MV
235 (*I)->HandleSIGWINCH(signum);
236}
237
fa211e2d
MV
238PackageManagerFancy::TermSize
239PackageManagerFancy::GetTerminalSize()
e96e4e9c
MV
240{
241 struct winsize win;
76bd63e2 242 PackageManagerFancy::TermSize s = { 0, 0 };
fa211e2d 243
5ed88785 244 // FIXME: get from "child_pty" instead?
e96e4e9c 245 if(ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&win) != 0)
fa211e2d 246 return s;
5ed88785
MV
247
248 if(_config->FindB("Debug::InstallProgress::Fancy", false) == true)
76bd63e2 249 std::cerr << "GetTerminalSize: " << win.ws_row << " x " << win.ws_col << std::endl;
fa211e2d
MV
250
251 s.rows = win.ws_row;
252 s.columns = win.ws_col;
253 return s;
e96e4e9c 254}
c7ea1eba 255
db78c60c 256void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows)
31f97d7b 257{
5ed88785
MV
258 if(_config->FindB("Debug::InstallProgress::Fancy", false) == true)
259 std::cerr << "SetupTerminalScrollArea: " << nr_rows << std::endl;
260
76bd63e2
DK
261 if (unlikely(nr_rows <= 1))
262 return;
263
31f97d7b
MV
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
45b19add 269 std::cout << "\0337";
31f97d7b
MV
270
271 // set scroll region (this will place the cursor in the top left)
b58e2c7c 272 std::cout << "\033[0;" << std::to_string(nr_rows - 1) << "r";
31f97d7b
MV
273
274 // restore cursor but ensure its inside the scrolling area
45b19add 275 std::cout << "\0338";
31f97d7b
MV
276 static const char *move_cursor_up = "\033[1A";
277 std::cout << move_cursor_up;
db78c60c 278
5ed88785 279 // ensure its flushed
31f97d7b 280 std::flush(std::cout);
31f97d7b 281
5ed88785
MV
282 // setup tty size to ensure xterm/linux console are working properly too
283 // see bug #731738
284 struct winsize win;
76bd63e2
DK
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 }
e96e4e9c
MV
290}
291
292void PackageManagerFancy::HandleSIGWINCH(int)
293{
76bd63e2 294 int const nr_terminal_rows = GetTerminalSize().rows;
e96e4e9c 295 SetupTerminalScrollArea(nr_terminal_rows);
76bd63e2 296 DrawStatusLine();
31f97d7b
MV
297}
298
5ed88785 299void PackageManagerFancy::Start(int a_child_pty)
31f97d7b 300{
5ed88785 301 child_pty = a_child_pty;
76bd63e2
DK
302 int const nr_terminal_rows = GetTerminalSize().rows;
303 SetupTerminalScrollArea(nr_terminal_rows);
31f97d7b
MV
304}
305
a22fdebf 306void PackageManagerFancy::Stop()
31f97d7b 307{
76bd63e2 308 int const nr_terminal_rows = GetTerminalSize().rows;
db78c60c
MV
309 if (nr_terminal_rows > 0)
310 {
311 SetupTerminalScrollArea(nr_terminal_rows + 1);
31f97d7b 312
db78c60c
MV
313 // override the progress line (sledgehammer)
314 static const char* clear_screen_below_cursor = "\033[J";
315 std::cout << clear_screen_below_cursor;
915f4ac6 316 std::flush(std::cout);
db78c60c 317 }
5ed88785 318 child_pty = -1;
31f97d7b
MV
319}
320
fa211e2d
MV
321std::string
322PackageManagerFancy::GetTextProgressStr(float Percent, int OutputSize)
323{
324 std::string output;
1cb04707 325 if (unlikely(OutputSize < 3))
fa211e2d 326 return output;
1cb04707
DK
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("]");
fa211e2d
MV
333 return output;
334}
335
6c5ae8ed 336bool PackageManagerFancy::StatusChanged(std::string PackageName,
31f97d7b 337 unsigned int StepsDone,
e6ad8031
MV
338 unsigned int TotalSteps,
339 std::string HumanReadableAction)
31f97d7b 340{
e6ad8031
MV
341 if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps,
342 HumanReadableAction))
6c5ae8ed 343 return false;
31f97d7b 344
76bd63e2
DK
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;
31f97d7b 352
45b19add
JM
353 static std::string save_cursor = "\0337";
354 static std::string restore_cursor = "\0338";
453b82a3 355
c34d1202
MV
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"));
453b82a3
DK
362
363 static std::string restore_bg = "\033[49m";
364 static std::string restore_fg = "\033[39m";
365
31f97d7b
MV
366 std::cout << save_cursor
367 // move cursor position to last row
b58e2c7c 368 << "\033[" << std::to_string(size.rows) << ";0f"
31f97d7b
MV
369 << set_bg_color
370 << set_fg_color
371 << progress_str
31f97d7b
MV
372 << restore_bg
373 << restore_fg;
374 std::flush(std::cout);
fa211e2d
MV
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();
76bd63e2 381 float current_percent = percentage / 100.0;
fa211e2d
MV
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
31f97d7b 392 last_reported_progress = percentage;
6c5ae8ed
MV
393
394 return true;
31f97d7b
MV
395}
396
6c5ae8ed 397bool PackageManagerText::StatusChanged(std::string PackageName,
31f97d7b 398 unsigned int StepsDone,
e6ad8031
MV
399 unsigned int TotalSteps,
400 std::string HumanReadableAction)
31f97d7b 401{
e6ad8031 402 if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps, HumanReadableAction))
6c5ae8ed 403 return false;
31f97d7b
MV
404
405 std::cout << progress_str << "\r\n";
406 std::flush(std::cout);
407
408 last_reported_progress = percentage;
6c5ae8ed
MV
409
410 return true;
31f97d7b
MV
411}
412
6c55f07a 413PackageManagerText::PackageManagerText() : PackageManager(), d(NULL) {}
c8a4ce6c
DK
414PackageManagerText::~PackageManagerText() {}
415
416
31f97d7b 417
c7ea1eba 418
d3e8fbb3
DK
419} // namespace progress
420} // namespace apt