install-progress.cc 13 KB

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