You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

427 lines
13 KiB

  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. PackageManagerFancy::TermSize
  215. PackageManagerFancy::GetTerminalSize()
  216. {
  217. struct winsize win;
  218. PackageManagerFancy::TermSize s;
  219. // FIXME: get from "child_pty" instead?
  220. if(ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&win) != 0)
  221. return s;
  222. if(_config->FindB("Debug::InstallProgress::Fancy", false) == true)
  223. std::cerr << "GetTerminalSize: " << win.ws_row << std::endl;
  224. s.rows = win.ws_row;
  225. s.columns = win.ws_col;
  226. return s;
  227. }
  228. void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows)
  229. {
  230. if(_config->FindB("Debug::InstallProgress::Fancy", false) == true)
  231. std::cerr << "SetupTerminalScrollArea: " << nr_rows << std::endl;
  232. // scroll down a bit to avoid visual glitch when the screen
  233. // area shrinks by one row
  234. std::cout << "\n";
  235. // save cursor
  236. std::cout << "\033[s";
  237. // set scroll region (this will place the cursor in the top left)
  238. std::cout << "\033[0;" << nr_rows - 1 << "r";
  239. // restore cursor but ensure its inside the scrolling area
  240. std::cout << "\033[u";
  241. static const char *move_cursor_up = "\033[1A";
  242. std::cout << move_cursor_up;
  243. // ensure its flushed
  244. std::flush(std::cout);
  245. // setup tty size to ensure xterm/linux console are working properly too
  246. // see bug #731738
  247. struct winsize win;
  248. ioctl(child_pty, TIOCGWINSZ, (char *)&win);
  249. win.ws_row = nr_rows - 1;
  250. ioctl(child_pty, TIOCSWINSZ, (char *)&win);
  251. }
  252. void PackageManagerFancy::HandleSIGWINCH(int)
  253. {
  254. int nr_terminal_rows = GetTerminalSize().rows;
  255. SetupTerminalScrollArea(nr_terminal_rows);
  256. }
  257. void PackageManagerFancy::Start(int a_child_pty)
  258. {
  259. child_pty = a_child_pty;
  260. int nr_terminal_rows = GetTerminalSize().rows;
  261. if (nr_terminal_rows > 0)
  262. SetupTerminalScrollArea(nr_terminal_rows);
  263. }
  264. void PackageManagerFancy::Stop()
  265. {
  266. int nr_terminal_rows = GetTerminalSize().rows;
  267. if (nr_terminal_rows > 0)
  268. {
  269. SetupTerminalScrollArea(nr_terminal_rows + 1);
  270. // override the progress line (sledgehammer)
  271. static const char* clear_screen_below_cursor = "\033[J";
  272. std::cout << clear_screen_below_cursor;
  273. }
  274. child_pty = -1;
  275. }
  276. std::string
  277. PackageManagerFancy::GetTextProgressStr(float Percent, int OutputSize)
  278. {
  279. std::string output;
  280. int i;
  281. // should we raise a exception here instead?
  282. if (Percent < 0.0 || Percent > 1.0 || OutputSize < 3)
  283. return output;
  284. int BarSize = OutputSize - 2; // bar without the leading "[" and trailing "]"
  285. output += "[";
  286. for(i=0; i < BarSize*Percent; i++)
  287. output += "#";
  288. for (/*nothing*/; i < BarSize; i++)
  289. output += ".";
  290. output += "]";
  291. return output;
  292. }
  293. bool PackageManagerFancy::StatusChanged(std::string PackageName,
  294. unsigned int StepsDone,
  295. unsigned int TotalSteps,
  296. std::string HumanReadableAction)
  297. {
  298. if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps,
  299. HumanReadableAction))
  300. return false;
  301. PackageManagerFancy::TermSize size = GetTerminalSize();
  302. static std::string save_cursor = "\033[s";
  303. static std::string restore_cursor = "\033[u";
  304. // green
  305. static std::string set_bg_color = DeQuoteString(
  306. _config->Find("Dpkg::Progress-Fancy::Progress-fg", "%1b[42m"));
  307. // black
  308. static std::string set_fg_color = DeQuoteString(
  309. _config->Find("Dpkg::Progress-Fancy::Progress-bg", "%1b[30m"));
  310. static std::string restore_bg = "\033[49m";
  311. static std::string restore_fg = "\033[39m";
  312. std::cout << save_cursor
  313. // move cursor position to last row
  314. << "\033[" << size.rows << ";0f"
  315. << set_bg_color
  316. << set_fg_color
  317. << progress_str
  318. << restore_bg
  319. << restore_fg;
  320. std::flush(std::cout);
  321. // draw text progress bar
  322. if (_config->FindB("Dpkg::Progress-Fancy::Progress-Bar", true))
  323. {
  324. int padding = 4;
  325. float progressbar_size = size.columns - padding - progress_str.size();
  326. float current_percent = (float)StepsDone/(float)TotalSteps;
  327. std::cout << " "
  328. << GetTextProgressStr(current_percent, progressbar_size)
  329. << " ";
  330. std::flush(std::cout);
  331. }
  332. // restore
  333. std::cout << restore_cursor;
  334. std::flush(std::cout);
  335. last_reported_progress = percentage;
  336. return true;
  337. }
  338. bool PackageManagerText::StatusChanged(std::string PackageName,
  339. unsigned int StepsDone,
  340. unsigned int TotalSteps,
  341. std::string HumanReadableAction)
  342. {
  343. if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps, HumanReadableAction))
  344. return false;
  345. std::cout << progress_str << "\r\n";
  346. std::flush(std::cout);
  347. last_reported_progress = percentage;
  348. return true;
  349. }
  350. } // namespace progress
  351. } // namespace apt