install-progress.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. /* 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. : StepsDone(0), StepsTotal(1)
  56. {
  57. OutStatusFd = progress_fd;
  58. }
  59. void PackageManagerProgressFd::WriteToStatusFd(std::string s)
  60. {
  61. if(OutStatusFd <= 0)
  62. return;
  63. FileFd::Write(OutStatusFd, s.c_str(), s.size());
  64. }
  65. void PackageManagerProgressFd::StartDpkg()
  66. {
  67. if(OutStatusFd <= 0)
  68. return;
  69. // FIXME: use SetCloseExec here once it taught about throwing
  70. // exceptions instead of doing _exit(100) on failure
  71. fcntl(OutStatusFd,F_SETFD,FD_CLOEXEC);
  72. // send status information that we are about to fork dpkg
  73. std::ostringstream status;
  74. status << "pmstatus:dpkg-exec:"
  75. << (StepsDone/float(StepsTotal)*100.0)
  76. << ":" << _("Running dpkg")
  77. << std::endl;
  78. WriteToStatusFd(status.str());
  79. }
  80. APT_CONST void PackageManagerProgressFd::Stop()
  81. {
  82. }
  83. void PackageManagerProgressFd::Error(std::string PackageName,
  84. unsigned int StepsDone,
  85. unsigned int TotalSteps,
  86. std::string ErrorMessage)
  87. {
  88. std::ostringstream status;
  89. status << "pmerror:" << PackageName
  90. << ":" << (StepsDone/float(TotalSteps)*100.0)
  91. << ":" << ErrorMessage
  92. << std::endl;
  93. WriteToStatusFd(status.str());
  94. }
  95. void PackageManagerProgressFd::ConffilePrompt(std::string PackageName,
  96. unsigned int StepsDone,
  97. unsigned int TotalSteps,
  98. std::string ConfMessage)
  99. {
  100. std::ostringstream status;
  101. status << "pmconffile:" << PackageName
  102. << ":" << (StepsDone/float(TotalSteps)*100.0)
  103. << ":" << ConfMessage
  104. << std::endl;
  105. WriteToStatusFd(status.str());
  106. }
  107. bool PackageManagerProgressFd::StatusChanged(std::string PackageName,
  108. unsigned int xStepsDone,
  109. unsigned int xTotalSteps,
  110. std::string pkg_action)
  111. {
  112. StepsDone = xStepsDone;
  113. StepsTotal = xTotalSteps;
  114. // build the status str
  115. std::ostringstream status;
  116. status << "pmstatus:" << StringSplit(PackageName, ":")[0]
  117. << ":" << (StepsDone/float(StepsTotal)*100.0)
  118. << ":" << pkg_action
  119. << std::endl;
  120. WriteToStatusFd(status.str());
  121. if(_config->FindB("Debug::APT::Progress::PackageManagerFd", false) == true)
  122. std::cerr << "progress: " << PackageName << " " << xStepsDone
  123. << " " << xTotalSteps << " " << pkg_action
  124. << std::endl;
  125. return true;
  126. }
  127. PackageManagerProgressDeb822Fd::PackageManagerProgressDeb822Fd(int progress_fd)
  128. : StepsDone(0), StepsTotal(1)
  129. {
  130. OutStatusFd = progress_fd;
  131. }
  132. void PackageManagerProgressDeb822Fd::WriteToStatusFd(std::string s)
  133. {
  134. FileFd::Write(OutStatusFd, s.c_str(), s.size());
  135. }
  136. void PackageManagerProgressDeb822Fd::StartDpkg()
  137. {
  138. // FIXME: use SetCloseExec here once it taught about throwing
  139. // exceptions instead of doing _exit(100) on failure
  140. fcntl(OutStatusFd,F_SETFD,FD_CLOEXEC);
  141. // send status information that we are about to fork dpkg
  142. std::ostringstream status;
  143. status << "Status: " << "progress" << std::endl
  144. << "Percent: " << (StepsDone/float(StepsTotal)*100.0) << std::endl
  145. << "Message: " << _("Running dpkg") << std::endl
  146. << std::endl;
  147. WriteToStatusFd(status.str());
  148. }
  149. APT_CONST void PackageManagerProgressDeb822Fd::Stop()
  150. {
  151. }
  152. void PackageManagerProgressDeb822Fd::Error(std::string PackageName,
  153. unsigned int StepsDone,
  154. unsigned int TotalSteps,
  155. std::string ErrorMessage)
  156. {
  157. std::ostringstream status;
  158. status << "Status: " << "Error" << std::endl
  159. << "Package:" << PackageName << std::endl
  160. << "Percent: " << (StepsDone/float(TotalSteps)*100.0) << std::endl
  161. << "Message: " << ErrorMessage << std::endl
  162. << std::endl;
  163. WriteToStatusFd(status.str());
  164. }
  165. void PackageManagerProgressDeb822Fd::ConffilePrompt(std::string PackageName,
  166. unsigned int StepsDone,
  167. unsigned int TotalSteps,
  168. std::string ConfMessage)
  169. {
  170. std::ostringstream status;
  171. status << "Status: " << "ConfFile" << std::endl
  172. << "Package:" << PackageName << std::endl
  173. << "Percent: " << (StepsDone/float(TotalSteps)*100.0) << std::endl
  174. << "Message: " << ConfMessage << std::endl
  175. << std::endl;
  176. WriteToStatusFd(status.str());
  177. }
  178. bool PackageManagerProgressDeb822Fd::StatusChanged(std::string PackageName,
  179. unsigned int xStepsDone,
  180. unsigned int xTotalSteps,
  181. std::string message)
  182. {
  183. StepsDone = xStepsDone;
  184. StepsTotal = xTotalSteps;
  185. // build the status str
  186. std::ostringstream status;
  187. status << "Status: " << "progress" << std::endl
  188. << "Package: " << PackageName << std::endl
  189. << "Percent: " << (StepsDone/float(StepsTotal)*100.0) << std::endl
  190. << "Message: " << message << std::endl
  191. << std::endl;
  192. WriteToStatusFd(status.str());
  193. return true;
  194. }
  195. PackageManagerFancy::PackageManagerFancy()
  196. : child_pty(-1)
  197. {
  198. // setup terminal size
  199. old_SIGWINCH = signal(SIGWINCH, PackageManagerFancy::staticSIGWINCH);
  200. instances.push_back(this);
  201. }
  202. std::vector<PackageManagerFancy*> PackageManagerFancy::instances;
  203. PackageManagerFancy::~PackageManagerFancy()
  204. {
  205. instances.erase(find(instances.begin(), instances.end(), this));
  206. signal(SIGWINCH, old_SIGWINCH);
  207. }
  208. void PackageManagerFancy::staticSIGWINCH(int signum)
  209. {
  210. std::vector<PackageManagerFancy *>::const_iterator I;
  211. for(I = instances.begin(); I != instances.end(); ++I)
  212. (*I)->HandleSIGWINCH(signum);
  213. }
  214. int PackageManagerFancy::GetNumberTerminalRows()
  215. {
  216. struct winsize win;
  217. // FIXME: get from "child_pty" instead?
  218. if(ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&win) != 0)
  219. return -1;
  220. if(_config->FindB("Debug::InstallProgress::Fancy", false) == true)
  221. std::cerr << "GetNumberTerminalRows: " << win.ws_row << std::endl;
  222. return win.ws_row;
  223. }
  224. void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows)
  225. {
  226. if(_config->FindB("Debug::InstallProgress::Fancy", false) == true)
  227. std::cerr << "SetupTerminalScrollArea: " << nr_rows << std::endl;
  228. // scroll down a bit to avoid visual glitch when the screen
  229. // area shrinks by one row
  230. std::cout << "\n";
  231. // save cursor
  232. std::cout << "\033[s";
  233. // set scroll region (this will place the cursor in the top left)
  234. std::cout << "\033[0;" << nr_rows - 1 << "r";
  235. // restore cursor but ensure its inside the scrolling area
  236. std::cout << "\033[u";
  237. static const char *move_cursor_up = "\033[1A";
  238. std::cout << move_cursor_up;
  239. // ensure its flushed
  240. std::flush(std::cout);
  241. // setup tty size to ensure xterm/linux console are working properly too
  242. // see bug #731738
  243. struct winsize win;
  244. ioctl(child_pty, TIOCGWINSZ, (char *)&win);
  245. win.ws_row = nr_rows - 1;
  246. ioctl(child_pty, TIOCSWINSZ, (char *)&win);
  247. }
  248. void PackageManagerFancy::HandleSIGWINCH(int)
  249. {
  250. int nr_terminal_rows = GetNumberTerminalRows();
  251. SetupTerminalScrollArea(nr_terminal_rows);
  252. }
  253. void PackageManagerFancy::Start(int a_child_pty)
  254. {
  255. child_pty = a_child_pty;
  256. int nr_terminal_rows = GetNumberTerminalRows();
  257. if (nr_terminal_rows > 0)
  258. SetupTerminalScrollArea(nr_terminal_rows);
  259. }
  260. void PackageManagerFancy::Stop()
  261. {
  262. int nr_terminal_rows = GetNumberTerminalRows();
  263. if (nr_terminal_rows > 0)
  264. {
  265. SetupTerminalScrollArea(nr_terminal_rows + 1);
  266. // override the progress line (sledgehammer)
  267. static const char* clear_screen_below_cursor = "\033[J";
  268. std::cout << clear_screen_below_cursor;
  269. }
  270. child_pty = -1;
  271. }
  272. bool PackageManagerFancy::StatusChanged(std::string PackageName,
  273. unsigned int StepsDone,
  274. unsigned int TotalSteps,
  275. std::string HumanReadableAction)
  276. {
  277. if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps,
  278. HumanReadableAction))
  279. return false;
  280. int row = GetNumberTerminalRows();
  281. static std::string save_cursor = "\033[s";
  282. static std::string restore_cursor = "\033[u";
  283. static std::string set_bg_color = "\033[42m"; // green
  284. static std::string set_fg_color = "\033[30m"; // black
  285. static std::string restore_bg = "\033[49m";
  286. static std::string restore_fg = "\033[39m";
  287. std::cout << save_cursor
  288. // move cursor position to last row
  289. << "\033[" << row << ";0f"
  290. << set_bg_color
  291. << set_fg_color
  292. << progress_str
  293. << restore_cursor
  294. << restore_bg
  295. << restore_fg;
  296. std::flush(std::cout);
  297. last_reported_progress = percentage;
  298. return true;
  299. }
  300. bool PackageManagerText::StatusChanged(std::string PackageName,
  301. unsigned int StepsDone,
  302. unsigned int TotalSteps,
  303. std::string HumanReadableAction)
  304. {
  305. if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps, HumanReadableAction))
  306. return false;
  307. std::cout << progress_str << "\r\n";
  308. std::flush(std::cout);
  309. last_reported_progress = percentage;
  310. return true;
  311. }
  312. } // namespace progress
  313. } // namespace apt