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>
13 #include <sys/ioctl.h>
25 /* Return a APT::Progress::PackageManager based on the global
26 * apt configuration (i.e. APT::Status-Fd and APT::Status-deb822-Fd)
28 PackageManager
* PackageManagerProgressFactory()
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);
34 APT::Progress::PackageManager
*progress
= NULL
;
35 if (status_deb822_fd
> 0)
36 progress
= new APT::Progress::PackageManagerProgressDeb822Fd(
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();
46 progress
= new APT::Progress::PackageManager();
50 bool PackageManager::StatusChanged(std::string
/*PackageName*/,
51 unsigned int StepsDone
,
52 unsigned int TotalSteps
,
53 std::string
/*HumanReadableAction*/)
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
);
59 if(percentage
< (last_reported_progress
+ reporting_steps
))
65 PackageManagerProgressFd::PackageManagerProgressFd(int progress_fd
)
66 : StepsDone(0), StepsTotal(1)
68 OutStatusFd
= progress_fd
;
71 void PackageManagerProgressFd::WriteToStatusFd(std::string s
)
75 FileFd::Write(OutStatusFd
, s
.c_str(), s
.size());
78 void PackageManagerProgressFd::StartDpkg()
83 // FIXME: use SetCloseExec here once it taught about throwing
84 // exceptions instead of doing _exit(100) on failure
85 fcntl(OutStatusFd
,F_SETFD
,FD_CLOEXEC
);
87 // send status information that we are about to fork dpkg
88 std::ostringstream status
;
89 status
<< "pmstatus:dpkg-exec:"
90 << (StepsDone
/float(StepsTotal
)*100.0)
91 << ":" << _("Running dpkg")
93 WriteToStatusFd(status
.str());
96 void PackageManagerProgressFd::Stop()
100 void PackageManagerProgressFd::Error(std::string PackageName
,
101 unsigned int StepsDone
,
102 unsigned int TotalSteps
,
103 std::string ErrorMessage
)
105 std::ostringstream status
;
106 status
<< "pmerror:" << PackageName
107 << ":" << (StepsDone
/float(TotalSteps
)*100.0)
108 << ":" << ErrorMessage
110 WriteToStatusFd(status
.str());
113 void PackageManagerProgressFd::ConffilePrompt(std::string PackageName
,
114 unsigned int StepsDone
,
115 unsigned int TotalSteps
,
116 std::string ConfMessage
)
118 std::ostringstream status
;
119 status
<< "pmconffile:" << PackageName
120 << ":" << (StepsDone
/float(TotalSteps
)*100.0)
121 << ":" << ConfMessage
123 WriteToStatusFd(status
.str());
127 bool PackageManagerProgressFd::StatusChanged(std::string PackageName
,
128 unsigned int xStepsDone
,
129 unsigned int xTotalSteps
,
130 std::string pkg_action
)
132 StepsDone
= xStepsDone
;
133 StepsTotal
= xTotalSteps
;
135 // build the status str
136 std::ostringstream status
;
137 status
<< "pmstatus:" << StringSplit(PackageName
, ":")[0]
138 << ":" << (StepsDone
/float(StepsTotal
)*100.0)
141 WriteToStatusFd(status
.str());
143 if(_config
->FindB("Debug::APT::Progress::PackageManagerFd", false) == true)
144 std::cerr
<< "progress: " << PackageName
<< " " << xStepsDone
145 << " " << xTotalSteps
<< " " << pkg_action
153 PackageManagerProgressDeb822Fd::PackageManagerProgressDeb822Fd(int progress_fd
)
154 : StepsDone(0), StepsTotal(1)
156 OutStatusFd
= progress_fd
;
159 void PackageManagerProgressDeb822Fd::WriteToStatusFd(std::string s
)
161 FileFd::Write(OutStatusFd
, s
.c_str(), s
.size());
164 void PackageManagerProgressDeb822Fd::StartDpkg()
166 // FIXME: use SetCloseExec here once it taught about throwing
167 // exceptions instead of doing _exit(100) on failure
168 fcntl(OutStatusFd
,F_SETFD
,FD_CLOEXEC
);
170 // send status information that we are about to fork dpkg
171 std::ostringstream status
;
172 status
<< "Status: " << "progress" << std::endl
173 << "Percent: " << (StepsDone
/float(StepsTotal
)*100.0) << std::endl
174 << "Message: " << _("Running dpkg") << std::endl
176 WriteToStatusFd(status
.str());
179 void PackageManagerProgressDeb822Fd::Stop()
183 void PackageManagerProgressDeb822Fd::Error(std::string PackageName
,
184 unsigned int StepsDone
,
185 unsigned int TotalSteps
,
186 std::string ErrorMessage
)
188 std::ostringstream status
;
189 status
<< "Status: " << "Error" << std::endl
190 << "Package:" << PackageName
<< std::endl
191 << "Percent: " << (StepsDone
/float(TotalSteps
)*100.0) << std::endl
192 << "Message: " << ErrorMessage
<< std::endl
194 WriteToStatusFd(status
.str());
197 void PackageManagerProgressDeb822Fd::ConffilePrompt(std::string PackageName
,
198 unsigned int StepsDone
,
199 unsigned int TotalSteps
,
200 std::string ConfMessage
)
202 std::ostringstream status
;
203 status
<< "Status: " << "ConfFile" << std::endl
204 << "Package:" << PackageName
<< std::endl
205 << "Percent: " << (StepsDone
/float(TotalSteps
)*100.0) << std::endl
206 << "Message: " << ConfMessage
<< std::endl
208 WriteToStatusFd(status
.str());
212 bool PackageManagerProgressDeb822Fd::StatusChanged(std::string PackageName
,
213 unsigned int xStepsDone
,
214 unsigned int xTotalSteps
,
217 StepsDone
= xStepsDone
;
218 StepsTotal
= xTotalSteps
;
220 // build the status str
221 std::ostringstream status
;
222 status
<< "Status: " << "progress" << std::endl
223 << "Package: " << PackageName
<< std::endl
224 << "Percent: " << (StepsDone
/float(StepsTotal
)*100.0) << std::endl
225 << "Message: " << message
<< std::endl
227 WriteToStatusFd(status
.str());
233 PackageManagerFancy::PackageManagerFancy()
236 // setup terminal size
237 old_SIGWINCH
= signal(SIGWINCH
, PackageManagerFancy::staticSIGWINCH
);
238 instances
.push_back(this);
240 std::vector
<PackageManagerFancy
*> PackageManagerFancy::instances
;
242 PackageManagerFancy::~PackageManagerFancy()
244 instances
.erase(find(instances
.begin(), instances
.end(), this));
245 signal(SIGWINCH
, old_SIGWINCH
);
248 void PackageManagerFancy::staticSIGWINCH(int signum
)
250 std::vector
<PackageManagerFancy
*>::const_iterator I
;
251 for(I
= instances
.begin(); I
!= instances
.end(); ++I
)
252 (*I
)->HandleSIGWINCH(signum
);
255 int PackageManagerFancy::GetNumberTerminalRows()
258 // FIXME: get from "child_pty" instead?
259 if(ioctl(STDOUT_FILENO
, TIOCGWINSZ
, (char *)&win
) != 0)
262 if(_config
->FindB("Debug::InstallProgress::Fancy", false) == true)
263 std::cerr
<< "GetNumberTerminalRows: " << win
.ws_row
<< std::endl
;
268 void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows
)
270 if(_config
->FindB("Debug::InstallProgress::Fancy", false) == true)
271 std::cerr
<< "SetupTerminalScrollArea: " << nr_rows
<< std::endl
;
273 // scroll down a bit to avoid visual glitch when the screen
274 // area shrinks by one row
278 std::cout
<< "\033[s";
280 // set scroll region (this will place the cursor in the top left)
281 std::cout
<< "\033[0;" << nr_rows
- 1 << "r";
283 // restore cursor but ensure its inside the scrolling area
284 std::cout
<< "\033[u";
285 static const char *move_cursor_up
= "\033[1A";
286 std::cout
<< move_cursor_up
;
288 // ensure its flushed
289 std::flush(std::cout
);
291 // setup tty size to ensure xterm/linux console are working properly too
294 ioctl(child_pty
, TIOCGWINSZ
, (char *)&win
);
295 win
.ws_row
= nr_rows
- 1;
296 ioctl(child_pty
, TIOCSWINSZ
, (char *)&win
);
299 void PackageManagerFancy::HandleSIGWINCH(int)
301 int nr_terminal_rows
= GetNumberTerminalRows();
302 SetupTerminalScrollArea(nr_terminal_rows
);
305 void PackageManagerFancy::Start(int a_child_pty
)
307 child_pty
= a_child_pty
;
308 int nr_terminal_rows
= GetNumberTerminalRows();
309 if (nr_terminal_rows
> 0)
310 SetupTerminalScrollArea(nr_terminal_rows
);
313 void PackageManagerFancy::Stop()
315 int nr_terminal_rows
= GetNumberTerminalRows();
316 if (nr_terminal_rows
> 0)
318 SetupTerminalScrollArea(nr_terminal_rows
+ 1);
320 // override the progress line (sledgehammer)
321 static const char* clear_screen_below_cursor
= "\033[J";
322 std::cout
<< clear_screen_below_cursor
;
327 bool PackageManagerFancy::StatusChanged(std::string PackageName
,
328 unsigned int StepsDone
,
329 unsigned int TotalSteps
,
330 std::string HumanReadableAction
)
332 if (!PackageManager::StatusChanged(PackageName
, StepsDone
, TotalSteps
,
333 HumanReadableAction
))
336 int row
= GetNumberTerminalRows();
338 static std::string save_cursor
= "\033[s";
339 static std::string restore_cursor
= "\033[u";
341 static std::string set_bg_color
= "\033[42m"; // green
342 static std::string set_fg_color
= "\033[30m"; // black
344 static std::string restore_bg
= "\033[49m";
345 static std::string restore_fg
= "\033[39m";
347 std::cout
<< save_cursor
348 // move cursor position to last row
349 << "\033[" << row
<< ";0f"
356 std::flush(std::cout
);
357 last_reported_progress
= percentage
;
362 bool PackageManagerText::StatusChanged(std::string PackageName
,
363 unsigned int StepsDone
,
364 unsigned int TotalSteps
,
365 std::string HumanReadableAction
)
367 if (!PackageManager::StatusChanged(PackageName
, StepsDone
, TotalSteps
, HumanReadableAction
))
370 std::cout
<< progress_str
<< "\r\n";
371 std::flush(std::cout
);
373 last_reported_progress
= percentage
;
380 } // namespace progress