install-progress.cc 12 KB

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