install-progress.cc 13 KB

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