install-progress.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 <stdio.h>
  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. int PackageManagerFancy::GetNumberTerminalRows()
  190. {
  191. struct winsize win;
  192. if(ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&win) != 0)
  193. return -1;
  194. return win.ws_row;
  195. }
  196. void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows)
  197. {
  198. // scroll down a bit to avoid visual glitch when the screen
  199. // area shrinks by one row
  200. std::cout << "\n";
  201. // save cursor
  202. std::cout << "\033[s";
  203. // set scroll region (this will place the cursor in the top left)
  204. std::cout << "\033[1;" << nr_rows - 1 << "r";
  205. // restore cursor but ensure its inside the scrolling area
  206. std::cout << "\033[u";
  207. static const char *move_cursor_up = "\033[1A";
  208. std::cout << move_cursor_up;
  209. // setup env for (hopefully!) ncurses
  210. string s;
  211. strprintf(s, "%i", nr_rows);
  212. setenv("LINES", s.c_str(), 1);
  213. std::flush(std::cout);
  214. }
  215. PackageManagerFancy::PackageManagerFancy()
  216. {
  217. // setup terminal size
  218. old_SIGWINCH = signal(SIGWINCH, HandleSIGWINCH);
  219. }
  220. PackageManagerFancy::~PackageManagerFancy()
  221. {
  222. signal(SIGWINCH, old_SIGWINCH);
  223. }
  224. void PackageManagerFancy::HandleSIGWINCH(int)
  225. {
  226. int nr_terminal_rows = GetNumberTerminalRows();
  227. SetupTerminalScrollArea(nr_terminal_rows);
  228. }
  229. void PackageManagerFancy::Start()
  230. {
  231. int nr_terminal_rows = GetNumberTerminalRows();
  232. if (nr_terminal_rows > 0)
  233. SetupTerminalScrollArea(nr_terminal_rows);
  234. }
  235. void PackageManagerFancy::Stop()
  236. {
  237. int nr_terminal_rows = GetNumberTerminalRows();
  238. if (nr_terminal_rows > 0)
  239. {
  240. SetupTerminalScrollArea(nr_terminal_rows + 1);
  241. // override the progress line (sledgehammer)
  242. static const char* clear_screen_below_cursor = "\033[J";
  243. std::cout << clear_screen_below_cursor;
  244. }
  245. }
  246. bool PackageManagerFancy::StatusChanged(std::string PackageName,
  247. unsigned int StepsDone,
  248. unsigned int TotalSteps,
  249. std::string HumanReadableAction)
  250. {
  251. if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps,
  252. HumanReadableAction))
  253. return false;
  254. int row = GetNumberTerminalRows();
  255. static string save_cursor = "\033[s";
  256. static string restore_cursor = "\033[u";
  257. static string set_bg_color = "\033[42m"; // green
  258. static string set_fg_color = "\033[30m"; // black
  259. static string restore_bg = "\033[49m";
  260. static string restore_fg = "\033[39m";
  261. std::cout << save_cursor
  262. // move cursor position to last row
  263. << "\033[" << row << ";0f"
  264. << set_bg_color
  265. << set_fg_color
  266. << progress_str
  267. << restore_cursor
  268. << restore_bg
  269. << restore_fg;
  270. std::flush(std::cout);
  271. last_reported_progress = percentage;
  272. return true;
  273. }
  274. bool PackageManagerText::StatusChanged(std::string PackageName,
  275. unsigned int StepsDone,
  276. unsigned int TotalSteps,
  277. std::string HumanReadableAction)
  278. {
  279. if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps, HumanReadableAction))
  280. return false;
  281. std::cout << progress_str << "\r\n";
  282. std::flush(std::cout);
  283. last_reported_progress = percentage;
  284. return true;
  285. }
  286. }; // namespace progress
  287. }; // namespace apt