apt-cdrom.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-cdrom.cc,v 1.45 2003/11/19 23:50:51 mdz Exp $
  4. /* ######################################################################
  5. APT CDROM - Tool for handling APT's CDROM database.
  6. Currently the only option is 'add' which will take the current CD
  7. in the drive and add it into the database.
  8. ##################################################################### */
  9. /*}}}*/
  10. // Include Files /*{{{*/
  11. #include <apt-pkg/cmndline.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/init.h>
  14. #include <apt-pkg/fileutl.h>
  15. #include <apt-pkg/progress.h>
  16. #include <apt-pkg/cdromutl.h>
  17. #include <apt-pkg/strutl.h>
  18. #include <apt-pkg/acquire.h>
  19. #include <apt-pkg/acquire-item.h>
  20. #include <apt-pkg/cdrom.h>
  21. #include <config.h>
  22. #include <apti18n.h>
  23. //#include "indexcopy.h"
  24. #include <locale.h>
  25. #include <iostream>
  26. #include <fstream>
  27. #include <vector>
  28. #include <algorithm>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #include <dirent.h>
  32. #include <unistd.h>
  33. #include <stdio.h>
  34. /*}}}*/
  35. using namespace std;
  36. class pkgCdromTextStatus : public pkgCdromStatus /*{{{*/
  37. {
  38. protected:
  39. OpTextProgress Progress;
  40. void Prompt(const char *Text);
  41. string PromptLine(const char *Text);
  42. bool AskCdromName(string &name);
  43. public:
  44. virtual void Update(string text, int current);
  45. virtual bool ChangeCdrom();
  46. virtual OpProgress* GetOpProgress();
  47. };
  48. void pkgCdromTextStatus::Prompt(const char *Text)
  49. {
  50. char C;
  51. cout << Text << ' ' << flush;
  52. read(STDIN_FILENO,&C,1);
  53. if (C != '\n')
  54. cout << endl;
  55. }
  56. string pkgCdromTextStatus::PromptLine(const char *Text)
  57. {
  58. cout << Text << ':' << endl;
  59. string Res;
  60. getline(cin,Res);
  61. return Res;
  62. }
  63. bool pkgCdromTextStatus::AskCdromName(string &name)
  64. {
  65. cout << _("Please provide a name for this Disc, such as 'Debian 5.0.3 Disk 1'") << flush;
  66. name = PromptLine("");
  67. return true;
  68. }
  69. void pkgCdromTextStatus::Update(string text, int current)
  70. {
  71. if(text.size() > 0)
  72. cout << text << flush;
  73. }
  74. bool pkgCdromTextStatus::ChangeCdrom()
  75. {
  76. Prompt(_("Please insert a Disc in the drive and press enter"));
  77. return true;
  78. }
  79. OpProgress* pkgCdromTextStatus::GetOpProgress()
  80. {
  81. return &Progress;
  82. };
  83. /*}}}*/
  84. // DoAdd - Add a new CDROM /*{{{*/
  85. // ---------------------------------------------------------------------
  86. /* This does the main add bit.. We show some status and things. The
  87. sequence is to mount/umount the CD, Ident it then scan it for package
  88. files and reduce that list. Then we copy over the package files and
  89. verify them. Then rewrite the database files */
  90. bool DoAdd(CommandLine &)
  91. {
  92. bool res = false;
  93. pkgCdromTextStatus log;
  94. pkgCdrom cdrom;
  95. res = cdrom.Add(&log);
  96. if(res)
  97. cout << _("Repeat this process for the rest of the CDs in your set.") << endl;
  98. return res;
  99. }
  100. /*}}}*/
  101. // DoIdent - Ident a CDROM /*{{{*/
  102. // ---------------------------------------------------------------------
  103. /* */
  104. bool DoIdent(CommandLine &)
  105. {
  106. string ident;
  107. pkgCdromTextStatus log;
  108. pkgCdrom cdrom;
  109. return cdrom.Ident(ident, &log);
  110. }
  111. /*}}}*/
  112. // ShowHelp - Show the help screen /*{{{*/
  113. // ---------------------------------------------------------------------
  114. /* */
  115. int ShowHelp()
  116. {
  117. ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
  118. COMMON_ARCH,__DATE__,__TIME__);
  119. if (_config->FindB("version") == true)
  120. return 0;
  121. cout <<
  122. "Usage: apt-cdrom [options] command\n"
  123. "\n"
  124. "apt-cdrom is a tool to add CDROM's to APT's source list. The\n"
  125. "CDROM mount point and device information is taken from apt.conf\n"
  126. "and /etc/fstab.\n"
  127. "\n"
  128. "Commands:\n"
  129. " add - Add a CDROM\n"
  130. " ident - Report the identity of a CDROM\n"
  131. "\n"
  132. "Options:\n"
  133. " -h This help text\n"
  134. " -d CD-ROM mount point\n"
  135. " -r Rename a recognized CD-ROM\n"
  136. " -m No mounting\n"
  137. " -f Fast mode, don't check package files\n"
  138. " -a Thorough scan mode\n"
  139. " -c=? Read this configuration file\n"
  140. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
  141. "See fstab(5)\n";
  142. return 0;
  143. }
  144. /*}}}*/
  145. int main(int argc,const char *argv[]) /*{{{*/
  146. {
  147. CommandLine::Args Args[] = {
  148. {'h',"help","help",0},
  149. {'v',"version","version",0},
  150. {'d',"cdrom","Acquire::cdrom::mount",CommandLine::HasArg},
  151. {'r',"rename","APT::CDROM::Rename",0},
  152. {'m',"no-mount","APT::CDROM::NoMount",0},
  153. {'f',"fast","APT::CDROM::Fast",0},
  154. {'n',"just-print","APT::CDROM::NoAct",0},
  155. {'n',"recon","APT::CDROM::NoAct",0},
  156. {'n',"no-act","APT::CDROM::NoAct",0},
  157. {'a',"thorough","APT::CDROM::Thorough",0},
  158. {'c',"config-file",0,CommandLine::ConfigFile},
  159. {'o',"option",0,CommandLine::ArbItem},
  160. {0,0,0,0}};
  161. CommandLine::Dispatch Cmds[] = {
  162. {"add",&DoAdd},
  163. {"ident",&DoIdent},
  164. {0,0}};
  165. // Set up gettext support
  166. setlocale(LC_ALL,"");
  167. textdomain(PACKAGE);
  168. // Parse the command line and initialize the package library
  169. CommandLine CmdL(Args,_config);
  170. if (pkgInitConfig(*_config) == false ||
  171. CmdL.Parse(argc,argv) == false ||
  172. pkgInitSystem(*_config,_system) == false)
  173. {
  174. _error->DumpErrors();
  175. return 100;
  176. }
  177. // See if the help should be shown
  178. if (_config->FindB("help") == true || _config->FindB("version") == true ||
  179. CmdL.FileSize() == 0)
  180. return ShowHelp();
  181. // Deal with stdout not being a tty
  182. if (isatty(STDOUT_FILENO) && _config->FindI("quiet",0) < 1)
  183. _config->Set("quiet","1");
  184. // Match the operation
  185. CmdL.DispatchArg(Cmds);
  186. // Print any errors or warnings found during parsing
  187. if (_error->empty() == false)
  188. {
  189. bool Errors = _error->PendingError();
  190. _error->DumpErrors();
  191. return Errors == true?100:0;
  192. }
  193. return 0;
  194. }
  195. /*}}}*/