install-progress.cc 13 KB

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